q6afe.c 297 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/slab.h>
  5. #include <linux/debugfs.h>
  6. #include <linux/kernel.h>
  7. #include <linux/kthread.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/wait.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/sched.h>
  12. #include <linux/delay.h>
  13. #include <dsp/msm_audio_ion.h>
  14. #include <dsp/apr_audio-v2.h>
  15. #include <dsp/audio_cal_utils.h>
  16. #include <dsp/q6afe-v2.h>
  17. #include <dsp/q6audio-v2.h>
  18. #include <dsp/q6common.h>
  19. #include <dsp/q6core.h>
  20. #include <dsp/msm-audio-event-notify.h>
  21. #include <ipc/apr_tal.h>
  22. #include "adsp_err.h"
  23. #include "q6afecal-hwdep.h"
  24. #define WAKELOCK_TIMEOUT 5000
  25. #define AFE_CLK_TOKEN 1024
  26. enum {
  27. AFE_COMMON_RX_CAL = 0,
  28. AFE_COMMON_TX_CAL,
  29. AFE_LSM_TX_CAL,
  30. AFE_AANC_CAL,
  31. AFE_FB_SPKR_PROT_CAL,
  32. AFE_HW_DELAY_CAL,
  33. AFE_SIDETONE_CAL,
  34. AFE_SIDETONE_IIR_CAL,
  35. AFE_TOPOLOGY_CAL,
  36. AFE_LSM_TOPOLOGY_CAL,
  37. AFE_CUST_TOPOLOGY_CAL,
  38. AFE_FB_SPKR_PROT_TH_VI_CAL,
  39. AFE_FB_SPKR_PROT_EX_VI_CAL,
  40. AFE_FB_SPKR_PROT_V4_EX_VI_CAL,
  41. MAX_AFE_CAL_TYPES
  42. };
  43. enum fbsp_state {
  44. FBSP_INCORRECT_OP_MODE,
  45. FBSP_INACTIVE,
  46. FBSP_WARMUP,
  47. FBSP_IN_PROGRESS,
  48. FBSP_SUCCESS,
  49. FBSP_FAILED,
  50. MAX_FBSP_STATE
  51. };
  52. static char fbsp_state[MAX_FBSP_STATE][50] = {
  53. [FBSP_INCORRECT_OP_MODE] = "incorrect operation mode",
  54. [FBSP_INACTIVE] = "port not started",
  55. [FBSP_WARMUP] = "waiting for warmup",
  56. [FBSP_IN_PROGRESS] = "in progress state",
  57. [FBSP_SUCCESS] = "success",
  58. [FBSP_FAILED] = "failed"
  59. };
  60. enum v_vali_state {
  61. V_VALI_FAILED,
  62. V_VALI_SUCCESS,
  63. V_VALI_INCORRECT_OP_MODE,
  64. V_VALI_INACTIVE,
  65. V_VALI_WARMUP,
  66. V_VALI_IN_PROGRESS,
  67. MAX_V_VALI_STATE
  68. };
  69. enum {
  70. USE_CALIBRATED_R0TO,
  71. USE_SAFE_R0TO
  72. };
  73. enum {
  74. QUICK_CALIB_DISABLE,
  75. QUICK_CALIB_ENABLE
  76. };
  77. enum {
  78. Q6AFE_MSM_SPKR_PROCESSING = 0,
  79. Q6AFE_MSM_SPKR_CALIBRATION,
  80. Q6AFE_MSM_SPKR_FTM_MODE,
  81. Q6AFE_MSM_SPKR_V_VALI_MODE
  82. };
  83. enum {
  84. APTX_AD_48 = 0,
  85. APTX_AD_44_1 = 1
  86. };
  87. enum {
  88. AFE_MATCHED_PORT_DISABLE,
  89. AFE_MATCHED_PORT_ENABLE
  90. };
  91. enum {
  92. AFE_FBSP_V4_EX_VI_MODE_NORMAL = 0,
  93. AFE_FBSP_V4_EX_VI_MODE_FTM = 1
  94. };
  95. struct wlock {
  96. struct wakeup_source *ws;
  97. };
  98. static struct wlock wl;
  99. struct afe_ctl {
  100. void *apr;
  101. atomic_t state;
  102. atomic_t status;
  103. atomic_t clk_state;
  104. atomic_t clk_status;
  105. wait_queue_head_t wait[AFE_MAX_PORTS];
  106. wait_queue_head_t wait_wakeup;
  107. wait_queue_head_t clk_wait;
  108. struct task_struct *task;
  109. wait_queue_head_t lpass_core_hw_wait;
  110. uint32_t lpass_hw_core_client_hdl[AFE_LPASS_CORE_HW_VOTE_MAX];
  111. void (*tx_cb)(uint32_t opcode,
  112. uint32_t token, uint32_t *payload, void *priv);
  113. void (*rx_cb)(uint32_t opcode,
  114. uint32_t token, uint32_t *payload, void *priv);
  115. void *tx_private_data;
  116. void *rx_private_data;
  117. uint32_t mmap_handle;
  118. void (*pri_spdif_tx_cb)(uint32_t opcode,
  119. uint32_t token, uint32_t *payload, void *priv);
  120. void (*sec_spdif_tx_cb)(uint32_t opcode,
  121. uint32_t token, uint32_t *payload, void *priv);
  122. void *pri_spdif_tx_private_data;
  123. void *sec_spdif_tx_private_data;
  124. int pri_spdif_config_change;
  125. int sec_spdif_config_change;
  126. struct work_struct afe_spdif_work;
  127. int topology[AFE_MAX_PORTS];
  128. struct cal_type_data *cal_data[MAX_AFE_CAL_TYPES];
  129. atomic_t mem_map_cal_handles[MAX_AFE_CAL_TYPES];
  130. atomic_t mem_map_cal_index;
  131. u32 afe_cal_mode[AFE_MAX_PORTS];
  132. u16 dtmf_gen_rx_portid;
  133. struct audio_cal_info_spk_prot_cfg prot_cfg;
  134. struct afe_spkr_prot_calib_get_resp calib_data;
  135. struct audio_cal_info_sp_th_vi_ftm_cfg th_ftm_cfg;
  136. struct audio_cal_info_sp_th_vi_v_vali_cfg v_vali_cfg;
  137. struct audio_cal_info_sp_ex_vi_ftm_cfg ex_ftm_cfg;
  138. struct afe_sp_th_vi_get_param_resp th_vi_resp;
  139. struct afe_sp_th_vi_v_vali_get_param_resp th_vi_v_vali_resp;
  140. struct afe_sp_ex_vi_get_param_resp ex_vi_resp;
  141. struct afe_sp_rx_tmax_xmax_logging_resp xt_logging_resp;
  142. struct afe_sp_v4_th_vi_calib_resp spv4_calib_data;
  143. struct afe_sp_v4_param_vi_channel_map_cfg v4_ch_map_cfg;
  144. struct afe_sp_v4_gen_get_param_resp *spv4_get_param_resp_ptr;
  145. uint32_t spv4_rcvd_param_size;
  146. struct afe_av_dev_drift_get_param_resp av_dev_drift_resp;
  147. struct afe_doa_tracking_mon_get_param_resp doa_tracking_mon_resp;
  148. int vi_tx_port;
  149. int vi_rx_port;
  150. uint32_t afe_sample_rates[AFE_MAX_PORTS];
  151. struct aanc_data aanc_info;
  152. struct mutex afe_cmd_lock;
  153. struct mutex afe_apr_lock;
  154. struct mutex afe_clk_lock;
  155. int set_custom_topology;
  156. int dev_acdb_id[AFE_MAX_PORTS];
  157. routing_cb rt_cb;
  158. struct audio_uevent_data *uevent_data;
  159. /* cal info for AFE */
  160. struct afe_fw_info *fw_data;
  161. u32 island_mode[AFE_MAX_PORTS];
  162. struct vad_config vad_cfg[AFE_MAX_PORTS];
  163. struct work_struct afe_dc_work;
  164. struct notifier_block event_notifier;
  165. /* FTM spk params */
  166. uint32_t initial_cal;
  167. uint32_t v_vali_flag;
  168. };
  169. static atomic_t afe_ports_mad_type[SLIMBUS_PORT_LAST - SLIMBUS_0_RX];
  170. static unsigned long afe_configured_cmd;
  171. static struct afe_ctl this_afe;
  172. #define TIMEOUT_MS 1000
  173. #define Q6AFE_MAX_VOLUME 0x3FFF
  174. static int pcm_afe_instance[2];
  175. static int proxy_afe_instance[2];
  176. bool afe_close_done[2] = {true, true};
  177. #define SIZEOF_CFG_CMD(y) \
  178. (sizeof(struct apr_hdr) + sizeof(u16) + (sizeof(struct y)))
  179. static int afe_get_cal_hw_delay(int32_t path,
  180. struct audio_cal_hw_delay_entry *entry);
  181. static int remap_cal_data(struct cal_block_data *cal_block, int cal_index);
  182. int afe_get_spk_initial_cal(void)
  183. {
  184. return this_afe.initial_cal;
  185. }
  186. void afe_get_spk_r0(int *spk_r0)
  187. {
  188. uint16_t i = 0;
  189. for (; i < SP_V2_NUM_MAX_SPKRS; i++)
  190. spk_r0[i] = this_afe.prot_cfg.r0[i];
  191. }
  192. void afe_get_spk_t0(int *spk_t0)
  193. {
  194. uint16_t i = 0;
  195. for (; i < SP_V2_NUM_MAX_SPKRS; i++)
  196. spk_t0[i] = this_afe.prot_cfg.t0[i];
  197. }
  198. int afe_get_spk_v_vali_flag(void)
  199. {
  200. return this_afe.v_vali_flag;
  201. }
  202. void afe_get_spk_v_vali_sts(int *spk_v_vali_sts)
  203. {
  204. uint16_t i = 0;
  205. for (; i < SP_V2_NUM_MAX_SPKRS; i++)
  206. spk_v_vali_sts[i] =
  207. this_afe.th_vi_v_vali_resp.param.status[i];
  208. }
  209. void afe_set_spk_initial_cal(int initial_cal)
  210. {
  211. this_afe.initial_cal = initial_cal;
  212. }
  213. void afe_set_spk_v_vali_flag(int v_vali_flag)
  214. {
  215. this_afe.v_vali_flag = v_vali_flag;
  216. }
  217. int afe_get_topology(int port_id)
  218. {
  219. int topology;
  220. int port_index = afe_get_port_index(port_id);
  221. if ((port_index < 0) || (port_index >= AFE_MAX_PORTS)) {
  222. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  223. topology = -EINVAL;
  224. goto done;
  225. }
  226. topology = this_afe.topology[port_index];
  227. done:
  228. return topology;
  229. }
  230. /**
  231. * afe_set_aanc_info -
  232. * Update AFE AANC info
  233. *
  234. * @q6_aanc_info: AFE AANC info params
  235. *
  236. */
  237. void afe_set_aanc_info(struct aanc_data *q6_aanc_info)
  238. {
  239. this_afe.aanc_info.aanc_active = q6_aanc_info->aanc_active;
  240. this_afe.aanc_info.aanc_rx_port = q6_aanc_info->aanc_rx_port;
  241. this_afe.aanc_info.aanc_tx_port = q6_aanc_info->aanc_tx_port;
  242. pr_debug("%s: aanc active is %d rx port is 0x%x, tx port is 0x%x\n",
  243. __func__,
  244. this_afe.aanc_info.aanc_active,
  245. this_afe.aanc_info.aanc_rx_port,
  246. this_afe.aanc_info.aanc_tx_port);
  247. }
  248. EXPORT_SYMBOL(afe_set_aanc_info);
  249. static void afe_callback_debug_print(struct apr_client_data *data)
  250. {
  251. uint32_t *payload;
  252. payload = data->payload;
  253. if (data->payload_size >= 8)
  254. pr_debug("%s: code = 0x%x PL#0[0x%x], PL#1[0x%x], size = %d\n",
  255. __func__, data->opcode, payload[0], payload[1],
  256. data->payload_size);
  257. else if (data->payload_size >= 4)
  258. pr_debug("%s: code = 0x%x PL#0[0x%x], size = %d\n",
  259. __func__, data->opcode, payload[0],
  260. data->payload_size);
  261. else
  262. pr_debug("%s: code = 0x%x, size = %d\n",
  263. __func__, data->opcode, data->payload_size);
  264. }
  265. static void av_dev_drift_afe_cb_handler(uint32_t opcode, uint32_t *payload,
  266. uint32_t payload_size)
  267. {
  268. u32 param_id;
  269. size_t expected_size =
  270. sizeof(u32) + sizeof(struct afe_param_id_dev_timing_stats);
  271. /* Get param ID depending on command type */
  272. param_id = (opcode == AFE_PORT_CMDRSP_GET_PARAM_V3) ? payload[3] :
  273. payload[2];
  274. if (param_id != AFE_PARAM_ID_DEV_TIMING_STATS) {
  275. pr_err("%s: Unrecognized param ID %d\n", __func__, param_id);
  276. return;
  277. }
  278. switch (opcode) {
  279. case AFE_PORT_CMDRSP_GET_PARAM_V2:
  280. expected_size += sizeof(struct param_hdr_v1);
  281. if (payload_size < expected_size) {
  282. pr_err("%s: Error: received size %d, expected size %zu\n",
  283. __func__, payload_size, expected_size);
  284. return;
  285. }
  286. /* Repack response to add IID */
  287. this_afe.av_dev_drift_resp.status = payload[0];
  288. this_afe.av_dev_drift_resp.pdata.module_id = payload[1];
  289. this_afe.av_dev_drift_resp.pdata.instance_id = INSTANCE_ID_0;
  290. this_afe.av_dev_drift_resp.pdata.param_id = payload[2];
  291. this_afe.av_dev_drift_resp.pdata.param_size = payload[3];
  292. memcpy(&this_afe.av_dev_drift_resp.timing_stats, &payload[4],
  293. sizeof(struct afe_param_id_dev_timing_stats));
  294. break;
  295. case AFE_PORT_CMDRSP_GET_PARAM_V3:
  296. expected_size += sizeof(struct param_hdr_v3);
  297. if (payload_size < expected_size) {
  298. pr_err("%s: Error: received size %d, expected size %zu\n",
  299. __func__, payload_size, expected_size);
  300. return;
  301. }
  302. memcpy(&this_afe.av_dev_drift_resp, payload,
  303. sizeof(this_afe.av_dev_drift_resp));
  304. break;
  305. default:
  306. pr_err("%s: Unrecognized command %d\n", __func__, opcode);
  307. return;
  308. }
  309. if (!this_afe.av_dev_drift_resp.status) {
  310. atomic_set(&this_afe.state, 0);
  311. } else {
  312. pr_debug("%s: av_dev_drift_resp status: %d\n", __func__,
  313. this_afe.av_dev_drift_resp.status);
  314. atomic_set(&this_afe.state, -1);
  315. }
  316. }
  317. static void doa_tracking_mon_afe_cb_handler(uint32_t opcode, uint32_t *payload,
  318. uint32_t payload_size)
  319. {
  320. size_t expected_size =
  321. sizeof(u32) + sizeof(struct doa_tracking_mon_param);
  322. if (payload[0]) {
  323. atomic_set(&this_afe.status, payload[0]);
  324. atomic_set(&this_afe.state, 0);
  325. pr_err("%s: doa_tracking_mon_resp status: %d payload size %d\n",
  326. __func__, payload[0], payload_size);
  327. return;
  328. }
  329. switch (opcode) {
  330. case AFE_PORT_CMDRSP_GET_PARAM_V2:
  331. expected_size += sizeof(struct param_hdr_v1);
  332. if (payload_size < expected_size) {
  333. pr_err("%s: Error: received size %d, expected size %zu\n",
  334. __func__, payload_size, expected_size);
  335. return;
  336. }
  337. /* Repack response to add IID */
  338. this_afe.doa_tracking_mon_resp.status = payload[0];
  339. this_afe.doa_tracking_mon_resp.pdata.module_id = payload[1];
  340. this_afe.doa_tracking_mon_resp.pdata.instance_id =
  341. INSTANCE_ID_0;
  342. this_afe.doa_tracking_mon_resp.pdata.param_id = payload[2];
  343. this_afe.doa_tracking_mon_resp.pdata.param_size = payload[3];
  344. memcpy(&this_afe.doa_tracking_mon_resp.doa, &payload[4],
  345. sizeof(struct doa_tracking_mon_param));
  346. break;
  347. case AFE_PORT_CMDRSP_GET_PARAM_V3:
  348. expected_size += sizeof(struct param_hdr_v3);
  349. if (payload_size < expected_size) {
  350. pr_err("%s: Error: received size %d, expected size %zu\n",
  351. __func__, payload_size, expected_size);
  352. return;
  353. }
  354. memcpy(&this_afe.doa_tracking_mon_resp, payload,
  355. sizeof(this_afe.doa_tracking_mon_resp));
  356. break;
  357. default:
  358. pr_err("%s: Unrecognized command %d\n", __func__, opcode);
  359. return;
  360. }
  361. atomic_set(&this_afe.state, 0);
  362. }
  363. static int32_t sp_make_afe_callback(uint32_t opcode, uint32_t *payload,
  364. uint32_t payload_size)
  365. {
  366. struct param_hdr_v3 param_hdr;
  367. u32 *data_dest = NULL;
  368. u32 *data_start = NULL;
  369. size_t expected_size = sizeof(u32);
  370. uint32_t num_ch = 0;
  371. memset(&param_hdr, 0, sizeof(param_hdr));
  372. /* Set command specific details */
  373. switch (opcode) {
  374. case AFE_PORT_CMDRSP_GET_PARAM_V2:
  375. if (payload_size < (5 * sizeof(uint32_t))) {
  376. pr_err("%s: Error: size %d is less than expected\n",
  377. __func__, payload_size);
  378. return -EINVAL;
  379. }
  380. expected_size += sizeof(struct param_hdr_v1);
  381. param_hdr.module_id = payload[1];
  382. param_hdr.instance_id = INSTANCE_ID_0;
  383. param_hdr.param_id = payload[2];
  384. param_hdr.param_size = payload[3];
  385. data_start = &payload[4];
  386. break;
  387. case AFE_PORT_CMDRSP_GET_PARAM_V3:
  388. if (payload_size < (6 * sizeof(uint32_t))) {
  389. pr_err("%s: Error: size %d is less than expected\n",
  390. __func__, payload_size);
  391. return -EINVAL;
  392. }
  393. expected_size += sizeof(struct param_hdr_v3);
  394. if (payload_size < expected_size) {
  395. pr_err("%s: Error: size %d is less than expected\n",
  396. __func__, payload_size);
  397. return -EINVAL;
  398. }
  399. memcpy(&param_hdr, &payload[1], sizeof(struct param_hdr_v3));
  400. data_start = &payload[5];
  401. break;
  402. default:
  403. pr_err("%s: Unrecognized command %d\n", __func__, opcode);
  404. return -EINVAL;
  405. }
  406. switch (param_hdr.param_id) {
  407. case AFE_PARAM_ID_CALIB_RES_CFG_V2:
  408. expected_size += sizeof(struct asm_calib_res_cfg);
  409. data_dest = (u32 *) &this_afe.calib_data;
  410. break;
  411. case AFE_PARAM_ID_SP_V2_TH_VI_FTM_PARAMS:
  412. expected_size += sizeof(struct afe_sp_th_vi_ftm_params);
  413. data_dest = (u32 *) &this_afe.th_vi_resp;
  414. break;
  415. case AFE_PARAM_ID_SP_V2_TH_VI_V_VALI_PARAMS:
  416. expected_size += sizeof(struct afe_sp_th_vi_v_vali_params);
  417. data_dest = (u32 *) &this_afe.th_vi_v_vali_resp;
  418. break;
  419. case AFE_PARAM_ID_SP_V2_EX_VI_FTM_PARAMS:
  420. expected_size += sizeof(struct afe_sp_ex_vi_ftm_params);
  421. data_dest = (u32 *) &this_afe.ex_vi_resp;
  422. break;
  423. case AFE_PARAM_ID_SP_RX_TMAX_XMAX_LOGGING:
  424. expected_size += sizeof(
  425. struct afe_sp_rx_tmax_xmax_logging_param);
  426. data_dest = (u32 *) &this_afe.xt_logging_resp;
  427. break;
  428. case AFE_PARAM_ID_SP_V4_CALIB_RES_CFG:
  429. expected_size += sizeof(
  430. struct afe_sp_v4_param_th_vi_calib_res_cfg);
  431. data_dest = (u32 *) &this_afe.spv4_calib_data;
  432. break;
  433. case AFE_PARAM_ID_SP_V4_TH_VI_FTM_PARAMS:
  434. num_ch = data_start[0];
  435. this_afe.spv4_rcvd_param_size =
  436. sizeof(struct afe_sp_v4_gen_get_param_resp) +
  437. sizeof(struct afe_sp_v4_param_th_vi_ftm_params) +
  438. (num_ch * sizeof(struct afe_sp_v4_channel_ftm_params));
  439. this_afe.spv4_get_param_resp_ptr =
  440. kzalloc(this_afe.spv4_rcvd_param_size, GFP_ATOMIC);
  441. data_dest = (u32 *)this_afe.spv4_get_param_resp_ptr;
  442. expected_size +=
  443. sizeof(struct afe_sp_v4_param_th_vi_ftm_params) +
  444. (num_ch * sizeof(struct afe_sp_v4_channel_ftm_params));
  445. break;
  446. case AFE_PARAM_ID_SP_V4_TH_VI_V_VALI_PARAMS:
  447. num_ch = data_start[0];
  448. this_afe.spv4_rcvd_param_size =
  449. sizeof(struct afe_sp_v4_gen_get_param_resp) +
  450. sizeof(struct afe_sp_v4_param_th_vi_v_vali_params) +
  451. (num_ch *
  452. sizeof(struct afe_sp_v4_channel_v_vali_params));
  453. this_afe.spv4_get_param_resp_ptr =
  454. kzalloc(this_afe.spv4_rcvd_param_size, GFP_ATOMIC);
  455. data_dest = (u32 *)this_afe.spv4_get_param_resp_ptr;
  456. expected_size +=
  457. sizeof(struct afe_sp_v4_param_th_vi_v_vali_params) +
  458. (num_ch *
  459. sizeof(struct afe_sp_v4_channel_v_vali_params));
  460. break;
  461. case AFE_PARAM_ID_SP_V4_EX_VI_FTM_PARAMS:
  462. num_ch = data_start[0];
  463. this_afe.spv4_rcvd_param_size =
  464. sizeof(struct afe_sp_v4_gen_get_param_resp) +
  465. sizeof(struct afe_sp_v4_param_ex_vi_ftm_params) +
  466. (num_ch *
  467. sizeof(struct afe_sp_v4_channel_ex_vi_ftm_params));
  468. this_afe.spv4_get_param_resp_ptr =
  469. kzalloc(this_afe.spv4_rcvd_param_size, GFP_ATOMIC);
  470. data_dest = (u32 *)this_afe.spv4_get_param_resp_ptr;
  471. expected_size +=
  472. sizeof(struct afe_sp_v4_param_ex_vi_ftm_params) +
  473. (num_ch *
  474. sizeof(struct afe_sp_v4_channel_ex_vi_ftm_params));
  475. break;
  476. case AFE_PARAM_ID_SP_V4_RX_TMAX_XMAX_LOGGING:
  477. num_ch = data_start[0];
  478. this_afe.spv4_rcvd_param_size =
  479. sizeof(struct afe_sp_v4_gen_get_param_resp) +
  480. sizeof(struct afe_sp_v4_param_tmax_xmax_logging) +
  481. (num_ch *
  482. sizeof(struct afe_sp_v4_channel_tmax_xmax_params));
  483. this_afe.spv4_get_param_resp_ptr =
  484. kzalloc(this_afe.spv4_rcvd_param_size, GFP_ATOMIC);
  485. data_dest = (u32 *)this_afe.spv4_get_param_resp_ptr;
  486. expected_size +=
  487. sizeof(struct afe_sp_v4_param_tmax_xmax_logging) +
  488. (num_ch *
  489. sizeof(struct afe_sp_v4_channel_tmax_xmax_params));
  490. break;
  491. default:
  492. pr_err("%s: Unrecognized param ID %d\n", __func__,
  493. param_hdr.param_id);
  494. return -EINVAL;
  495. }
  496. if (!data_dest)
  497. return -ENOMEM;
  498. if (payload_size < expected_size) {
  499. pr_err(
  500. "%s: Error: received size %d, expected size %zu for param %d\n",
  501. __func__, payload_size, expected_size,
  502. param_hdr.param_id);
  503. return -EINVAL;
  504. }
  505. data_dest[0] = payload[0];
  506. memcpy(&data_dest[1], &param_hdr, sizeof(struct param_hdr_v3));
  507. memcpy(&data_dest[5], data_start, param_hdr.param_size);
  508. if (!data_dest[0]) {
  509. atomic_set(&this_afe.state, 0);
  510. } else {
  511. pr_debug("%s: status: %d", __func__, data_dest[0]);
  512. atomic_set(&this_afe.state, -1);
  513. }
  514. return 0;
  515. }
  516. static void afe_notify_dc_presence(void)
  517. {
  518. pr_debug("%s: DC detected\n", __func__);
  519. msm_aud_evt_notifier_call_chain(MSM_AUD_DC_EVENT, NULL);
  520. schedule_work(&this_afe.afe_dc_work);
  521. }
  522. static void afe_notify_dc_presence_work_fn(struct work_struct *work)
  523. {
  524. int ret = 0;
  525. char event[] = "DC_PRESENCE=TRUE";
  526. ret = q6core_send_uevent(this_afe.uevent_data, event);
  527. if (ret)
  528. pr_err("%s: Send UEvent %s failed :%d\n",
  529. __func__, event, ret);
  530. }
  531. static int afe_aud_event_notify(struct notifier_block *self,
  532. unsigned long action, void *data)
  533. {
  534. switch (action) {
  535. case SWR_WAKE_IRQ_REGISTER:
  536. afe_send_cmd_wakeup_register(data, true);
  537. break;
  538. case SWR_WAKE_IRQ_DEREGISTER:
  539. afe_send_cmd_wakeup_register(data, false);
  540. break;
  541. default:
  542. pr_err("%s: invalid event type: %lu\n", __func__, action);
  543. return -EINVAL;
  544. }
  545. return 0;
  546. }
  547. static void afe_notify_spdif_fmt_update_work_fn(struct work_struct *work)
  548. {
  549. int ret = 0;
  550. char event_pri[] = "PRI_SPDIF_TX=MEDIA_CONFIG_CHANGE";
  551. char event_sec[] = "SEC_SPDIF_TX=MEDIA_CONFIG_CHANGE";
  552. if (this_afe.pri_spdif_config_change) {
  553. this_afe.pri_spdif_config_change = 0;
  554. ret = q6core_send_uevent(this_afe.uevent_data, event_pri);
  555. if (ret)
  556. pr_err("%s: Send UEvent %s failed :%d\n",
  557. __func__, event_pri, ret);
  558. }
  559. if (this_afe.sec_spdif_config_change) {
  560. this_afe.sec_spdif_config_change = 0;
  561. ret = q6core_send_uevent(this_afe.uevent_data, event_sec);
  562. if (ret)
  563. pr_err("%s: Send UEvent %s failed :%d\n",
  564. __func__, event_sec, ret);
  565. }
  566. }
  567. static void afe_notify_spdif_fmt_update(void *payload)
  568. {
  569. struct afe_port_mod_evt_rsp_hdr *evt_pl;
  570. evt_pl = (struct afe_port_mod_evt_rsp_hdr *)payload;
  571. if (evt_pl->port_id == AFE_PORT_ID_PRIMARY_SPDIF_TX)
  572. this_afe.pri_spdif_config_change = 1;
  573. else
  574. this_afe.sec_spdif_config_change = 1;
  575. schedule_work(&this_afe.afe_spdif_work);
  576. }
  577. static bool afe_token_is_valid(uint32_t token)
  578. {
  579. if (token >= AFE_MAX_PORTS) {
  580. pr_err("%s: token %d is invalid.\n", __func__, token);
  581. return false;
  582. }
  583. return true;
  584. }
  585. static int32_t afe_callback(struct apr_client_data *data, void *priv)
  586. {
  587. uint16_t i = 0;
  588. if (!data) {
  589. pr_err("%s: Invalid param data\n", __func__);
  590. return -EINVAL;
  591. }
  592. if (data->opcode == RESET_EVENTS) {
  593. pr_debug("%s: reset event = %d %d apr[%pK]\n",
  594. __func__,
  595. data->reset_event, data->reset_proc, this_afe.apr);
  596. cal_utils_clear_cal_block_q6maps(MAX_AFE_CAL_TYPES,
  597. this_afe.cal_data);
  598. /* Reset the custom topology mode: to resend again to AFE. */
  599. mutex_lock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  600. this_afe.set_custom_topology = 1;
  601. mutex_unlock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  602. rtac_clear_mapping(AFE_RTAC_CAL);
  603. if (this_afe.apr) {
  604. apr_reset(this_afe.apr);
  605. atomic_set(&this_afe.state, 0);
  606. this_afe.apr = NULL;
  607. rtac_set_afe_handle(this_afe.apr);
  608. }
  609. /* Reset the core client handle in SSR/PDR use cases */
  610. mutex_lock(&this_afe.afe_cmd_lock);
  611. for (i = 0; i < AFE_LPASS_CORE_HW_VOTE_MAX; i++)
  612. this_afe.lpass_hw_core_client_hdl[i] = 0;
  613. mutex_unlock(&this_afe.afe_cmd_lock);
  614. /*
  615. * Pass reset events to proxy driver, if cb is registered
  616. */
  617. if (this_afe.tx_cb) {
  618. this_afe.tx_cb(data->opcode, data->token,
  619. data->payload,
  620. this_afe.tx_private_data);
  621. this_afe.tx_cb = NULL;
  622. }
  623. if (this_afe.rx_cb) {
  624. this_afe.rx_cb(data->opcode, data->token,
  625. data->payload,
  626. this_afe.rx_private_data);
  627. this_afe.rx_cb = NULL;
  628. }
  629. return 0;
  630. }
  631. afe_callback_debug_print(data);
  632. if (data->opcode == AFE_PORT_CMDRSP_GET_PARAM_V2 ||
  633. data->opcode == AFE_PORT_CMDRSP_GET_PARAM_V3) {
  634. uint32_t *payload = data->payload;
  635. uint32_t param_id;
  636. uint32_t param_id_pos = 0;
  637. if (!payload || (data->token >= AFE_MAX_PORTS)) {
  638. pr_err("%s: Error: size %d payload %pK token %d\n",
  639. __func__, data->payload_size,
  640. payload, data->token);
  641. return -EINVAL;
  642. }
  643. if (rtac_make_afe_callback(data->payload,
  644. data->payload_size))
  645. return 0;
  646. if (data->opcode == AFE_PORT_CMDRSP_GET_PARAM_V3)
  647. param_id_pos = 4;
  648. else
  649. param_id_pos = 3;
  650. if (data->payload_size >= param_id_pos * sizeof(uint32_t))
  651. param_id = payload[param_id_pos - 1];
  652. else {
  653. pr_err("%s: Error: size %d is less than expected\n",
  654. __func__, data->payload_size);
  655. return -EINVAL;
  656. }
  657. if (param_id == AUDPROC_PARAM_ID_FFV_DOA_TRACKING_MONITOR) {
  658. doa_tracking_mon_afe_cb_handler(data->opcode,
  659. data->payload, data->payload_size);
  660. } else if (param_id == AFE_PARAM_ID_DEV_TIMING_STATS) {
  661. av_dev_drift_afe_cb_handler(data->opcode, data->payload,
  662. data->payload_size);
  663. } else {
  664. if (sp_make_afe_callback(data->opcode, data->payload,
  665. data->payload_size))
  666. return -EINVAL;
  667. }
  668. if (afe_token_is_valid(data->token))
  669. wake_up(&this_afe.wait[data->token]);
  670. else
  671. return -EINVAL;
  672. } else if (data->opcode == AFE_EVENT_MBHC_DETECTION_SW_WA) {
  673. msm_aud_evt_notifier_call_chain(SWR_WAKE_IRQ_EVENT, NULL);
  674. } else if (data->opcode ==
  675. AFE_CMD_RSP_REMOTE_LPASS_CORE_HW_VOTE_REQUEST) {
  676. uint32_t *payload = data->payload;
  677. pr_debug("%s: AFE_CMD_RSP_REMOTE_LPASS_CORE_HW_VOTE_REQUEST handle %d\n",
  678. __func__, payload[0]);
  679. if (data->token < AFE_LPASS_CORE_HW_VOTE_MAX)
  680. this_afe.lpass_hw_core_client_hdl[data->token] =
  681. payload[0];
  682. atomic_set(&this_afe.clk_state, 0);
  683. atomic_set(&this_afe.clk_status, 0);
  684. wake_up(&this_afe.lpass_core_hw_wait);
  685. } else if (data->payload_size) {
  686. uint32_t *payload;
  687. uint16_t port_id = 0;
  688. payload = data->payload;
  689. if (data->opcode == APR_BASIC_RSP_RESULT) {
  690. if (data->payload_size < (2 * sizeof(uint32_t))) {
  691. pr_err("%s: Error: size %d is less than expected\n",
  692. __func__, data->payload_size);
  693. return -EINVAL;
  694. }
  695. pr_debug("%s:opcode = 0x%x cmd = 0x%x status = 0x%x token=%d\n",
  696. __func__, data->opcode,
  697. payload[0], payload[1], data->token);
  698. /* payload[1] contains the error status for response */
  699. if (payload[1] != 0) {
  700. if(data->token == AFE_CLK_TOKEN)
  701. atomic_set(&this_afe.clk_status, payload[1]);
  702. else
  703. atomic_set(&this_afe.status, payload[1]);
  704. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  705. __func__, payload[0], payload[1]);
  706. }
  707. switch (payload[0]) {
  708. case AFE_PORT_CMD_SET_PARAM_V2:
  709. case AFE_PORT_CMD_SET_PARAM_V3:
  710. if (rtac_make_afe_callback(payload,
  711. data->payload_size))
  712. return 0;
  713. case AFE_PORT_CMD_DEVICE_STOP:
  714. case AFE_PORT_CMD_DEVICE_START:
  715. case AFE_PSEUDOPORT_CMD_START:
  716. case AFE_PSEUDOPORT_CMD_STOP:
  717. case AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS:
  718. case AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS:
  719. case AFE_SERVICE_CMD_UNREGISTER_RT_PORT_DRIVER:
  720. case AFE_PORTS_CMD_DTMF_CTL:
  721. case AFE_SVC_CMD_SET_PARAM:
  722. case AFE_SVC_CMD_SET_PARAM_V2:
  723. case AFE_PORT_CMD_MOD_EVENT_CFG:
  724. if(data->token == AFE_CLK_TOKEN) {
  725. atomic_set(&this_afe.clk_state, 0);
  726. wake_up(&this_afe.clk_wait);
  727. } else {
  728. atomic_set(&this_afe.state, 0);
  729. if (afe_token_is_valid(data->token))
  730. wake_up(&this_afe.wait[data->token]);
  731. else
  732. return -EINVAL;
  733. }
  734. break;
  735. case AFE_SERVICE_CMD_REGISTER_RT_PORT_DRIVER:
  736. break;
  737. case AFE_PORT_DATA_CMD_RT_PROXY_PORT_WRITE_V2:
  738. port_id = RT_PROXY_PORT_001_TX;
  739. break;
  740. case AFE_PORT_DATA_CMD_RT_PROXY_PORT_READ_V2:
  741. port_id = RT_PROXY_PORT_001_RX;
  742. break;
  743. case AFE_CMD_ADD_TOPOLOGIES:
  744. atomic_set(&this_afe.state, 0);
  745. if (afe_token_is_valid(data->token))
  746. wake_up(&this_afe.wait[data->token]);
  747. else
  748. return -EINVAL;
  749. pr_debug("%s: AFE_CMD_ADD_TOPOLOGIES cmd 0x%x\n",
  750. __func__, payload[1]);
  751. break;
  752. case AFE_PORT_CMD_GET_PARAM_V2:
  753. case AFE_PORT_CMD_GET_PARAM_V3:
  754. /*
  755. * Should only come here if there is an APR
  756. * error or malformed APR packet. Otherwise
  757. * response will be returned as
  758. * AFE_PORT_CMDRSP_GET_PARAM_V2/3
  759. */
  760. pr_debug("%s: AFE Get Param opcode 0x%x token 0x%x src %d dest %d\n",
  761. __func__, data->opcode, data->token,
  762. data->src_port, data->dest_port);
  763. if (payload[1] != 0) {
  764. pr_err("%s: AFE Get Param failed with error %d\n",
  765. __func__, payload[1]);
  766. if (rtac_make_afe_callback(
  767. payload,
  768. data->payload_size))
  769. return 0;
  770. }
  771. atomic_set(&this_afe.state, payload[1]);
  772. if (afe_token_is_valid(data->token))
  773. wake_up(&this_afe.wait[data->token]);
  774. else
  775. return -EINVAL;
  776. break;
  777. case AFE_CMD_REMOTE_LPASS_CORE_HW_VOTE_REQUEST:
  778. case AFE_CMD_REMOTE_LPASS_CORE_HW_DEVOTE_REQUEST:
  779. atomic_set(&this_afe.clk_state, 0);
  780. if (payload[1] != 0)
  781. atomic_set(&this_afe.clk_status,
  782. payload[1]);
  783. wake_up(&this_afe.lpass_core_hw_wait);
  784. break;
  785. case AFE_SVC_CMD_EVENT_CFG:
  786. atomic_set(&this_afe.state, payload[1]);
  787. wake_up(&this_afe.wait_wakeup);
  788. break;
  789. default:
  790. pr_err("%s: Unknown cmd 0x%x\n", __func__,
  791. payload[0]);
  792. break;
  793. }
  794. } else if (data->opcode ==
  795. AFE_SERVICE_CMDRSP_SHARED_MEM_MAP_REGIONS) {
  796. pr_debug("%s: mmap_handle: 0x%x, cal index %d\n",
  797. __func__, payload[0],
  798. atomic_read(&this_afe.mem_map_cal_index));
  799. if (atomic_read(&this_afe.mem_map_cal_index) != -1)
  800. atomic_set(&this_afe.mem_map_cal_handles[
  801. atomic_read(
  802. &this_afe.mem_map_cal_index)],
  803. (uint32_t)payload[0]);
  804. else
  805. this_afe.mmap_handle = payload[0];
  806. atomic_set(&this_afe.state, 0);
  807. if (afe_token_is_valid(data->token))
  808. wake_up(&this_afe.wait[data->token]);
  809. else
  810. return -EINVAL;
  811. } else if (data->opcode == AFE_EVENT_RT_PROXY_PORT_STATUS) {
  812. port_id = (uint16_t)(0x0000FFFF & payload[0]);
  813. } else if (data->opcode == AFE_PORT_MOD_EVENT) {
  814. u32 flag_dc_presence[2];
  815. uint32_t *payload = data->payload;
  816. struct afe_port_mod_evt_rsp_hdr *evt_pl =
  817. (struct afe_port_mod_evt_rsp_hdr *)payload;
  818. if (!payload || (data->token >= AFE_MAX_PORTS)) {
  819. pr_err("%s: Error: size %d payload %pK token %d\n",
  820. __func__, data->payload_size,
  821. payload, data->token);
  822. return -EINVAL;
  823. }
  824. if ((evt_pl->module_id == AFE_MODULE_SPEAKER_PROTECTION_V2_EX_VI) &&
  825. (evt_pl->event_id == AFE_PORT_SP_DC_DETECTION_EVENT) &&
  826. (evt_pl->payload_size == sizeof(flag_dc_presence))) {
  827. memcpy(&flag_dc_presence,
  828. (uint8_t *)payload +
  829. sizeof(struct afe_port_mod_evt_rsp_hdr),
  830. evt_pl->payload_size);
  831. if (flag_dc_presence[0] == 1 ||
  832. flag_dc_presence[1] == 1) {
  833. afe_notify_dc_presence();
  834. }
  835. } else if ((evt_pl->module_id ==
  836. AFE_MODULE_SPEAKER_PROTECTION_V4_VI) &&
  837. (evt_pl->event_id ==
  838. AFE_PORT_SP_DC_DETECTION_EVENT)) {
  839. bool dc_detected = false;
  840. uint32_t *num_channels =
  841. (uint32_t *)((uint8_t *)payload +
  842. sizeof(struct afe_port_mod_evt_rsp_hdr));
  843. uint32_t *dc_presence_flag = num_channels + 1;
  844. for (i = 0; i < *num_channels; i++) {
  845. if (dc_presence_flag[i] == 1)
  846. dc_detected = true;
  847. }
  848. if (dc_detected)
  849. afe_notify_dc_presence();
  850. } else if (evt_pl->port_id == AFE_PORT_ID_PRIMARY_SPDIF_TX) {
  851. if (this_afe.pri_spdif_tx_cb) {
  852. this_afe.pri_spdif_tx_cb(data->opcode,
  853. data->token, data->payload,
  854. this_afe.pri_spdif_tx_private_data);
  855. }
  856. afe_notify_spdif_fmt_update(data->payload);
  857. } else if (evt_pl->port_id == AFE_PORT_ID_SECONDARY_SPDIF_TX) {
  858. if (this_afe.sec_spdif_tx_cb) {
  859. this_afe.sec_spdif_tx_cb(data->opcode,
  860. data->token, data->payload,
  861. this_afe.sec_spdif_tx_private_data);
  862. }
  863. afe_notify_spdif_fmt_update(data->payload);
  864. } else {
  865. pr_debug("%s: mod ID = 0x%x event_id = 0x%x\n",
  866. __func__, evt_pl->module_id,
  867. evt_pl->event_id);
  868. }
  869. }
  870. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  871. switch (port_id) {
  872. case RT_PROXY_PORT_001_TX: {
  873. if (this_afe.tx_cb) {
  874. this_afe.tx_cb(data->opcode, data->token,
  875. data->payload,
  876. this_afe.tx_private_data);
  877. }
  878. break;
  879. }
  880. case RT_PROXY_PORT_001_RX: {
  881. if (this_afe.rx_cb) {
  882. this_afe.rx_cb(data->opcode, data->token,
  883. data->payload,
  884. this_afe.rx_private_data);
  885. }
  886. break;
  887. }
  888. default:
  889. pr_debug("%s: default case 0x%x\n", __func__, port_id);
  890. break;
  891. }
  892. }
  893. return 0;
  894. }
  895. /**
  896. * afe_get_port_type -
  897. * Retrieve AFE port type whether RX or TX
  898. *
  899. * @port_id: AFE Port ID number
  900. *
  901. * Returns RX/TX type.
  902. */
  903. int afe_get_port_type(u16 port_id)
  904. {
  905. int ret = MSM_AFE_PORT_TYPE_RX;
  906. switch (port_id) {
  907. case VOICE_RECORD_RX:
  908. case VOICE_RECORD_TX:
  909. ret = MSM_AFE_PORT_TYPE_TX;
  910. break;
  911. case VOICE_PLAYBACK_TX:
  912. case VOICE2_PLAYBACK_TX:
  913. ret = MSM_AFE_PORT_TYPE_RX;
  914. break;
  915. default:
  916. /* Odd numbered ports are TX and Rx are Even numbered */
  917. if (port_id & 0x1)
  918. ret = MSM_AFE_PORT_TYPE_TX;
  919. else
  920. ret = MSM_AFE_PORT_TYPE_RX;
  921. break;
  922. }
  923. return ret;
  924. }
  925. EXPORT_SYMBOL(afe_get_port_type);
  926. int afe_sizeof_cfg_cmd(u16 port_id)
  927. {
  928. int ret_size;
  929. switch (port_id) {
  930. case PRIMARY_I2S_RX:
  931. case PRIMARY_I2S_TX:
  932. case SECONDARY_I2S_RX:
  933. case SECONDARY_I2S_TX:
  934. case MI2S_RX:
  935. case MI2S_TX:
  936. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  937. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  938. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  939. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  940. case AFE_PORT_ID_QUINARY_MI2S_RX:
  941. case AFE_PORT_ID_QUINARY_MI2S_TX:
  942. case AFE_PORT_ID_SENARY_MI2S_RX:
  943. case AFE_PORT_ID_SENARY_MI2S_TX:
  944. ret_size = SIZEOF_CFG_CMD(afe_param_id_i2s_cfg);
  945. break;
  946. case AFE_PORT_ID_PRIMARY_META_MI2S_RX:
  947. case AFE_PORT_ID_SECONDARY_META_MI2S_RX:
  948. ret_size = SIZEOF_CFG_CMD(afe_param_id_meta_i2s_cfg);
  949. break;
  950. case HDMI_RX:
  951. case DISPLAY_PORT_RX:
  952. ret_size =
  953. SIZEOF_CFG_CMD(afe_param_id_hdmi_multi_chan_audio_cfg);
  954. break;
  955. case AFE_PORT_ID_PRIMARY_SPDIF_RX:
  956. case AFE_PORT_ID_PRIMARY_SPDIF_TX:
  957. case AFE_PORT_ID_SECONDARY_SPDIF_RX:
  958. case AFE_PORT_ID_SECONDARY_SPDIF_TX:
  959. ret_size =
  960. SIZEOF_CFG_CMD(afe_param_id_spdif_cfg_v2);
  961. break;
  962. case SLIMBUS_0_RX:
  963. case SLIMBUS_0_TX:
  964. case SLIMBUS_1_RX:
  965. case SLIMBUS_1_TX:
  966. case SLIMBUS_2_RX:
  967. case SLIMBUS_2_TX:
  968. case SLIMBUS_3_RX:
  969. case SLIMBUS_3_TX:
  970. case SLIMBUS_4_RX:
  971. case SLIMBUS_4_TX:
  972. case SLIMBUS_5_RX:
  973. case SLIMBUS_5_TX:
  974. case SLIMBUS_6_RX:
  975. case SLIMBUS_6_TX:
  976. case SLIMBUS_7_RX:
  977. case SLIMBUS_7_TX:
  978. case SLIMBUS_8_RX:
  979. case SLIMBUS_8_TX:
  980. case SLIMBUS_9_RX:
  981. case SLIMBUS_9_TX:
  982. ret_size = SIZEOF_CFG_CMD(afe_param_id_slimbus_cfg);
  983. break;
  984. case VOICE_PLAYBACK_TX:
  985. case VOICE2_PLAYBACK_TX:
  986. case VOICE_RECORD_RX:
  987. case VOICE_RECORD_TX:
  988. ret_size = SIZEOF_CFG_CMD(afe_param_id_pseudo_port_cfg);
  989. break;
  990. case RT_PROXY_PORT_001_RX:
  991. case RT_PROXY_PORT_001_TX:
  992. case RT_PROXY_PORT_002_RX:
  993. case RT_PROXY_PORT_002_TX:
  994. ret_size = SIZEOF_CFG_CMD(afe_param_id_rt_proxy_port_cfg);
  995. break;
  996. case AFE_PORT_ID_USB_RX:
  997. case AFE_PORT_ID_USB_TX:
  998. ret_size = SIZEOF_CFG_CMD(afe_param_id_usb_audio_cfg);
  999. break;
  1000. case AFE_PORT_ID_WSA_CODEC_DMA_RX_0:
  1001. case AFE_PORT_ID_WSA_CODEC_DMA_TX_0:
  1002. case AFE_PORT_ID_WSA_CODEC_DMA_RX_1:
  1003. case AFE_PORT_ID_WSA_CODEC_DMA_TX_1:
  1004. case AFE_PORT_ID_WSA_CODEC_DMA_TX_2:
  1005. case AFE_PORT_ID_VA_CODEC_DMA_TX_0:
  1006. case AFE_PORT_ID_VA_CODEC_DMA_TX_1:
  1007. case AFE_PORT_ID_VA_CODEC_DMA_TX_2:
  1008. case AFE_PORT_ID_RX_CODEC_DMA_RX_0:
  1009. case AFE_PORT_ID_TX_CODEC_DMA_TX_0:
  1010. case AFE_PORT_ID_RX_CODEC_DMA_RX_1:
  1011. case AFE_PORT_ID_TX_CODEC_DMA_TX_1:
  1012. case AFE_PORT_ID_RX_CODEC_DMA_RX_2:
  1013. case AFE_PORT_ID_TX_CODEC_DMA_TX_2:
  1014. case AFE_PORT_ID_RX_CODEC_DMA_RX_3:
  1015. case AFE_PORT_ID_TX_CODEC_DMA_TX_3:
  1016. case AFE_PORT_ID_RX_CODEC_DMA_RX_4:
  1017. case AFE_PORT_ID_TX_CODEC_DMA_TX_4:
  1018. case AFE_PORT_ID_RX_CODEC_DMA_RX_5:
  1019. case AFE_PORT_ID_TX_CODEC_DMA_TX_5:
  1020. case AFE_PORT_ID_RX_CODEC_DMA_RX_6:
  1021. case AFE_PORT_ID_RX_CODEC_DMA_RX_7:
  1022. ret_size = SIZEOF_CFG_CMD(afe_param_id_cdc_dma_cfg_t);
  1023. break;
  1024. case AFE_PORT_ID_PRIMARY_PCM_RX:
  1025. case AFE_PORT_ID_PRIMARY_PCM_TX:
  1026. case AFE_PORT_ID_SECONDARY_PCM_RX:
  1027. case AFE_PORT_ID_SECONDARY_PCM_TX:
  1028. case AFE_PORT_ID_TERTIARY_PCM_RX:
  1029. case AFE_PORT_ID_TERTIARY_PCM_TX:
  1030. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  1031. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  1032. case AFE_PORT_ID_QUINARY_PCM_RX:
  1033. case AFE_PORT_ID_QUINARY_PCM_TX:
  1034. case AFE_PORT_ID_SENARY_PCM_RX:
  1035. case AFE_PORT_ID_SENARY_PCM_TX:
  1036. default:
  1037. pr_debug("%s: default case 0x%x\n", __func__, port_id);
  1038. ret_size = SIZEOF_CFG_CMD(afe_param_id_pcm_cfg);
  1039. break;
  1040. }
  1041. return ret_size;
  1042. }
  1043. /**
  1044. * afe_q6_interface_prepare -
  1045. * wrapper API to check Q6 AFE registered to APR otherwise registers
  1046. *
  1047. * Returns 0 on success or error on failure.
  1048. */
  1049. int afe_q6_interface_prepare(void)
  1050. {
  1051. int ret = 0;
  1052. pr_debug("%s:\n", __func__);
  1053. if (this_afe.apr == NULL) {
  1054. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  1055. 0xFFFFFFFF, &this_afe);
  1056. if (this_afe.apr == NULL) {
  1057. pr_err("%s: Unable to register AFE\n", __func__);
  1058. ret = -ENETRESET;
  1059. }
  1060. rtac_set_afe_handle(this_afe.apr);
  1061. }
  1062. return ret;
  1063. }
  1064. EXPORT_SYMBOL(afe_q6_interface_prepare);
  1065. /*
  1066. * afe_apr_send_pkt : returns 0 on success, negative otherwise.
  1067. */
  1068. static int afe_apr_send_pkt(void *data, wait_queue_head_t *wait)
  1069. {
  1070. int ret;
  1071. mutex_lock(&this_afe.afe_apr_lock);
  1072. if (wait)
  1073. atomic_set(&this_afe.state, 1);
  1074. atomic_set(&this_afe.status, 0);
  1075. ret = apr_send_pkt(this_afe.apr, data);
  1076. if (ret > 0) {
  1077. if (wait) {
  1078. ret = wait_event_timeout(*wait,
  1079. (atomic_read(&this_afe.state) == 0),
  1080. msecs_to_jiffies(2 * TIMEOUT_MS));
  1081. if (!ret) {
  1082. pr_err_ratelimited("%s: request timedout\n",
  1083. __func__);
  1084. ret = -ETIMEDOUT;
  1085. trace_printk("%s: wait for ADSP response timed out\n",
  1086. __func__);
  1087. } else if (atomic_read(&this_afe.status) > 0) {
  1088. pr_err("%s: DSP returned error[%s]\n", __func__,
  1089. adsp_err_get_err_str(atomic_read(
  1090. &this_afe.status)));
  1091. ret = adsp_err_get_lnx_err_code(
  1092. atomic_read(&this_afe.status));
  1093. } else {
  1094. ret = 0;
  1095. }
  1096. } else {
  1097. ret = 0;
  1098. }
  1099. } else if (ret == 0) {
  1100. pr_err("%s: packet not transmitted\n", __func__);
  1101. /* apr_send_pkt can return 0 when nothing is transmitted */
  1102. ret = -EINVAL;
  1103. }
  1104. pr_debug("%s: leave %d\n", __func__, ret);
  1105. mutex_unlock(&this_afe.afe_apr_lock);
  1106. return ret;
  1107. }
  1108. /*
  1109. * afe_apr_send_clk_pkt : returns 0 on success, negative otherwise.
  1110. */
  1111. static int afe_apr_send_clk_pkt(void *data, wait_queue_head_t *wait)
  1112. {
  1113. int ret;
  1114. if (wait)
  1115. atomic_set(&this_afe.clk_state, 1);
  1116. atomic_set(&this_afe.clk_status, 0);
  1117. ret = apr_send_pkt(this_afe.apr, data);
  1118. if (ret > 0) {
  1119. if (wait) {
  1120. ret = wait_event_timeout(*wait,
  1121. (atomic_read(&this_afe.clk_state) == 0),
  1122. msecs_to_jiffies(2 * TIMEOUT_MS));
  1123. if (!ret) {
  1124. pr_err("%s: timeout\n", __func__);
  1125. ret = -ETIMEDOUT;
  1126. } else if (atomic_read(&this_afe.clk_status) > 0) {
  1127. pr_err("%s: DSP returned error[%s]\n", __func__,
  1128. adsp_err_get_err_str(atomic_read(
  1129. &this_afe.clk_status)));
  1130. ret = adsp_err_get_lnx_err_code(
  1131. atomic_read(&this_afe.clk_status));
  1132. } else {
  1133. ret = 0;
  1134. }
  1135. } else {
  1136. ret = 0;
  1137. }
  1138. } else if (ret == 0) {
  1139. pr_err("%s: packet not transmitted\n", __func__);
  1140. /* apr_send_pkt can return 0 when nothing is transmitted */
  1141. ret = -EINVAL;
  1142. }
  1143. pr_debug("%s: leave %d\n", __func__, ret);
  1144. return ret;
  1145. }
  1146. /* This function shouldn't be called directly. Instead call q6afe_set_params. */
  1147. static int q6afe_set_params_v2(u16 port_id, int index,
  1148. struct mem_mapping_hdr *mem_hdr,
  1149. u8 *packed_param_data, u32 packed_data_size)
  1150. {
  1151. struct afe_port_cmd_set_param_v2 *set_param = NULL;
  1152. uint32_t size = sizeof(struct afe_port_cmd_set_param_v2);
  1153. int rc = 0;
  1154. if (packed_param_data != NULL)
  1155. size += packed_data_size;
  1156. set_param = kzalloc(size, GFP_KERNEL);
  1157. if (set_param == NULL)
  1158. return -ENOMEM;
  1159. set_param->apr_hdr.hdr_field =
  1160. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  1161. APR_PKT_VER);
  1162. set_param->apr_hdr.pkt_size = size;
  1163. set_param->apr_hdr.src_port = 0;
  1164. set_param->apr_hdr.dest_port = 0;
  1165. set_param->apr_hdr.token = index;
  1166. set_param->apr_hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1167. set_param->port_id = port_id;
  1168. if (packed_data_size > U16_MAX) {
  1169. pr_err("%s: Invalid data size for set params V2 %d\n", __func__,
  1170. packed_data_size);
  1171. rc = -EINVAL;
  1172. goto done;
  1173. }
  1174. set_param->payload_size = packed_data_size;
  1175. if (mem_hdr != NULL) {
  1176. set_param->mem_hdr = *mem_hdr;
  1177. } else if (packed_param_data != NULL) {
  1178. memcpy(&set_param->param_data, packed_param_data,
  1179. packed_data_size);
  1180. } else {
  1181. pr_err("%s: Both memory header and param data are NULL\n",
  1182. __func__);
  1183. rc = -EINVAL;
  1184. goto done;
  1185. }
  1186. rc = afe_apr_send_pkt(set_param, &this_afe.wait[index]);
  1187. done:
  1188. kfree(set_param);
  1189. return rc;
  1190. }
  1191. /* This function shouldn't be called directly. Instead call q6afe_set_params. */
  1192. static int q6afe_set_params_v3(u16 port_id, int index,
  1193. struct mem_mapping_hdr *mem_hdr,
  1194. u8 *packed_param_data, u32 packed_data_size)
  1195. {
  1196. struct afe_port_cmd_set_param_v3 *set_param = NULL;
  1197. uint32_t size = sizeof(struct afe_port_cmd_set_param_v3);
  1198. int rc = 0;
  1199. if (packed_param_data != NULL)
  1200. size += packed_data_size;
  1201. set_param = kzalloc(size, GFP_KERNEL);
  1202. if (set_param == NULL)
  1203. return -ENOMEM;
  1204. set_param->apr_hdr.hdr_field =
  1205. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  1206. APR_PKT_VER);
  1207. set_param->apr_hdr.pkt_size = size;
  1208. set_param->apr_hdr.src_port = 0;
  1209. set_param->apr_hdr.dest_port = 0;
  1210. set_param->apr_hdr.token = index;
  1211. set_param->apr_hdr.opcode = AFE_PORT_CMD_SET_PARAM_V3;
  1212. set_param->port_id = port_id;
  1213. set_param->payload_size = packed_data_size;
  1214. if (mem_hdr != NULL) {
  1215. set_param->mem_hdr = *mem_hdr;
  1216. } else if (packed_param_data != NULL) {
  1217. memcpy(&set_param->param_data, packed_param_data,
  1218. packed_data_size);
  1219. } else {
  1220. pr_err("%s: Both memory header and param data are NULL\n",
  1221. __func__);
  1222. rc = -EINVAL;
  1223. goto done;
  1224. }
  1225. rc = afe_apr_send_pkt(set_param, &this_afe.wait[index]);
  1226. done:
  1227. kfree(set_param);
  1228. return rc;
  1229. }
  1230. static int q6afe_set_params(u16 port_id, int index,
  1231. struct mem_mapping_hdr *mem_hdr,
  1232. u8 *packed_param_data, u32 packed_data_size)
  1233. {
  1234. int ret = 0;
  1235. ret = afe_q6_interface_prepare();
  1236. if (ret != 0) {
  1237. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  1238. return ret;
  1239. }
  1240. port_id = q6audio_get_port_id(port_id);
  1241. ret = q6audio_validate_port(port_id);
  1242. if (ret < 0) {
  1243. pr_err("%s: Not a valid port id = 0x%x ret %d\n", __func__,
  1244. port_id, ret);
  1245. return -EINVAL;
  1246. }
  1247. if (index < 0 || index >= AFE_MAX_PORTS) {
  1248. pr_err("%s: AFE port index[%d] invalid\n", __func__, index);
  1249. return -EINVAL;
  1250. }
  1251. if (q6common_is_instance_id_supported())
  1252. return q6afe_set_params_v3(port_id, index, mem_hdr,
  1253. packed_param_data, packed_data_size);
  1254. else
  1255. return q6afe_set_params_v2(port_id, index, mem_hdr,
  1256. packed_param_data, packed_data_size);
  1257. }
  1258. static int q6afe_pack_and_set_param_in_band(u16 port_id, int index,
  1259. struct param_hdr_v3 param_hdr,
  1260. u8 *param_data)
  1261. {
  1262. u8 *packed_param_data = NULL;
  1263. int packed_data_size = sizeof(union param_hdrs) + param_hdr.param_size;
  1264. int ret;
  1265. packed_param_data = kzalloc(packed_data_size, GFP_KERNEL);
  1266. if (packed_param_data == NULL)
  1267. return -ENOMEM;
  1268. ret = q6common_pack_pp_params(packed_param_data, &param_hdr, param_data,
  1269. &packed_data_size);
  1270. if (ret) {
  1271. pr_err("%s: Failed to pack param header and data, error %d\n",
  1272. __func__, ret);
  1273. goto fail_cmd;
  1274. }
  1275. ret = q6afe_set_params(port_id, index, NULL, packed_param_data,
  1276. packed_data_size);
  1277. fail_cmd:
  1278. kfree(packed_param_data);
  1279. return ret;
  1280. }
  1281. static int q6afe_set_aanc_level(void)
  1282. {
  1283. struct param_hdr_v3 param_hdr;
  1284. struct afe_param_id_aanc_noise_reduction aanc_noise_level;
  1285. int ret = 0;
  1286. uint16_t tx_port = 0;
  1287. if (!this_afe.aanc_info.aanc_active)
  1288. return -EINVAL;
  1289. pr_debug("%s: level: %d\n", __func__, this_afe.aanc_info.level);
  1290. memset(&aanc_noise_level, 0, sizeof(aanc_noise_level));
  1291. aanc_noise_level.minor_version = 1;
  1292. aanc_noise_level.ad_beta = this_afe.aanc_info.level;
  1293. memset(&param_hdr, 0, sizeof(param_hdr));
  1294. param_hdr.module_id = AFE_MODULE_AANC;
  1295. param_hdr.instance_id = INSTANCE_ID_0;
  1296. param_hdr.param_id = AFE_PARAM_ID_AANC_NOISE_REDUCTION;
  1297. param_hdr.param_size = sizeof(struct afe_param_id_aanc_noise_reduction);
  1298. tx_port = this_afe.aanc_info.aanc_tx_port;
  1299. ret = q6afe_pack_and_set_param_in_band(tx_port,
  1300. q6audio_get_port_index(tx_port),
  1301. param_hdr,
  1302. (u8 *) &aanc_noise_level);
  1303. if (ret)
  1304. pr_err("%s: AANC noise level enable failed for tx_port 0x%x ret %d\n",
  1305. __func__, tx_port, ret);
  1306. return ret;
  1307. }
  1308. /**
  1309. * afe_set_aanc_noise_level - controls aanc noise reduction strength
  1310. *
  1311. * @level: Noise level to be controlled
  1312. *
  1313. * Returns 0 on success or error on failure.
  1314. */
  1315. int afe_set_aanc_noise_level(int level)
  1316. {
  1317. int ret = 0;
  1318. if (this_afe.aanc_info.level == level)
  1319. return ret;
  1320. mutex_lock(&this_afe.afe_cmd_lock);
  1321. this_afe.aanc_info.level = level;
  1322. ret = q6afe_set_aanc_level();
  1323. mutex_unlock(&this_afe.afe_cmd_lock);
  1324. return ret;
  1325. }
  1326. EXPORT_SYMBOL(afe_set_aanc_noise_level);
  1327. /* This function shouldn't be called directly. Instead call q6afe_get_param. */
  1328. static int q6afe_get_params_v2(u16 port_id, int index,
  1329. struct mem_mapping_hdr *mem_hdr,
  1330. struct param_hdr_v3 *param_hdr)
  1331. {
  1332. struct afe_port_cmd_get_param_v2 afe_get_param;
  1333. u32 param_size = param_hdr->param_size;
  1334. memset(&afe_get_param, 0, sizeof(afe_get_param));
  1335. afe_get_param.apr_hdr.hdr_field =
  1336. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  1337. APR_PKT_VER);
  1338. afe_get_param.apr_hdr.pkt_size = sizeof(afe_get_param);
  1339. afe_get_param.apr_hdr.src_port = 0;
  1340. afe_get_param.apr_hdr.dest_port = 0;
  1341. afe_get_param.apr_hdr.token = index;
  1342. afe_get_param.apr_hdr.opcode = AFE_PORT_CMD_GET_PARAM_V2;
  1343. afe_get_param.port_id = port_id;
  1344. afe_get_param.payload_size = sizeof(struct param_hdr_v1) + param_size;
  1345. if (mem_hdr != NULL)
  1346. afe_get_param.mem_hdr = *mem_hdr;
  1347. /* Set MID and PID in command */
  1348. afe_get_param.module_id = param_hdr->module_id;
  1349. afe_get_param.param_id = param_hdr->param_id;
  1350. /* Set param header in payload */
  1351. afe_get_param.param_hdr.module_id = param_hdr->module_id;
  1352. afe_get_param.param_hdr.param_id = param_hdr->param_id;
  1353. afe_get_param.param_hdr.param_size = param_size;
  1354. return afe_apr_send_pkt(&afe_get_param, &this_afe.wait[index]);
  1355. }
  1356. /* This function shouldn't be called directly. Instead call q6afe_get_param. */
  1357. static int q6afe_get_params_v3(u16 port_id, int index,
  1358. struct mem_mapping_hdr *mem_hdr,
  1359. struct param_hdr_v3 *param_hdr)
  1360. {
  1361. struct afe_port_cmd_get_param_v3 afe_get_param;
  1362. memset(&afe_get_param, 0, sizeof(afe_get_param));
  1363. afe_get_param.apr_hdr.hdr_field =
  1364. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  1365. APR_PKT_VER);
  1366. afe_get_param.apr_hdr.pkt_size = sizeof(afe_get_param);
  1367. afe_get_param.apr_hdr.src_port = 0;
  1368. afe_get_param.apr_hdr.dest_port = 0;
  1369. afe_get_param.apr_hdr.token = index;
  1370. afe_get_param.apr_hdr.opcode = AFE_PORT_CMD_GET_PARAM_V3;
  1371. afe_get_param.port_id = port_id;
  1372. if (mem_hdr != NULL)
  1373. afe_get_param.mem_hdr = *mem_hdr;
  1374. /* Set param header in command, no payload in V3 */
  1375. afe_get_param.param_hdr = *param_hdr;
  1376. return afe_apr_send_pkt(&afe_get_param, &this_afe.wait[index]);
  1377. }
  1378. /*
  1379. * Calling functions copy param data directly from this_afe. Do not copy data
  1380. * back to caller here.
  1381. */
  1382. static int q6afe_get_params(u16 port_id, struct mem_mapping_hdr *mem_hdr,
  1383. struct param_hdr_v3 *param_hdr)
  1384. {
  1385. int index;
  1386. int ret;
  1387. ret = afe_q6_interface_prepare();
  1388. if (ret != 0) {
  1389. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  1390. return ret;
  1391. }
  1392. port_id = q6audio_get_port_id(port_id);
  1393. ret = q6audio_validate_port(port_id);
  1394. if (ret < 0) {
  1395. pr_err("%s: Not a valid port id = 0x%x ret %d\n", __func__,
  1396. port_id, ret);
  1397. return -EINVAL;
  1398. }
  1399. index = q6audio_get_port_index(port_id);
  1400. if (index < 0 || index >= AFE_MAX_PORTS) {
  1401. pr_err("%s: AFE port index[%d] invalid\n", __func__, index);
  1402. return -EINVAL;
  1403. }
  1404. if (q6common_is_instance_id_supported())
  1405. return q6afe_get_params_v3(port_id, index, NULL, param_hdr);
  1406. else
  1407. return q6afe_get_params_v2(port_id, index, NULL, param_hdr);
  1408. }
  1409. /*
  1410. * This function shouldn't be called directly. Instead call
  1411. * q6afe_svc_set_params.
  1412. */
  1413. static int q6afe_svc_set_params_v1(int index, struct mem_mapping_hdr *mem_hdr,
  1414. u8 *packed_param_data, u32 packed_data_size)
  1415. {
  1416. struct afe_svc_cmd_set_param_v1 *svc_set_param = NULL;
  1417. uint32_t size = sizeof(struct afe_svc_cmd_set_param_v1);
  1418. int rc = 0;
  1419. if (packed_param_data != NULL)
  1420. size += packed_data_size;
  1421. svc_set_param = kzalloc(size, GFP_KERNEL);
  1422. if (svc_set_param == NULL)
  1423. return -ENOMEM;
  1424. svc_set_param->apr_hdr.hdr_field =
  1425. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  1426. APR_PKT_VER);
  1427. svc_set_param->apr_hdr.pkt_size = size;
  1428. svc_set_param->apr_hdr.src_port = 0;
  1429. svc_set_param->apr_hdr.dest_port = 0;
  1430. svc_set_param->apr_hdr.token = index;
  1431. svc_set_param->apr_hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  1432. svc_set_param->payload_size = packed_data_size;
  1433. if (mem_hdr != NULL) {
  1434. /* Out of band case. */
  1435. svc_set_param->mem_hdr = *mem_hdr;
  1436. } else if (packed_param_data != NULL) {
  1437. /* In band case. */
  1438. memcpy(&svc_set_param->param_data, packed_param_data,
  1439. packed_data_size);
  1440. } else {
  1441. pr_err("%s: Both memory header and param data are NULL\n",
  1442. __func__);
  1443. rc = -EINVAL;
  1444. goto done;
  1445. }
  1446. rc = afe_apr_send_pkt(svc_set_param, &this_afe.wait[index]);
  1447. done:
  1448. kfree(svc_set_param);
  1449. return rc;
  1450. }
  1451. /*
  1452. * This function shouldn't be called directly. Instead call
  1453. * q6afe_svc_set_params.
  1454. */
  1455. static int q6afe_svc_set_params_v2(int index, struct mem_mapping_hdr *mem_hdr,
  1456. u8 *packed_param_data, u32 packed_data_size)
  1457. {
  1458. struct afe_svc_cmd_set_param_v2 *svc_set_param = NULL;
  1459. uint16_t size = sizeof(struct afe_svc_cmd_set_param_v2);
  1460. int rc = 0;
  1461. if (packed_param_data != NULL)
  1462. size += packed_data_size;
  1463. svc_set_param = kzalloc(size, GFP_KERNEL);
  1464. if (svc_set_param == NULL)
  1465. return -ENOMEM;
  1466. svc_set_param->apr_hdr.hdr_field =
  1467. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  1468. APR_PKT_VER);
  1469. svc_set_param->apr_hdr.pkt_size = size;
  1470. svc_set_param->apr_hdr.src_port = 0;
  1471. svc_set_param->apr_hdr.dest_port = 0;
  1472. svc_set_param->apr_hdr.token = index;
  1473. svc_set_param->apr_hdr.opcode = AFE_SVC_CMD_SET_PARAM_V2;
  1474. svc_set_param->payload_size = packed_data_size;
  1475. if (mem_hdr != NULL) {
  1476. /* Out of band case. */
  1477. svc_set_param->mem_hdr = *mem_hdr;
  1478. } else if (packed_param_data != NULL) {
  1479. /* In band case. */
  1480. memcpy(&svc_set_param->param_data, packed_param_data,
  1481. packed_data_size);
  1482. } else {
  1483. pr_err("%s: Both memory header and param data are NULL\n",
  1484. __func__);
  1485. rc = -EINVAL;
  1486. goto done;
  1487. }
  1488. rc = afe_apr_send_pkt(svc_set_param, &this_afe.wait[index]);
  1489. done:
  1490. kfree(svc_set_param);
  1491. return rc;
  1492. }
  1493. /*
  1494. * This function shouldn't be called directly. Instead call
  1495. * q6afe_clk_set_params.
  1496. */
  1497. static int q6afe_clk_set_params_v1(int index, struct mem_mapping_hdr *mem_hdr,
  1498. u8 *packed_param_data, u32 packed_data_size)
  1499. {
  1500. struct afe_svc_cmd_set_param_v1 *svc_set_param = NULL;
  1501. uint32_t size = sizeof(struct afe_svc_cmd_set_param_v1);
  1502. int rc = 0;
  1503. if (packed_param_data != NULL)
  1504. size += packed_data_size;
  1505. svc_set_param = kzalloc(size, GFP_KERNEL);
  1506. if (svc_set_param == NULL)
  1507. return -ENOMEM;
  1508. svc_set_param->apr_hdr.hdr_field =
  1509. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  1510. APR_PKT_VER);
  1511. svc_set_param->apr_hdr.pkt_size = size;
  1512. svc_set_param->apr_hdr.src_port = 0;
  1513. svc_set_param->apr_hdr.dest_port = 0;
  1514. svc_set_param->apr_hdr.token = AFE_CLK_TOKEN;
  1515. svc_set_param->apr_hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  1516. svc_set_param->payload_size = packed_data_size;
  1517. if (mem_hdr != NULL) {
  1518. /* Out of band case. */
  1519. svc_set_param->mem_hdr = *mem_hdr;
  1520. } else if (packed_param_data != NULL) {
  1521. /* In band case. */
  1522. memcpy(&svc_set_param->param_data, packed_param_data,
  1523. packed_data_size);
  1524. } else {
  1525. pr_err("%s: Both memory header and param data are NULL\n",
  1526. __func__);
  1527. rc = -EINVAL;
  1528. goto done;
  1529. }
  1530. rc = afe_apr_send_clk_pkt(svc_set_param, &this_afe.clk_wait);
  1531. done:
  1532. kfree(svc_set_param);
  1533. return rc;
  1534. }
  1535. /*
  1536. * This function shouldn't be called directly. Instead call
  1537. * q6afe_clk_set_params.
  1538. */
  1539. static int q6afe_clk_set_params_v2(int index, struct mem_mapping_hdr *mem_hdr,
  1540. u8 *packed_param_data, u32 packed_data_size)
  1541. {
  1542. struct afe_svc_cmd_set_param_v2 *svc_set_param = NULL;
  1543. uint16_t size = sizeof(struct afe_svc_cmd_set_param_v2);
  1544. int rc = 0;
  1545. if (packed_param_data != NULL)
  1546. size += packed_data_size;
  1547. svc_set_param = kzalloc(size, GFP_KERNEL);
  1548. if (svc_set_param == NULL)
  1549. return -ENOMEM;
  1550. svc_set_param->apr_hdr.hdr_field =
  1551. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  1552. APR_PKT_VER);
  1553. svc_set_param->apr_hdr.pkt_size = size;
  1554. svc_set_param->apr_hdr.src_port = 0;
  1555. svc_set_param->apr_hdr.dest_port = 0;
  1556. svc_set_param->apr_hdr.token = AFE_CLK_TOKEN;
  1557. svc_set_param->apr_hdr.opcode = AFE_SVC_CMD_SET_PARAM_V2;
  1558. svc_set_param->payload_size = packed_data_size;
  1559. if (mem_hdr != NULL) {
  1560. /* Out of band case. */
  1561. svc_set_param->mem_hdr = *mem_hdr;
  1562. } else if (packed_param_data != NULL) {
  1563. /* In band case. */
  1564. memcpy(&svc_set_param->param_data, packed_param_data,
  1565. packed_data_size);
  1566. } else {
  1567. pr_err("%s: Both memory header and param data are NULL\n",
  1568. __func__);
  1569. rc = -EINVAL;
  1570. goto done;
  1571. }
  1572. rc = afe_apr_send_clk_pkt(svc_set_param, &this_afe.clk_wait);
  1573. done:
  1574. kfree(svc_set_param);
  1575. return rc;
  1576. }
  1577. static int q6afe_clk_set_params(int index, struct mem_mapping_hdr *mem_hdr,
  1578. u8 *packed_param_data, u32 packed_data_size,
  1579. bool is_iid_supported)
  1580. {
  1581. int ret;
  1582. ret = afe_q6_interface_prepare();
  1583. if (ret != 0) {
  1584. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  1585. return ret;
  1586. }
  1587. if (is_iid_supported)
  1588. return q6afe_clk_set_params_v2(index, mem_hdr,
  1589. packed_param_data,
  1590. packed_data_size);
  1591. else
  1592. return q6afe_clk_set_params_v1(index, mem_hdr,
  1593. packed_param_data,
  1594. packed_data_size);
  1595. }
  1596. static int q6afe_svc_set_params(int index, struct mem_mapping_hdr *mem_hdr,
  1597. u8 *packed_param_data, u32 packed_data_size,
  1598. bool is_iid_supported)
  1599. {
  1600. int ret;
  1601. ret = afe_q6_interface_prepare();
  1602. if (ret != 0) {
  1603. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  1604. return ret;
  1605. }
  1606. if (is_iid_supported)
  1607. return q6afe_svc_set_params_v2(index, mem_hdr,
  1608. packed_param_data,
  1609. packed_data_size);
  1610. else
  1611. return q6afe_svc_set_params_v1(index, mem_hdr,
  1612. packed_param_data,
  1613. packed_data_size);
  1614. }
  1615. static int q6afe_svc_pack_and_set_param_in_band(int index,
  1616. struct param_hdr_v3 param_hdr,
  1617. u8 *param_data)
  1618. {
  1619. u8 *packed_param_data = NULL;
  1620. u32 packed_data_size =
  1621. sizeof(struct param_hdr_v3) + param_hdr.param_size;
  1622. int ret = 0;
  1623. bool is_iid_supported = q6common_is_instance_id_supported();
  1624. packed_param_data = kzalloc(packed_data_size, GFP_KERNEL);
  1625. if (!packed_param_data)
  1626. return -ENOMEM;
  1627. ret = q6common_pack_pp_params_v2(packed_param_data, &param_hdr,
  1628. param_data, &packed_data_size,
  1629. is_iid_supported);
  1630. if (ret) {
  1631. pr_err("%s: Failed to pack parameter header and data, error %d\n",
  1632. __func__, ret);
  1633. goto done;
  1634. }
  1635. if (param_hdr.module_id == AFE_MODULE_CLOCK_SET)
  1636. ret = q6afe_clk_set_params(index, NULL, packed_param_data,
  1637. packed_data_size, is_iid_supported);
  1638. else
  1639. ret = q6afe_svc_set_params(index, NULL, packed_param_data,
  1640. packed_data_size, is_iid_supported);
  1641. done:
  1642. kfree(packed_param_data);
  1643. return ret;
  1644. }
  1645. static int afe_send_cal_block(u16 port_id, struct cal_block_data *cal_block)
  1646. {
  1647. struct mem_mapping_hdr mem_hdr;
  1648. int payload_size = 0;
  1649. int result = 0;
  1650. memset(&mem_hdr, 0, sizeof(mem_hdr));
  1651. if (!cal_block) {
  1652. pr_debug("%s: No AFE cal to send!\n", __func__);
  1653. result = -EINVAL;
  1654. goto done;
  1655. }
  1656. if (cal_block->cal_data.size <= 0) {
  1657. pr_debug("%s: AFE cal has invalid size!\n", __func__);
  1658. result = -EINVAL;
  1659. goto done;
  1660. }
  1661. payload_size = cal_block->cal_data.size;
  1662. mem_hdr.data_payload_addr_lsw =
  1663. lower_32_bits(cal_block->cal_data.paddr);
  1664. mem_hdr.data_payload_addr_msw =
  1665. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  1666. mem_hdr.mem_map_handle = cal_block->map_data.q6map_handle;
  1667. pr_debug("%s: AFE cal sent for device port = 0x%x, cal size = %zd, cal addr = 0x%pK\n",
  1668. __func__, port_id,
  1669. cal_block->cal_data.size, &cal_block->cal_data.paddr);
  1670. result = q6afe_set_params(port_id, q6audio_get_port_index(port_id),
  1671. &mem_hdr, NULL, payload_size);
  1672. if (result)
  1673. pr_err("%s: AFE cal for port 0x%x failed %d\n",
  1674. __func__, port_id, result);
  1675. done:
  1676. return result;
  1677. }
  1678. static int afe_send_custom_topology_block(struct cal_block_data *cal_block)
  1679. {
  1680. int result = 0;
  1681. int index = 0;
  1682. struct cmd_set_topologies afe_cal;
  1683. if (!cal_block) {
  1684. pr_err("%s: No AFE SVC cal to send!\n", __func__);
  1685. return -EINVAL;
  1686. }
  1687. if (cal_block->cal_data.size <= 0) {
  1688. pr_err("%s: AFE SVC cal has invalid size: %zd!\n",
  1689. __func__, cal_block->cal_data.size);
  1690. return -EINVAL;
  1691. }
  1692. afe_cal.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1693. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1694. afe_cal.hdr.pkt_size = sizeof(afe_cal);
  1695. afe_cal.hdr.src_port = 0;
  1696. afe_cal.hdr.dest_port = 0;
  1697. afe_cal.hdr.token = index;
  1698. afe_cal.hdr.opcode = AFE_CMD_ADD_TOPOLOGIES;
  1699. afe_cal.payload_size = cal_block->cal_data.size;
  1700. afe_cal.payload_addr_lsw =
  1701. lower_32_bits(cal_block->cal_data.paddr);
  1702. afe_cal.payload_addr_msw =
  1703. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  1704. afe_cal.mem_map_handle = cal_block->map_data.q6map_handle;
  1705. pr_debug("%s:cmd_id:0x%x calsize:%zd memmap_hdl:0x%x caladdr:0x%pK",
  1706. __func__, AFE_CMD_ADD_TOPOLOGIES, cal_block->cal_data.size,
  1707. afe_cal.mem_map_handle, &cal_block->cal_data.paddr);
  1708. result = afe_apr_send_pkt(&afe_cal, &this_afe.wait[index]);
  1709. if (result)
  1710. pr_err("%s: AFE send topology for command 0x%x failed %d\n",
  1711. __func__, AFE_CMD_ADD_TOPOLOGIES, result);
  1712. return result;
  1713. }
  1714. static void afe_send_custom_topology(void)
  1715. {
  1716. struct cal_block_data *cal_block = NULL;
  1717. int cal_index = AFE_CUST_TOPOLOGY_CAL;
  1718. int ret;
  1719. if (this_afe.cal_data[cal_index] == NULL) {
  1720. pr_err("%s: cal_index %d not allocated!\n",
  1721. __func__, cal_index);
  1722. return;
  1723. }
  1724. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  1725. if (!this_afe.set_custom_topology)
  1726. goto unlock;
  1727. this_afe.set_custom_topology = 0;
  1728. cal_block = cal_utils_get_only_cal_block(this_afe.cal_data[cal_index]);
  1729. if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
  1730. pr_err("%s cal_block not found!!\n", __func__);
  1731. goto unlock;
  1732. }
  1733. pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
  1734. ret = remap_cal_data(cal_block, cal_index);
  1735. if (ret) {
  1736. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  1737. __func__, cal_index);
  1738. goto unlock;
  1739. }
  1740. ret = afe_send_custom_topology_block(cal_block);
  1741. if (ret < 0) {
  1742. pr_err("%s: No cal sent for cal_index %d! ret %d\n",
  1743. __func__, cal_index, ret);
  1744. goto unlock;
  1745. }
  1746. pr_debug("%s:sent custom topology for AFE\n", __func__);
  1747. unlock:
  1748. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  1749. }
  1750. static int afe_spk_ramp_dn_cfg(int port)
  1751. {
  1752. struct param_hdr_v3 param_info;
  1753. int ret = -EINVAL;
  1754. memset(&param_info, 0, sizeof(param_info));
  1755. if (afe_get_port_type(port) != MSM_AFE_PORT_TYPE_RX) {
  1756. pr_debug("%s: port doesn't match 0x%x\n", __func__, port);
  1757. return 0;
  1758. }
  1759. if (this_afe.prot_cfg.mode == MSM_SPKR_PROT_DISABLED ||
  1760. (this_afe.vi_rx_port != port)) {
  1761. pr_debug("%s: spkr protection disabled port 0x%x %d 0x%x\n",
  1762. __func__, port, ret, this_afe.vi_rx_port);
  1763. return 0;
  1764. }
  1765. param_info.module_id = AFE_MODULE_FB_SPKR_PROT_V2_RX;
  1766. param_info.instance_id = INSTANCE_ID_0;
  1767. param_info.param_id = AFE_PARAM_ID_FBSP_PTONE_RAMP_CFG;
  1768. param_info.param_size = 0;
  1769. ret = q6afe_pack_and_set_param_in_band(port,
  1770. q6audio_get_port_index(port),
  1771. param_info, NULL);
  1772. if (ret) {
  1773. pr_err("%s: Failed to set speaker ramp duration param, err %d\n",
  1774. __func__, ret);
  1775. goto fail_cmd;
  1776. }
  1777. /* dsp needs atleast 15ms to ramp down pilot tone*/
  1778. usleep_range(15000, 15010);
  1779. ret = 0;
  1780. fail_cmd:
  1781. pr_debug("%s: config.pdata.param_id 0x%x status %d\n", __func__,
  1782. param_info.param_id, ret);
  1783. return ret;
  1784. }
  1785. static int afe_spk_prot_prepare(int src_port, int dst_port, int param_id,
  1786. union afe_spkr_prot_config *prot_config, uint32_t param_size)
  1787. {
  1788. struct param_hdr_v3 param_info;
  1789. int ret = -EINVAL;
  1790. memset(&param_info, 0, sizeof(param_info));
  1791. ret = q6audio_validate_port(src_port);
  1792. if (ret < 0) {
  1793. pr_err("%s: Invalid src port 0x%x ret %d", __func__, src_port,
  1794. ret);
  1795. ret = -EINVAL;
  1796. goto fail_cmd;
  1797. }
  1798. ret = q6audio_validate_port(dst_port);
  1799. if (ret < 0) {
  1800. pr_err("%s: Invalid dst port 0x%x ret %d", __func__,
  1801. dst_port, ret);
  1802. ret = -EINVAL;
  1803. goto fail_cmd;
  1804. }
  1805. switch (param_id) {
  1806. case AFE_PARAM_ID_FBSP_MODE_RX_CFG:
  1807. case AFE_PARAM_ID_SP_RX_LIMITER_TH:
  1808. param_info.module_id = AFE_MODULE_FB_SPKR_PROT_V2_RX;
  1809. break;
  1810. case AFE_PARAM_ID_SP_V4_OP_MODE:
  1811. param_info.module_id = AFE_MODULE_SPEAKER_PROTECTION_V4_RX;
  1812. break;
  1813. case AFE_PARAM_ID_FEEDBACK_PATH_CFG:
  1814. this_afe.vi_tx_port = src_port;
  1815. this_afe.vi_rx_port = dst_port;
  1816. param_info.module_id = AFE_MODULE_FEEDBACK;
  1817. break;
  1818. /*
  1819. * AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG_V2 is same as
  1820. * AFE_PARAM_ID_SP_V2_TH_VI_MODE_CFG. V_VALI_CFG uses
  1821. * same module TH_VI.
  1822. */
  1823. case AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG_V2:
  1824. case AFE_PARAM_ID_SP_V2_TH_VI_FTM_CFG:
  1825. case AFE_PARAM_ID_SP_V2_TH_VI_V_VALI_CFG:
  1826. param_info.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_TH_VI;
  1827. break;
  1828. case AFE_PARAM_ID_SP_V2_EX_VI_MODE_CFG:
  1829. case AFE_PARAM_ID_SP_V2_EX_VI_FTM_CFG:
  1830. param_info.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_EX_VI;
  1831. break;
  1832. case AFE_PARAM_ID_SP_V4_VI_CHANNEL_MAP_CFG:
  1833. case AFE_PARAM_ID_SP_V4_VI_OP_MODE_CFG:
  1834. case AFE_PARAM_ID_SP_V4_VI_R0T0_CFG:
  1835. case AFE_PARAM_ID_SP_V4_TH_VI_FTM_CFG:
  1836. case AFE_PARAM_ID_SP_V4_TH_VI_V_VALI_CFG:
  1837. case AFE_PARAM_ID_SP_V4_EX_VI_MODE_CFG:
  1838. case AFE_PARAM_ID_SP_V4_EX_VI_FTM_CFG:
  1839. param_info.module_id = AFE_MODULE_SPEAKER_PROTECTION_V4_VI;
  1840. break;
  1841. default:
  1842. pr_err("%s: default case 0x%x\n", __func__, param_id);
  1843. goto fail_cmd;
  1844. }
  1845. param_info.instance_id = INSTANCE_ID_0;
  1846. param_info.param_id = param_id;
  1847. param_info.param_size = param_size;
  1848. ret = q6afe_pack_and_set_param_in_band(src_port,
  1849. q6audio_get_port_index(src_port),
  1850. param_info, (u8 *) prot_config);
  1851. if (ret)
  1852. pr_err("%s: port = 0x%x param = 0x%x failed %d\n", __func__,
  1853. src_port, param_id, ret);
  1854. fail_cmd:
  1855. pr_debug("%s: config.pdata.param_id 0x%x status %d 0x%x\n", __func__,
  1856. param_info.param_id, ret, src_port);
  1857. return ret;
  1858. }
  1859. static int afe_spkr_prot_reg_event_cfg(u16 port_id, uint32_t module_id)
  1860. {
  1861. struct afe_port_cmd_event_cfg *config;
  1862. struct afe_port_cmd_mod_evt_cfg_payload pl;
  1863. int index;
  1864. int ret;
  1865. int num_events = 1;
  1866. int cmd_size = sizeof(struct afe_port_cmd_event_cfg) +
  1867. (num_events * sizeof(struct afe_port_cmd_mod_evt_cfg_payload));
  1868. config = kzalloc(cmd_size, GFP_KERNEL);
  1869. if (!config)
  1870. return -ENOMEM;
  1871. index = q6audio_get_port_index(port_id);
  1872. if (index < 0) {
  1873. pr_err("%s: Invalid index number: %d\n", __func__, index);
  1874. ret = -EINVAL;
  1875. goto fail_idx;
  1876. }
  1877. memset(&pl, 0, sizeof(pl));
  1878. pl.module_id = module_id;
  1879. pl.event_id = AFE_PORT_SP_DC_DETECTION_EVENT;
  1880. pl.reg_flag = AFE_MODULE_REGISTER_EVENT_FLAG;
  1881. config->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1882. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1883. config->hdr.pkt_size = cmd_size;
  1884. config->hdr.src_port = 0;
  1885. config->hdr.dest_port = 0;
  1886. config->hdr.token = index;
  1887. config->hdr.opcode = AFE_PORT_CMD_MOD_EVENT_CFG;
  1888. config->port_id = q6audio_get_port_id(port_id);
  1889. config->num_events = num_events;
  1890. config->version = 1;
  1891. memcpy(config->payload, &pl, sizeof(pl));
  1892. ret = afe_apr_send_pkt((uint32_t *) config, &this_afe.wait[index]);
  1893. fail_idx:
  1894. kfree(config);
  1895. return ret;
  1896. }
  1897. static void afe_send_cal_spv4_tx(int port_id)
  1898. {
  1899. union afe_spkr_prot_config afe_spk_config;
  1900. uint32_t size = 0;
  1901. void *tmp_ptr = NULL;
  1902. struct afe_sp_v4_param_th_vi_r0t0_cfg *th_vi_r0t0_cfg = NULL;
  1903. struct afe_sp_v4_channel_r0t0 *ch_r0t0_cfg = NULL;
  1904. struct afe_sp_v4_param_th_vi_ftm_cfg *th_vi_ftm_cfg = NULL;
  1905. struct afe_sp_v4_channel_ftm_cfg *ch_ftm_cfg = NULL;
  1906. struct afe_sp_v4_param_th_vi_v_vali_cfg *th_vi_v_vali_cfg = NULL;
  1907. struct afe_sp_v4_channel_v_vali_cfg *ch_v_vali_cfg = NULL;
  1908. struct afe_sp_v4_param_ex_vi_ftm_cfg *ex_vi_ftm_cfg = NULL;
  1909. struct afe_sp_v4_channel_ex_vi_ftm *ch_ex_vi_ftm_cfg = NULL;
  1910. pr_debug("%s: Entry.. port_id %d\n", __func__, port_id);
  1911. if (this_afe.vi_tx_port == port_id) {
  1912. memcpy(&afe_spk_config.v4_ch_map_cfg, &this_afe.v4_ch_map_cfg,
  1913. sizeof(struct afe_sp_v4_param_vi_channel_map_cfg));
  1914. if (afe_spk_prot_prepare(port_id, this_afe.vi_rx_port,
  1915. AFE_PARAM_ID_SP_V4_VI_CHANNEL_MAP_CFG, &afe_spk_config,
  1916. sizeof(struct afe_sp_v4_param_vi_channel_map_cfg)))
  1917. pr_info("%s: SPKR_CALIB_CHANNEL_MAP_CFG failed\n",
  1918. __func__);
  1919. }
  1920. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL ||
  1921. this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL ||
  1922. this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL] == NULL) {
  1923. pr_info("%s: Returning as no cal data cached\n", __func__);
  1924. return;
  1925. }
  1926. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  1927. if ((this_afe.prot_cfg.mode != MSM_SPKR_PROT_DISABLED) &&
  1928. (this_afe.vi_tx_port == port_id) &&
  1929. (this_afe.prot_cfg.sp_version >= AFE_API_VERSION_V9)) {
  1930. if (this_afe.prot_cfg.mode ==
  1931. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS) {
  1932. afe_spk_config.v4_vi_op_mode.th_operation_mode =
  1933. Q6AFE_MSM_SPKR_CALIBRATION;
  1934. afe_spk_config.v4_vi_op_mode.th_quick_calib_flag =
  1935. this_afe.prot_cfg.quick_calib_flag;
  1936. } else {
  1937. afe_spk_config.v4_vi_op_mode.th_operation_mode =
  1938. Q6AFE_MSM_SPKR_PROCESSING;
  1939. }
  1940. if (this_afe.th_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE)
  1941. afe_spk_config.v4_vi_op_mode.th_operation_mode =
  1942. Q6AFE_MSM_SPKR_FTM_MODE;
  1943. else if (this_afe.v_vali_cfg.mode ==
  1944. MSM_SPKR_PROT_IN_V_VALI_MODE)
  1945. afe_spk_config.v4_vi_op_mode.th_operation_mode =
  1946. Q6AFE_MSM_SPKR_V_VALI_MODE;
  1947. if (this_afe.prot_cfg.mode != MSM_SPKR_PROT_NOT_CALIBRATED) {
  1948. struct afe_sp_v4_param_vi_op_mode_cfg *v4_vi_op_mode;
  1949. v4_vi_op_mode = &afe_spk_config.v4_vi_op_mode;
  1950. v4_vi_op_mode->th_r0t0_selection_flag[SP_V2_SPKR_1] =
  1951. USE_CALIBRATED_R0TO;
  1952. v4_vi_op_mode->th_r0t0_selection_flag[SP_V2_SPKR_2] =
  1953. USE_CALIBRATED_R0TO;
  1954. } else {
  1955. struct afe_sp_v4_param_vi_op_mode_cfg *v4_vi_op_mode;
  1956. v4_vi_op_mode = &afe_spk_config.v4_vi_op_mode;
  1957. v4_vi_op_mode->th_r0t0_selection_flag[SP_V2_SPKR_1] =
  1958. USE_SAFE_R0TO;
  1959. v4_vi_op_mode->th_r0t0_selection_flag[SP_V2_SPKR_2] =
  1960. USE_SAFE_R0TO;
  1961. }
  1962. afe_spk_config.v4_vi_op_mode.num_speakers = SP_V2_NUM_MAX_SPKRS;
  1963. if (afe_spk_prot_prepare(port_id, 0,
  1964. AFE_PARAM_ID_SP_V4_VI_OP_MODE_CFG,
  1965. &afe_spk_config,
  1966. sizeof(struct afe_sp_v4_param_vi_op_mode_cfg)))
  1967. pr_info("%s: SPKR_CALIB_VI_PROC_CFG failed\n",
  1968. __func__);
  1969. size = sizeof(struct afe_sp_v4_param_th_vi_r0t0_cfg) +
  1970. (SP_V2_NUM_MAX_SPKRS * sizeof(struct afe_sp_v4_channel_r0t0));
  1971. tmp_ptr = kzalloc(size, GFP_KERNEL);
  1972. if (!tmp_ptr) {
  1973. mutex_unlock(
  1974. &this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  1975. return;
  1976. }
  1977. memset(tmp_ptr, 0, size);
  1978. th_vi_r0t0_cfg =
  1979. (struct afe_sp_v4_param_th_vi_r0t0_cfg *)tmp_ptr;
  1980. ch_r0t0_cfg =
  1981. (struct afe_sp_v4_channel_r0t0 *)(th_vi_r0t0_cfg + 1);
  1982. th_vi_r0t0_cfg->num_speakers = SP_V2_NUM_MAX_SPKRS;
  1983. ch_r0t0_cfg[SP_V2_SPKR_1].r0_cali_q24 =
  1984. (uint32_t) this_afe.prot_cfg.r0[SP_V2_SPKR_1];
  1985. ch_r0t0_cfg[SP_V2_SPKR_2].r0_cali_q24 =
  1986. (uint32_t) this_afe.prot_cfg.r0[SP_V2_SPKR_2];
  1987. ch_r0t0_cfg[SP_V2_SPKR_1].t0_cali_q6 =
  1988. (uint32_t) this_afe.prot_cfg.t0[SP_V2_SPKR_1];
  1989. ch_r0t0_cfg[SP_V2_SPKR_2].t0_cali_q6 =
  1990. (uint32_t) this_afe.prot_cfg.t0[SP_V2_SPKR_2];
  1991. if (afe_spk_prot_prepare(port_id, 0,
  1992. AFE_PARAM_ID_SP_V4_VI_R0T0_CFG,
  1993. (union afe_spkr_prot_config *)tmp_ptr, size))
  1994. pr_info("%s: th vi ftm cfg failed\n", __func__);
  1995. kfree(tmp_ptr);
  1996. }
  1997. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  1998. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  1999. if ((this_afe.th_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE) &&
  2000. (this_afe.vi_tx_port == port_id) &&
  2001. (this_afe.prot_cfg.sp_version >= AFE_API_VERSION_V9)) {
  2002. size = sizeof(struct afe_sp_v4_param_th_vi_ftm_cfg) +
  2003. (SP_V2_NUM_MAX_SPKRS*sizeof(struct afe_sp_v4_channel_ftm_cfg));
  2004. tmp_ptr = kzalloc(size, GFP_KERNEL);
  2005. if (!tmp_ptr) {
  2006. mutex_unlock(
  2007. &this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  2008. return;
  2009. }
  2010. memset(tmp_ptr, 0, size);
  2011. th_vi_ftm_cfg = (struct afe_sp_v4_param_th_vi_ftm_cfg *)tmp_ptr;
  2012. ch_ftm_cfg =
  2013. (struct afe_sp_v4_channel_ftm_cfg *)(th_vi_ftm_cfg+1);
  2014. th_vi_ftm_cfg->num_ch = SP_V2_NUM_MAX_SPKRS;
  2015. ch_ftm_cfg[SP_V2_SPKR_1].wait_time_ms =
  2016. this_afe.th_ftm_cfg.wait_time[SP_V2_SPKR_1];
  2017. ch_ftm_cfg[SP_V2_SPKR_2].wait_time_ms =
  2018. this_afe.th_ftm_cfg.wait_time[SP_V2_SPKR_2];
  2019. ch_ftm_cfg[SP_V2_SPKR_1].ftm_time_ms =
  2020. this_afe.th_ftm_cfg.ftm_time[SP_V2_SPKR_1];
  2021. ch_ftm_cfg[SP_V2_SPKR_2].ftm_time_ms =
  2022. this_afe.th_ftm_cfg.ftm_time[SP_V2_SPKR_2];
  2023. if (afe_spk_prot_prepare(port_id, 0,
  2024. AFE_PARAM_ID_SP_V4_TH_VI_FTM_CFG,
  2025. (union afe_spkr_prot_config *)tmp_ptr, size))
  2026. pr_info("%s: th vi ftm cfg failed\n", __func__);
  2027. kfree(tmp_ptr);
  2028. this_afe.th_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  2029. } else if ((this_afe.v_vali_cfg.mode ==
  2030. MSM_SPKR_PROT_IN_V_VALI_MODE) &&
  2031. (this_afe.vi_tx_port == port_id)) {
  2032. size = sizeof(struct afe_sp_v4_param_th_vi_v_vali_cfg) +
  2033. (SP_V2_NUM_MAX_SPKRS *
  2034. sizeof(struct afe_sp_v4_channel_v_vali_cfg));
  2035. tmp_ptr = kzalloc(size, GFP_KERNEL);
  2036. if (!tmp_ptr) {
  2037. mutex_unlock(
  2038. &this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  2039. return;
  2040. }
  2041. memset(tmp_ptr, 0, size);
  2042. th_vi_v_vali_cfg =
  2043. (struct afe_sp_v4_param_th_vi_v_vali_cfg *)tmp_ptr;
  2044. ch_v_vali_cfg =
  2045. (struct afe_sp_v4_channel_v_vali_cfg *)(th_vi_v_vali_cfg + 1);
  2046. th_vi_v_vali_cfg->num_ch = SP_V2_NUM_MAX_SPKRS;
  2047. ch_v_vali_cfg[SP_V2_SPKR_1].wait_time_ms =
  2048. this_afe.v_vali_cfg.wait_time[SP_V2_SPKR_1];
  2049. ch_v_vali_cfg[SP_V2_SPKR_2].wait_time_ms =
  2050. this_afe.v_vali_cfg.wait_time[SP_V2_SPKR_2];
  2051. ch_v_vali_cfg[SP_V2_SPKR_1].vali_time_ms =
  2052. this_afe.v_vali_cfg.vali_time[SP_V2_SPKR_1];
  2053. ch_v_vali_cfg[SP_V2_SPKR_2].vali_time_ms =
  2054. this_afe.v_vali_cfg.vali_time[SP_V2_SPKR_2];
  2055. if (afe_spk_prot_prepare(port_id, 0,
  2056. AFE_PARAM_ID_SP_V4_TH_VI_V_VALI_CFG,
  2057. (union afe_spkr_prot_config *)tmp_ptr, size))
  2058. pr_info("%s: th vi v-vali cfg failed\n", __func__);
  2059. kfree(tmp_ptr);
  2060. this_afe.v_vali_cfg.mode = MSM_SPKR_PROT_DISABLED;
  2061. }
  2062. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  2063. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  2064. if ((this_afe.ex_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE) &&
  2065. (this_afe.vi_tx_port == port_id) &&
  2066. (this_afe.prot_cfg.sp_version >= AFE_API_VERSION_V9)) {
  2067. size = sizeof(struct afe_sp_v4_param_ex_vi_ftm_cfg) +
  2068. (SP_V2_NUM_MAX_SPKRS *
  2069. sizeof(struct afe_sp_v4_channel_ex_vi_ftm));
  2070. tmp_ptr = kzalloc(size, GFP_KERNEL);
  2071. if (!tmp_ptr) {
  2072. mutex_unlock(
  2073. &this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  2074. return;
  2075. }
  2076. memset(tmp_ptr, 0, size);
  2077. ex_vi_ftm_cfg = (struct afe_sp_v4_param_ex_vi_ftm_cfg *)tmp_ptr;
  2078. ch_ex_vi_ftm_cfg =
  2079. (struct afe_sp_v4_channel_ex_vi_ftm *)(ex_vi_ftm_cfg + 1);
  2080. afe_spk_config.v4_ex_vi_mode_cfg.operation_mode =
  2081. AFE_FBSP_V4_EX_VI_MODE_FTM;
  2082. if (afe_spk_prot_prepare(port_id, 0,
  2083. AFE_PARAM_ID_SP_V4_EX_VI_MODE_CFG,
  2084. &afe_spk_config,
  2085. sizeof(struct afe_sp_v4_param_ex_vi_mode_cfg)))
  2086. pr_info("%s: ex vi mode cfg failed\n", __func__);
  2087. ex_vi_ftm_cfg->num_ch = SP_V2_NUM_MAX_SPKRS;
  2088. ch_ex_vi_ftm_cfg[SP_V2_SPKR_1].wait_time_ms =
  2089. this_afe.ex_ftm_cfg.wait_time[SP_V2_SPKR_1];
  2090. ch_ex_vi_ftm_cfg[SP_V2_SPKR_2].wait_time_ms =
  2091. this_afe.ex_ftm_cfg.wait_time[SP_V2_SPKR_2];
  2092. ch_ex_vi_ftm_cfg[SP_V2_SPKR_1].ftm_time_ms =
  2093. this_afe.ex_ftm_cfg.ftm_time[SP_V2_SPKR_1];
  2094. ch_ex_vi_ftm_cfg[SP_V2_SPKR_2].ftm_time_ms =
  2095. this_afe.ex_ftm_cfg.ftm_time[SP_V2_SPKR_2];
  2096. if (afe_spk_prot_prepare(port_id, 0,
  2097. AFE_PARAM_ID_SP_V4_EX_VI_FTM_CFG,
  2098. (union afe_spkr_prot_config *)tmp_ptr, size))
  2099. pr_info("%s: ex vi ftm cfg failed\n", __func__);
  2100. kfree(tmp_ptr);
  2101. this_afe.ex_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  2102. }
  2103. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  2104. /* Register for DC detection event if speaker protection is enabled */
  2105. if (this_afe.prot_cfg.mode != MSM_SPKR_PROT_DISABLED &&
  2106. (this_afe.vi_tx_port == port_id)) {
  2107. afe_spkr_prot_reg_event_cfg(port_id,
  2108. AFE_MODULE_SPEAKER_PROTECTION_V4_VI);
  2109. }
  2110. }
  2111. static void afe_send_cal_spkr_prot_tx(int port_id)
  2112. {
  2113. union afe_spkr_prot_config afe_spk_config;
  2114. if (q6core_get_avcs_api_version_per_service(
  2115. APRV2_IDS_SERVICE_ID_ADSP_AFE_V) >= AFE_API_VERSION_V9) {
  2116. afe_send_cal_spv4_tx(port_id);
  2117. return;
  2118. }
  2119. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL ||
  2120. this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL ||
  2121. this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL] == NULL)
  2122. return;
  2123. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  2124. if ((this_afe.prot_cfg.mode != MSM_SPKR_PROT_DISABLED) &&
  2125. (this_afe.vi_tx_port == port_id)) {
  2126. if (this_afe.prot_cfg.mode ==
  2127. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS) {
  2128. afe_spk_config.vi_proc_cfg.operation_mode =
  2129. Q6AFE_MSM_SPKR_CALIBRATION;
  2130. afe_spk_config.vi_proc_cfg.quick_calib_flag =
  2131. this_afe.prot_cfg.quick_calib_flag;
  2132. } else {
  2133. afe_spk_config.vi_proc_cfg.operation_mode =
  2134. Q6AFE_MSM_SPKR_PROCESSING;
  2135. }
  2136. if (this_afe.th_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE)
  2137. afe_spk_config.vi_proc_cfg.operation_mode =
  2138. Q6AFE_MSM_SPKR_FTM_MODE;
  2139. else if (this_afe.v_vali_cfg.mode ==
  2140. MSM_SPKR_PROT_IN_V_VALI_MODE)
  2141. afe_spk_config.vi_proc_cfg.operation_mode =
  2142. Q6AFE_MSM_SPKR_V_VALI_MODE;
  2143. afe_spk_config.vi_proc_cfg.minor_version = 1;
  2144. afe_spk_config.vi_proc_cfg.r0_cali_q24[SP_V2_SPKR_1] =
  2145. (uint32_t) this_afe.prot_cfg.r0[SP_V2_SPKR_1];
  2146. afe_spk_config.vi_proc_cfg.r0_cali_q24[SP_V2_SPKR_2] =
  2147. (uint32_t) this_afe.prot_cfg.r0[SP_V2_SPKR_2];
  2148. afe_spk_config.vi_proc_cfg.t0_cali_q6[SP_V2_SPKR_1] =
  2149. (uint32_t) this_afe.prot_cfg.t0[SP_V2_SPKR_1];
  2150. afe_spk_config.vi_proc_cfg.t0_cali_q6[SP_V2_SPKR_2] =
  2151. (uint32_t) this_afe.prot_cfg.t0[SP_V2_SPKR_2];
  2152. if (this_afe.prot_cfg.mode != MSM_SPKR_PROT_NOT_CALIBRATED) {
  2153. struct asm_spkr_calib_vi_proc_cfg *vi_proc_cfg;
  2154. vi_proc_cfg = &afe_spk_config.vi_proc_cfg;
  2155. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_1] =
  2156. USE_CALIBRATED_R0TO;
  2157. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_2] =
  2158. USE_CALIBRATED_R0TO;
  2159. } else {
  2160. struct asm_spkr_calib_vi_proc_cfg *vi_proc_cfg;
  2161. vi_proc_cfg = &afe_spk_config.vi_proc_cfg;
  2162. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_1] =
  2163. USE_SAFE_R0TO;
  2164. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_2] =
  2165. USE_SAFE_R0TO;
  2166. }
  2167. if (afe_spk_prot_prepare(port_id, 0,
  2168. AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG_V2,
  2169. &afe_spk_config, sizeof(union afe_spkr_prot_config)))
  2170. pr_err("%s: SPKR_CALIB_VI_PROC_CFG failed\n",
  2171. __func__);
  2172. }
  2173. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  2174. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  2175. if ((this_afe.th_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE) &&
  2176. (this_afe.vi_tx_port == port_id)) {
  2177. afe_spk_config.th_vi_ftm_cfg.minor_version = 1;
  2178. afe_spk_config.th_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_1] =
  2179. this_afe.th_ftm_cfg.wait_time[SP_V2_SPKR_1];
  2180. afe_spk_config.th_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_2] =
  2181. this_afe.th_ftm_cfg.wait_time[SP_V2_SPKR_2];
  2182. afe_spk_config.th_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_1] =
  2183. this_afe.th_ftm_cfg.ftm_time[SP_V2_SPKR_1];
  2184. afe_spk_config.th_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_2] =
  2185. this_afe.th_ftm_cfg.ftm_time[SP_V2_SPKR_2];
  2186. if (afe_spk_prot_prepare(port_id, 0,
  2187. AFE_PARAM_ID_SP_V2_TH_VI_FTM_CFG,
  2188. &afe_spk_config,
  2189. sizeof(union afe_spkr_prot_config)))
  2190. pr_err("%s: th vi ftm cfg failed\n", __func__);
  2191. this_afe.th_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  2192. } else if ((this_afe.v_vali_cfg.mode ==
  2193. MSM_SPKR_PROT_IN_V_VALI_MODE) &&
  2194. (this_afe.vi_tx_port == port_id)) {
  2195. afe_spk_config.th_vi_v_vali_cfg.minor_version = 1;
  2196. afe_spk_config.th_vi_v_vali_cfg.wait_time_ms[SP_V2_SPKR_1] =
  2197. this_afe.v_vali_cfg.wait_time[SP_V2_SPKR_1];
  2198. afe_spk_config.th_vi_v_vali_cfg.wait_time_ms[SP_V2_SPKR_2] =
  2199. this_afe.v_vali_cfg.wait_time[SP_V2_SPKR_2];
  2200. afe_spk_config.th_vi_v_vali_cfg.vali_time_ms[SP_V2_SPKR_1] =
  2201. this_afe.v_vali_cfg.vali_time[SP_V2_SPKR_1];
  2202. afe_spk_config.th_vi_v_vali_cfg.vali_time_ms[SP_V2_SPKR_2] =
  2203. this_afe.v_vali_cfg.vali_time[SP_V2_SPKR_2];
  2204. if (afe_spk_prot_prepare(port_id, 0,
  2205. AFE_PARAM_ID_SP_V2_TH_VI_V_VALI_CFG,
  2206. &afe_spk_config,
  2207. sizeof(union afe_spkr_prot_config)))
  2208. pr_err("%s: th vi v-vali cfg failed\n", __func__);
  2209. this_afe.v_vali_cfg.mode = MSM_SPKR_PROT_DISABLED;
  2210. }
  2211. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  2212. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  2213. if ((this_afe.ex_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE) &&
  2214. (this_afe.vi_tx_port == port_id)) {
  2215. afe_spk_config.ex_vi_mode_cfg.minor_version = 1;
  2216. afe_spk_config.ex_vi_mode_cfg.operation_mode =
  2217. Q6AFE_MSM_SPKR_FTM_MODE;
  2218. if (afe_spk_prot_prepare(port_id, 0,
  2219. AFE_PARAM_ID_SP_V2_EX_VI_MODE_CFG,
  2220. &afe_spk_config,
  2221. sizeof(union afe_spkr_prot_config)))
  2222. pr_err("%s: ex vi mode cfg failed\n", __func__);
  2223. afe_spk_config.ex_vi_ftm_cfg.minor_version = 1;
  2224. afe_spk_config.ex_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_1] =
  2225. this_afe.ex_ftm_cfg.wait_time[SP_V2_SPKR_1];
  2226. afe_spk_config.ex_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_2] =
  2227. this_afe.ex_ftm_cfg.wait_time[SP_V2_SPKR_2];
  2228. afe_spk_config.ex_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_1] =
  2229. this_afe.ex_ftm_cfg.ftm_time[SP_V2_SPKR_1];
  2230. afe_spk_config.ex_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_2] =
  2231. this_afe.ex_ftm_cfg.ftm_time[SP_V2_SPKR_2];
  2232. if (afe_spk_prot_prepare(port_id, 0,
  2233. AFE_PARAM_ID_SP_V2_EX_VI_FTM_CFG,
  2234. &afe_spk_config,
  2235. sizeof(union afe_spkr_prot_config)))
  2236. pr_err("%s: ex vi ftm cfg failed\n", __func__);
  2237. this_afe.ex_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  2238. }
  2239. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  2240. /* Register for DC detection event if speaker protection is enabled */
  2241. if (this_afe.prot_cfg.mode != MSM_SPKR_PROT_DISABLED &&
  2242. (this_afe.vi_tx_port == port_id)) {
  2243. afe_spkr_prot_reg_event_cfg(port_id,
  2244. AFE_MODULE_SPEAKER_PROTECTION_V2_EX_VI);
  2245. }
  2246. }
  2247. static void afe_send_cal_spv4_rx(int port_id)
  2248. {
  2249. union afe_spkr_prot_config afe_spk_config;
  2250. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL)
  2251. return;
  2252. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  2253. if (this_afe.prot_cfg.mode != MSM_SPKR_PROT_DISABLED &&
  2254. (this_afe.vi_rx_port == port_id) &&
  2255. (this_afe.prot_cfg.sp_version >= AFE_API_VERSION_V9)) {
  2256. if (this_afe.prot_cfg.mode ==
  2257. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS)
  2258. afe_spk_config.v4_op_mode.mode =
  2259. Q6AFE_MSM_SPKR_CALIBRATION;
  2260. else
  2261. afe_spk_config.v4_op_mode.mode =
  2262. Q6AFE_MSM_SPKR_PROCESSING;
  2263. if (afe_spk_prot_prepare(port_id, 0,
  2264. AFE_PARAM_ID_SP_V4_OP_MODE,
  2265. &afe_spk_config, sizeof(union afe_spkr_prot_config)))
  2266. pr_info("%s: RX MODE_VI_PROC_CFG failed\n",
  2267. __func__);
  2268. }
  2269. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  2270. }
  2271. static void afe_send_cal_spkr_prot_rx(int port_id)
  2272. {
  2273. union afe_spkr_prot_config afe_spk_config;
  2274. union afe_spkr_prot_config afe_spk_limiter_config;
  2275. if (q6core_get_avcs_api_version_per_service(
  2276. APRV2_IDS_SERVICE_ID_ADSP_AFE_V) >= AFE_API_VERSION_V9) {
  2277. afe_send_cal_spv4_rx(port_id);
  2278. return;
  2279. }
  2280. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL)
  2281. goto done;
  2282. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  2283. if (this_afe.prot_cfg.mode != MSM_SPKR_PROT_DISABLED &&
  2284. (this_afe.vi_rx_port == port_id)) {
  2285. if (this_afe.prot_cfg.mode ==
  2286. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS)
  2287. afe_spk_config.mode_rx_cfg.mode =
  2288. Q6AFE_MSM_SPKR_CALIBRATION;
  2289. else
  2290. afe_spk_config.mode_rx_cfg.mode =
  2291. Q6AFE_MSM_SPKR_PROCESSING;
  2292. afe_spk_config.mode_rx_cfg.minor_version = 1;
  2293. if (afe_spk_prot_prepare(port_id, 0,
  2294. AFE_PARAM_ID_FBSP_MODE_RX_CFG,
  2295. &afe_spk_config, sizeof(union afe_spkr_prot_config)))
  2296. pr_err("%s: RX MODE_VI_PROC_CFG failed\n",
  2297. __func__);
  2298. if (afe_spk_config.mode_rx_cfg.mode ==
  2299. Q6AFE_MSM_SPKR_PROCESSING) {
  2300. if (this_afe.prot_cfg.sp_version >=
  2301. AFE_API_VERSION_SUPPORT_SPV3) {
  2302. afe_spk_limiter_config.limiter_th_cfg.
  2303. minor_version = 1;
  2304. afe_spk_limiter_config.limiter_th_cfg.
  2305. lim_thr_per_calib_q27[SP_V2_SPKR_1] =
  2306. this_afe.prot_cfg.limiter_th[SP_V2_SPKR_1];
  2307. afe_spk_limiter_config.limiter_th_cfg.
  2308. lim_thr_per_calib_q27[SP_V2_SPKR_2] =
  2309. this_afe.prot_cfg.limiter_th[SP_V2_SPKR_2];
  2310. if (afe_spk_prot_prepare(port_id, 0,
  2311. AFE_PARAM_ID_SP_RX_LIMITER_TH,
  2312. &afe_spk_limiter_config,
  2313. sizeof(union afe_spkr_prot_config)))
  2314. pr_err("%s: SP_RX_LIMITER_TH failed.\n",
  2315. __func__);
  2316. } else {
  2317. pr_debug("%s: SPv3 failed to apply on AFE API version=%d.\n",
  2318. __func__,
  2319. this_afe.prot_cfg.sp_version);
  2320. }
  2321. }
  2322. }
  2323. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  2324. done:
  2325. return;
  2326. }
  2327. static int afe_send_hw_delay(u16 port_id, u32 rate)
  2328. {
  2329. struct audio_cal_hw_delay_entry delay_entry;
  2330. struct afe_param_id_device_hw_delay_cfg hw_delay;
  2331. struct param_hdr_v3 param_info;
  2332. int ret = -EINVAL;
  2333. pr_debug("%s:\n", __func__);
  2334. memset(&delay_entry, 0, sizeof(delay_entry));
  2335. memset(&param_info, 0, sizeof(param_info));
  2336. delay_entry.sample_rate = rate;
  2337. if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX)
  2338. ret = afe_get_cal_hw_delay(TX_DEVICE, &delay_entry);
  2339. else if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_RX)
  2340. ret = afe_get_cal_hw_delay(RX_DEVICE, &delay_entry);
  2341. /*
  2342. * HW delay is only used for IMS calls to sync audio with video
  2343. * It is only needed for devices & sample rates used for IMS video
  2344. * calls. Values are received from ACDB calbration files
  2345. */
  2346. if (ret != 0) {
  2347. pr_debug("%s: debug: HW delay info not available %d\n",
  2348. __func__, ret);
  2349. goto fail_cmd;
  2350. }
  2351. param_info.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2352. param_info.instance_id = INSTANCE_ID_0;
  2353. param_info.param_id = AFE_PARAM_ID_DEVICE_HW_DELAY;
  2354. param_info.param_size = sizeof(hw_delay);
  2355. hw_delay.delay_in_us = delay_entry.delay_usec;
  2356. hw_delay.device_hw_delay_minor_version =
  2357. AFE_API_VERSION_DEVICE_HW_DELAY;
  2358. ret = q6afe_pack_and_set_param_in_band(port_id,
  2359. q6audio_get_port_index(port_id),
  2360. param_info, (u8 *) &hw_delay);
  2361. if (ret)
  2362. pr_err("%s: AFE hw delay for port 0x%x failed %d\n",
  2363. __func__, port_id, ret);
  2364. fail_cmd:
  2365. pr_info("%s: port_id 0x%x rate %u delay_usec %d status %d\n",
  2366. __func__, port_id, rate, delay_entry.delay_usec, ret);
  2367. return ret;
  2368. }
  2369. static struct cal_block_data *afe_find_cal_topo_id_by_port(
  2370. struct cal_type_data *cal_type, u16 port_id)
  2371. {
  2372. struct list_head *ptr, *next;
  2373. struct cal_block_data *cal_block = NULL;
  2374. int32_t path;
  2375. struct audio_cal_info_afe_top *afe_top;
  2376. int afe_port_index = q6audio_get_port_index(port_id);
  2377. if (afe_port_index < 0)
  2378. goto err_exit;
  2379. list_for_each_safe(ptr, next,
  2380. &cal_type->cal_blocks) {
  2381. cal_block = list_entry(ptr,
  2382. struct cal_block_data, list);
  2383. /* Skip cal_block if it is already marked stale */
  2384. if (cal_utils_is_cal_stale(cal_block))
  2385. continue;
  2386. pr_info("%s: port id: 0x%x, dev_acdb_id: %d\n", __func__,
  2387. port_id, this_afe.dev_acdb_id[afe_port_index]);
  2388. path = ((afe_get_port_type(port_id) ==
  2389. MSM_AFE_PORT_TYPE_TX)?(TX_DEVICE):(RX_DEVICE));
  2390. afe_top =
  2391. (struct audio_cal_info_afe_top *)cal_block->cal_info;
  2392. if (afe_top->path == path) {
  2393. if (this_afe.dev_acdb_id[afe_port_index] > 0) {
  2394. if (afe_top->acdb_id ==
  2395. this_afe.dev_acdb_id[afe_port_index]) {
  2396. pr_info("%s: top_id:%x acdb_id:%d afe_port_id:0x%x\n",
  2397. __func__, afe_top->topology,
  2398. afe_top->acdb_id,
  2399. q6audio_get_port_id(port_id));
  2400. return cal_block;
  2401. }
  2402. } else {
  2403. pr_info("%s: top_id:%x acdb_id:%d afe_port:0x%x\n",
  2404. __func__, afe_top->topology, afe_top->acdb_id,
  2405. q6audio_get_port_id(port_id));
  2406. return cal_block;
  2407. }
  2408. }
  2409. }
  2410. err_exit:
  2411. return NULL;
  2412. }
  2413. /*
  2414. * Retrieving cal_block will mark cal_block as stale.
  2415. * Hence it cannot be reused or resent unless the flag
  2416. * is reset.
  2417. */
  2418. static int afe_get_cal_topology_id(u16 port_id, u32 *topology_id,
  2419. int cal_type_index)
  2420. {
  2421. int ret = 0;
  2422. struct cal_block_data *cal_block = NULL;
  2423. struct audio_cal_info_afe_top *afe_top_info = NULL;
  2424. if (this_afe.cal_data[cal_type_index] == NULL) {
  2425. pr_err("%s: cal_type %d not initialized\n", __func__,
  2426. cal_type_index);
  2427. return -EINVAL;
  2428. }
  2429. if (topology_id == NULL) {
  2430. pr_err("%s: topology_id is NULL\n", __func__);
  2431. return -EINVAL;
  2432. }
  2433. *topology_id = 0;
  2434. mutex_lock(&this_afe.cal_data[cal_type_index]->lock);
  2435. cal_block = afe_find_cal_topo_id_by_port(
  2436. this_afe.cal_data[cal_type_index], port_id);
  2437. if (cal_block == NULL) {
  2438. pr_err("%s: cal_type %d not initialized for this port %d\n",
  2439. __func__, cal_type_index, port_id);
  2440. ret = -EINVAL;
  2441. goto unlock;
  2442. }
  2443. afe_top_info = ((struct audio_cal_info_afe_top *)
  2444. cal_block->cal_info);
  2445. if (!afe_top_info->topology) {
  2446. pr_err("%s: invalid topology id : [%d, %d]\n",
  2447. __func__, afe_top_info->acdb_id, afe_top_info->topology);
  2448. ret = -EINVAL;
  2449. goto unlock;
  2450. }
  2451. *topology_id = (u32)afe_top_info->topology;
  2452. cal_utils_mark_cal_used(cal_block);
  2453. pr_info("%s: port_id = 0x%x acdb_id = %d topology_id = 0x%x cal_type_index=%d ret=%d\n",
  2454. __func__, port_id, afe_top_info->acdb_id,
  2455. afe_top_info->topology, cal_type_index, ret);
  2456. unlock:
  2457. mutex_unlock(&this_afe.cal_data[cal_type_index]->lock);
  2458. return ret;
  2459. }
  2460. static int afe_send_port_topology_id(u16 port_id)
  2461. {
  2462. struct afe_param_id_set_topology_cfg topology;
  2463. struct param_hdr_v3 param_info;
  2464. u32 topology_id = 0;
  2465. int index = 0;
  2466. int ret = 0;
  2467. memset(&topology, 0, sizeof(topology));
  2468. memset(&param_info, 0, sizeof(param_info));
  2469. index = q6audio_get_port_index(port_id);
  2470. if (index < 0 || index >= AFE_MAX_PORTS) {
  2471. pr_err("%s: AFE port index[%d] invalid!\n",
  2472. __func__, index);
  2473. return -EINVAL;
  2474. }
  2475. ret = afe_get_cal_topology_id(port_id, &topology_id, AFE_TOPOLOGY_CAL);
  2476. if (ret < 0) {
  2477. pr_debug("%s: Check for LSM topology\n", __func__);
  2478. ret = afe_get_cal_topology_id(port_id, &topology_id,
  2479. AFE_LSM_TOPOLOGY_CAL);
  2480. }
  2481. if (ret || !topology_id) {
  2482. pr_debug("%s: AFE port[%d] get_cal_topology[%d] invalid!\n",
  2483. __func__, port_id, topology_id);
  2484. goto done;
  2485. }
  2486. param_info.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2487. param_info.instance_id = INSTANCE_ID_0;
  2488. param_info.param_id = AFE_PARAM_ID_SET_TOPOLOGY;
  2489. param_info.param_size = sizeof(topology);
  2490. topology.minor_version = AFE_API_VERSION_TOPOLOGY_V1;
  2491. topology.topology_id = topology_id;
  2492. ret = q6afe_pack_and_set_param_in_band(port_id,
  2493. q6audio_get_port_index(port_id),
  2494. param_info, (u8 *) &topology);
  2495. if (ret) {
  2496. pr_err("%s: AFE set topology id enable for port 0x%x failed %d\n",
  2497. __func__, port_id, ret);
  2498. goto done;
  2499. }
  2500. this_afe.topology[index] = topology_id;
  2501. rtac_update_afe_topology(port_id);
  2502. done:
  2503. pr_info("%s: AFE set topology id 0x%x enable for port 0x%x ret %d\n",
  2504. __func__, topology_id, port_id, ret);
  2505. return ret;
  2506. }
  2507. static int afe_get_island_mode(u16 port_id, u32 *island_mode)
  2508. {
  2509. int ret = 0;
  2510. int index = 0;
  2511. *island_mode = 0;
  2512. index = q6audio_get_port_index(port_id);
  2513. if (index < 0 || index >= AFE_MAX_PORTS) {
  2514. pr_err("%s: AFE port index[%d] invalid!\n",
  2515. __func__, index);
  2516. return -EINVAL;
  2517. }
  2518. *island_mode = this_afe.island_mode[index];
  2519. return ret;
  2520. }
  2521. /*
  2522. * afe_send_port_island_mode -
  2523. * for sending island mode to AFE
  2524. *
  2525. * @port_id: AFE port id number
  2526. *
  2527. * Returns 0 on success or error on failure.
  2528. */
  2529. int afe_send_port_island_mode(u16 port_id)
  2530. {
  2531. struct afe_param_id_island_cfg_t island_cfg;
  2532. struct param_hdr_v3 param_info;
  2533. u32 island_mode = 0;
  2534. int ret = 0;
  2535. if (!(q6core_get_avcs_api_version_per_service(
  2536. APRV2_IDS_SERVICE_ID_ADSP_AFE_V) >= AFE_API_VERSION_V4)) {
  2537. pr_debug("%s: AFE port[%d] API version is invalid!\n",
  2538. __func__, port_id);
  2539. return 0;
  2540. }
  2541. memset(&island_cfg, 0, sizeof(island_cfg));
  2542. memset(&param_info, 0, sizeof(param_info));
  2543. ret = afe_get_island_mode(port_id, &island_mode);
  2544. if (ret) {
  2545. pr_err("%s: AFE port[%d] get island mode is invalid!\n",
  2546. __func__, port_id);
  2547. return ret;
  2548. }
  2549. param_info.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2550. param_info.instance_id = INSTANCE_ID_0;
  2551. param_info.param_id = AFE_PARAM_ID_ISLAND_CONFIG;
  2552. param_info.param_size = sizeof(island_cfg);
  2553. island_cfg.island_cfg_minor_version = AFE_API_VERSION_ISLAND_CONFIG;
  2554. island_cfg.island_enable = island_mode;
  2555. ret = q6afe_pack_and_set_param_in_band(port_id,
  2556. q6audio_get_port_index(port_id),
  2557. param_info, (u8 *) &island_cfg);
  2558. if (ret) {
  2559. pr_err("%s: AFE set island mode enable for port 0x%x failed %d\n",
  2560. __func__, port_id, ret);
  2561. return ret;
  2562. }
  2563. pr_debug("%s: AFE set island mode 0x%x enable for port 0x%x ret %d\n",
  2564. __func__, island_mode, port_id, ret);
  2565. trace_printk("%s: AFE set island mode 0x%x enable for port 0x%x ret %d\n",
  2566. __func__, island_mode, port_id, ret);
  2567. return ret;
  2568. }
  2569. EXPORT_SYMBOL(afe_send_port_island_mode);
  2570. static int afe_get_vad_preroll_cfg(u16 port_id, u32 *preroll_cfg)
  2571. {
  2572. int ret = 0;
  2573. int index = 0;
  2574. *preroll_cfg = 0;
  2575. index = q6audio_get_port_index(port_id);
  2576. if (index < 0 || index >= AFE_MAX_PORTS) {
  2577. pr_err("%s: AFE port index[%d] invalid!\n",
  2578. __func__, index);
  2579. return -EINVAL;
  2580. }
  2581. *preroll_cfg = this_afe.vad_cfg[index].pre_roll;
  2582. return ret;
  2583. }
  2584. int afe_send_port_vad_cfg_params(u16 port_id)
  2585. {
  2586. struct afe_param_id_vad_cfg_t vad_cfg;
  2587. struct afe_mod_enable_param vad_enable;
  2588. struct param_hdr_v3 param_info;
  2589. u32 pre_roll_cfg = 0;
  2590. struct firmware_cal *hwdep_cal = NULL;
  2591. int ret = 0;
  2592. uint16_t port_index = 0;
  2593. if (!(q6core_get_avcs_api_version_per_service(
  2594. APRV2_IDS_SERVICE_ID_ADSP_AFE_V) >= AFE_API_VERSION_V4)) {
  2595. pr_err("%s: AFE port[%d]: AFE API version doesn't support VAD config\n",
  2596. __func__, port_id);
  2597. return 0;
  2598. }
  2599. port_index = afe_get_port_index(port_id);
  2600. if (this_afe.vad_cfg[port_index].is_enable) {
  2601. memset(&vad_cfg, 0, sizeof(vad_cfg));
  2602. memset(&param_info, 0, sizeof(param_info));
  2603. ret = afe_get_vad_preroll_cfg(port_id, &pre_roll_cfg);
  2604. if (ret) {
  2605. pr_err("%s: AFE port[%d] get preroll cfg is invalid!\n",
  2606. __func__, port_id);
  2607. return ret;
  2608. }
  2609. param_info.module_id = AFE_MODULE_VAD;
  2610. param_info.instance_id = INSTANCE_ID_0;
  2611. param_info.param_id = AFE_PARAM_ID_VAD_CFG;
  2612. param_info.param_size = sizeof(vad_cfg);
  2613. vad_cfg.vad_cfg_minor_version = AFE_API_VERSION_VAD_CFG;
  2614. vad_cfg.pre_roll_in_ms = pre_roll_cfg;
  2615. ret = q6afe_pack_and_set_param_in_band(port_id,
  2616. q6audio_get_port_index(port_id),
  2617. param_info, (u8 *) &vad_cfg);
  2618. if (ret) {
  2619. pr_err("%s: AFE set vad cfg for port 0x%x failed %d\n",
  2620. __func__, port_id, ret);
  2621. return ret;
  2622. }
  2623. memset(&param_info, 0, sizeof(param_info));
  2624. hwdep_cal = q6afecal_get_fw_cal(this_afe.fw_data,
  2625. Q6AFE_VAD_CORE_CAL);
  2626. if (!hwdep_cal) {
  2627. pr_err("%s: error in retrieving vad core calibration",
  2628. __func__);
  2629. return -EINVAL;
  2630. }
  2631. param_info.module_id = AFE_MODULE_VAD;
  2632. param_info.instance_id = INSTANCE_ID_0;
  2633. param_info.param_id = AFE_PARAM_ID_VAD_CORE_CFG;
  2634. param_info.param_size = hwdep_cal->size;
  2635. ret = q6afe_pack_and_set_param_in_band(port_id,
  2636. q6audio_get_port_index(port_id),
  2637. param_info,
  2638. (u8 *) hwdep_cal->data);
  2639. if (ret) {
  2640. pr_err("%s: AFE set vad cfg for port 0x%x failed %d\n",
  2641. __func__, port_id, ret);
  2642. return ret;
  2643. }
  2644. }
  2645. if (q6core_get_avcs_api_version_per_service(
  2646. APRV2_IDS_SERVICE_ID_ADSP_AFE_V) >= AFE_API_VERSION_V6) {
  2647. memset(&vad_enable, 0, sizeof(vad_enable));
  2648. memset(&param_info, 0, sizeof(param_info));
  2649. param_info.module_id = AFE_MODULE_VAD;
  2650. param_info.instance_id = INSTANCE_ID_0;
  2651. param_info.param_id = AFE_PARAM_ID_ENABLE;
  2652. param_info.param_size = sizeof(vad_enable);
  2653. port_index = afe_get_port_index(port_id);
  2654. vad_enable.enable = this_afe.vad_cfg[port_index].is_enable;
  2655. ret = q6afe_pack_and_set_param_in_band(port_id,
  2656. q6audio_get_port_index(port_id),
  2657. param_info, (u8 *) &vad_enable);
  2658. if (ret) {
  2659. pr_err("%s: AFE set vad enable for port 0x%x failed %d\n",
  2660. __func__, port_id, ret);
  2661. return ret;
  2662. }
  2663. }
  2664. pr_debug("%s: AFE set preroll cfg %d vad core cfg port 0x%x ret %d\n",
  2665. __func__, pre_roll_cfg, port_id, ret);
  2666. return ret;
  2667. }
  2668. EXPORT_SYMBOL(afe_send_port_vad_cfg_params);
  2669. static int remap_cal_data(struct cal_block_data *cal_block, int cal_index)
  2670. {
  2671. int ret = 0;
  2672. if (cal_block->map_data.dma_buf == NULL) {
  2673. pr_err("%s: No ION allocation for cal index %d!\n",
  2674. __func__, cal_index);
  2675. ret = -EINVAL;
  2676. goto done;
  2677. }
  2678. if ((cal_block->map_data.map_size > 0) &&
  2679. (cal_block->map_data.q6map_handle == 0)) {
  2680. atomic_set(&this_afe.mem_map_cal_index, cal_index);
  2681. ret = afe_cmd_memory_map(cal_block->cal_data.paddr,
  2682. cal_block->map_data.map_size);
  2683. atomic_set(&this_afe.mem_map_cal_index, -1);
  2684. if (ret < 0) {
  2685. pr_err("%s: mmap did not work! size = %zd ret %d\n",
  2686. __func__,
  2687. cal_block->map_data.map_size, ret);
  2688. pr_debug("%s: mmap did not work! addr = 0x%pK, size = %zd\n",
  2689. __func__,
  2690. &cal_block->cal_data.paddr,
  2691. cal_block->map_data.map_size);
  2692. goto done;
  2693. }
  2694. cal_block->map_data.q6map_handle = atomic_read(&this_afe.
  2695. mem_map_cal_handles[cal_index]);
  2696. }
  2697. done:
  2698. return ret;
  2699. }
  2700. static struct cal_block_data *afe_find_cal(int cal_index, int port_id)
  2701. {
  2702. struct list_head *ptr, *next;
  2703. struct cal_block_data *cal_block = NULL;
  2704. struct audio_cal_info_afe *afe_cal_info = NULL;
  2705. int afe_port_index = q6audio_get_port_index(port_id);
  2706. pr_info("%s: cal_index %d port_id 0x%x port_index %d\n", __func__,
  2707. cal_index, port_id, afe_port_index);
  2708. if (afe_port_index < 0) {
  2709. pr_err("%s: Error getting AFE port index %d\n",
  2710. __func__, afe_port_index);
  2711. goto exit;
  2712. }
  2713. list_for_each_safe(ptr, next,
  2714. &this_afe.cal_data[cal_index]->cal_blocks) {
  2715. cal_block = list_entry(ptr, struct cal_block_data, list);
  2716. afe_cal_info = cal_block->cal_info;
  2717. pr_info("%s: acdb_id %d dev_acdb_id %d sample_rate %d afe_sample_rates %d\n",
  2718. __func__, afe_cal_info->acdb_id,
  2719. this_afe.dev_acdb_id[afe_port_index],
  2720. afe_cal_info->sample_rate,
  2721. this_afe.afe_sample_rates[afe_port_index]);
  2722. if ((afe_cal_info->acdb_id ==
  2723. this_afe.dev_acdb_id[afe_port_index]) &&
  2724. (afe_cal_info->sample_rate ==
  2725. this_afe.afe_sample_rates[afe_port_index])) {
  2726. pr_info("%s: cal block is a match, size is %zd\n",
  2727. __func__, cal_block->cal_data.size);
  2728. goto exit;
  2729. }
  2730. }
  2731. pr_info("%s: no matching cal_block found\n", __func__);
  2732. cal_block = NULL;
  2733. exit:
  2734. return cal_block;
  2735. }
  2736. static int send_afe_cal_type(int cal_index, int port_id)
  2737. {
  2738. struct cal_block_data *cal_block = NULL;
  2739. int ret;
  2740. int afe_port_index = q6audio_get_port_index(port_id);
  2741. pr_info("%s: cal_index is %d\n", __func__, cal_index);
  2742. if (this_afe.cal_data[cal_index] == NULL) {
  2743. pr_warn("%s: cal_index %d not allocated!\n",
  2744. __func__, cal_index);
  2745. ret = -EINVAL;
  2746. goto done;
  2747. }
  2748. if (afe_port_index < 0) {
  2749. pr_err("%s: Error getting AFE port index %d\n",
  2750. __func__, afe_port_index);
  2751. ret = -EINVAL;
  2752. goto done;
  2753. }
  2754. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  2755. pr_info("%s: dev_acdb_id[%d] is %d\n",
  2756. __func__, afe_port_index,
  2757. this_afe.dev_acdb_id[afe_port_index]);
  2758. if (((cal_index == AFE_COMMON_RX_CAL) ||
  2759. (cal_index == AFE_COMMON_TX_CAL) ||
  2760. (cal_index == AFE_LSM_TX_CAL)) &&
  2761. (this_afe.dev_acdb_id[afe_port_index] > 0))
  2762. cal_block = afe_find_cal(cal_index, port_id);
  2763. else
  2764. cal_block = cal_utils_get_only_cal_block(
  2765. this_afe.cal_data[cal_index]);
  2766. if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
  2767. pr_err("%s cal_block not found!!\n", __func__);
  2768. ret = -EINVAL;
  2769. goto unlock;
  2770. }
  2771. pr_info("%s: Sending cal_index cal %d\n", __func__, cal_index);
  2772. ret = remap_cal_data(cal_block, cal_index);
  2773. if (ret) {
  2774. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  2775. __func__, cal_index);
  2776. ret = -EINVAL;
  2777. goto unlock;
  2778. }
  2779. ret = afe_send_cal_block(port_id, cal_block);
  2780. if (ret < 0)
  2781. pr_err("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d\n",
  2782. __func__, cal_index, port_id, ret);
  2783. cal_utils_mark_cal_used(cal_block);
  2784. unlock:
  2785. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  2786. done:
  2787. return ret;
  2788. }
  2789. void afe_send_cal(u16 port_id)
  2790. {
  2791. int ret;
  2792. pr_debug("%s: port_id=0x%x\n", __func__, port_id);
  2793. if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) {
  2794. afe_send_cal_spkr_prot_tx(port_id);
  2795. ret = send_afe_cal_type(AFE_COMMON_TX_CAL, port_id);
  2796. if (ret < 0)
  2797. send_afe_cal_type(AFE_LSM_TX_CAL, port_id);
  2798. } else if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_RX) {
  2799. send_afe_cal_type(AFE_COMMON_RX_CAL, port_id);
  2800. afe_send_cal_spkr_prot_rx(port_id);
  2801. }
  2802. }
  2803. int afe_turn_onoff_hw_mad(u16 mad_type, u16 enable)
  2804. {
  2805. struct afe_param_hw_mad_ctrl mad_enable_param;
  2806. struct param_hdr_v3 param_info;
  2807. int ret;
  2808. pr_debug("%s: enter\n", __func__);
  2809. memset(&mad_enable_param, 0, sizeof(mad_enable_param));
  2810. memset(&param_info, 0, sizeof(param_info));
  2811. param_info.module_id = AFE_MODULE_HW_MAD;
  2812. param_info.instance_id = INSTANCE_ID_0;
  2813. param_info.param_id = AFE_PARAM_ID_HW_MAD_CTRL;
  2814. param_info.param_size = sizeof(mad_enable_param);
  2815. mad_enable_param.minor_version = 1;
  2816. mad_enable_param.mad_type = mad_type;
  2817. mad_enable_param.mad_enable = enable;
  2818. ret = q6afe_pack_and_set_param_in_band(SLIMBUS_5_TX, IDX_GLOBAL_CFG,
  2819. param_info,
  2820. (u8 *) &mad_enable_param);
  2821. if (ret)
  2822. pr_err("%s: AFE_PARAM_ID_HW_MAD_CTRL failed %d\n", __func__,
  2823. ret);
  2824. return ret;
  2825. }
  2826. static int afe_send_slimbus_slave_cfg(
  2827. struct afe_param_cdc_slimbus_slave_cfg *sb_slave_cfg)
  2828. {
  2829. struct param_hdr_v3 param_hdr;
  2830. int ret;
  2831. pr_debug("%s: enter\n", __func__);
  2832. memset(&param_hdr, 0, sizeof(param_hdr));
  2833. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  2834. param_hdr.instance_id = INSTANCE_ID_0;
  2835. param_hdr.param_id = AFE_PARAM_ID_CDC_SLIMBUS_SLAVE_CFG;
  2836. param_hdr.param_size = sizeof(struct afe_param_cdc_slimbus_slave_cfg);
  2837. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  2838. (u8 *) sb_slave_cfg);
  2839. if (ret)
  2840. pr_err("%s: AFE_PARAM_ID_CDC_SLIMBUS_SLAVE_CFG failed %d\n",
  2841. __func__, ret);
  2842. pr_debug("%s: leave %d\n", __func__, ret);
  2843. return ret;
  2844. }
  2845. static int afe_send_codec_reg_page_config(
  2846. struct afe_param_cdc_reg_page_cfg *cdc_reg_page_cfg)
  2847. {
  2848. struct param_hdr_v3 param_hdr;
  2849. int ret;
  2850. memset(&param_hdr, 0, sizeof(param_hdr));
  2851. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  2852. param_hdr.instance_id = INSTANCE_ID_0;
  2853. param_hdr.param_id = AFE_PARAM_ID_CDC_REG_PAGE_CFG;
  2854. param_hdr.param_size = sizeof(struct afe_param_cdc_reg_page_cfg);
  2855. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  2856. (u8 *) cdc_reg_page_cfg);
  2857. if (ret)
  2858. pr_err("%s: AFE_PARAM_ID_CDC_REG_PAGE_CFG failed %d\n",
  2859. __func__, ret);
  2860. return ret;
  2861. }
  2862. static int afe_send_codec_reg_config(
  2863. struct afe_param_cdc_reg_cfg_data *cdc_reg_cfg)
  2864. {
  2865. u8 *packed_param_data = NULL;
  2866. u32 packed_data_size = 0;
  2867. u32 single_param_size = 0;
  2868. u32 max_data_size = 0;
  2869. u32 max_single_param = 0;
  2870. struct param_hdr_v3 param_hdr;
  2871. int idx = 0;
  2872. int ret = -EINVAL;
  2873. bool is_iid_supported = q6common_is_instance_id_supported();
  2874. memset(&param_hdr, 0, sizeof(param_hdr));
  2875. max_single_param = sizeof(struct param_hdr_v3) +
  2876. sizeof(struct afe_param_cdc_reg_cfg);
  2877. max_data_size = APR_MAX_BUF - sizeof(struct afe_svc_cmd_set_param_v2);
  2878. packed_param_data = kzalloc(max_data_size, GFP_KERNEL);
  2879. if (!packed_param_data)
  2880. return -ENOMEM;
  2881. /* param_hdr is the same for all params sent, set once at top */
  2882. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  2883. param_hdr.instance_id = INSTANCE_ID_0;
  2884. param_hdr.param_id = AFE_PARAM_ID_CDC_REG_CFG;
  2885. param_hdr.param_size = sizeof(struct afe_param_cdc_reg_cfg);
  2886. while (idx < cdc_reg_cfg->num_registers) {
  2887. memset(packed_param_data, 0, max_data_size);
  2888. packed_data_size = 0;
  2889. single_param_size = 0;
  2890. while (packed_data_size + max_single_param < max_data_size &&
  2891. idx < cdc_reg_cfg->num_registers) {
  2892. ret = q6common_pack_pp_params_v2(
  2893. packed_param_data + packed_data_size,
  2894. &param_hdr, (u8 *) &cdc_reg_cfg->reg_data[idx],
  2895. &single_param_size, is_iid_supported);
  2896. if (ret) {
  2897. pr_err("%s: Failed to pack parameters with error %d\n",
  2898. __func__, ret);
  2899. goto done;
  2900. }
  2901. packed_data_size += single_param_size;
  2902. idx++;
  2903. }
  2904. ret = q6afe_svc_set_params(IDX_GLOBAL_CFG, NULL,
  2905. packed_param_data, packed_data_size,
  2906. is_iid_supported);
  2907. if (ret) {
  2908. pr_err("%s: AFE_PARAM_ID_CDC_REG_CFG failed %d\n",
  2909. __func__, ret);
  2910. break;
  2911. }
  2912. }
  2913. done:
  2914. kfree(packed_param_data);
  2915. return ret;
  2916. }
  2917. static int afe_init_cdc_reg_config(void)
  2918. {
  2919. struct param_hdr_v3 param_hdr;
  2920. int ret;
  2921. pr_debug("%s: enter\n", __func__);
  2922. memset(&param_hdr, 0, sizeof(param_hdr));
  2923. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  2924. param_hdr.instance_id = INSTANCE_ID_0;
  2925. param_hdr.param_id = AFE_PARAM_ID_CDC_REG_CFG_INIT;
  2926. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  2927. NULL);
  2928. if (ret)
  2929. pr_err("%s: AFE_PARAM_ID_CDC_INIT_REG_CFG failed %d\n",
  2930. __func__, ret);
  2931. return ret;
  2932. }
  2933. static int afe_send_slimbus_slave_port_cfg(
  2934. struct afe_param_slimbus_slave_port_cfg *slim_slave_config, u16 port_id)
  2935. {
  2936. struct param_hdr_v3 param_hdr;
  2937. int ret;
  2938. pr_debug("%s: enter, port_id = 0x%x\n", __func__, port_id);
  2939. memset(&param_hdr, 0, sizeof(param_hdr));
  2940. param_hdr.module_id = AFE_MODULE_HW_MAD;
  2941. param_hdr.instance_id = INSTANCE_ID_0;
  2942. param_hdr.reserved = 0;
  2943. param_hdr.param_id = AFE_PARAM_ID_SLIMBUS_SLAVE_PORT_CFG;
  2944. param_hdr.param_size = sizeof(struct afe_param_slimbus_slave_port_cfg);
  2945. ret = q6afe_pack_and_set_param_in_band(port_id,
  2946. q6audio_get_port_index(port_id),
  2947. param_hdr,
  2948. (u8 *) slim_slave_config);
  2949. if (ret)
  2950. pr_err("%s: AFE_PARAM_ID_SLIMBUS_SLAVE_PORT_CFG failed %d\n",
  2951. __func__, ret);
  2952. pr_debug("%s: leave %d\n", __func__, ret);
  2953. return ret;
  2954. }
  2955. static int afe_aanc_port_cfg(void *apr, uint16_t tx_port, uint16_t rx_port)
  2956. {
  2957. struct afe_param_aanc_port_cfg aanc_port_cfg;
  2958. struct param_hdr_v3 param_hdr;
  2959. int ret = 0;
  2960. pr_debug("%s: tx_port 0x%x, rx_port 0x%x\n",
  2961. __func__, tx_port, rx_port);
  2962. pr_debug("%s: AANC sample rate tx rate: %d rx rate %d\n", __func__,
  2963. this_afe.aanc_info.aanc_tx_port_sample_rate,
  2964. this_afe.aanc_info.aanc_rx_port_sample_rate);
  2965. memset(&aanc_port_cfg, 0, sizeof(aanc_port_cfg));
  2966. memset(&param_hdr, 0, sizeof(param_hdr));
  2967. /*
  2968. * If aanc tx sample rate or rx sample rate is zero, skip aanc
  2969. * configuration as AFE resampler will fail for invalid sample
  2970. * rates.
  2971. */
  2972. if (!this_afe.aanc_info.aanc_tx_port_sample_rate ||
  2973. !this_afe.aanc_info.aanc_rx_port_sample_rate) {
  2974. return -EINVAL;
  2975. }
  2976. param_hdr.module_id = AFE_MODULE_AANC;
  2977. param_hdr.instance_id = INSTANCE_ID_0;
  2978. param_hdr.param_id = AFE_PARAM_ID_AANC_PORT_CONFIG;
  2979. param_hdr.param_size = sizeof(struct afe_param_aanc_port_cfg);
  2980. aanc_port_cfg.aanc_port_cfg_minor_version =
  2981. AFE_API_VERSION_AANC_PORT_CONFIG;
  2982. aanc_port_cfg.tx_port_sample_rate =
  2983. this_afe.aanc_info.aanc_tx_port_sample_rate;
  2984. aanc_port_cfg.tx_port_channel_map[0] = AANC_TX_VOICE_MIC;
  2985. aanc_port_cfg.tx_port_channel_map[1] = AANC_TX_NOISE_MIC;
  2986. aanc_port_cfg.tx_port_channel_map[2] = AANC_TX_ERROR_MIC;
  2987. aanc_port_cfg.tx_port_channel_map[3] = AANC_TX_MIC_UNUSED;
  2988. aanc_port_cfg.tx_port_channel_map[4] = AANC_TX_MIC_UNUSED;
  2989. aanc_port_cfg.tx_port_channel_map[5] = AANC_TX_MIC_UNUSED;
  2990. aanc_port_cfg.tx_port_channel_map[6] = AANC_TX_MIC_UNUSED;
  2991. aanc_port_cfg.tx_port_channel_map[7] = AANC_TX_MIC_UNUSED;
  2992. aanc_port_cfg.tx_port_num_channels = 3;
  2993. aanc_port_cfg.rx_path_ref_port_id = rx_port;
  2994. aanc_port_cfg.ref_port_sample_rate =
  2995. this_afe.aanc_info.aanc_rx_port_sample_rate;
  2996. ret = q6afe_pack_and_set_param_in_band(tx_port,
  2997. q6audio_get_port_index(tx_port),
  2998. param_hdr,
  2999. (u8 *) &aanc_port_cfg);
  3000. if (ret)
  3001. pr_err("%s: AFE AANC port config failed for tx_port 0x%x, rx_port 0x%x ret %d\n",
  3002. __func__, tx_port, rx_port, ret);
  3003. else
  3004. q6afe_set_aanc_level();
  3005. return ret;
  3006. }
  3007. static int afe_aanc_mod_enable(void *apr, uint16_t tx_port, uint16_t enable)
  3008. {
  3009. struct afe_mod_enable_param mod_enable;
  3010. struct param_hdr_v3 param_hdr;
  3011. int ret = 0;
  3012. pr_debug("%s: tx_port 0x%x\n", __func__, tx_port);
  3013. memset(&mod_enable, 0, sizeof(mod_enable));
  3014. memset(&param_hdr, 0, sizeof(param_hdr));
  3015. param_hdr.module_id = AFE_MODULE_AANC;
  3016. param_hdr.instance_id = INSTANCE_ID_0;
  3017. param_hdr.param_id = AFE_PARAM_ID_ENABLE;
  3018. param_hdr.param_size = sizeof(struct afe_mod_enable_param);
  3019. mod_enable.enable = enable;
  3020. mod_enable.reserved = 0;
  3021. ret = q6afe_pack_and_set_param_in_band(tx_port,
  3022. q6audio_get_port_index(tx_port),
  3023. param_hdr, (u8 *) &mod_enable);
  3024. if (ret)
  3025. pr_err("%s: AFE AANC enable failed for tx_port 0x%x ret %d\n",
  3026. __func__, tx_port, ret);
  3027. return ret;
  3028. }
  3029. static int afe_send_bank_selection_clip(
  3030. struct afe_param_id_clip_bank_sel *param)
  3031. {
  3032. struct param_hdr_v3 param_hdr;
  3033. int ret;
  3034. if (!param) {
  3035. pr_err("%s: Invalid params", __func__);
  3036. return -EINVAL;
  3037. }
  3038. memset(&param_hdr, 0, sizeof(param_hdr));
  3039. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  3040. param_hdr.instance_id = INSTANCE_ID_0;
  3041. param_hdr.param_id = AFE_PARAM_ID_CLIP_BANK_SEL_CFG;
  3042. param_hdr.param_size = sizeof(struct afe_param_id_clip_bank_sel);
  3043. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  3044. (u8 *) param);
  3045. if (ret)
  3046. pr_err("%s: AFE_PARAM_ID_CLIP_BANK_SEL_CFG failed %d\n",
  3047. __func__, ret);
  3048. return ret;
  3049. }
  3050. int afe_send_aanc_version(
  3051. struct afe_param_id_cdc_aanc_version *version_cfg)
  3052. {
  3053. struct param_hdr_v3 param_hdr;
  3054. int ret;
  3055. pr_debug("%s: enter\n", __func__);
  3056. memset(&param_hdr, 0, sizeof(param_hdr));
  3057. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  3058. param_hdr.instance_id = INSTANCE_ID_0;
  3059. param_hdr.param_id = AFE_PARAM_ID_CDC_AANC_VERSION;
  3060. param_hdr.param_size = sizeof(struct afe_param_id_cdc_aanc_version);
  3061. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  3062. (u8 *) version_cfg);
  3063. if (ret)
  3064. pr_err("%s: AFE_PARAM_ID_CDC_AANC_VERSION failed %d\n",
  3065. __func__, ret);
  3066. return ret;
  3067. }
  3068. /**
  3069. * afe_port_set_mad_type -
  3070. * to update mad type
  3071. *
  3072. * @port_id: AFE port id number
  3073. * @mad_type: MAD type enum value
  3074. *
  3075. * Returns 0 on success or error on failure.
  3076. */
  3077. int afe_port_set_mad_type(u16 port_id, enum afe_mad_type mad_type)
  3078. {
  3079. int i;
  3080. if (port_id == AFE_PORT_ID_TERTIARY_MI2S_TX ||
  3081. port_id == AFE_PORT_ID_INT3_MI2S_TX ||
  3082. port_id == AFE_PORT_ID_TX_CODEC_DMA_TX_3 ||
  3083. port_id == AFE_PORT_ID_TERTIARY_TDM_TX) {
  3084. mad_type = MAD_SW_AUDIO;
  3085. return 0;
  3086. }
  3087. i = port_id - SLIMBUS_0_RX;
  3088. if (i < 0 || i >= ARRAY_SIZE(afe_ports_mad_type)) {
  3089. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  3090. return -EINVAL;
  3091. }
  3092. atomic_set(&afe_ports_mad_type[i], mad_type);
  3093. return 0;
  3094. }
  3095. EXPORT_SYMBOL(afe_port_set_mad_type);
  3096. /**
  3097. * afe_port_get_mad_type -
  3098. * to retrieve mad type
  3099. *
  3100. * @port_id: AFE port id number
  3101. *
  3102. * Returns valid enum value on success or MAD_HW_NONE on failure.
  3103. */
  3104. enum afe_mad_type afe_port_get_mad_type(u16 port_id)
  3105. {
  3106. int i;
  3107. if (port_id == AFE_PORT_ID_TERTIARY_MI2S_TX ||
  3108. port_id == AFE_PORT_ID_INT3_MI2S_TX ||
  3109. port_id == AFE_PORT_ID_TX_CODEC_DMA_TX_3 ||
  3110. port_id == AFE_PORT_ID_TERTIARY_TDM_TX)
  3111. return MAD_SW_AUDIO;
  3112. i = port_id - SLIMBUS_0_RX;
  3113. if (i < 0 || i >= ARRAY_SIZE(afe_ports_mad_type)) {
  3114. pr_debug("%s: Non Slimbus port_id 0x%x\n", __func__, port_id);
  3115. return MAD_HW_NONE;
  3116. }
  3117. return (enum afe_mad_type) atomic_read(&afe_ports_mad_type[i]);
  3118. }
  3119. EXPORT_SYMBOL(afe_port_get_mad_type);
  3120. /**
  3121. * afe_set_config -
  3122. * to configure AFE session with
  3123. * specified configuration for given config type
  3124. *
  3125. * @config_type: config type
  3126. * @config_data: configuration to pass to AFE session
  3127. * @arg: argument used in specific config types
  3128. *
  3129. * Returns 0 on success or error value on port start failure.
  3130. */
  3131. int afe_set_config(enum afe_config_type config_type, void *config_data, int arg)
  3132. {
  3133. int ret;
  3134. pr_debug("%s: enter config_type %d\n", __func__, config_type);
  3135. ret = afe_q6_interface_prepare();
  3136. if (ret) {
  3137. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  3138. return ret;
  3139. }
  3140. switch (config_type) {
  3141. case AFE_SLIMBUS_SLAVE_CONFIG:
  3142. ret = afe_send_slimbus_slave_cfg(config_data);
  3143. if (!ret)
  3144. ret = afe_init_cdc_reg_config();
  3145. else
  3146. pr_err("%s: Sending slimbus slave config failed %d\n",
  3147. __func__, ret);
  3148. break;
  3149. case AFE_CDC_REGISTERS_CONFIG:
  3150. ret = afe_send_codec_reg_config(config_data);
  3151. break;
  3152. case AFE_SLIMBUS_SLAVE_PORT_CONFIG:
  3153. ret = afe_send_slimbus_slave_port_cfg(config_data, arg);
  3154. break;
  3155. case AFE_AANC_VERSION:
  3156. ret = afe_send_aanc_version(config_data);
  3157. break;
  3158. case AFE_CLIP_BANK_SEL:
  3159. ret = afe_send_bank_selection_clip(config_data);
  3160. break;
  3161. case AFE_CDC_CLIP_REGISTERS_CONFIG:
  3162. ret = afe_send_codec_reg_config(config_data);
  3163. break;
  3164. case AFE_CDC_REGISTER_PAGE_CONFIG:
  3165. ret = afe_send_codec_reg_page_config(config_data);
  3166. break;
  3167. default:
  3168. pr_err("%s: unknown configuration type %d",
  3169. __func__, config_type);
  3170. ret = -EINVAL;
  3171. }
  3172. if (!ret)
  3173. set_bit(config_type, &afe_configured_cmd);
  3174. return ret;
  3175. }
  3176. EXPORT_SYMBOL(afe_set_config);
  3177. /*
  3178. * afe_clear_config - If SSR happens ADSP loses AFE configs, let AFE driver know
  3179. * about the state so client driver can wait until AFE is
  3180. * reconfigured.
  3181. */
  3182. void afe_clear_config(enum afe_config_type config)
  3183. {
  3184. clear_bit(config, &afe_configured_cmd);
  3185. }
  3186. EXPORT_SYMBOL(afe_clear_config);
  3187. bool afe_has_config(enum afe_config_type config)
  3188. {
  3189. return !!test_bit(config, &afe_configured_cmd);
  3190. }
  3191. int afe_send_spdif_clk_cfg(struct afe_param_id_spdif_clk_cfg *cfg,
  3192. u16 port_id)
  3193. {
  3194. struct afe_param_id_spdif_clk_cfg clk_cfg;
  3195. struct param_hdr_v3 param_hdr;
  3196. int ret = 0;
  3197. if (!cfg) {
  3198. pr_err("%s: Error, no configuration data\n", __func__);
  3199. return -EINVAL;
  3200. }
  3201. memset(&clk_cfg, 0, sizeof(clk_cfg));
  3202. memset(&param_hdr, 0, sizeof(param_hdr));
  3203. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  3204. param_hdr.instance_id = INSTANCE_ID_0;
  3205. param_hdr.param_id = AFE_PARAM_ID_SPDIF_CLK_CONFIG;
  3206. param_hdr.param_size = sizeof(struct afe_param_id_spdif_clk_cfg);
  3207. pr_debug("%s: Minor version = 0x%x clk val = %d clk root = 0x%x port id = 0x%x\n",
  3208. __func__, clk_cfg.clk_cfg_minor_version, clk_cfg.clk_value,
  3209. clk_cfg.clk_root, q6audio_get_port_id(port_id));
  3210. ret = q6afe_pack_and_set_param_in_band(port_id,
  3211. q6audio_get_port_index(port_id),
  3212. param_hdr, (u8 *) &clk_cfg);
  3213. if (ret < 0)
  3214. pr_err("%s: AFE send clock config for port 0x%x failed ret = %d\n",
  3215. __func__, port_id, ret);
  3216. return ret;
  3217. }
  3218. /**
  3219. * afe_send_spdif_ch_status_cfg -
  3220. * to configure AFE session with
  3221. * specified channel status configuration
  3222. *
  3223. * @ch_status_cfg: channel status configutation
  3224. * @port_id: AFE port id number
  3225. *
  3226. * Returns 0 on success or error value on port start failure.
  3227. */
  3228. int afe_send_spdif_ch_status_cfg(struct afe_param_id_spdif_ch_status_cfg
  3229. *ch_status_cfg, u16 port_id)
  3230. {
  3231. struct param_hdr_v3 param_hdr;
  3232. int ret = 0;
  3233. if (!ch_status_cfg) {
  3234. pr_err("%s: Error, no configuration data\n", __func__);
  3235. return -EINVAL;
  3236. }
  3237. memset(&param_hdr, 0, sizeof(param_hdr));
  3238. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  3239. param_hdr.instance_id = INSTANCE_ID_0;
  3240. param_hdr.param_id = AFE_PARAM_ID_SPDIF_CLK_CONFIG;
  3241. param_hdr.param_size = sizeof(struct afe_param_id_spdif_ch_status_cfg);
  3242. ret = q6afe_pack_and_set_param_in_band(port_id,
  3243. q6audio_get_port_index(port_id),
  3244. param_hdr, (u8 *) ch_status_cfg);
  3245. if (ret < 0)
  3246. pr_err("%s: AFE send channel status for port 0x%x failed ret = %d\n",
  3247. __func__, port_id, ret);
  3248. return ret;
  3249. }
  3250. EXPORT_SYMBOL(afe_send_spdif_ch_status_cfg);
  3251. int afe_send_cmd_wakeup_register(void *handle, bool enable)
  3252. {
  3253. struct afe_svc_cmd_evt_cfg_payload wakeup_irq;
  3254. int ret = 0;
  3255. pr_debug("%s: enter\n", __func__);
  3256. wakeup_irq.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3257. APR_HDR_LEN(APR_HDR_SIZE),
  3258. APR_PKT_VER);
  3259. wakeup_irq.hdr.pkt_size = sizeof(wakeup_irq);
  3260. wakeup_irq.hdr.src_port = 0;
  3261. wakeup_irq.hdr.dest_port = 0;
  3262. wakeup_irq.hdr.token = 0x0;
  3263. wakeup_irq.hdr.opcode = AFE_SVC_CMD_EVENT_CFG;
  3264. wakeup_irq.event_id = AFE_EVENT_ID_MBHC_DETECTION_SW_WA;
  3265. wakeup_irq.reg_flag = enable;
  3266. pr_debug("%s: cmd wakeup register opcode[0x%x] register:%d\n",
  3267. __func__, wakeup_irq.hdr.opcode, wakeup_irq.reg_flag);
  3268. ret = afe_apr_send_pkt(&wakeup_irq, &this_afe.wait_wakeup);
  3269. if (ret)
  3270. pr_err("%s: AFE wakeup command register %d failed %d\n",
  3271. __func__, enable, ret);
  3272. return ret;
  3273. }
  3274. EXPORT_SYMBOL(afe_send_cmd_wakeup_register);
  3275. static int afe_send_cmd_port_start(u16 port_id)
  3276. {
  3277. struct afe_port_cmd_device_start start;
  3278. int ret, index;
  3279. pr_debug("%s: enter\n", __func__);
  3280. index = q6audio_get_port_index(port_id);
  3281. if (index < 0 || index >= AFE_MAX_PORTS) {
  3282. pr_err("%s: AFE port index[%d] invalid!\n",
  3283. __func__, index);
  3284. return -EINVAL;
  3285. }
  3286. ret = q6audio_validate_port(port_id);
  3287. if (ret < 0) {
  3288. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  3289. return -EINVAL;
  3290. }
  3291. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3292. APR_HDR_LEN(APR_HDR_SIZE),
  3293. APR_PKT_VER);
  3294. start.hdr.pkt_size = sizeof(start);
  3295. start.hdr.src_port = 0;
  3296. start.hdr.dest_port = 0;
  3297. start.hdr.token = index;
  3298. start.hdr.opcode = AFE_PORT_CMD_DEVICE_START;
  3299. start.port_id = q6audio_get_port_id(port_id);
  3300. pr_debug("%s: cmd device start opcode[0x%x] port id[0x%x]\n",
  3301. __func__, start.hdr.opcode, start.port_id);
  3302. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  3303. if (ret)
  3304. pr_err("%s: AFE enable for port 0x%x failed %d\n", __func__,
  3305. port_id, ret);
  3306. return ret;
  3307. }
  3308. static int afe_aanc_start(uint16_t tx_port_id, uint16_t rx_port_id)
  3309. {
  3310. int ret;
  3311. pr_debug("%s: Tx port is 0x%x, Rx port is 0x%x\n",
  3312. __func__, tx_port_id, rx_port_id);
  3313. ret = afe_aanc_port_cfg(this_afe.apr, tx_port_id, rx_port_id);
  3314. if (ret) {
  3315. pr_err("%s: Send AANC Port Config failed %d\n",
  3316. __func__, ret);
  3317. goto fail_cmd;
  3318. }
  3319. send_afe_cal_type(AFE_AANC_CAL, tx_port_id);
  3320. fail_cmd:
  3321. return ret;
  3322. }
  3323. /**
  3324. * afe_spdif_port_start - to configure AFE session with
  3325. * specified port configuration
  3326. *
  3327. * @port_id: AFE port id number
  3328. * @spdif_port: spdif port configutation
  3329. * @rate: sampling rate of port
  3330. *
  3331. * Returns 0 on success or error value on port start failure.
  3332. */
  3333. int afe_spdif_port_start(u16 port_id, struct afe_spdif_port_config *spdif_port,
  3334. u32 rate)
  3335. {
  3336. struct param_hdr_v3 param_hdr;
  3337. uint16_t port_index;
  3338. int ret = 0;
  3339. if (!spdif_port) {
  3340. pr_err("%s: Error, no configuration data\n", __func__);
  3341. ret = -EINVAL;
  3342. return ret;
  3343. }
  3344. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  3345. memset(&param_hdr, 0, sizeof(param_hdr));
  3346. ret = q6audio_validate_port(port_id);
  3347. if (ret < 0) {
  3348. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  3349. return -EINVAL;
  3350. }
  3351. afe_send_cal(port_id);
  3352. afe_send_hw_delay(port_id, rate);
  3353. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  3354. param_hdr.instance_id = INSTANCE_ID_0;
  3355. param_hdr.param_id = AFE_PARAM_ID_SPDIF_CONFIG;
  3356. param_hdr.param_size = sizeof(struct afe_spdif_port_config);
  3357. ret = q6afe_pack_and_set_param_in_band(port_id,
  3358. q6audio_get_port_index(port_id),
  3359. param_hdr, (u8 *) spdif_port);
  3360. if (ret) {
  3361. pr_err("%s: AFE enable for port 0x%x failed ret = %d\n",
  3362. __func__, port_id, ret);
  3363. goto fail_cmd;
  3364. }
  3365. port_index = afe_get_port_index(port_id);
  3366. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  3367. this_afe.afe_sample_rates[port_index] = rate;
  3368. } else {
  3369. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  3370. ret = -EINVAL;
  3371. goto fail_cmd;
  3372. }
  3373. if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_RX) {
  3374. ret = afe_send_spdif_ch_status_cfg(&spdif_port->ch_status,
  3375. port_id);
  3376. if (ret < 0) {
  3377. pr_err("%s: afe send failed %d\n", __func__, ret);
  3378. goto fail_cmd;
  3379. }
  3380. }
  3381. return afe_send_cmd_port_start(port_id);
  3382. fail_cmd:
  3383. return ret;
  3384. }
  3385. EXPORT_SYMBOL(afe_spdif_port_start);
  3386. /**
  3387. * afe_spdif_reg_event_cfg -
  3388. * register for event from AFE spdif port
  3389. *
  3390. * @port_id: Port ID to register event
  3391. * @reg_flag: register or unregister
  3392. * @cb: callback function to invoke for events from module
  3393. * @private_data: private data to sent back in callback fn
  3394. *
  3395. * Returns 0 on success or error on failure
  3396. */
  3397. int afe_spdif_reg_event_cfg(u16 port_id, u16 reg_flag,
  3398. void (*cb)(uint32_t opcode,
  3399. uint32_t token, uint32_t *payload, void *priv),
  3400. void *private_data)
  3401. {
  3402. struct afe_port_cmd_event_cfg *config;
  3403. struct afe_port_cmd_mod_evt_cfg_payload pl;
  3404. int index;
  3405. int ret;
  3406. int num_events = 1;
  3407. int cmd_size = sizeof(struct afe_port_cmd_event_cfg) +
  3408. (num_events * sizeof(struct afe_port_cmd_mod_evt_cfg_payload));
  3409. config = kzalloc(cmd_size, GFP_KERNEL);
  3410. if (!config)
  3411. return -ENOMEM;
  3412. if (port_id == AFE_PORT_ID_PRIMARY_SPDIF_TX) {
  3413. this_afe.pri_spdif_tx_cb = cb;
  3414. this_afe.pri_spdif_tx_private_data = private_data;
  3415. } else if (port_id == AFE_PORT_ID_SECONDARY_SPDIF_TX) {
  3416. this_afe.sec_spdif_tx_cb = cb;
  3417. this_afe.sec_spdif_tx_private_data = private_data;
  3418. } else {
  3419. pr_err("%s: wrong port id 0x%x\n", __func__, port_id);
  3420. ret = -EINVAL;
  3421. goto fail_idx;
  3422. }
  3423. index = q6audio_get_port_index(port_id);
  3424. if (index < 0) {
  3425. pr_err("%s: Invalid index number: %d\n", __func__, index);
  3426. ret = -EINVAL;
  3427. goto fail_idx;
  3428. }
  3429. memset(&pl, 0, sizeof(pl));
  3430. pl.module_id = AFE_MODULE_CUSTOM_EVENTS;
  3431. pl.event_id = AFE_PORT_FMT_UPDATE_EVENT;
  3432. pl.reg_flag = reg_flag;
  3433. config->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3434. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3435. config->hdr.pkt_size = cmd_size;
  3436. config->hdr.src_port = 1;
  3437. config->hdr.dest_port = 1;
  3438. config->hdr.token = index;
  3439. config->hdr.opcode = AFE_PORT_CMD_MOD_EVENT_CFG;
  3440. config->port_id = q6audio_get_port_id(port_id);
  3441. config->num_events = num_events;
  3442. config->version = 1;
  3443. memcpy(config->payload, &pl, sizeof(pl));
  3444. ret = afe_apr_send_pkt((uint32_t *) config, &this_afe.wait[index]);
  3445. fail_idx:
  3446. kfree(config);
  3447. return ret;
  3448. }
  3449. EXPORT_SYMBOL(afe_spdif_reg_event_cfg);
  3450. int afe_send_slot_mapping_cfg(
  3451. struct afe_param_id_slot_mapping_cfg *slot_mapping_cfg,
  3452. u16 port_id)
  3453. {
  3454. struct param_hdr_v3 param_hdr;
  3455. int ret = 0;
  3456. if (!slot_mapping_cfg) {
  3457. pr_err("%s: Error, no configuration data\n", __func__);
  3458. return -EINVAL;
  3459. }
  3460. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  3461. memset(&param_hdr, 0, sizeof(param_hdr));
  3462. param_hdr.module_id = AFE_MODULE_TDM;
  3463. param_hdr.instance_id = INSTANCE_ID_0;
  3464. param_hdr.param_id = AFE_PARAM_ID_PORT_SLOT_MAPPING_CONFIG;
  3465. param_hdr.param_size = sizeof(struct afe_param_id_slot_mapping_cfg);
  3466. ret = q6afe_pack_and_set_param_in_band(port_id,
  3467. q6audio_get_port_index(port_id),
  3468. param_hdr,
  3469. (u8 *) slot_mapping_cfg);
  3470. if (ret < 0)
  3471. pr_err("%s: AFE send slot mapping for port 0x%x failed ret = %d\n",
  3472. __func__, port_id, ret);
  3473. return ret;
  3474. }
  3475. int afe_send_slot_mapping_cfg_v2(
  3476. struct afe_param_id_slot_mapping_cfg_v2 *slot_mapping_cfg,
  3477. u16 port_id)
  3478. {
  3479. struct param_hdr_v3 param_hdr;
  3480. int ret = 0;
  3481. if (!slot_mapping_cfg) {
  3482. pr_err("%s: Error, no configuration data\n", __func__);
  3483. return -EINVAL;
  3484. }
  3485. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  3486. memset(&param_hdr, 0, sizeof(param_hdr));
  3487. param_hdr.module_id = AFE_MODULE_TDM;
  3488. param_hdr.instance_id = INSTANCE_ID_0;
  3489. param_hdr.param_id = AFE_PARAM_ID_PORT_SLOT_MAPPING_CONFIG;
  3490. param_hdr.param_size = sizeof(struct afe_param_id_slot_mapping_cfg_v2);
  3491. ret = q6afe_pack_and_set_param_in_band(port_id,
  3492. q6audio_get_port_index(port_id),
  3493. param_hdr,
  3494. (u8 *) slot_mapping_cfg);
  3495. if (ret < 0)
  3496. pr_err("%s: AFE send slot mapping for port 0x%x failed ret = %d\n",
  3497. __func__, port_id, ret);
  3498. return ret;
  3499. }
  3500. int afe_send_custom_tdm_header_cfg(
  3501. struct afe_param_id_custom_tdm_header_cfg *custom_tdm_header_cfg,
  3502. u16 port_id)
  3503. {
  3504. struct param_hdr_v3 param_hdr;
  3505. int ret = 0;
  3506. if (!custom_tdm_header_cfg) {
  3507. pr_err("%s: Error, no configuration data\n", __func__);
  3508. return -EINVAL;
  3509. }
  3510. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  3511. memset(&param_hdr, 0, sizeof(param_hdr));
  3512. param_hdr.module_id = AFE_MODULE_TDM;
  3513. param_hdr.instance_id = INSTANCE_ID_0;
  3514. param_hdr.param_id = AFE_PARAM_ID_CUSTOM_TDM_HEADER_CONFIG;
  3515. param_hdr.param_size =
  3516. sizeof(struct afe_param_id_custom_tdm_header_cfg);
  3517. ret = q6afe_pack_and_set_param_in_band(port_id,
  3518. q6audio_get_port_index(port_id),
  3519. param_hdr,
  3520. (u8 *) custom_tdm_header_cfg);
  3521. if (ret < 0)
  3522. pr_err("%s: AFE send custom tdm header for port 0x%x failed ret = %d\n",
  3523. __func__, port_id, ret);
  3524. return ret;
  3525. }
  3526. /**
  3527. * afe_tdm_port_start - to configure AFE session with
  3528. * specified port configuration
  3529. *
  3530. * @port_id: AFE port id number
  3531. * @tdm_port: TDM port configutation
  3532. * @rate: sampling rate of port
  3533. * @num_groups: number of TDM groups
  3534. *
  3535. * Returns 0 on success or error value on port start failure.
  3536. */
  3537. int afe_tdm_port_start(u16 port_id, struct afe_tdm_port_config *tdm_port,
  3538. u32 rate, u16 num_groups)
  3539. {
  3540. struct param_hdr_v3 param_hdr;
  3541. int index = 0;
  3542. uint16_t port_index = 0;
  3543. enum afe_mad_type mad_type = MAD_HW_NONE;
  3544. int ret = 0;
  3545. if (!tdm_port) {
  3546. pr_err("%s: Error, no configuration data\n", __func__);
  3547. return -EINVAL;
  3548. }
  3549. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  3550. memset(&param_hdr, 0, sizeof(param_hdr));
  3551. index = q6audio_get_port_index(port_id);
  3552. if (index < 0 || index >= AFE_MAX_PORTS) {
  3553. pr_err("%s: AFE port index[%d] invalid!\n",
  3554. __func__, index);
  3555. return -EINVAL;
  3556. }
  3557. ret = q6audio_validate_port(port_id);
  3558. if (ret < 0) {
  3559. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  3560. return -EINVAL;
  3561. }
  3562. ret = afe_q6_interface_prepare();
  3563. if (ret != 0) {
  3564. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  3565. return ret;
  3566. }
  3567. if ((index >= 0) && (index < AFE_MAX_PORTS)) {
  3568. this_afe.afe_sample_rates[index] = rate;
  3569. if (this_afe.rt_cb)
  3570. this_afe.dev_acdb_id[index] = this_afe.rt_cb(port_id);
  3571. }
  3572. port_index = afe_get_port_index(port_id);
  3573. /* Also send the topology id here: */
  3574. if (!(this_afe.afe_cal_mode[port_index] == AFE_CAL_MODE_NONE)) {
  3575. /* One time call: only for first time */
  3576. afe_send_custom_topology();
  3577. afe_send_port_topology_id(port_id);
  3578. afe_send_cal(port_id);
  3579. afe_send_hw_delay(port_id, rate);
  3580. }
  3581. /* Start SW MAD module */
  3582. mad_type = afe_port_get_mad_type(port_id);
  3583. pr_debug("%s: port_id 0x%x, mad_type %d\n", __func__, port_id,
  3584. mad_type);
  3585. if (mad_type != MAD_HW_NONE && mad_type != MAD_SW_AUDIO) {
  3586. if (!afe_has_config(AFE_CDC_REGISTERS_CONFIG) ||
  3587. !afe_has_config(AFE_SLIMBUS_SLAVE_CONFIG)) {
  3588. pr_err("%s: AFE isn't configured yet for\n"
  3589. "HW MAD try Again\n", __func__);
  3590. ret = -EAGAIN;
  3591. goto fail_cmd;
  3592. }
  3593. ret = afe_turn_onoff_hw_mad(mad_type, true);
  3594. if (ret) {
  3595. pr_err("%s: afe_turn_onoff_hw_mad failed %d\n",
  3596. __func__, ret);
  3597. goto fail_cmd;
  3598. }
  3599. }
  3600. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  3601. param_hdr.instance_id = INSTANCE_ID_0;
  3602. param_hdr.param_id = AFE_PARAM_ID_TDM_CONFIG;
  3603. param_hdr.param_size = sizeof(struct afe_param_id_tdm_cfg);
  3604. ret = q6afe_pack_and_set_param_in_band(port_id,
  3605. q6audio_get_port_index(port_id),
  3606. param_hdr,
  3607. (u8 *) &tdm_port->tdm);
  3608. if (ret) {
  3609. pr_err("%s: AFE enable for port 0x%x failed ret = %d\n",
  3610. __func__, port_id, ret);
  3611. goto fail_cmd;
  3612. }
  3613. port_index = afe_get_port_index(port_id);
  3614. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  3615. this_afe.afe_sample_rates[port_index] = rate;
  3616. } else {
  3617. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  3618. ret = -EINVAL;
  3619. goto fail_cmd;
  3620. }
  3621. if (q6core_get_avcs_api_version_per_service(
  3622. APRV2_IDS_SERVICE_ID_ADSP_AFE_V) >= AFE_API_VERSION_V3)
  3623. ret = afe_send_slot_mapping_cfg_v2(
  3624. &tdm_port->slot_mapping_v2, port_id);
  3625. else
  3626. ret = afe_send_slot_mapping_cfg(
  3627. &tdm_port->slot_mapping,
  3628. port_id);
  3629. if (ret < 0) {
  3630. pr_err("%s: afe send failed %d\n", __func__, ret);
  3631. goto fail_cmd;
  3632. }
  3633. if (tdm_port->custom_tdm_header.header_type) {
  3634. ret = afe_send_custom_tdm_header_cfg(
  3635. &tdm_port->custom_tdm_header, port_id);
  3636. if (ret < 0) {
  3637. pr_err("%s: afe send failed %d\n", __func__, ret);
  3638. goto fail_cmd;
  3639. }
  3640. }
  3641. ret = afe_send_cmd_port_start(port_id);
  3642. fail_cmd:
  3643. return ret;
  3644. }
  3645. EXPORT_SYMBOL(afe_tdm_port_start);
  3646. /**
  3647. * afe_set_cal_mode -
  3648. * set cal mode for AFE calibration
  3649. *
  3650. * @port_id: AFE port id number
  3651. * @afe_cal_mode: AFE calib mode
  3652. *
  3653. */
  3654. void afe_set_cal_mode(u16 port_id, enum afe_cal_mode afe_cal_mode)
  3655. {
  3656. uint16_t port_index;
  3657. port_index = afe_get_port_index(port_id);
  3658. this_afe.afe_cal_mode[port_index] = afe_cal_mode;
  3659. }
  3660. EXPORT_SYMBOL(afe_set_cal_mode);
  3661. /**
  3662. * afe_set_vad_cfg -
  3663. * set configuration for VAD
  3664. *
  3665. * @port_id: AFE port id number
  3666. * @vad_enable: enable/disable vad
  3667. * @preroll_config: Preroll configuration
  3668. *
  3669. */
  3670. void afe_set_vad_cfg(u32 vad_enable, u32 preroll_config,
  3671. u32 port_id)
  3672. {
  3673. uint16_t port_index;
  3674. port_index = afe_get_port_index(port_id);
  3675. this_afe.vad_cfg[port_index].is_enable = vad_enable;
  3676. this_afe.vad_cfg[port_index].pre_roll = preroll_config;
  3677. }
  3678. EXPORT_SYMBOL(afe_set_vad_cfg);
  3679. /**
  3680. * afe_get_island_mode_cfg -
  3681. * get island mode configuration
  3682. *
  3683. * @port_id: AFE port id number
  3684. * @enable_flag: Enable or Disable
  3685. *
  3686. */
  3687. void afe_get_island_mode_cfg(u16 port_id, u32 *enable_flag)
  3688. {
  3689. uint16_t port_index;
  3690. if (enable_flag) {
  3691. port_index = afe_get_port_index(port_id);
  3692. *enable_flag = this_afe.island_mode[port_index];
  3693. }
  3694. }
  3695. EXPORT_SYMBOL(afe_get_island_mode_cfg);
  3696. /**
  3697. * afe_set_island_mode_cfg -
  3698. * set island mode configuration
  3699. *
  3700. * @port_id: AFE port id number
  3701. * @enable_flag: Enable or Disable
  3702. *
  3703. */
  3704. void afe_set_island_mode_cfg(u16 port_id, u32 enable_flag)
  3705. {
  3706. uint16_t port_index;
  3707. port_index = afe_get_port_index(port_id);
  3708. this_afe.island_mode[port_index] = enable_flag;
  3709. trace_printk("%s: set island mode cfg 0x%x for port 0x%x\n",
  3710. __func__, this_afe.island_mode[port_index], port_id);
  3711. }
  3712. EXPORT_SYMBOL(afe_set_island_mode_cfg);
  3713. /**
  3714. * afe_set_routing_callback -
  3715. * Update callback function for routing
  3716. *
  3717. * @cb: callback function to update with
  3718. *
  3719. */
  3720. void afe_set_routing_callback(routing_cb cb)
  3721. {
  3722. this_afe.rt_cb = cb;
  3723. }
  3724. EXPORT_SYMBOL(afe_set_routing_callback);
  3725. int afe_port_send_usb_dev_param(u16 port_id, union afe_port_config *afe_config)
  3726. {
  3727. struct afe_param_id_usb_audio_dev_params usb_dev;
  3728. struct afe_param_id_usb_audio_dev_lpcm_fmt lpcm_fmt;
  3729. struct afe_param_id_usb_audio_svc_interval svc_int;
  3730. struct param_hdr_v3 param_hdr;
  3731. int ret = 0, index = 0;
  3732. if (!afe_config) {
  3733. pr_err("%s: Error, no configuration data\n", __func__);
  3734. ret = -EINVAL;
  3735. goto exit;
  3736. }
  3737. index = q6audio_get_port_index(port_id);
  3738. if (index < 0 || index >= AFE_MAX_PORTS) {
  3739. pr_err("%s: AFE port index[%d] invalid!\n",
  3740. __func__, index);
  3741. return -EINVAL;
  3742. }
  3743. memset(&usb_dev, 0, sizeof(usb_dev));
  3744. memset(&lpcm_fmt, 0, sizeof(lpcm_fmt));
  3745. memset(&param_hdr, 0, sizeof(param_hdr));
  3746. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  3747. param_hdr.instance_id = INSTANCE_ID_0;
  3748. param_hdr.param_id = AFE_PARAM_ID_USB_AUDIO_DEV_PARAMS;
  3749. param_hdr.param_size = sizeof(usb_dev);
  3750. usb_dev.cfg_minor_version = AFE_API_MINOR_VERSION_USB_AUDIO_CONFIG;
  3751. usb_dev.dev_token = afe_config->usb_audio.dev_token;
  3752. ret = q6afe_pack_and_set_param_in_band(port_id,
  3753. q6audio_get_port_index(port_id),
  3754. param_hdr, (u8 *) &usb_dev);
  3755. if (ret) {
  3756. pr_err("%s: AFE device param cmd failed %d\n",
  3757. __func__, ret);
  3758. goto exit;
  3759. }
  3760. param_hdr.param_id = AFE_PARAM_ID_USB_AUDIO_DEV_LPCM_FMT;
  3761. param_hdr.param_size = sizeof(lpcm_fmt);
  3762. lpcm_fmt.cfg_minor_version = AFE_API_MINOR_VERSION_USB_AUDIO_CONFIG;
  3763. lpcm_fmt.endian = afe_config->usb_audio.endian;
  3764. ret = q6afe_pack_and_set_param_in_band(port_id,
  3765. q6audio_get_port_index(port_id),
  3766. param_hdr, (u8 *) &lpcm_fmt);
  3767. if (ret) {
  3768. pr_err("%s: AFE device param cmd LPCM_FMT failed %d\n",
  3769. __func__, ret);
  3770. goto exit;
  3771. }
  3772. param_hdr.param_id = AFE_PARAM_ID_USB_AUDIO_SVC_INTERVAL;
  3773. param_hdr.param_size = sizeof(svc_int);
  3774. svc_int.cfg_minor_version =
  3775. AFE_API_MINOR_VERSION_USB_AUDIO_CONFIG;
  3776. svc_int.svc_interval = afe_config->usb_audio.service_interval;
  3777. pr_debug("%s: AFE device param cmd sending SVC_INTERVAL %d\n",
  3778. __func__, svc_int.svc_interval);
  3779. ret = q6afe_pack_and_set_param_in_band(port_id,
  3780. q6audio_get_port_index(port_id),
  3781. param_hdr, (u8 *) &svc_int);
  3782. if (ret) {
  3783. pr_err("%s: AFE device param cmd svc_interval failed %d\n",
  3784. __func__, ret);
  3785. ret = -EINVAL;
  3786. goto exit;
  3787. }
  3788. exit:
  3789. return ret;
  3790. }
  3791. static int q6afe_send_dec_config(u16 port_id,
  3792. union afe_port_config afe_config,
  3793. struct afe_dec_config *cfg,
  3794. u32 format,
  3795. u16 afe_in_channels, u16 afe_in_bit_width)
  3796. {
  3797. struct afe_dec_media_fmt_t dec_media_fmt;
  3798. struct avs_dec_depacketizer_id_param_t dec_depkt_id_param;
  3799. struct avs_dec_congestion_buffer_param_t dec_buffer_id_param;
  3800. struct afe_enc_dec_imc_info_param_t imc_info_param;
  3801. struct afe_port_media_type_t media_type;
  3802. struct afe_matched_port_t matched_port_param;
  3803. struct asm_aptx_ad_speech_mode_cfg_t speech_codec_init_param;
  3804. struct param_hdr_v3 param_hdr;
  3805. int ret;
  3806. u32 dec_fmt;
  3807. memset(&dec_depkt_id_param, 0, sizeof(dec_depkt_id_param));
  3808. memset(&dec_media_fmt, 0, sizeof(dec_media_fmt));
  3809. memset(&imc_info_param, 0, sizeof(imc_info_param));
  3810. memset(&media_type, 0, sizeof(media_type));
  3811. memset(&matched_port_param, 0, sizeof(matched_port_param));
  3812. memset(&speech_codec_init_param, 0, sizeof(speech_codec_init_param));
  3813. memset(&param_hdr, 0, sizeof(param_hdr));
  3814. param_hdr.module_id = AFE_MODULE_ID_DECODER;
  3815. param_hdr.instance_id = INSTANCE_ID_0;
  3816. pr_debug("%s: sending AFE_DECODER_PARAM_ID_DEPACKETIZER to DSP payload\n",
  3817. __func__);
  3818. param_hdr.param_id = AFE_DECODER_PARAM_ID_DEPACKETIZER_ID;
  3819. param_hdr.param_size = sizeof(struct avs_dec_depacketizer_id_param_t);
  3820. dec_depkt_id_param.dec_depacketizer_id =
  3821. AFE_MODULE_ID_DEPACKETIZER_COP_V1;
  3822. if (cfg->format == ENC_CODEC_TYPE_LDAC)
  3823. dec_depkt_id_param.dec_depacketizer_id =
  3824. AFE_MODULE_ID_DEPACKETIZER_COP;
  3825. ret = q6afe_pack_and_set_param_in_band(port_id,
  3826. q6audio_get_port_index(port_id),
  3827. param_hdr,
  3828. (u8 *) &dec_depkt_id_param);
  3829. if (ret) {
  3830. pr_err("%s: AFE_DECODER_PARAM_ID_DEPACKETIZER for port 0x%x failed %d\n",
  3831. __func__, port_id, ret);
  3832. goto exit;
  3833. }
  3834. switch (cfg->format) {
  3835. case ASM_MEDIA_FMT_SBC:
  3836. case ASM_MEDIA_FMT_AAC_V2:
  3837. case ASM_MEDIA_FMT_MP3:
  3838. if (port_id == SLIMBUS_9_TX) {
  3839. dec_buffer_id_param.max_nr_buffers = 200;
  3840. dec_buffer_id_param.pre_buffer_size = 200;
  3841. } else {
  3842. dec_buffer_id_param.max_nr_buffers = 0;
  3843. dec_buffer_id_param.pre_buffer_size = 0;
  3844. }
  3845. pr_debug("%s: sending AFE_DECODER_PARAM_ID_CONGESTION_BUFFER_SIZE to DSP payload\n",
  3846. __func__);
  3847. param_hdr.param_id =
  3848. AFE_DECODER_PARAM_ID_CONGESTION_BUFFER_SIZE;
  3849. param_hdr.param_size =
  3850. sizeof(struct avs_dec_congestion_buffer_param_t);
  3851. dec_buffer_id_param.version = 0;
  3852. ret = q6afe_pack_and_set_param_in_band(port_id,
  3853. q6audio_get_port_index(port_id),
  3854. param_hdr,
  3855. (u8 *) &dec_buffer_id_param);
  3856. if (ret) {
  3857. pr_err("%s: AFE_DECODER_PARAM_ID_CONGESTION_BUFFER_SIZE for port 0x%x failed %d\n",
  3858. __func__, port_id, ret);
  3859. goto exit;
  3860. }
  3861. break;
  3862. case ASM_MEDIA_FMT_APTX_ADAPTIVE:
  3863. if (!cfg->abr_dec_cfg.is_abr_enabled) {
  3864. pr_debug("%s: sending aptx adaptive congestion buffer size to dsp\n",
  3865. __func__);
  3866. param_hdr.param_id =
  3867. AFE_DECODER_PARAM_ID_CONGESTION_BUFFER_SIZE;
  3868. param_hdr.param_size =
  3869. sizeof(struct avs_dec_congestion_buffer_param_t);
  3870. dec_buffer_id_param.version = 0;
  3871. dec_buffer_id_param.max_nr_buffers = 226;
  3872. dec_buffer_id_param.pre_buffer_size = 226;
  3873. ret = q6afe_pack_and_set_param_in_band(port_id,
  3874. q6audio_get_port_index(port_id),
  3875. param_hdr,
  3876. (u8 *) &dec_buffer_id_param);
  3877. if (ret) {
  3878. pr_err("%s: aptx adaptive congestion buffer size for port 0x%x failed %d\n",
  3879. __func__, port_id, ret);
  3880. goto exit;
  3881. }
  3882. break;
  3883. }
  3884. /* fall through for abr enabled case */
  3885. default:
  3886. pr_debug("%s:sending AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION to DSP payload\n",
  3887. __func__);
  3888. param_hdr.param_id =
  3889. AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION;
  3890. param_hdr.param_size =
  3891. sizeof(struct afe_enc_dec_imc_info_param_t);
  3892. imc_info_param.imc_info = cfg->abr_dec_cfg.imc_info;
  3893. ret = q6afe_pack_and_set_param_in_band(port_id,
  3894. q6audio_get_port_index(port_id),
  3895. param_hdr,
  3896. (u8 *) &imc_info_param);
  3897. if (ret) {
  3898. pr_err("%s: AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION for port 0x%x failed %d\n",
  3899. __func__, port_id, ret);
  3900. goto exit;
  3901. }
  3902. break;
  3903. }
  3904. pr_debug("%s: Send AFE_API_VERSION_PORT_MEDIA_TYPE to DSP\n", __func__);
  3905. param_hdr.module_id = AFE_MODULE_PORT;
  3906. param_hdr.param_id = AFE_PARAM_ID_PORT_MEDIA_TYPE;
  3907. param_hdr.param_size = sizeof(struct afe_port_media_type_t);
  3908. media_type.minor_version = AFE_API_VERSION_PORT_MEDIA_TYPE;
  3909. switch (cfg->format) {
  3910. case ASM_MEDIA_FMT_AAC_V2:
  3911. media_type.sample_rate =
  3912. cfg->data.aac_config.sample_rate;
  3913. break;
  3914. case ASM_MEDIA_FMT_SBC:
  3915. media_type.sample_rate =
  3916. cfg->data.sbc_config.sample_rate;
  3917. break;
  3918. case ASM_MEDIA_FMT_APTX_ADAPTIVE:
  3919. if (!cfg->abr_dec_cfg.is_abr_enabled) {
  3920. media_type.sample_rate =
  3921. (cfg->data.aptx_ad_config.sample_rate == APTX_AD_44_1) ?
  3922. AFE_PORT_SAMPLE_RATE_44_1K :
  3923. AFE_PORT_SAMPLE_RATE_48K;
  3924. break;
  3925. }
  3926. /* fall through for abr enabled case */
  3927. case ASM_MEDIA_FMT_APTX_AD_SPEECH:
  3928. media_type.sample_rate = AFE_PORT_SAMPLE_RATE_32K;
  3929. break;
  3930. default:
  3931. media_type.sample_rate =
  3932. afe_config.slim_sch.sample_rate;
  3933. }
  3934. if (afe_in_bit_width)
  3935. media_type.bit_width = afe_in_bit_width;
  3936. else
  3937. media_type.bit_width = afe_config.slim_sch.bit_width;
  3938. if (afe_in_channels)
  3939. media_type.num_channels = afe_in_channels;
  3940. else
  3941. media_type.num_channels = afe_config.slim_sch.num_channels;
  3942. media_type.data_format = AFE_PORT_DATA_FORMAT_PCM;
  3943. media_type.reserved = 0;
  3944. ret = q6afe_pack_and_set_param_in_band(port_id,
  3945. q6audio_get_port_index(port_id),
  3946. param_hdr, (u8 *) &media_type);
  3947. if (ret) {
  3948. pr_err("%s: AFE_API_VERSION_PORT_MEDIA_TYPE for port 0x%x failed %d\n",
  3949. __func__, port_id, ret);
  3950. goto exit;
  3951. }
  3952. if (format != ASM_MEDIA_FMT_SBC && format != ASM_MEDIA_FMT_AAC_V2 &&
  3953. format != ASM_MEDIA_FMT_APTX_ADAPTIVE &&
  3954. format != ASM_MEDIA_FMT_APTX_AD_SPEECH) {
  3955. pr_debug("%s:Unsuppported dec format. Ignore AFE config %u\n",
  3956. __func__, format);
  3957. goto exit;
  3958. }
  3959. if (format == ASM_MEDIA_FMT_APTX_ADAPTIVE &&
  3960. cfg->abr_dec_cfg.is_abr_enabled) {
  3961. pr_debug("%s: Ignore AFE config for abr case\n", __func__);
  3962. goto exit;
  3963. }
  3964. if (format == ASM_MEDIA_FMT_APTX_AD_SPEECH) {
  3965. pr_debug("%s: sending AFE_PARAM_ID_RATE_MATCHED_PORT to DSP payload\n",
  3966. __func__);
  3967. param_hdr.param_id = AFE_PARAM_ID_RATE_MATCHED_PORT;
  3968. param_hdr.param_size =
  3969. sizeof(struct afe_matched_port_t);
  3970. matched_port_param.minor_version = AFE_API_VERSION_PORT_MEDIA_TYPE;
  3971. matched_port_param.enable = AFE_MATCHED_PORT_ENABLE;
  3972. ret = q6afe_pack_and_set_param_in_band(port_id,
  3973. q6audio_get_port_index(port_id),
  3974. param_hdr,
  3975. (u8 *) &matched_port_param);
  3976. if (ret) {
  3977. pr_err("%s: AFE_PARAM_ID_RATE_MATCHED_PORT for port 0x%x failed %d\n",
  3978. __func__, port_id, ret);
  3979. goto exit;
  3980. }
  3981. }
  3982. pr_debug("%s: sending AFE_DECODER_PARAM_ID_DEC_MEDIA_FMT to DSP payload\n",
  3983. __func__);
  3984. param_hdr.module_id = AFE_MODULE_ID_DECODER;
  3985. param_hdr.instance_id = INSTANCE_ID_0;
  3986. param_hdr.param_id = AFE_DECODER_PARAM_ID_DEC_FMT_ID;
  3987. param_hdr.param_size = sizeof(dec_fmt);
  3988. dec_fmt = format;
  3989. ret = q6afe_pack_and_set_param_in_band(port_id,
  3990. q6audio_get_port_index(port_id),
  3991. param_hdr, (u8 *) &dec_fmt);
  3992. if (ret) {
  3993. pr_err("%s: AFE_DECODER_PARAM_ID_DEC_MEDIA_FMT for port 0x%x failed %d\n",
  3994. __func__, port_id, ret);
  3995. goto exit;
  3996. }
  3997. switch (cfg->format) {
  3998. case ASM_MEDIA_FMT_AAC_V2:
  3999. case ASM_MEDIA_FMT_APTX_ADAPTIVE:
  4000. param_hdr.param_size = sizeof(struct afe_dec_media_fmt_t);
  4001. pr_debug("%s:send AVS_DECODER_PARAM_ID DEC_MEDIA_FMT to DSP payload\n",
  4002. __func__);
  4003. param_hdr.param_id = AVS_DECODER_PARAM_ID_DEC_MEDIA_FMT;
  4004. dec_media_fmt.dec_media_config = cfg->data;
  4005. ret = q6afe_pack_and_set_param_in_band(port_id,
  4006. q6audio_get_port_index(port_id),
  4007. param_hdr,
  4008. (u8 *) &dec_media_fmt);
  4009. if (ret) {
  4010. pr_err("%s: AVS_DECODER_PARAM_ID DEC_MEDIA_FMT for port 0x%x failed %d\n",
  4011. __func__, port_id, ret);
  4012. goto exit;
  4013. }
  4014. break;
  4015. case ASM_MEDIA_FMT_APTX_AD_SPEECH:
  4016. param_hdr.param_size =
  4017. sizeof(struct asm_aptx_ad_speech_dec_cfg_t);
  4018. pr_debug("%s: send AVS_DECODER_PARAM_ID_APTX_AD_SPEECH_DEC_INIT to DSP payload\n",
  4019. __func__);
  4020. param_hdr.param_id =
  4021. AVS_DECODER_PARAM_ID_APTX_AD_SPEECH_DEC_INIT;
  4022. speech_codec_init_param =
  4023. cfg->data.aptx_ad_speech_config.speech_mode;
  4024. ret = q6afe_pack_and_set_param_in_band(port_id,
  4025. q6audio_get_port_index(port_id),
  4026. param_hdr,
  4027. (u8 *) &speech_codec_init_param);
  4028. if (ret) {
  4029. pr_err("%s: AVS_DECODER_PARAM_ID_APTX_ADAPTIVE_SPEECH_DEC_INIT for port 0x%x failed %d\n",
  4030. __func__, port_id, ret);
  4031. goto exit;
  4032. }
  4033. break;
  4034. default:
  4035. pr_debug("%s:No need to send DEC_MEDIA_FMT to DSP payload\n",
  4036. __func__);
  4037. }
  4038. exit:
  4039. return ret;
  4040. }
  4041. static int q6afe_send_enc_config(u16 port_id,
  4042. union afe_enc_config_data *cfg, u32 format,
  4043. union afe_port_config afe_config,
  4044. u16 afe_in_channels, u16 afe_in_bit_width,
  4045. u32 scrambler_mode, u32 mono_mode)
  4046. {
  4047. u32 enc_fmt;
  4048. struct afe_enc_cfg_blk_param_t enc_blk_param;
  4049. struct afe_param_id_aptx_sync_mode sync_mode_param;
  4050. struct afe_id_aptx_adaptive_enc_init aptx_adaptive_enc_init;
  4051. struct avs_enc_packetizer_id_param_t enc_pkt_id_param;
  4052. struct avs_enc_set_scrambler_param_t enc_set_scrambler_param;
  4053. struct afe_enc_level_to_bitrate_map_param_t map_param;
  4054. struct afe_enc_dec_imc_info_param_t imc_info_param;
  4055. struct asm_aac_frame_size_control_t frame_ctl_param;
  4056. struct afe_port_media_type_t media_type;
  4057. struct aptx_channel_mode_param_t channel_mode_param;
  4058. struct afe_matched_port_t matched_port_param;
  4059. struct asm_aptx_ad_speech_mode_cfg_t speech_codec_init_param;
  4060. struct param_hdr_v3 param_hdr;
  4061. int ret;
  4062. uint32_t frame_size_ctl_value_v2;
  4063. pr_debug("%s:update DSP for enc format = %d\n", __func__, format);
  4064. memset(&enc_blk_param, 0, sizeof(enc_blk_param));
  4065. memset(&sync_mode_param, 0, sizeof(sync_mode_param));
  4066. memset(&aptx_adaptive_enc_init, 0, sizeof(aptx_adaptive_enc_init));
  4067. memset(&enc_pkt_id_param, 0, sizeof(enc_pkt_id_param));
  4068. memset(&enc_set_scrambler_param, 0, sizeof(enc_set_scrambler_param));
  4069. memset(&map_param, 0, sizeof(map_param));
  4070. memset(&imc_info_param, 0, sizeof(imc_info_param));
  4071. memset(&frame_ctl_param, 0, sizeof(frame_ctl_param));
  4072. memset(&media_type, 0, sizeof(media_type));
  4073. memset(&matched_port_param, 0, sizeof(matched_port_param));
  4074. memset(&speech_codec_init_param, 0, sizeof(speech_codec_init_param));
  4075. memset(&param_hdr, 0, sizeof(param_hdr));
  4076. if (format != ASM_MEDIA_FMT_SBC && format != ASM_MEDIA_FMT_AAC_V2 &&
  4077. format != ASM_MEDIA_FMT_APTX && format != ASM_MEDIA_FMT_APTX_HD &&
  4078. format != ASM_MEDIA_FMT_CELT && format != ASM_MEDIA_FMT_LDAC &&
  4079. format != ASM_MEDIA_FMT_APTX_ADAPTIVE &&
  4080. format != ASM_MEDIA_FMT_APTX_AD_SPEECH) {
  4081. pr_err("%s:Unsuppported enc format. Ignore AFE config\n",
  4082. __func__);
  4083. return 0;
  4084. }
  4085. param_hdr.module_id = AFE_MODULE_ID_ENCODER;
  4086. param_hdr.instance_id = INSTANCE_ID_0;
  4087. param_hdr.param_id = AFE_ENCODER_PARAM_ID_ENC_FMT_ID;
  4088. param_hdr.param_size = sizeof(enc_fmt);
  4089. enc_fmt = format;
  4090. pr_debug("%s:sending AFE_ENCODER_PARAM_ID_ENC_FMT_ID payload\n",
  4091. __func__);
  4092. ret = q6afe_pack_and_set_param_in_band(port_id,
  4093. q6audio_get_port_index(port_id),
  4094. param_hdr, (u8 *) &enc_fmt);
  4095. if (ret) {
  4096. pr_err("%s:unable to send AFE_ENCODER_PARAM_ID_ENC_FMT_ID",
  4097. __func__);
  4098. goto exit;
  4099. }
  4100. if (format == ASM_MEDIA_FMT_LDAC) {
  4101. param_hdr.param_size = sizeof(struct afe_enc_cfg_blk_param_t)
  4102. - sizeof(struct afe_abr_enc_cfg_t);
  4103. enc_blk_param.enc_cfg_blk_size =
  4104. sizeof(union afe_enc_config_data)
  4105. - sizeof(struct afe_abr_enc_cfg_t);
  4106. } else if (format == ASM_MEDIA_FMT_AAC_V2) {
  4107. param_hdr.param_size = sizeof(enc_blk_param)
  4108. - sizeof(struct asm_aac_frame_size_control_t);
  4109. enc_blk_param.enc_cfg_blk_size =
  4110. sizeof(enc_blk_param.enc_blk_config)
  4111. - sizeof(struct asm_aac_frame_size_control_t);
  4112. } else if (format == ASM_MEDIA_FMT_APTX_AD_SPEECH) {
  4113. param_hdr.param_size = sizeof(struct afe_enc_aptx_ad_speech_cfg_blk_param_t);
  4114. enc_blk_param.enc_cfg_blk_size = sizeof(struct asm_custom_enc_cfg_t);
  4115. } else {
  4116. param_hdr.param_size = sizeof(struct afe_enc_cfg_blk_param_t);
  4117. enc_blk_param.enc_cfg_blk_size =
  4118. sizeof(union afe_enc_config_data);
  4119. }
  4120. pr_debug("%s:send AFE_ENCODER_PARAM_ID_ENC_CFG_BLK to DSP payload\n",
  4121. __func__);
  4122. param_hdr.param_id = AFE_ENCODER_PARAM_ID_ENC_CFG_BLK;
  4123. enc_blk_param.enc_blk_config = *cfg;
  4124. ret = q6afe_pack_and_set_param_in_band(port_id,
  4125. q6audio_get_port_index(port_id),
  4126. param_hdr,
  4127. (u8 *) &enc_blk_param);
  4128. if (ret) {
  4129. pr_err("%s: AFE_ENCODER_PARAM_ID_ENC_CFG_BLK for port 0x%x failed %d\n",
  4130. __func__, port_id, ret);
  4131. goto exit;
  4132. }
  4133. if (format == ASM_MEDIA_FMT_AAC_V2) {
  4134. uint32_t frame_size_ctl_value = enc_blk_param.enc_blk_config.
  4135. aac_config.frame_ctl.ctl_value;
  4136. if (frame_size_ctl_value > 0) {
  4137. param_hdr.param_id =
  4138. AFE_PARAM_ID_AAC_FRM_SIZE_CONTROL;
  4139. param_hdr.param_size = sizeof(frame_ctl_param);
  4140. frame_ctl_param.ctl_type = enc_blk_param.
  4141. enc_blk_config.aac_config.frame_ctl.ctl_type;
  4142. frame_ctl_param.ctl_value = frame_size_ctl_value;
  4143. pr_debug("%s: send AFE_PARAM_ID_AAC_FRM_SIZE_CONTROL\n",
  4144. __func__);
  4145. ret = q6afe_pack_and_set_param_in_band(port_id,
  4146. q6audio_get_port_index(port_id),
  4147. param_hdr,
  4148. (u8 *) &frame_ctl_param);
  4149. if (ret) {
  4150. pr_err("%s: AAC_FRM_SIZE_CONTROL failed %d\n",
  4151. __func__, ret);
  4152. goto exit;
  4153. }
  4154. }
  4155. frame_size_ctl_value_v2 = enc_blk_param.enc_blk_config.
  4156. aac_config.frame_ctl_v2.ctl_value;
  4157. if (frame_size_ctl_value_v2 > 0) {
  4158. param_hdr.param_id =
  4159. AFE_PARAM_ID_AAC_FRM_SIZE_CONTROL;
  4160. param_hdr.param_size = sizeof(frame_ctl_param);
  4161. frame_ctl_param.ctl_type = enc_blk_param.
  4162. enc_blk_config.aac_config.frame_ctl_v2.ctl_type;
  4163. frame_ctl_param.ctl_value = enc_blk_param.
  4164. enc_blk_config.aac_config.frame_ctl_v2.ctl_value;
  4165. pr_debug("%s: send AFE_PARAM_ID_AAC_FRM_SIZE_CONTROL V2\n",
  4166. __func__);
  4167. ret = q6afe_pack_and_set_param_in_band(port_id,
  4168. q6audio_get_port_index(port_id),
  4169. param_hdr,
  4170. (u8 *) &frame_ctl_param);
  4171. if (ret) {
  4172. pr_err("%s: AAC_FRM_SIZE_CONTROL with VBR support failed %d\n",
  4173. __func__, ret);
  4174. goto exit;
  4175. }
  4176. }
  4177. }
  4178. if (format == ASM_MEDIA_FMT_APTX) {
  4179. pr_debug("%s: sending AFE_PARAM_ID_APTX_SYNC_MODE to DSP",
  4180. __func__);
  4181. param_hdr.param_id = AFE_PARAM_ID_APTX_SYNC_MODE;
  4182. param_hdr.param_size =
  4183. sizeof(struct afe_param_id_aptx_sync_mode);
  4184. sync_mode_param.sync_mode =
  4185. enc_blk_param.enc_blk_config.aptx_config.
  4186. aptx_v2_cfg.sync_mode;
  4187. ret = q6afe_pack_and_set_param_in_band(port_id,
  4188. q6audio_get_port_index(port_id),
  4189. param_hdr,
  4190. (u8 *) &sync_mode_param);
  4191. if (ret) {
  4192. pr_err("%s: AFE_PARAM_ID_APTX_SYNC_MODE for port 0x%x failed %d\n",
  4193. __func__, port_id, ret);
  4194. goto exit;
  4195. }
  4196. }
  4197. if (format == ASM_MEDIA_FMT_APTX_ADAPTIVE) {
  4198. pr_debug("%s: sending AFE_ID_APTX_ADAPTIVE_ENC_INIT to DSP\n",
  4199. __func__);
  4200. param_hdr.param_id = AFE_ID_APTX_ADAPTIVE_ENC_INIT;
  4201. param_hdr.param_size =
  4202. sizeof(struct afe_id_aptx_adaptive_enc_init);
  4203. aptx_adaptive_enc_init =
  4204. enc_blk_param.enc_blk_config.aptx_ad_config.
  4205. aptx_ad_cfg;
  4206. ret = q6afe_pack_and_set_param_in_band(port_id,
  4207. q6audio_get_port_index(port_id),
  4208. param_hdr,
  4209. (u8 *) &aptx_adaptive_enc_init);
  4210. if (ret) {
  4211. pr_err("%s: AFE_ID_APTX_ADAPTIVE_ENC_INIT for port 0x%x failed %d\n",
  4212. __func__, port_id, ret);
  4213. goto exit;
  4214. }
  4215. }
  4216. if (format == ASM_MEDIA_FMT_APTX_AD_SPEECH) {
  4217. pr_debug("%s: sending AVS_DECODER_PARAM_ID_APTX_AD_SPEECH_ENC_INIT to DSP\n",
  4218. __func__);
  4219. param_hdr.param_id = AVS_DECODER_PARAM_ID_APTX_AD_SPEECH_ENC_INIT;
  4220. param_hdr.param_size =
  4221. sizeof(struct asm_aptx_ad_speech_dec_cfg_t);
  4222. speech_codec_init_param = cfg->aptx_ad_speech_config.speech_mode;
  4223. ret = q6afe_pack_and_set_param_in_band(port_id,
  4224. q6audio_get_port_index(port_id),
  4225. param_hdr,
  4226. (u8 *) &speech_codec_init_param);
  4227. if (ret) {
  4228. pr_err("%s: AFE_ID_APTX_ADAPTIVE_ENC_INIT for port 0x%x failed %d\n",
  4229. __func__, port_id, ret);
  4230. goto exit;
  4231. }
  4232. }
  4233. pr_debug("%s:sending AFE_ENCODER_PARAM_ID_PACKETIZER to DSP\n",
  4234. __func__);
  4235. param_hdr.param_id = AFE_ENCODER_PARAM_ID_PACKETIZER_ID;
  4236. param_hdr.param_size = sizeof(struct avs_enc_packetizer_id_param_t);
  4237. enc_pkt_id_param.enc_packetizer_id = AFE_MODULE_ID_PACKETIZER_COP;
  4238. ret = q6afe_pack_and_set_param_in_band(port_id,
  4239. q6audio_get_port_index(port_id),
  4240. param_hdr,
  4241. (u8 *) &enc_pkt_id_param);
  4242. if (ret) {
  4243. pr_err("%s: AFE_ENCODER_PARAM_ID_PACKETIZER for port 0x%x failed %d\n",
  4244. __func__, port_id, ret);
  4245. goto exit;
  4246. }
  4247. if (format != ASM_MEDIA_FMT_APTX_AD_SPEECH) {
  4248. pr_debug("%s:sending AFE_ENCODER_PARAM_ID_ENABLE_SCRAMBLING mode= %d to DSP payload\n",
  4249. __func__, scrambler_mode);
  4250. param_hdr.param_id = AFE_ENCODER_PARAM_ID_ENABLE_SCRAMBLING;
  4251. param_hdr.param_size = sizeof(struct avs_enc_set_scrambler_param_t);
  4252. enc_set_scrambler_param.enable_scrambler = scrambler_mode;
  4253. ret = q6afe_pack_and_set_param_in_band(port_id,
  4254. q6audio_get_port_index(port_id),
  4255. param_hdr,
  4256. (u8 *) &enc_set_scrambler_param);
  4257. if (ret) {
  4258. pr_err("%s: AFE_ENCODER_PARAM_ID_ENABLE_SCRAMBLING for port 0x%x failed %d\n",
  4259. __func__, port_id, ret);
  4260. goto exit;
  4261. }
  4262. }
  4263. if (format == ASM_MEDIA_FMT_APTX) {
  4264. pr_debug("%s:sending CAPI_V2_PARAM_ID_APTX_ENC_SWITCH_TO_MONO mode= %d to DSP payload\n",
  4265. __func__, mono_mode);
  4266. param_hdr.param_id = CAPI_V2_PARAM_ID_APTX_ENC_SWITCH_TO_MONO;
  4267. param_hdr.param_size = sizeof(channel_mode_param);
  4268. channel_mode_param.channel_mode = mono_mode;
  4269. ret = q6afe_pack_and_set_param_in_band(port_id,
  4270. q6audio_get_port_index(port_id),
  4271. param_hdr,
  4272. (u8 *) &channel_mode_param);
  4273. if (ret) {
  4274. pr_err("%s: CAPI_V2_PARAM_ID_APTX_ENC_SWITCH_TO_MONO for port 0x%x failed %d\n",
  4275. __func__, port_id, ret);
  4276. }
  4277. }
  4278. if ((format == ASM_MEDIA_FMT_LDAC &&
  4279. cfg->ldac_config.abr_config.is_abr_enabled) ||
  4280. format == ASM_MEDIA_FMT_APTX_ADAPTIVE ||
  4281. format == ASM_MEDIA_FMT_APTX_AD_SPEECH) {
  4282. if (format != ASM_MEDIA_FMT_APTX_AD_SPEECH) {
  4283. pr_debug("%s:sending AFE_ENCODER_PARAM_ID_BIT_RATE_LEVEL_MAP to DSP payload",
  4284. __func__);
  4285. param_hdr.param_id = AFE_ENCODER_PARAM_ID_BIT_RATE_LEVEL_MAP;
  4286. param_hdr.param_size =
  4287. sizeof(struct afe_enc_level_to_bitrate_map_param_t);
  4288. map_param.mapping_table =
  4289. cfg->ldac_config.abr_config.mapping_info;
  4290. ret = q6afe_pack_and_set_param_in_band(port_id,
  4291. q6audio_get_port_index(port_id),
  4292. param_hdr,
  4293. (u8 *) &map_param);
  4294. if (ret) {
  4295. pr_err("%s: AFE_ENCODER_PARAM_ID_BIT_RATE_LEVEL_MAP for port 0x%x failed %d\n",
  4296. __func__, port_id, ret);
  4297. goto exit;
  4298. }
  4299. }
  4300. pr_debug("%s: sending AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION to DSP payload",
  4301. __func__);
  4302. param_hdr.param_id =
  4303. AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION;
  4304. param_hdr.param_size =
  4305. sizeof(struct afe_enc_dec_imc_info_param_t);
  4306. if (format == ASM_MEDIA_FMT_APTX_ADAPTIVE)
  4307. imc_info_param.imc_info =
  4308. cfg->aptx_ad_config.abr_cfg.imc_info;
  4309. else if (format == ASM_MEDIA_FMT_APTX_AD_SPEECH)
  4310. imc_info_param.imc_info =
  4311. cfg->aptx_ad_speech_config.imc_info;
  4312. else
  4313. imc_info_param.imc_info =
  4314. cfg->ldac_config.abr_config.imc_info;
  4315. ret = q6afe_pack_and_set_param_in_band(port_id,
  4316. q6audio_get_port_index(port_id),
  4317. param_hdr,
  4318. (u8 *) &imc_info_param);
  4319. if (ret) {
  4320. pr_err("%s: AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION for port 0x%x failed %d\n",
  4321. __func__, port_id, ret);
  4322. goto exit;
  4323. }
  4324. }
  4325. pr_debug("%s:Sending AFE_API_VERSION_PORT_MEDIA_TYPE to DSP", __func__);
  4326. param_hdr.module_id = AFE_MODULE_PORT;
  4327. param_hdr.param_id = AFE_PARAM_ID_PORT_MEDIA_TYPE;
  4328. param_hdr.param_size = sizeof(struct afe_port_media_type_t);
  4329. media_type.minor_version = AFE_API_VERSION_PORT_MEDIA_TYPE;
  4330. if (format == ASM_MEDIA_FMT_LDAC)
  4331. media_type.sample_rate =
  4332. cfg->ldac_config.custom_config.sample_rate;
  4333. else if (format == ASM_MEDIA_FMT_APTX_ADAPTIVE)
  4334. media_type.sample_rate =
  4335. cfg->aptx_ad_config.custom_cfg.sample_rate;
  4336. else if (format == ASM_MEDIA_FMT_APTX_AD_SPEECH)
  4337. media_type.sample_rate =
  4338. cfg->aptx_ad_speech_config.custom_cfg.sample_rate;
  4339. else
  4340. media_type.sample_rate =
  4341. afe_config.slim_sch.sample_rate;
  4342. if (afe_in_bit_width)
  4343. media_type.bit_width = afe_in_bit_width;
  4344. else
  4345. media_type.bit_width = afe_config.slim_sch.bit_width;
  4346. if (afe_in_channels)
  4347. media_type.num_channels = afe_in_channels;
  4348. else
  4349. media_type.num_channels = afe_config.slim_sch.num_channels;
  4350. media_type.data_format = AFE_PORT_DATA_FORMAT_PCM;
  4351. media_type.reserved = 0;
  4352. ret = q6afe_pack_and_set_param_in_band(port_id,
  4353. q6audio_get_port_index(port_id),
  4354. param_hdr, (u8 *) &media_type);
  4355. if (ret) {
  4356. pr_err("%s: AFE_API_VERSION_PORT_MEDIA_TYPE for port 0x%x failed %d\n",
  4357. __func__, port_id, ret);
  4358. goto exit;
  4359. }
  4360. if (format == ASM_MEDIA_FMT_APTX_AD_SPEECH) {
  4361. pr_debug("%s: sending AFE_PARAM_ID_RATE_MATCHED_PORT to DSP payload",
  4362. __func__);
  4363. param_hdr.param_id = AFE_PARAM_ID_RATE_MATCHED_PORT;
  4364. param_hdr.param_size =
  4365. sizeof(struct afe_matched_port_t);
  4366. matched_port_param.minor_version = AFE_API_VERSION_PORT_MEDIA_TYPE;
  4367. matched_port_param.enable = AFE_MATCHED_PORT_ENABLE;
  4368. ret = q6afe_pack_and_set_param_in_band(port_id,
  4369. q6audio_get_port_index(port_id),
  4370. param_hdr,
  4371. (u8 *) &matched_port_param);
  4372. if (ret) {
  4373. pr_err("%s: AFE_PARAM_ID_RATE_MATCHED_PORT for port 0x%x failed %d\n",
  4374. __func__, port_id, ret);
  4375. goto exit;
  4376. }
  4377. }
  4378. exit:
  4379. return ret;
  4380. }
  4381. int afe_set_tws_channel_mode(u32 format, u16 port_id, u32 channel_mode)
  4382. {
  4383. struct aptx_channel_mode_param_t channel_mode_param;
  4384. struct param_hdr_v3 param_info;
  4385. int ret = 0;
  4386. u32 param_id = 0;
  4387. if (format == ASM_MEDIA_FMT_APTX) {
  4388. param_id = CAPI_V2_PARAM_ID_APTX_ENC_SWITCH_TO_MONO;
  4389. } else if (format == ASM_MEDIA_FMT_APTX_ADAPTIVE) {
  4390. param_id = CAPI_V2_PARAM_ID_APTX_AD_ENC_SWITCH_TO_MONO;
  4391. } else {
  4392. pr_err("%s: Not supported format 0x%x\n", __func__, format);
  4393. return -EINVAL;
  4394. }
  4395. memset(&param_info, 0, sizeof(param_info));
  4396. memset(&channel_mode_param, 0, sizeof(channel_mode_param));
  4397. param_info.module_id = AFE_MODULE_ID_ENCODER;
  4398. param_info.instance_id = INSTANCE_ID_0;
  4399. param_info.param_id = param_id;
  4400. param_info.param_size = sizeof(channel_mode_param);
  4401. channel_mode_param.channel_mode = channel_mode;
  4402. ret = q6afe_pack_and_set_param_in_band(port_id,
  4403. q6audio_get_port_index(port_id),
  4404. param_info,
  4405. (u8 *) &channel_mode_param);
  4406. if (ret)
  4407. pr_err("%s: AFE set channel mode cfg for port 0x%x failed %d\n",
  4408. __func__, port_id, ret);
  4409. return ret;
  4410. }
  4411. EXPORT_SYMBOL(afe_set_tws_channel_mode);
  4412. static int __afe_port_start(u16 port_id, union afe_port_config *afe_config,
  4413. u32 rate, u16 afe_in_channels, u16 afe_in_bit_width,
  4414. union afe_enc_config_data *enc_cfg,
  4415. u32 codec_format, u32 scrambler_mode, u32 mono_mode,
  4416. struct afe_dec_config *dec_cfg)
  4417. {
  4418. union afe_port_config port_cfg;
  4419. struct param_hdr_v3 param_hdr;
  4420. int ret = 0;
  4421. int cfg_type;
  4422. int index = 0;
  4423. enum afe_mad_type mad_type;
  4424. uint16_t port_index;
  4425. memset(&param_hdr, 0, sizeof(param_hdr));
  4426. memset(&port_cfg, 0, sizeof(port_cfg));
  4427. if (!afe_config) {
  4428. pr_err("%s: Error, no configuration data\n", __func__);
  4429. ret = -EINVAL;
  4430. return ret;
  4431. }
  4432. if ((port_id == RT_PROXY_DAI_001_RX) ||
  4433. (port_id == RT_PROXY_DAI_002_TX)) {
  4434. pr_debug("%s: before incrementing pcm_afe_instance %d port_id 0x%x\n",
  4435. __func__,
  4436. pcm_afe_instance[port_id & 0x1], port_id);
  4437. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  4438. pcm_afe_instance[port_id & 0x1]++;
  4439. return 0;
  4440. }
  4441. if ((port_id == RT_PROXY_DAI_002_RX) ||
  4442. (port_id == RT_PROXY_DAI_001_TX)) {
  4443. pr_debug("%s: before incrementing proxy_afe_instance %d port_id 0x%x\n",
  4444. __func__,
  4445. proxy_afe_instance[port_id & 0x1], port_id);
  4446. if (!afe_close_done[port_id & 0x1]) {
  4447. /*close pcm dai corresponding to the proxy dai*/
  4448. afe_close(port_id - 0x10);
  4449. pcm_afe_instance[port_id & 0x1]++;
  4450. pr_debug("%s: reconfigure afe port again\n", __func__);
  4451. }
  4452. proxy_afe_instance[port_id & 0x1]++;
  4453. afe_close_done[port_id & 0x1] = false;
  4454. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  4455. }
  4456. pr_info("%s: port id: 0x%x\n", __func__, port_id);
  4457. index = q6audio_get_port_index(port_id);
  4458. if (index < 0 || index >= AFE_MAX_PORTS) {
  4459. pr_err("%s: AFE port index[%d] invalid!\n",
  4460. __func__, index);
  4461. return -EINVAL;
  4462. }
  4463. ret = q6audio_validate_port(port_id);
  4464. if (ret < 0) {
  4465. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  4466. return -EINVAL;
  4467. }
  4468. ret = afe_q6_interface_prepare();
  4469. if (ret != 0) {
  4470. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  4471. return ret;
  4472. }
  4473. if ((index >= 0) && (index < AFE_MAX_PORTS)) {
  4474. this_afe.afe_sample_rates[index] = rate;
  4475. if (this_afe.rt_cb)
  4476. this_afe.dev_acdb_id[index] = this_afe.rt_cb(port_id);
  4477. }
  4478. mutex_lock(&this_afe.afe_cmd_lock);
  4479. port_index = afe_get_port_index(port_id);
  4480. /* Also send the topology id here: */
  4481. if (!(this_afe.afe_cal_mode[port_index] == AFE_CAL_MODE_NONE)) {
  4482. /* One time call: only for first time */
  4483. afe_send_custom_topology();
  4484. afe_send_port_topology_id(port_id);
  4485. afe_send_cal(port_id);
  4486. afe_send_hw_delay(port_id, rate);
  4487. }
  4488. /* Start SW MAD module */
  4489. mad_type = afe_port_get_mad_type(port_id);
  4490. pr_debug("%s: port_id 0x%x, mad_type %d\n", __func__, port_id,
  4491. mad_type);
  4492. if (mad_type != MAD_HW_NONE && mad_type != MAD_SW_AUDIO) {
  4493. if (!afe_has_config(AFE_CDC_REGISTERS_CONFIG) ||
  4494. !afe_has_config(AFE_SLIMBUS_SLAVE_CONFIG)) {
  4495. pr_err("%s: AFE isn't configured yet for\n"
  4496. "HW MAD try Again\n", __func__);
  4497. ret = -EAGAIN;
  4498. goto fail_cmd;
  4499. }
  4500. ret = afe_turn_onoff_hw_mad(mad_type, true);
  4501. if (ret) {
  4502. pr_err("%s: afe_turn_onoff_hw_mad failed %d\n",
  4503. __func__, ret);
  4504. goto fail_cmd;
  4505. }
  4506. }
  4507. if ((this_afe.aanc_info.aanc_active) &&
  4508. (this_afe.aanc_info.aanc_tx_port == port_id)) {
  4509. this_afe.aanc_info.aanc_tx_port_sample_rate = rate;
  4510. port_index =
  4511. afe_get_port_index(this_afe.aanc_info.aanc_rx_port);
  4512. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  4513. this_afe.aanc_info.aanc_rx_port_sample_rate =
  4514. this_afe.afe_sample_rates[port_index];
  4515. } else {
  4516. pr_err("%s: Invalid port index %d\n",
  4517. __func__, port_index);
  4518. ret = -EINVAL;
  4519. goto fail_cmd;
  4520. }
  4521. ret = afe_aanc_start(this_afe.aanc_info.aanc_tx_port,
  4522. this_afe.aanc_info.aanc_rx_port);
  4523. pr_debug("%s: afe_aanc_start ret %d\n", __func__, ret);
  4524. }
  4525. if ((port_id == AFE_PORT_ID_USB_RX) ||
  4526. (port_id == AFE_PORT_ID_USB_TX)) {
  4527. ret = afe_port_send_usb_dev_param(port_id, afe_config);
  4528. if (ret) {
  4529. pr_err("%s: AFE device param for port 0x%x failed %d\n",
  4530. __func__, port_id, ret);
  4531. ret = -EINVAL;
  4532. goto fail_cmd;
  4533. }
  4534. }
  4535. switch (port_id) {
  4536. case AFE_PORT_ID_PRIMARY_PCM_RX:
  4537. case AFE_PORT_ID_PRIMARY_PCM_TX:
  4538. case AFE_PORT_ID_SECONDARY_PCM_RX:
  4539. case AFE_PORT_ID_SECONDARY_PCM_TX:
  4540. case AFE_PORT_ID_TERTIARY_PCM_RX:
  4541. case AFE_PORT_ID_TERTIARY_PCM_TX:
  4542. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  4543. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  4544. case AFE_PORT_ID_QUINARY_PCM_RX:
  4545. case AFE_PORT_ID_QUINARY_PCM_TX:
  4546. case AFE_PORT_ID_SENARY_PCM_RX:
  4547. case AFE_PORT_ID_SENARY_PCM_TX:
  4548. cfg_type = AFE_PARAM_ID_PCM_CONFIG;
  4549. break;
  4550. case PRIMARY_I2S_RX:
  4551. case PRIMARY_I2S_TX:
  4552. case SECONDARY_I2S_RX:
  4553. case SECONDARY_I2S_TX:
  4554. case MI2S_RX:
  4555. case MI2S_TX:
  4556. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  4557. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  4558. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  4559. case AFE_PORT_ID_SECONDARY_MI2S_RX_SD1:
  4560. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  4561. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  4562. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  4563. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  4564. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  4565. case AFE_PORT_ID_QUINARY_MI2S_RX:
  4566. case AFE_PORT_ID_QUINARY_MI2S_TX:
  4567. case AFE_PORT_ID_SENARY_MI2S_RX:
  4568. case AFE_PORT_ID_SENARY_MI2S_TX:
  4569. case AFE_PORT_ID_INT0_MI2S_RX:
  4570. case AFE_PORT_ID_INT0_MI2S_TX:
  4571. case AFE_PORT_ID_INT1_MI2S_RX:
  4572. case AFE_PORT_ID_INT1_MI2S_TX:
  4573. case AFE_PORT_ID_INT2_MI2S_RX:
  4574. case AFE_PORT_ID_INT2_MI2S_TX:
  4575. case AFE_PORT_ID_INT3_MI2S_RX:
  4576. case AFE_PORT_ID_INT3_MI2S_TX:
  4577. case AFE_PORT_ID_INT4_MI2S_RX:
  4578. case AFE_PORT_ID_INT4_MI2S_TX:
  4579. case AFE_PORT_ID_INT5_MI2S_RX:
  4580. case AFE_PORT_ID_INT5_MI2S_TX:
  4581. case AFE_PORT_ID_INT6_MI2S_RX:
  4582. case AFE_PORT_ID_INT6_MI2S_TX:
  4583. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  4584. break;
  4585. case AFE_PORT_ID_PRIMARY_META_MI2S_RX:
  4586. case AFE_PORT_ID_SECONDARY_META_MI2S_RX:
  4587. cfg_type = AFE_PARAM_ID_META_I2S_CONFIG;
  4588. break;
  4589. case HDMI_RX:
  4590. case DISPLAY_PORT_RX:
  4591. cfg_type = AFE_PARAM_ID_HDMI_CONFIG;
  4592. break;
  4593. case VOICE_PLAYBACK_TX:
  4594. case VOICE2_PLAYBACK_TX:
  4595. case VOICE_RECORD_RX:
  4596. case VOICE_RECORD_TX:
  4597. cfg_type = AFE_PARAM_ID_PSEUDO_PORT_CONFIG;
  4598. break;
  4599. case SLIMBUS_0_RX:
  4600. case SLIMBUS_0_TX:
  4601. case SLIMBUS_1_RX:
  4602. case SLIMBUS_1_TX:
  4603. case SLIMBUS_2_RX:
  4604. case SLIMBUS_2_TX:
  4605. case SLIMBUS_3_RX:
  4606. case SLIMBUS_3_TX:
  4607. case SLIMBUS_4_RX:
  4608. case SLIMBUS_4_TX:
  4609. case SLIMBUS_5_RX:
  4610. case SLIMBUS_5_TX:
  4611. case SLIMBUS_6_RX:
  4612. case SLIMBUS_6_TX:
  4613. case SLIMBUS_7_RX:
  4614. case SLIMBUS_7_TX:
  4615. case SLIMBUS_8_RX:
  4616. case SLIMBUS_8_TX:
  4617. case SLIMBUS_9_RX:
  4618. case SLIMBUS_9_TX:
  4619. cfg_type = AFE_PARAM_ID_SLIMBUS_CONFIG;
  4620. break;
  4621. case AFE_PORT_ID_USB_RX:
  4622. case AFE_PORT_ID_USB_TX:
  4623. cfg_type = AFE_PARAM_ID_USB_AUDIO_CONFIG;
  4624. break;
  4625. case RT_PROXY_PORT_001_RX:
  4626. case RT_PROXY_PORT_001_TX:
  4627. case RT_PROXY_PORT_002_RX:
  4628. case RT_PROXY_PORT_002_TX:
  4629. cfg_type = AFE_PARAM_ID_RT_PROXY_CONFIG;
  4630. break;
  4631. case INT_BT_SCO_RX:
  4632. case INT_BT_A2DP_RX:
  4633. case INT_BT_SCO_TX:
  4634. case INT_FM_RX:
  4635. case INT_FM_TX:
  4636. cfg_type = AFE_PARAM_ID_INTERNAL_BT_FM_CONFIG;
  4637. break;
  4638. case AFE_PORT_ID_WSA_CODEC_DMA_RX_0:
  4639. case AFE_PORT_ID_WSA_CODEC_DMA_TX_0:
  4640. case AFE_PORT_ID_WSA_CODEC_DMA_RX_1:
  4641. case AFE_PORT_ID_WSA_CODEC_DMA_TX_1:
  4642. case AFE_PORT_ID_WSA_CODEC_DMA_TX_2:
  4643. case AFE_PORT_ID_VA_CODEC_DMA_TX_0:
  4644. case AFE_PORT_ID_VA_CODEC_DMA_TX_1:
  4645. case AFE_PORT_ID_VA_CODEC_DMA_TX_2:
  4646. case AFE_PORT_ID_RX_CODEC_DMA_RX_0:
  4647. case AFE_PORT_ID_TX_CODEC_DMA_TX_0:
  4648. case AFE_PORT_ID_RX_CODEC_DMA_RX_1:
  4649. case AFE_PORT_ID_TX_CODEC_DMA_TX_1:
  4650. case AFE_PORT_ID_RX_CODEC_DMA_RX_2:
  4651. case AFE_PORT_ID_TX_CODEC_DMA_TX_2:
  4652. case AFE_PORT_ID_RX_CODEC_DMA_RX_3:
  4653. case AFE_PORT_ID_TX_CODEC_DMA_TX_3:
  4654. case AFE_PORT_ID_RX_CODEC_DMA_RX_4:
  4655. case AFE_PORT_ID_TX_CODEC_DMA_TX_4:
  4656. case AFE_PORT_ID_RX_CODEC_DMA_RX_5:
  4657. case AFE_PORT_ID_TX_CODEC_DMA_TX_5:
  4658. case AFE_PORT_ID_RX_CODEC_DMA_RX_6:
  4659. case AFE_PORT_ID_RX_CODEC_DMA_RX_7:
  4660. cfg_type = AFE_PARAM_ID_CODEC_DMA_CONFIG;
  4661. break;
  4662. default:
  4663. pr_err("%s: Invalid port id 0x%x\n", __func__, port_id);
  4664. ret = -EINVAL;
  4665. goto fail_cmd;
  4666. }
  4667. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  4668. param_hdr.instance_id = INSTANCE_ID_0;
  4669. param_hdr.param_id = cfg_type;
  4670. param_hdr.param_size = sizeof(union afe_port_config);
  4671. port_cfg = *afe_config;
  4672. if (((enc_cfg != NULL) || (dec_cfg != NULL)) &&
  4673. (codec_format != ASM_MEDIA_FMT_NONE) &&
  4674. (cfg_type == AFE_PARAM_ID_SLIMBUS_CONFIG)) {
  4675. port_cfg.slim_sch.data_format =
  4676. AFE_SB_DATA_FORMAT_GENERIC_COMPRESSED;
  4677. }
  4678. ret = q6afe_pack_and_set_param_in_band(port_id,
  4679. q6audio_get_port_index(port_id),
  4680. param_hdr, (u8 *) &port_cfg);
  4681. if (ret) {
  4682. pr_err("%s: AFE enable for port 0x%x failed %d\n",
  4683. __func__, port_id, ret);
  4684. goto fail_cmd;
  4685. }
  4686. if ((codec_format != ASM_MEDIA_FMT_NONE) &&
  4687. (cfg_type == AFE_PARAM_ID_SLIMBUS_CONFIG)) {
  4688. if (enc_cfg != NULL) {
  4689. pr_debug("%s: Found AFE encoder support for SLIMBUS format = %d\n",
  4690. __func__, codec_format);
  4691. ret = q6afe_send_enc_config(port_id, enc_cfg,
  4692. codec_format, *afe_config,
  4693. afe_in_channels,
  4694. afe_in_bit_width,
  4695. scrambler_mode, mono_mode);
  4696. if (ret) {
  4697. pr_err("%s: AFE encoder config for port 0x%x failed %d\n",
  4698. __func__, port_id, ret);
  4699. goto fail_cmd;
  4700. }
  4701. }
  4702. if (dec_cfg != NULL) {
  4703. pr_debug("%s: Found AFE decoder support for SLIMBUS format = %d\n",
  4704. __func__, codec_format);
  4705. ret = q6afe_send_dec_config(port_id, *afe_config,
  4706. dec_cfg, codec_format,
  4707. afe_in_channels,
  4708. afe_in_bit_width);
  4709. if (ret) {
  4710. pr_err("%s: AFE decoder config for port 0x%x failed %d\n",
  4711. __func__, port_id, ret);
  4712. goto fail_cmd;
  4713. }
  4714. }
  4715. }
  4716. port_index = afe_get_port_index(port_id);
  4717. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  4718. /*
  4719. * If afe_port_start() for tx port called before
  4720. * rx port, then aanc rx sample rate is zero. So,
  4721. * AANC state machine in AFE will not get triggered.
  4722. * Make sure to check whether aanc is active during
  4723. * afe_port_start() for rx port and if aanc rx
  4724. * sample rate is zero, call afe_aanc_start to configure
  4725. * aanc with valid sample rates.
  4726. */
  4727. if (this_afe.aanc_info.aanc_active &&
  4728. !this_afe.aanc_info.aanc_rx_port_sample_rate) {
  4729. this_afe.aanc_info.aanc_rx_port_sample_rate =
  4730. this_afe.afe_sample_rates[port_index];
  4731. ret = afe_aanc_start(this_afe.aanc_info.aanc_tx_port,
  4732. this_afe.aanc_info.aanc_rx_port);
  4733. pr_debug("%s: afe_aanc_start ret %d\n", __func__, ret);
  4734. }
  4735. } else {
  4736. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  4737. ret = -EINVAL;
  4738. goto fail_cmd;
  4739. }
  4740. ret = afe_send_cmd_port_start(port_id);
  4741. fail_cmd:
  4742. mutex_unlock(&this_afe.afe_cmd_lock);
  4743. return ret;
  4744. }
  4745. /**
  4746. * afe_port_start - to configure AFE session with
  4747. * specified port configuration
  4748. *
  4749. * @port_id: AFE port id number
  4750. * @afe_config: port configutation
  4751. * @rate: sampling rate of port
  4752. *
  4753. * Returns 0 on success or error value on port start failure.
  4754. */
  4755. int afe_port_start(u16 port_id, union afe_port_config *afe_config,
  4756. u32 rate)
  4757. {
  4758. return __afe_port_start(port_id, afe_config, rate,
  4759. 0, 0, NULL, ASM_MEDIA_FMT_NONE, 0, 0, NULL);
  4760. }
  4761. EXPORT_SYMBOL(afe_port_start);
  4762. /**
  4763. * afe_port_start_v2 - to configure AFE session with
  4764. * specified port configuration and encoder /decoder params
  4765. *
  4766. * @port_id: AFE port id number
  4767. * @afe_config: port configutation
  4768. * @rate: sampling rate of port
  4769. * @enc_cfg: AFE enc configuration information to setup encoder
  4770. * @afe_in_channels: AFE input channel configuration, this needs
  4771. * update only if input channel is differ from AFE output
  4772. * @dec_cfg: AFE dec configuration information to set up decoder
  4773. *
  4774. * Returns 0 on success or error value on port start failure.
  4775. */
  4776. int afe_port_start_v2(u16 port_id, union afe_port_config *afe_config,
  4777. u32 rate, u16 afe_in_channels, u16 afe_in_bit_width,
  4778. struct afe_enc_config *enc_cfg,
  4779. struct afe_dec_config *dec_cfg)
  4780. {
  4781. int ret = 0;
  4782. if (enc_cfg != NULL)
  4783. ret = __afe_port_start(port_id, afe_config, rate,
  4784. afe_in_channels, afe_in_bit_width,
  4785. &enc_cfg->data, enc_cfg->format,
  4786. enc_cfg->scrambler_mode,
  4787. enc_cfg->mono_mode, dec_cfg);
  4788. else if (dec_cfg != NULL)
  4789. ret = __afe_port_start(port_id, afe_config, rate,
  4790. afe_in_channels, afe_in_bit_width,
  4791. NULL, dec_cfg->format, 0, 0, dec_cfg);
  4792. return ret;
  4793. }
  4794. EXPORT_SYMBOL(afe_port_start_v2);
  4795. int afe_get_port_index(u16 port_id)
  4796. {
  4797. switch (port_id) {
  4798. case PRIMARY_I2S_RX: return IDX_PRIMARY_I2S_RX;
  4799. case PRIMARY_I2S_TX: return IDX_PRIMARY_I2S_TX;
  4800. case AFE_PORT_ID_PRIMARY_PCM_RX:
  4801. return IDX_AFE_PORT_ID_PRIMARY_PCM_RX;
  4802. case AFE_PORT_ID_PRIMARY_PCM_TX:
  4803. return IDX_AFE_PORT_ID_PRIMARY_PCM_TX;
  4804. case AFE_PORT_ID_SECONDARY_PCM_RX:
  4805. return IDX_AFE_PORT_ID_SECONDARY_PCM_RX;
  4806. case AFE_PORT_ID_SECONDARY_PCM_TX:
  4807. return IDX_AFE_PORT_ID_SECONDARY_PCM_TX;
  4808. case AFE_PORT_ID_TERTIARY_PCM_RX:
  4809. return IDX_AFE_PORT_ID_TERTIARY_PCM_RX;
  4810. case AFE_PORT_ID_TERTIARY_PCM_TX:
  4811. return IDX_AFE_PORT_ID_TERTIARY_PCM_TX;
  4812. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  4813. return IDX_AFE_PORT_ID_QUATERNARY_PCM_RX;
  4814. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  4815. return IDX_AFE_PORT_ID_QUATERNARY_PCM_TX;
  4816. case AFE_PORT_ID_QUINARY_PCM_RX:
  4817. return IDX_AFE_PORT_ID_QUINARY_PCM_RX;
  4818. case AFE_PORT_ID_QUINARY_PCM_TX:
  4819. return IDX_AFE_PORT_ID_QUINARY_PCM_TX;
  4820. case AFE_PORT_ID_SENARY_PCM_RX:
  4821. return IDX_AFE_PORT_ID_SENARY_PCM_RX;
  4822. case AFE_PORT_ID_SENARY_PCM_TX:
  4823. return IDX_AFE_PORT_ID_SENARY_PCM_TX;
  4824. case SECONDARY_I2S_RX: return IDX_SECONDARY_I2S_RX;
  4825. case SECONDARY_I2S_TX: return IDX_SECONDARY_I2S_TX;
  4826. case MI2S_RX: return IDX_MI2S_RX;
  4827. case MI2S_TX: return IDX_MI2S_TX;
  4828. case HDMI_RX: return IDX_HDMI_RX;
  4829. case DISPLAY_PORT_RX: return IDX_DISPLAY_PORT_RX;
  4830. case AFE_PORT_ID_PRIMARY_SPDIF_RX: return IDX_PRIMARY_SPDIF_RX;
  4831. case AFE_PORT_ID_PRIMARY_SPDIF_TX: return IDX_PRIMARY_SPDIF_TX;
  4832. case AFE_PORT_ID_SECONDARY_SPDIF_RX: return IDX_SECONDARY_SPDIF_RX;
  4833. case AFE_PORT_ID_SECONDARY_SPDIF_TX: return IDX_SECONDARY_SPDIF_TX;
  4834. case RSVD_2: return IDX_RSVD_2;
  4835. case RSVD_3: return IDX_RSVD_3;
  4836. case DIGI_MIC_TX: return IDX_DIGI_MIC_TX;
  4837. case VOICE_RECORD_RX: return IDX_VOICE_RECORD_RX;
  4838. case VOICE_RECORD_TX: return IDX_VOICE_RECORD_TX;
  4839. case VOICE_PLAYBACK_TX: return IDX_VOICE_PLAYBACK_TX;
  4840. case VOICE2_PLAYBACK_TX: return IDX_VOICE2_PLAYBACK_TX;
  4841. case SLIMBUS_0_RX: return IDX_SLIMBUS_0_RX;
  4842. case SLIMBUS_0_TX: return IDX_SLIMBUS_0_TX;
  4843. case SLIMBUS_1_RX: return IDX_SLIMBUS_1_RX;
  4844. case SLIMBUS_1_TX: return IDX_SLIMBUS_1_TX;
  4845. case SLIMBUS_2_RX: return IDX_SLIMBUS_2_RX;
  4846. case SLIMBUS_2_TX: return IDX_SLIMBUS_2_TX;
  4847. case SLIMBUS_3_RX: return IDX_SLIMBUS_3_RX;
  4848. case SLIMBUS_3_TX: return IDX_SLIMBUS_3_TX;
  4849. case INT_BT_SCO_RX: return IDX_INT_BT_SCO_RX;
  4850. case INT_BT_SCO_TX: return IDX_INT_BT_SCO_TX;
  4851. case INT_BT_A2DP_RX: return IDX_INT_BT_A2DP_RX;
  4852. case INT_FM_RX: return IDX_INT_FM_RX;
  4853. case INT_FM_TX: return IDX_INT_FM_TX;
  4854. case RT_PROXY_PORT_001_RX: return IDX_RT_PROXY_PORT_001_RX;
  4855. case RT_PROXY_PORT_001_TX: return IDX_RT_PROXY_PORT_001_TX;
  4856. case SLIMBUS_4_RX: return IDX_SLIMBUS_4_RX;
  4857. case SLIMBUS_4_TX: return IDX_SLIMBUS_4_TX;
  4858. case SLIMBUS_5_RX: return IDX_SLIMBUS_5_RX;
  4859. case SLIMBUS_5_TX: return IDX_SLIMBUS_5_TX;
  4860. case SLIMBUS_6_RX: return IDX_SLIMBUS_6_RX;
  4861. case SLIMBUS_6_TX: return IDX_SLIMBUS_6_TX;
  4862. case SLIMBUS_7_RX: return IDX_SLIMBUS_7_RX;
  4863. case SLIMBUS_7_TX: return IDX_SLIMBUS_7_TX;
  4864. case SLIMBUS_8_RX: return IDX_SLIMBUS_8_RX;
  4865. case SLIMBUS_8_TX: return IDX_SLIMBUS_8_TX;
  4866. case SLIMBUS_9_RX: return IDX_SLIMBUS_9_RX;
  4867. case SLIMBUS_9_TX: return IDX_SLIMBUS_9_TX;
  4868. case AFE_PORT_ID_USB_RX: return IDX_AFE_PORT_ID_USB_RX;
  4869. case AFE_PORT_ID_USB_TX: return IDX_AFE_PORT_ID_USB_TX;
  4870. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  4871. return IDX_AFE_PORT_ID_PRIMARY_MI2S_RX;
  4872. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  4873. return IDX_AFE_PORT_ID_PRIMARY_MI2S_TX;
  4874. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  4875. return IDX_AFE_PORT_ID_QUATERNARY_MI2S_RX;
  4876. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  4877. return IDX_AFE_PORT_ID_QUATERNARY_MI2S_TX;
  4878. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  4879. return IDX_AFE_PORT_ID_SECONDARY_MI2S_RX;
  4880. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  4881. return IDX_AFE_PORT_ID_SECONDARY_MI2S_TX;
  4882. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  4883. return IDX_AFE_PORT_ID_TERTIARY_MI2S_RX;
  4884. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  4885. return IDX_AFE_PORT_ID_TERTIARY_MI2S_TX;
  4886. case AFE_PORT_ID_SECONDARY_MI2S_RX_SD1:
  4887. return IDX_AFE_PORT_ID_SECONDARY_MI2S_RX_SD1;
  4888. case AFE_PORT_ID_QUINARY_MI2S_RX:
  4889. return IDX_AFE_PORT_ID_QUINARY_MI2S_RX;
  4890. case AFE_PORT_ID_QUINARY_MI2S_TX:
  4891. return IDX_AFE_PORT_ID_QUINARY_MI2S_TX;
  4892. case AFE_PORT_ID_SENARY_MI2S_RX:
  4893. return IDX_AFE_PORT_ID_SENARY_MI2S_RX;
  4894. case AFE_PORT_ID_SENARY_MI2S_TX:
  4895. return IDX_AFE_PORT_ID_SENARY_MI2S_TX;
  4896. case AFE_PORT_ID_PRIMARY_TDM_RX:
  4897. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_0;
  4898. case AFE_PORT_ID_PRIMARY_TDM_TX:
  4899. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_0;
  4900. case AFE_PORT_ID_PRIMARY_TDM_RX_1:
  4901. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_1;
  4902. case AFE_PORT_ID_PRIMARY_TDM_TX_1:
  4903. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_1;
  4904. case AFE_PORT_ID_PRIMARY_TDM_RX_2:
  4905. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_2;
  4906. case AFE_PORT_ID_PRIMARY_TDM_TX_2:
  4907. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_2;
  4908. case AFE_PORT_ID_PRIMARY_TDM_RX_3:
  4909. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_3;
  4910. case AFE_PORT_ID_PRIMARY_TDM_TX_3:
  4911. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_3;
  4912. case AFE_PORT_ID_PRIMARY_TDM_RX_4:
  4913. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_4;
  4914. case AFE_PORT_ID_PRIMARY_TDM_TX_4:
  4915. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_4;
  4916. case AFE_PORT_ID_PRIMARY_TDM_RX_5:
  4917. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_5;
  4918. case AFE_PORT_ID_PRIMARY_TDM_TX_5:
  4919. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_5;
  4920. case AFE_PORT_ID_PRIMARY_TDM_RX_6:
  4921. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_6;
  4922. case AFE_PORT_ID_PRIMARY_TDM_TX_6:
  4923. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_6;
  4924. case AFE_PORT_ID_PRIMARY_TDM_RX_7:
  4925. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_7;
  4926. case AFE_PORT_ID_PRIMARY_TDM_TX_7:
  4927. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_7;
  4928. case AFE_PORT_ID_SECONDARY_TDM_RX:
  4929. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_0;
  4930. case AFE_PORT_ID_SECONDARY_TDM_TX:
  4931. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_0;
  4932. case AFE_PORT_ID_SECONDARY_TDM_RX_1:
  4933. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_1;
  4934. case AFE_PORT_ID_SECONDARY_TDM_TX_1:
  4935. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_1;
  4936. case AFE_PORT_ID_SECONDARY_TDM_RX_2:
  4937. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_2;
  4938. case AFE_PORT_ID_SECONDARY_TDM_TX_2:
  4939. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_2;
  4940. case AFE_PORT_ID_SECONDARY_TDM_RX_3:
  4941. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_3;
  4942. case AFE_PORT_ID_SECONDARY_TDM_TX_3:
  4943. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_3;
  4944. case AFE_PORT_ID_SECONDARY_TDM_RX_4:
  4945. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_4;
  4946. case AFE_PORT_ID_SECONDARY_TDM_TX_4:
  4947. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_4;
  4948. case AFE_PORT_ID_SECONDARY_TDM_RX_5:
  4949. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_5;
  4950. case AFE_PORT_ID_SECONDARY_TDM_TX_5:
  4951. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_5;
  4952. case AFE_PORT_ID_SECONDARY_TDM_RX_6:
  4953. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_6;
  4954. case AFE_PORT_ID_SECONDARY_TDM_TX_6:
  4955. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_6;
  4956. case AFE_PORT_ID_SECONDARY_TDM_RX_7:
  4957. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_7;
  4958. case AFE_PORT_ID_SECONDARY_TDM_TX_7:
  4959. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_7;
  4960. case AFE_PORT_ID_TERTIARY_TDM_RX:
  4961. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_0;
  4962. case AFE_PORT_ID_TERTIARY_TDM_TX:
  4963. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_0;
  4964. case AFE_PORT_ID_TERTIARY_TDM_RX_1:
  4965. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_1;
  4966. case AFE_PORT_ID_TERTIARY_TDM_TX_1:
  4967. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_1;
  4968. case AFE_PORT_ID_TERTIARY_TDM_RX_2:
  4969. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_2;
  4970. case AFE_PORT_ID_TERTIARY_TDM_TX_2:
  4971. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_2;
  4972. case AFE_PORT_ID_TERTIARY_TDM_RX_3:
  4973. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_3;
  4974. case AFE_PORT_ID_TERTIARY_TDM_TX_3:
  4975. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_3;
  4976. case AFE_PORT_ID_TERTIARY_TDM_RX_4:
  4977. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_4;
  4978. case AFE_PORT_ID_TERTIARY_TDM_TX_4:
  4979. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_4;
  4980. case AFE_PORT_ID_TERTIARY_TDM_RX_5:
  4981. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_5;
  4982. case AFE_PORT_ID_TERTIARY_TDM_TX_5:
  4983. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_5;
  4984. case AFE_PORT_ID_TERTIARY_TDM_RX_6:
  4985. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_6;
  4986. case AFE_PORT_ID_TERTIARY_TDM_TX_6:
  4987. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_6;
  4988. case AFE_PORT_ID_TERTIARY_TDM_RX_7:
  4989. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_7;
  4990. case AFE_PORT_ID_TERTIARY_TDM_TX_7:
  4991. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_7;
  4992. case AFE_PORT_ID_QUATERNARY_TDM_RX:
  4993. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_0;
  4994. case AFE_PORT_ID_QUATERNARY_TDM_TX:
  4995. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_0;
  4996. case AFE_PORT_ID_QUATERNARY_TDM_RX_1:
  4997. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_1;
  4998. case AFE_PORT_ID_QUATERNARY_TDM_TX_1:
  4999. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_1;
  5000. case AFE_PORT_ID_QUATERNARY_TDM_RX_2:
  5001. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_2;
  5002. case AFE_PORT_ID_QUATERNARY_TDM_TX_2:
  5003. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_2;
  5004. case AFE_PORT_ID_QUATERNARY_TDM_RX_3:
  5005. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_3;
  5006. case AFE_PORT_ID_QUATERNARY_TDM_TX_3:
  5007. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_3;
  5008. case AFE_PORT_ID_QUATERNARY_TDM_RX_4:
  5009. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_4;
  5010. case AFE_PORT_ID_QUATERNARY_TDM_TX_4:
  5011. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_4;
  5012. case AFE_PORT_ID_QUATERNARY_TDM_RX_5:
  5013. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_5;
  5014. case AFE_PORT_ID_QUATERNARY_TDM_TX_5:
  5015. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_5;
  5016. case AFE_PORT_ID_QUATERNARY_TDM_RX_6:
  5017. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_6;
  5018. case AFE_PORT_ID_QUATERNARY_TDM_TX_6:
  5019. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_6;
  5020. case AFE_PORT_ID_QUATERNARY_TDM_RX_7:
  5021. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_7;
  5022. case AFE_PORT_ID_QUATERNARY_TDM_TX_7:
  5023. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_7;
  5024. case AFE_PORT_ID_QUINARY_TDM_RX:
  5025. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_0;
  5026. case AFE_PORT_ID_QUINARY_TDM_TX:
  5027. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_0;
  5028. case AFE_PORT_ID_QUINARY_TDM_RX_1:
  5029. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_1;
  5030. case AFE_PORT_ID_QUINARY_TDM_TX_1:
  5031. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_1;
  5032. case AFE_PORT_ID_QUINARY_TDM_RX_2:
  5033. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_2;
  5034. case AFE_PORT_ID_QUINARY_TDM_TX_2:
  5035. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_2;
  5036. case AFE_PORT_ID_QUINARY_TDM_RX_3:
  5037. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_3;
  5038. case AFE_PORT_ID_QUINARY_TDM_TX_3:
  5039. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_3;
  5040. case AFE_PORT_ID_QUINARY_TDM_RX_4:
  5041. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_4;
  5042. case AFE_PORT_ID_QUINARY_TDM_TX_4:
  5043. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_4;
  5044. case AFE_PORT_ID_QUINARY_TDM_RX_5:
  5045. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_5;
  5046. case AFE_PORT_ID_QUINARY_TDM_TX_5:
  5047. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_5;
  5048. case AFE_PORT_ID_QUINARY_TDM_RX_6:
  5049. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_6;
  5050. case AFE_PORT_ID_QUINARY_TDM_TX_6:
  5051. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_6;
  5052. case AFE_PORT_ID_QUINARY_TDM_RX_7:
  5053. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_7;
  5054. case AFE_PORT_ID_QUINARY_TDM_TX_7:
  5055. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_7;
  5056. case AFE_PORT_ID_SENARY_TDM_RX:
  5057. return IDX_AFE_PORT_ID_SENARY_TDM_RX_0;
  5058. case AFE_PORT_ID_SENARY_TDM_TX:
  5059. return IDX_AFE_PORT_ID_SENARY_TDM_TX_0;
  5060. case AFE_PORT_ID_SENARY_TDM_RX_1:
  5061. return IDX_AFE_PORT_ID_SENARY_TDM_RX_1;
  5062. case AFE_PORT_ID_SENARY_TDM_TX_1:
  5063. return IDX_AFE_PORT_ID_SENARY_TDM_TX_1;
  5064. case AFE_PORT_ID_SENARY_TDM_RX_2:
  5065. return IDX_AFE_PORT_ID_SENARY_TDM_RX_2;
  5066. case AFE_PORT_ID_SENARY_TDM_TX_2:
  5067. return IDX_AFE_PORT_ID_SENARY_TDM_TX_2;
  5068. case AFE_PORT_ID_SENARY_TDM_RX_3:
  5069. return IDX_AFE_PORT_ID_SENARY_TDM_RX_3;
  5070. case AFE_PORT_ID_SENARY_TDM_TX_3:
  5071. return IDX_AFE_PORT_ID_SENARY_TDM_TX_3;
  5072. case AFE_PORT_ID_SENARY_TDM_RX_4:
  5073. return IDX_AFE_PORT_ID_SENARY_TDM_RX_4;
  5074. case AFE_PORT_ID_SENARY_TDM_TX_4:
  5075. return IDX_AFE_PORT_ID_SENARY_TDM_TX_4;
  5076. case AFE_PORT_ID_SENARY_TDM_RX_5:
  5077. return IDX_AFE_PORT_ID_SENARY_TDM_RX_5;
  5078. case AFE_PORT_ID_SENARY_TDM_TX_5:
  5079. return IDX_AFE_PORT_ID_SENARY_TDM_TX_5;
  5080. case AFE_PORT_ID_SENARY_TDM_RX_6:
  5081. return IDX_AFE_PORT_ID_SENARY_TDM_RX_6;
  5082. case AFE_PORT_ID_SENARY_TDM_TX_6:
  5083. return IDX_AFE_PORT_ID_SENARY_TDM_TX_6;
  5084. case AFE_PORT_ID_SENARY_TDM_RX_7:
  5085. return IDX_AFE_PORT_ID_SENARY_TDM_RX_7;
  5086. case AFE_PORT_ID_SENARY_TDM_TX_7:
  5087. return IDX_AFE_PORT_ID_SENARY_TDM_TX_7;
  5088. case AFE_PORT_ID_INT0_MI2S_RX:
  5089. return IDX_AFE_PORT_ID_INT0_MI2S_RX;
  5090. case AFE_PORT_ID_INT0_MI2S_TX:
  5091. return IDX_AFE_PORT_ID_INT0_MI2S_TX;
  5092. case AFE_PORT_ID_INT1_MI2S_RX:
  5093. return IDX_AFE_PORT_ID_INT1_MI2S_RX;
  5094. case AFE_PORT_ID_INT1_MI2S_TX:
  5095. return IDX_AFE_PORT_ID_INT1_MI2S_TX;
  5096. case AFE_PORT_ID_INT2_MI2S_RX:
  5097. return IDX_AFE_PORT_ID_INT2_MI2S_RX;
  5098. case AFE_PORT_ID_INT2_MI2S_TX:
  5099. return IDX_AFE_PORT_ID_INT2_MI2S_TX;
  5100. case AFE_PORT_ID_INT3_MI2S_RX:
  5101. return IDX_AFE_PORT_ID_INT3_MI2S_RX;
  5102. case AFE_PORT_ID_INT3_MI2S_TX:
  5103. return IDX_AFE_PORT_ID_INT3_MI2S_TX;
  5104. case AFE_PORT_ID_INT4_MI2S_RX:
  5105. return IDX_AFE_PORT_ID_INT4_MI2S_RX;
  5106. case AFE_PORT_ID_INT4_MI2S_TX:
  5107. return IDX_AFE_PORT_ID_INT4_MI2S_TX;
  5108. case AFE_PORT_ID_INT5_MI2S_RX:
  5109. return IDX_AFE_PORT_ID_INT5_MI2S_RX;
  5110. case AFE_PORT_ID_INT5_MI2S_TX:
  5111. return IDX_AFE_PORT_ID_INT5_MI2S_TX;
  5112. case AFE_PORT_ID_INT6_MI2S_RX:
  5113. return IDX_AFE_PORT_ID_INT6_MI2S_RX;
  5114. case AFE_PORT_ID_INT6_MI2S_TX:
  5115. return IDX_AFE_PORT_ID_INT6_MI2S_TX;
  5116. case AFE_PORT_ID_PRIMARY_META_MI2S_RX:
  5117. return IDX_AFE_PORT_ID_PRIMARY_META_MI2S_RX;
  5118. case AFE_PORT_ID_SECONDARY_META_MI2S_RX:
  5119. return IDX_AFE_PORT_ID_SECONDARY_META_MI2S_RX;
  5120. case AFE_PORT_ID_VA_CODEC_DMA_TX_0:
  5121. return IDX_AFE_PORT_ID_VA_CODEC_DMA_TX_0;
  5122. case AFE_PORT_ID_VA_CODEC_DMA_TX_1:
  5123. return IDX_AFE_PORT_ID_VA_CODEC_DMA_TX_1;
  5124. case AFE_PORT_ID_VA_CODEC_DMA_TX_2:
  5125. return IDX_AFE_PORT_ID_VA_CODEC_DMA_TX_2;
  5126. case AFE_PORT_ID_WSA_CODEC_DMA_RX_0:
  5127. return IDX_AFE_PORT_ID_WSA_CODEC_DMA_RX_0;
  5128. case AFE_PORT_ID_WSA_CODEC_DMA_TX_0:
  5129. return IDX_AFE_PORT_ID_WSA_CODEC_DMA_TX_0;
  5130. case AFE_PORT_ID_WSA_CODEC_DMA_RX_1:
  5131. return IDX_AFE_PORT_ID_WSA_CODEC_DMA_RX_1;
  5132. case AFE_PORT_ID_WSA_CODEC_DMA_TX_1:
  5133. return IDX_AFE_PORT_ID_WSA_CODEC_DMA_TX_1;
  5134. case AFE_PORT_ID_WSA_CODEC_DMA_TX_2:
  5135. return IDX_AFE_PORT_ID_WSA_CODEC_DMA_TX_2;
  5136. case AFE_PORT_ID_RX_CODEC_DMA_RX_0:
  5137. return IDX_AFE_PORT_ID_RX_CODEC_DMA_RX_0;
  5138. case AFE_PORT_ID_TX_CODEC_DMA_TX_0:
  5139. return IDX_AFE_PORT_ID_TX_CODEC_DMA_TX_0;
  5140. case AFE_PORT_ID_RX_CODEC_DMA_RX_1:
  5141. return IDX_AFE_PORT_ID_RX_CODEC_DMA_RX_1;
  5142. case AFE_PORT_ID_TX_CODEC_DMA_TX_1:
  5143. return IDX_AFE_PORT_ID_TX_CODEC_DMA_TX_1;
  5144. case AFE_PORT_ID_RX_CODEC_DMA_RX_2:
  5145. return IDX_AFE_PORT_ID_RX_CODEC_DMA_RX_2;
  5146. case AFE_PORT_ID_TX_CODEC_DMA_TX_2:
  5147. return IDX_AFE_PORT_ID_TX_CODEC_DMA_TX_2;
  5148. case AFE_PORT_ID_RX_CODEC_DMA_RX_3:
  5149. return IDX_AFE_PORT_ID_RX_CODEC_DMA_RX_3;
  5150. case AFE_PORT_ID_TX_CODEC_DMA_TX_3:
  5151. return IDX_AFE_PORT_ID_TX_CODEC_DMA_TX_3;
  5152. case AFE_PORT_ID_RX_CODEC_DMA_RX_4:
  5153. return IDX_AFE_PORT_ID_RX_CODEC_DMA_RX_4;
  5154. case AFE_PORT_ID_TX_CODEC_DMA_TX_4:
  5155. return IDX_AFE_PORT_ID_TX_CODEC_DMA_TX_4;
  5156. case AFE_PORT_ID_RX_CODEC_DMA_RX_5:
  5157. return IDX_AFE_PORT_ID_RX_CODEC_DMA_RX_5;
  5158. case AFE_PORT_ID_TX_CODEC_DMA_TX_5:
  5159. return IDX_AFE_PORT_ID_TX_CODEC_DMA_TX_5;
  5160. case AFE_PORT_ID_RX_CODEC_DMA_RX_6:
  5161. return IDX_AFE_PORT_ID_RX_CODEC_DMA_RX_6;
  5162. case AFE_PORT_ID_RX_CODEC_DMA_RX_7:
  5163. return IDX_AFE_PORT_ID_RX_CODEC_DMA_RX_7;
  5164. case AFE_LOOPBACK_TX:
  5165. return IDX_AFE_LOOPBACK_TX;
  5166. case RT_PROXY_PORT_002_RX:
  5167. return IDX_RT_PROXY_PORT_002_RX;
  5168. case RT_PROXY_PORT_002_TX:
  5169. return IDX_RT_PROXY_PORT_002_TX;
  5170. default:
  5171. pr_err("%s: port 0x%x\n", __func__, port_id);
  5172. return -EINVAL;
  5173. }
  5174. }
  5175. /**
  5176. * afe_open -
  5177. * command to open AFE port
  5178. *
  5179. * @port_id: AFE port id
  5180. * @afe_config: AFE port config to pass
  5181. * @rate: sample rate
  5182. *
  5183. * Returns 0 on success or error on failure
  5184. */
  5185. int afe_open(u16 port_id,
  5186. union afe_port_config *afe_config, int rate)
  5187. {
  5188. struct afe_port_cmd_device_start start;
  5189. union afe_port_config port_cfg;
  5190. struct param_hdr_v3 param_hdr;
  5191. int ret = 0;
  5192. int cfg_type;
  5193. int index = 0;
  5194. memset(&param_hdr, 0, sizeof(param_hdr));
  5195. memset(&start, 0, sizeof(start));
  5196. memset(&port_cfg, 0, sizeof(port_cfg));
  5197. if (!afe_config) {
  5198. pr_err("%s: Error, no configuration data\n", __func__);
  5199. ret = -EINVAL;
  5200. return ret;
  5201. }
  5202. pr_err("%s: port_id 0x%x rate %d\n", __func__, port_id, rate);
  5203. index = q6audio_get_port_index(port_id);
  5204. if (index < 0 || index >= AFE_MAX_PORTS) {
  5205. pr_err("%s: AFE port index[%d] invalid!\n",
  5206. __func__, index);
  5207. return -EINVAL;
  5208. }
  5209. ret = q6audio_validate_port(port_id);
  5210. if (ret < 0) {
  5211. pr_err("%s: Invalid port 0x%x ret %d", __func__, port_id, ret);
  5212. return -EINVAL;
  5213. }
  5214. if ((port_id == RT_PROXY_DAI_001_RX) ||
  5215. (port_id == RT_PROXY_DAI_002_TX)) {
  5216. pr_err("%s: wrong port 0x%x\n", __func__, port_id);
  5217. return -EINVAL;
  5218. }
  5219. if ((port_id == RT_PROXY_DAI_002_RX) ||
  5220. (port_id == RT_PROXY_DAI_001_TX))
  5221. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  5222. ret = afe_q6_interface_prepare();
  5223. if (ret != 0) {
  5224. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  5225. return -EINVAL;
  5226. }
  5227. if ((index >= 0) && (index < AFE_MAX_PORTS)) {
  5228. this_afe.afe_sample_rates[index] = rate;
  5229. if (this_afe.rt_cb)
  5230. this_afe.dev_acdb_id[index] = this_afe.rt_cb(port_id);
  5231. }
  5232. /* Also send the topology id here: */
  5233. afe_send_custom_topology(); /* One time call: only for first time */
  5234. afe_send_port_topology_id(port_id);
  5235. ret = q6audio_validate_port(port_id);
  5236. if (ret < 0) {
  5237. pr_err("%s: Failed : Invalid Port id = 0x%x ret %d\n",
  5238. __func__, port_id, ret);
  5239. return -EINVAL;
  5240. }
  5241. mutex_lock(&this_afe.afe_cmd_lock);
  5242. switch (port_id) {
  5243. case PRIMARY_I2S_RX:
  5244. case PRIMARY_I2S_TX:
  5245. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  5246. break;
  5247. case AFE_PORT_ID_PRIMARY_PCM_RX:
  5248. case AFE_PORT_ID_PRIMARY_PCM_TX:
  5249. case AFE_PORT_ID_SECONDARY_PCM_RX:
  5250. case AFE_PORT_ID_SECONDARY_PCM_TX:
  5251. case AFE_PORT_ID_TERTIARY_PCM_RX:
  5252. case AFE_PORT_ID_TERTIARY_PCM_TX:
  5253. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  5254. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  5255. case AFE_PORT_ID_QUINARY_PCM_RX:
  5256. case AFE_PORT_ID_QUINARY_PCM_TX:
  5257. case AFE_PORT_ID_SENARY_PCM_RX:
  5258. case AFE_PORT_ID_SENARY_PCM_TX:
  5259. cfg_type = AFE_PARAM_ID_PCM_CONFIG;
  5260. break;
  5261. case SECONDARY_I2S_RX:
  5262. case SECONDARY_I2S_TX:
  5263. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  5264. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  5265. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  5266. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  5267. case MI2S_RX:
  5268. case MI2S_TX:
  5269. case AFE_PORT_ID_QUINARY_MI2S_RX:
  5270. case AFE_PORT_ID_QUINARY_MI2S_TX:
  5271. case AFE_PORT_ID_SENARY_MI2S_RX:
  5272. case AFE_PORT_ID_SENARY_MI2S_TX:
  5273. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  5274. break;
  5275. case AFE_PORT_ID_PRIMARY_META_MI2S_RX:
  5276. case AFE_PORT_ID_SECONDARY_META_MI2S_RX:
  5277. cfg_type = AFE_PARAM_ID_META_I2S_CONFIG;
  5278. break;
  5279. case HDMI_RX:
  5280. case DISPLAY_PORT_RX:
  5281. cfg_type = AFE_PARAM_ID_HDMI_CONFIG;
  5282. break;
  5283. case AFE_PORT_ID_PRIMARY_SPDIF_RX:
  5284. case AFE_PORT_ID_PRIMARY_SPDIF_TX:
  5285. case AFE_PORT_ID_SECONDARY_SPDIF_RX:
  5286. case AFE_PORT_ID_SECONDARY_SPDIF_TX:
  5287. cfg_type = AFE_PARAM_ID_SPDIF_CONFIG;
  5288. break;
  5289. case SLIMBUS_0_RX:
  5290. case SLIMBUS_0_TX:
  5291. case SLIMBUS_1_RX:
  5292. case SLIMBUS_1_TX:
  5293. case SLIMBUS_2_RX:
  5294. case SLIMBUS_2_TX:
  5295. case SLIMBUS_3_RX:
  5296. case SLIMBUS_3_TX:
  5297. case SLIMBUS_4_RX:
  5298. case SLIMBUS_4_TX:
  5299. case SLIMBUS_5_RX:
  5300. case SLIMBUS_6_RX:
  5301. case SLIMBUS_6_TX:
  5302. case SLIMBUS_7_RX:
  5303. case SLIMBUS_7_TX:
  5304. case SLIMBUS_8_RX:
  5305. case SLIMBUS_8_TX:
  5306. case SLIMBUS_9_RX:
  5307. case SLIMBUS_9_TX:
  5308. cfg_type = AFE_PARAM_ID_SLIMBUS_CONFIG;
  5309. break;
  5310. case AFE_PORT_ID_USB_RX:
  5311. case AFE_PORT_ID_USB_TX:
  5312. cfg_type = AFE_PARAM_ID_USB_AUDIO_CONFIG;
  5313. break;
  5314. case AFE_PORT_ID_WSA_CODEC_DMA_RX_0:
  5315. case AFE_PORT_ID_WSA_CODEC_DMA_TX_0:
  5316. case AFE_PORT_ID_WSA_CODEC_DMA_RX_1:
  5317. case AFE_PORT_ID_WSA_CODEC_DMA_TX_1:
  5318. case AFE_PORT_ID_WSA_CODEC_DMA_TX_2:
  5319. case AFE_PORT_ID_VA_CODEC_DMA_TX_0:
  5320. case AFE_PORT_ID_VA_CODEC_DMA_TX_1:
  5321. case AFE_PORT_ID_VA_CODEC_DMA_TX_2:
  5322. case AFE_PORT_ID_RX_CODEC_DMA_RX_0:
  5323. case AFE_PORT_ID_TX_CODEC_DMA_TX_0:
  5324. case AFE_PORT_ID_RX_CODEC_DMA_RX_1:
  5325. case AFE_PORT_ID_TX_CODEC_DMA_TX_1:
  5326. case AFE_PORT_ID_RX_CODEC_DMA_RX_2:
  5327. case AFE_PORT_ID_TX_CODEC_DMA_TX_2:
  5328. case AFE_PORT_ID_RX_CODEC_DMA_RX_3:
  5329. case AFE_PORT_ID_TX_CODEC_DMA_TX_3:
  5330. case AFE_PORT_ID_RX_CODEC_DMA_RX_4:
  5331. case AFE_PORT_ID_TX_CODEC_DMA_TX_4:
  5332. case AFE_PORT_ID_RX_CODEC_DMA_RX_5:
  5333. case AFE_PORT_ID_TX_CODEC_DMA_TX_5:
  5334. case AFE_PORT_ID_RX_CODEC_DMA_RX_6:
  5335. case AFE_PORT_ID_RX_CODEC_DMA_RX_7:
  5336. cfg_type = AFE_PARAM_ID_CODEC_DMA_CONFIG;
  5337. break;
  5338. default:
  5339. pr_err("%s: Invalid port id 0x%x\n",
  5340. __func__, port_id);
  5341. ret = -EINVAL;
  5342. goto fail_cmd;
  5343. }
  5344. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5345. param_hdr.instance_id = INSTANCE_ID_0;
  5346. param_hdr.param_id = cfg_type;
  5347. param_hdr.param_size = sizeof(union afe_port_config);
  5348. port_cfg = *afe_config;
  5349. ret = q6afe_pack_and_set_param_in_band(port_id,
  5350. q6audio_get_port_index(port_id),
  5351. param_hdr, (u8 *) &port_cfg);
  5352. if (ret) {
  5353. pr_err("%s: AFE enable for port 0x%x opcode[0x%x]failed %d\n",
  5354. __func__, port_id, cfg_type, ret);
  5355. goto fail_cmd;
  5356. }
  5357. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5358. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5359. start.hdr.pkt_size = sizeof(start);
  5360. start.hdr.src_port = 0;
  5361. start.hdr.dest_port = 0;
  5362. start.hdr.token = index;
  5363. start.hdr.opcode = AFE_PORT_CMD_DEVICE_START;
  5364. start.port_id = q6audio_get_port_id(port_id);
  5365. pr_debug("%s: cmd device start opcode[0x%x] port id[0x%x]\n",
  5366. __func__, start.hdr.opcode, start.port_id);
  5367. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  5368. if (ret) {
  5369. pr_err("%s: AFE enable for port 0x%x failed %d\n", __func__,
  5370. port_id, ret);
  5371. goto fail_cmd;
  5372. }
  5373. fail_cmd:
  5374. mutex_unlock(&this_afe.afe_cmd_lock);
  5375. return ret;
  5376. }
  5377. EXPORT_SYMBOL(afe_open);
  5378. /**
  5379. * afe_loopback -
  5380. * command to set loopback between AFE ports
  5381. *
  5382. * @enable: enable or disable loopback
  5383. * @rx_port: AFE RX port ID
  5384. * @tx_port: AFE TX port ID
  5385. *
  5386. * Returns 0 on success or error on failure
  5387. */
  5388. int afe_loopback(u16 enable, u16 rx_port, u16 tx_port)
  5389. {
  5390. struct afe_loopback_cfg_v1 lb_param;
  5391. struct param_hdr_v3 param_hdr;
  5392. int ret = 0;
  5393. memset(&lb_param, 0, sizeof(lb_param));
  5394. memset(&param_hdr, 0, sizeof(param_hdr));
  5395. if (rx_port == MI2S_RX)
  5396. rx_port = AFE_PORT_ID_PRIMARY_MI2S_RX;
  5397. if (tx_port == MI2S_TX)
  5398. tx_port = AFE_PORT_ID_PRIMARY_MI2S_TX;
  5399. param_hdr.module_id = AFE_MODULE_LOOPBACK;
  5400. param_hdr.instance_id = INSTANCE_ID_0;
  5401. param_hdr.param_id = AFE_PARAM_ID_LOOPBACK_CONFIG;
  5402. param_hdr.param_size = sizeof(struct afe_loopback_cfg_v1);
  5403. lb_param.dst_port_id = rx_port;
  5404. lb_param.routing_mode = LB_MODE_DEFAULT;
  5405. lb_param.enable = (enable ? 1 : 0);
  5406. lb_param.loopback_cfg_minor_version = AFE_API_VERSION_LOOPBACK_CONFIG;
  5407. ret = q6afe_pack_and_set_param_in_band(tx_port,
  5408. q6audio_get_port_index(tx_port),
  5409. param_hdr, (u8 *) &lb_param);
  5410. if (ret)
  5411. pr_err("%s: AFE loopback failed %d\n", __func__, ret);
  5412. return ret;
  5413. }
  5414. EXPORT_SYMBOL(afe_loopback);
  5415. /**
  5416. * afe_loopback_gain -
  5417. * command to set gain for AFE loopback
  5418. *
  5419. * @port_id: AFE port id
  5420. * @volume: gain value to set
  5421. *
  5422. * Returns 0 on success or error on failure
  5423. */
  5424. int afe_loopback_gain(u16 port_id, u16 volume)
  5425. {
  5426. struct afe_loopback_gain_per_path_param set_param;
  5427. struct param_hdr_v3 param_hdr;
  5428. int ret = 0;
  5429. memset(&set_param, 0, sizeof(set_param));
  5430. memset(&param_hdr, 0, sizeof(param_hdr));
  5431. if (this_afe.apr == NULL) {
  5432. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  5433. 0xFFFFFFFF, &this_afe);
  5434. pr_debug("%s: Register AFE\n", __func__);
  5435. if (this_afe.apr == NULL) {
  5436. pr_err("%s: Unable to register AFE\n", __func__);
  5437. ret = -ENODEV;
  5438. return ret;
  5439. }
  5440. rtac_set_afe_handle(this_afe.apr);
  5441. }
  5442. ret = q6audio_validate_port(port_id);
  5443. if (ret < 0) {
  5444. pr_err("%s: Failed : Invalid Port id = 0x%x ret %d\n",
  5445. __func__, port_id, ret);
  5446. ret = -EINVAL;
  5447. goto fail_cmd;
  5448. }
  5449. /* RX ports numbers are even .TX ports numbers are odd. */
  5450. if (port_id % 2 == 0) {
  5451. pr_err("%s: Failed : afe loopback gain only for TX ports. port_id %d\n",
  5452. __func__, port_id);
  5453. ret = -EINVAL;
  5454. goto fail_cmd;
  5455. }
  5456. pr_debug("%s: port 0x%x volume %d\n", __func__, port_id, volume);
  5457. param_hdr.module_id = AFE_MODULE_LOOPBACK;
  5458. param_hdr.instance_id = INSTANCE_ID_0;
  5459. param_hdr.param_id = AFE_PARAM_ID_LOOPBACK_GAIN_PER_PATH;
  5460. param_hdr.param_size = sizeof(struct afe_loopback_gain_per_path_param);
  5461. set_param.rx_port_id = port_id;
  5462. set_param.gain = volume;
  5463. ret = q6afe_pack_and_set_param_in_band(port_id,
  5464. q6audio_get_port_index(port_id),
  5465. param_hdr, (u8 *) &set_param);
  5466. if (ret)
  5467. pr_err("%s: AFE param set failed for port 0x%x ret %d\n",
  5468. __func__, port_id, ret);
  5469. fail_cmd:
  5470. return ret;
  5471. }
  5472. EXPORT_SYMBOL(afe_loopback_gain);
  5473. int afe_pseudo_port_start_nowait(u16 port_id)
  5474. {
  5475. struct afe_pseudoport_start_command start;
  5476. int ret = 0;
  5477. pr_debug("%s: port_id=0x%x\n", __func__, port_id);
  5478. if (this_afe.apr == NULL) {
  5479. pr_err("%s: AFE APR is not registered\n", __func__);
  5480. return -ENODEV;
  5481. }
  5482. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5483. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5484. start.hdr.pkt_size = sizeof(start);
  5485. start.hdr.src_port = 0;
  5486. start.hdr.dest_port = 0;
  5487. start.hdr.token = 0;
  5488. start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
  5489. start.port_id = port_id;
  5490. start.timing = 1;
  5491. ret = afe_apr_send_pkt(&start, NULL);
  5492. if (ret) {
  5493. pr_err("%s: AFE enable for port 0x%x failed %d\n",
  5494. __func__, port_id, ret);
  5495. return ret;
  5496. }
  5497. return 0;
  5498. }
  5499. int afe_start_pseudo_port(u16 port_id)
  5500. {
  5501. int ret = 0;
  5502. struct afe_pseudoport_start_command start;
  5503. int index = 0;
  5504. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  5505. ret = afe_q6_interface_prepare();
  5506. if (ret != 0) {
  5507. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  5508. return ret;
  5509. }
  5510. index = q6audio_get_port_index(port_id);
  5511. if (index < 0 || index >= AFE_MAX_PORTS) {
  5512. pr_err("%s: AFE port index[%d] invalid!\n",
  5513. __func__, index);
  5514. return -EINVAL;
  5515. }
  5516. ret = q6audio_validate_port(port_id);
  5517. if (ret < 0) {
  5518. pr_err("%s: Invalid port 0x%x ret %d",
  5519. __func__, port_id, ret);
  5520. return -EINVAL;
  5521. }
  5522. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5523. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5524. start.hdr.pkt_size = sizeof(start);
  5525. start.hdr.src_port = 0;
  5526. start.hdr.dest_port = 0;
  5527. start.hdr.token = 0;
  5528. start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
  5529. start.port_id = port_id;
  5530. start.timing = 1;
  5531. start.hdr.token = index;
  5532. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  5533. if (ret)
  5534. pr_err("%s: AFE enable for port 0x%x failed %d\n",
  5535. __func__, port_id, ret);
  5536. return ret;
  5537. }
  5538. int afe_pseudo_port_stop_nowait(u16 port_id)
  5539. {
  5540. int ret = 0;
  5541. struct afe_pseudoport_stop_command stop;
  5542. int index = 0;
  5543. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  5544. if (this_afe.apr == NULL) {
  5545. pr_err("%s: AFE is already closed\n", __func__);
  5546. return -EINVAL;
  5547. }
  5548. index = q6audio_get_port_index(port_id);
  5549. if (index < 0 || index >= AFE_MAX_PORTS) {
  5550. pr_err("%s: AFE port index[%d] invalid!\n",
  5551. __func__, index);
  5552. return -EINVAL;
  5553. }
  5554. ret = q6audio_validate_port(port_id);
  5555. if (ret < 0) {
  5556. pr_err("%s: Invalid port 0x%x ret %d",
  5557. __func__, port_id, ret);
  5558. return -EINVAL;
  5559. }
  5560. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5561. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5562. stop.hdr.pkt_size = sizeof(stop);
  5563. stop.hdr.src_port = 0;
  5564. stop.hdr.dest_port = 0;
  5565. stop.hdr.token = 0;
  5566. stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
  5567. stop.port_id = port_id;
  5568. stop.reserved = 0;
  5569. stop.hdr.token = index;
  5570. ret = afe_apr_send_pkt(&stop, NULL);
  5571. if (ret)
  5572. pr_err("%s: AFE close failed %d\n", __func__, ret);
  5573. return ret;
  5574. }
  5575. int afe_port_group_set_param(u16 group_id,
  5576. union afe_port_group_config *afe_group_config)
  5577. {
  5578. struct param_hdr_v3 param_hdr;
  5579. int cfg_type;
  5580. int ret;
  5581. if (!afe_group_config) {
  5582. pr_err("%s: Error, no configuration data\n", __func__);
  5583. return -EINVAL;
  5584. }
  5585. pr_debug("%s: group id: 0x%x\n", __func__, group_id);
  5586. memset(&param_hdr, 0, sizeof(param_hdr));
  5587. ret = afe_q6_interface_prepare();
  5588. if (ret != 0) {
  5589. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  5590. return ret;
  5591. }
  5592. switch (group_id) {
  5593. case AFE_GROUP_DEVICE_ID_PRIMARY_TDM_RX:
  5594. case AFE_GROUP_DEVICE_ID_PRIMARY_TDM_TX:
  5595. case AFE_GROUP_DEVICE_ID_SECONDARY_TDM_RX:
  5596. case AFE_GROUP_DEVICE_ID_SECONDARY_TDM_TX:
  5597. case AFE_GROUP_DEVICE_ID_TERTIARY_TDM_RX:
  5598. case AFE_GROUP_DEVICE_ID_TERTIARY_TDM_TX:
  5599. case AFE_GROUP_DEVICE_ID_QUATERNARY_TDM_RX:
  5600. case AFE_GROUP_DEVICE_ID_QUATERNARY_TDM_TX:
  5601. case AFE_GROUP_DEVICE_ID_QUINARY_TDM_RX:
  5602. case AFE_GROUP_DEVICE_ID_QUINARY_TDM_TX:
  5603. case AFE_GROUP_DEVICE_ID_SENARY_TDM_RX:
  5604. case AFE_GROUP_DEVICE_ID_SENARY_TDM_TX:
  5605. cfg_type = AFE_PARAM_ID_GROUP_DEVICE_TDM_CONFIG;
  5606. break;
  5607. default:
  5608. pr_err("%s: Invalid group id 0x%x\n", __func__, group_id);
  5609. return -EINVAL;
  5610. }
  5611. param_hdr.module_id = AFE_MODULE_GROUP_DEVICE;
  5612. param_hdr.instance_id = INSTANCE_ID_0;
  5613. param_hdr.param_id = cfg_type;
  5614. param_hdr.param_size = sizeof(union afe_port_group_config);
  5615. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  5616. (u8 *) afe_group_config);
  5617. if (ret)
  5618. pr_err("%s: AFE_PARAM_ID_GROUP_DEVICE_CFG failed %d\n",
  5619. __func__, ret);
  5620. return ret;
  5621. }
  5622. /**
  5623. * afe_port_tdm_lane_config -
  5624. * to configure group TDM lane mask with specified configuration
  5625. *
  5626. * @group_id: AFE group id number
  5627. * @lane_cfg: TDM lane mask configutation
  5628. *
  5629. * Returns 0 on success or error value on failure.
  5630. */
  5631. static int afe_port_tdm_lane_config(u16 group_id,
  5632. struct afe_param_id_tdm_lane_cfg *lane_cfg)
  5633. {
  5634. struct param_hdr_v3 param_hdr;
  5635. int ret = 0;
  5636. if (lane_cfg == NULL ||
  5637. lane_cfg->lane_mask == AFE_LANE_MASK_INVALID) {
  5638. pr_debug("%s: lane cfg not supported for group id: 0x%x\n",
  5639. __func__, group_id);
  5640. return ret;
  5641. }
  5642. pr_debug("%s: group id: 0x%x lane mask 0x%x\n", __func__,
  5643. group_id, lane_cfg->lane_mask);
  5644. memset(&param_hdr, 0, sizeof(param_hdr));
  5645. ret = afe_q6_interface_prepare();
  5646. if (ret != 0) {
  5647. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  5648. return ret;
  5649. }
  5650. param_hdr.module_id = AFE_MODULE_GROUP_DEVICE;
  5651. param_hdr.instance_id = INSTANCE_ID_0;
  5652. param_hdr.param_id = AFE_PARAM_ID_TDM_LANE_CONFIG;
  5653. param_hdr.param_size = sizeof(struct afe_param_id_tdm_lane_cfg);
  5654. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  5655. (u8 *)lane_cfg);
  5656. if (ret)
  5657. pr_err("%s: AFE_PARAM_ID_TDM_LANE_CONFIG failed %d\n",
  5658. __func__, ret);
  5659. return ret;
  5660. }
  5661. /**
  5662. * afe_port_group_enable -
  5663. * command to enable AFE port group
  5664. *
  5665. * @group_id: group ID for AFE port group
  5666. * @afe_group_config: config for AFE group
  5667. * @enable: flag to indicate enable or disable
  5668. * @lane_cfg: TDM lane mask configutation
  5669. *
  5670. * Returns 0 on success or error on failure
  5671. */
  5672. int afe_port_group_enable(u16 group_id,
  5673. union afe_port_group_config *afe_group_config,
  5674. u16 enable,
  5675. struct afe_param_id_tdm_lane_cfg *lane_cfg)
  5676. {
  5677. struct afe_group_device_enable group_enable;
  5678. struct param_hdr_v3 param_hdr;
  5679. int ret;
  5680. pr_debug("%s: group id: 0x%x enable: %d\n", __func__,
  5681. group_id, enable);
  5682. memset(&group_enable, 0, sizeof(group_enable));
  5683. memset(&param_hdr, 0, sizeof(param_hdr));
  5684. ret = afe_q6_interface_prepare();
  5685. if (ret != 0) {
  5686. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  5687. return ret;
  5688. }
  5689. if (enable) {
  5690. ret = afe_port_group_set_param(group_id, afe_group_config);
  5691. if (ret < 0) {
  5692. pr_err("%s: afe send failed %d\n", __func__, ret);
  5693. return ret;
  5694. }
  5695. ret = afe_port_tdm_lane_config(group_id, lane_cfg);
  5696. if (ret < 0) {
  5697. pr_err("%s: afe send lane config failed %d\n",
  5698. __func__, ret);
  5699. return ret;
  5700. }
  5701. }
  5702. param_hdr.module_id = AFE_MODULE_GROUP_DEVICE;
  5703. param_hdr.instance_id = INSTANCE_ID_0;
  5704. param_hdr.param_id = AFE_PARAM_ID_GROUP_DEVICE_ENABLE;
  5705. param_hdr.param_size = sizeof(struct afe_group_device_enable);
  5706. group_enable.group_id = group_id;
  5707. group_enable.enable = enable;
  5708. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  5709. (u8 *) &group_enable);
  5710. if (ret)
  5711. pr_err("%s: AFE_PARAM_ID_GROUP_DEVICE_ENABLE failed %d\n",
  5712. __func__, ret);
  5713. return ret;
  5714. }
  5715. EXPORT_SYMBOL(afe_port_group_enable);
  5716. int afe_stop_pseudo_port(u16 port_id)
  5717. {
  5718. int ret = 0;
  5719. struct afe_pseudoport_stop_command stop;
  5720. int index = 0;
  5721. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  5722. if (this_afe.apr == NULL) {
  5723. pr_err("%s: AFE is already closed\n", __func__);
  5724. return -EINVAL;
  5725. }
  5726. index = q6audio_get_port_index(port_id);
  5727. if (index < 0 || index >= AFE_MAX_PORTS) {
  5728. pr_err("%s: AFE port index[%d] invalid!\n",
  5729. __func__, index);
  5730. return -EINVAL;
  5731. }
  5732. ret = q6audio_validate_port(port_id);
  5733. if (ret < 0) {
  5734. pr_err("%s: Invalid port 0x%x ret %d\n",
  5735. __func__, port_id, ret);
  5736. return -EINVAL;
  5737. }
  5738. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5739. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5740. stop.hdr.pkt_size = sizeof(stop);
  5741. stop.hdr.src_port = 0;
  5742. stop.hdr.dest_port = 0;
  5743. stop.hdr.token = 0;
  5744. stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
  5745. stop.port_id = port_id;
  5746. stop.reserved = 0;
  5747. stop.hdr.token = index;
  5748. ret = afe_apr_send_pkt(&stop, &this_afe.wait[index]);
  5749. if (ret)
  5750. pr_err("%s: AFE close failed %d\n", __func__, ret);
  5751. return ret;
  5752. }
  5753. /**
  5754. * afe_req_mmap_handle -
  5755. * Retrieve AFE memory map handle
  5756. *
  5757. * @ac: AFE audio client
  5758. *
  5759. * Returns memory map handle
  5760. */
  5761. uint32_t afe_req_mmap_handle(struct afe_audio_client *ac)
  5762. {
  5763. return ac->mem_map_handle;
  5764. }
  5765. EXPORT_SYMBOL(afe_req_mmap_handle);
  5766. /**
  5767. * q6afe_audio_client_alloc -
  5768. * Assign new AFE audio client
  5769. *
  5770. * @priv: privata data to hold for audio client
  5771. *
  5772. * Returns ac pointer on success or NULL on failure
  5773. */
  5774. struct afe_audio_client *q6afe_audio_client_alloc(void *priv)
  5775. {
  5776. struct afe_audio_client *ac;
  5777. int lcnt = 0;
  5778. ac = kzalloc(sizeof(struct afe_audio_client), GFP_KERNEL);
  5779. if (!ac)
  5780. return NULL;
  5781. ac->priv = priv;
  5782. init_waitqueue_head(&ac->cmd_wait);
  5783. INIT_LIST_HEAD(&ac->port[0].mem_map_handle);
  5784. INIT_LIST_HEAD(&ac->port[1].mem_map_handle);
  5785. pr_debug("%s: mem_map_handle list init'ed\n", __func__);
  5786. mutex_init(&ac->cmd_lock);
  5787. for (lcnt = 0; lcnt <= OUT; lcnt++) {
  5788. mutex_init(&ac->port[lcnt].lock);
  5789. spin_lock_init(&ac->port[lcnt].dsp_lock);
  5790. }
  5791. atomic_set(&ac->cmd_state, 0);
  5792. return ac;
  5793. }
  5794. EXPORT_SYMBOL(q6afe_audio_client_alloc);
  5795. /**
  5796. * q6afe_audio_client_buf_alloc_contiguous -
  5797. * Allocate contiguous shared buffers
  5798. *
  5799. * @dir: RX or TX direction of AFE port
  5800. * @ac: AFE audio client handle
  5801. * @bufsz: size of each shared buffer
  5802. * @bufcnt: number of buffers
  5803. *
  5804. * Returns 0 on success or error on failure
  5805. */
  5806. int q6afe_audio_client_buf_alloc_contiguous(unsigned int dir,
  5807. struct afe_audio_client *ac,
  5808. unsigned int bufsz,
  5809. unsigned int bufcnt)
  5810. {
  5811. int cnt = 0;
  5812. int rc = 0;
  5813. struct afe_audio_buffer *buf;
  5814. size_t len;
  5815. if (!(ac) || ((dir != IN) && (dir != OUT))) {
  5816. pr_err("%s: ac %pK dir %d\n", __func__, ac, dir);
  5817. return -EINVAL;
  5818. }
  5819. pr_debug("%s: bufsz[%d]bufcnt[%d]\n",
  5820. __func__,
  5821. bufsz, bufcnt);
  5822. if (ac->port[dir].buf) {
  5823. pr_debug("%s: buffer already allocated\n", __func__);
  5824. return 0;
  5825. }
  5826. mutex_lock(&ac->cmd_lock);
  5827. buf = kzalloc(((sizeof(struct afe_audio_buffer))*bufcnt),
  5828. GFP_KERNEL);
  5829. if (!buf) {
  5830. pr_err("%s: null buf\n", __func__);
  5831. mutex_unlock(&ac->cmd_lock);
  5832. goto fail;
  5833. }
  5834. ac->port[dir].buf = buf;
  5835. rc = msm_audio_ion_alloc(&buf[0].dma_buf,
  5836. bufsz * bufcnt,
  5837. &buf[0].phys, &len,
  5838. &buf[0].data);
  5839. if (rc) {
  5840. pr_err("%s: audio ION alloc failed, rc = %d\n",
  5841. __func__, rc);
  5842. mutex_unlock(&ac->cmd_lock);
  5843. goto fail;
  5844. }
  5845. buf[0].used = dir ^ 1;
  5846. buf[0].size = bufsz;
  5847. buf[0].actual_size = bufsz;
  5848. cnt = 1;
  5849. while (cnt < bufcnt) {
  5850. if (bufsz > 0) {
  5851. buf[cnt].data = buf[0].data + (cnt * bufsz);
  5852. buf[cnt].phys = buf[0].phys + (cnt * bufsz);
  5853. if (!buf[cnt].data) {
  5854. pr_err("%s: Buf alloc failed\n",
  5855. __func__);
  5856. mutex_unlock(&ac->cmd_lock);
  5857. goto fail;
  5858. }
  5859. buf[cnt].used = dir ^ 1;
  5860. buf[cnt].size = bufsz;
  5861. buf[cnt].actual_size = bufsz;
  5862. pr_debug("%s: data[%pK]phys[%pK][%pK]\n", __func__,
  5863. buf[cnt].data,
  5864. &buf[cnt].phys,
  5865. &buf[cnt].phys);
  5866. }
  5867. cnt++;
  5868. }
  5869. ac->port[dir].max_buf_cnt = cnt;
  5870. mutex_unlock(&ac->cmd_lock);
  5871. return 0;
  5872. fail:
  5873. pr_err("%s: jump fail\n", __func__);
  5874. q6afe_audio_client_buf_free_contiguous(dir, ac);
  5875. return -EINVAL;
  5876. }
  5877. EXPORT_SYMBOL(q6afe_audio_client_buf_alloc_contiguous);
  5878. /**
  5879. * afe_memory_map -
  5880. * command to map shared buffers to AFE
  5881. *
  5882. * @dma_addr_p: DMA physical address
  5883. * @dma_buf_sz: shared DMA buffer size
  5884. * @ac: AFE audio client handle
  5885. *
  5886. * Returns 0 on success or error on failure
  5887. */
  5888. int afe_memory_map(phys_addr_t dma_addr_p, u32 dma_buf_sz,
  5889. struct afe_audio_client *ac)
  5890. {
  5891. int ret = 0;
  5892. mutex_lock(&this_afe.afe_cmd_lock);
  5893. ac->mem_map_handle = 0;
  5894. ret = afe_cmd_memory_map(dma_addr_p, dma_buf_sz);
  5895. if (ret < 0) {
  5896. pr_err("%s: afe_cmd_memory_map failed %d\n",
  5897. __func__, ret);
  5898. mutex_unlock(&this_afe.afe_cmd_lock);
  5899. return ret;
  5900. }
  5901. ac->mem_map_handle = this_afe.mmap_handle;
  5902. mutex_unlock(&this_afe.afe_cmd_lock);
  5903. return ret;
  5904. }
  5905. EXPORT_SYMBOL(afe_memory_map);
  5906. int afe_cmd_memory_map(phys_addr_t dma_addr_p, u32 dma_buf_sz)
  5907. {
  5908. int ret = 0;
  5909. int cmd_size = 0;
  5910. void *payload = NULL;
  5911. void *mmap_region_cmd = NULL;
  5912. struct afe_service_cmd_shared_mem_map_regions *mregion = NULL;
  5913. struct afe_service_shared_map_region_payload *mregion_pl = NULL;
  5914. int index = 0;
  5915. pr_debug("%s:\n", __func__);
  5916. if (this_afe.apr == NULL) {
  5917. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  5918. 0xFFFFFFFF, &this_afe);
  5919. pr_debug("%s: Register AFE\n", __func__);
  5920. if (this_afe.apr == NULL) {
  5921. pr_err("%s: Unable to register AFE\n", __func__);
  5922. ret = -ENODEV;
  5923. return ret;
  5924. }
  5925. rtac_set_afe_handle(this_afe.apr);
  5926. }
  5927. if (dma_buf_sz % SZ_4K != 0) {
  5928. /*
  5929. * The memory allocated by msm_audio_ion_alloc is always 4kB
  5930. * aligned, ADSP expects the size to be 4kB aligned as well
  5931. * so re-adjusts the buffer size before passing to ADSP.
  5932. */
  5933. dma_buf_sz = PAGE_ALIGN(dma_buf_sz);
  5934. }
  5935. cmd_size = sizeof(struct afe_service_cmd_shared_mem_map_regions)
  5936. + sizeof(struct afe_service_shared_map_region_payload);
  5937. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  5938. if (!mmap_region_cmd)
  5939. return -ENOMEM;
  5940. mregion = (struct afe_service_cmd_shared_mem_map_regions *)
  5941. mmap_region_cmd;
  5942. mregion->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5943. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5944. mregion->hdr.pkt_size = cmd_size;
  5945. mregion->hdr.src_port = 0;
  5946. mregion->hdr.dest_port = 0;
  5947. mregion->hdr.token = 0;
  5948. mregion->hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS;
  5949. mregion->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  5950. mregion->num_regions = 1;
  5951. mregion->property_flag = 0x00;
  5952. /* Todo */
  5953. index = mregion->hdr.token = IDX_RSVD_2;
  5954. payload = ((u8 *) mmap_region_cmd +
  5955. sizeof(struct afe_service_cmd_shared_mem_map_regions));
  5956. mregion_pl = (struct afe_service_shared_map_region_payload *)payload;
  5957. mregion_pl->shm_addr_lsw = lower_32_bits(dma_addr_p);
  5958. mregion_pl->shm_addr_msw = msm_audio_populate_upper_32_bits(dma_addr_p);
  5959. mregion_pl->mem_size_bytes = dma_buf_sz;
  5960. pr_debug("%s: dma_addr_p 0x%pK , size %d\n", __func__,
  5961. &dma_addr_p, dma_buf_sz);
  5962. this_afe.mmap_handle = 0;
  5963. ret = afe_apr_send_pkt((uint32_t *) mmap_region_cmd,
  5964. &this_afe.wait[index]);
  5965. kfree(mmap_region_cmd);
  5966. return ret;
  5967. }
  5968. int afe_cmd_memory_map_nowait(int port_id, phys_addr_t dma_addr_p,
  5969. u32 dma_buf_sz)
  5970. {
  5971. int ret = 0;
  5972. int cmd_size = 0;
  5973. void *payload = NULL;
  5974. void *mmap_region_cmd = NULL;
  5975. struct afe_service_cmd_shared_mem_map_regions *mregion = NULL;
  5976. struct afe_service_shared_map_region_payload *mregion_pl = NULL;
  5977. int index = 0;
  5978. pr_debug("%s:\n", __func__);
  5979. if (this_afe.apr == NULL) {
  5980. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  5981. 0xFFFFFFFF, &this_afe);
  5982. pr_debug("%s: Register AFE\n", __func__);
  5983. if (this_afe.apr == NULL) {
  5984. pr_err("%s: Unable to register AFE\n", __func__);
  5985. ret = -ENODEV;
  5986. return ret;
  5987. }
  5988. rtac_set_afe_handle(this_afe.apr);
  5989. }
  5990. index = q6audio_get_port_index(port_id);
  5991. if (index < 0 || index >= AFE_MAX_PORTS) {
  5992. pr_err("%s: AFE port index[%d] invalid!\n",
  5993. __func__, index);
  5994. return -EINVAL;
  5995. }
  5996. ret = q6audio_validate_port(port_id);
  5997. if (ret < 0) {
  5998. pr_err("%s: Invalid port 0x%x ret %d",
  5999. __func__, port_id, ret);
  6000. return -EINVAL;
  6001. }
  6002. cmd_size = sizeof(struct afe_service_cmd_shared_mem_map_regions)
  6003. + sizeof(struct afe_service_shared_map_region_payload);
  6004. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  6005. if (!mmap_region_cmd)
  6006. return -ENOMEM;
  6007. mregion = (struct afe_service_cmd_shared_mem_map_regions *)
  6008. mmap_region_cmd;
  6009. mregion->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  6010. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  6011. mregion->hdr.pkt_size = sizeof(mregion);
  6012. mregion->hdr.src_port = 0;
  6013. mregion->hdr.dest_port = 0;
  6014. mregion->hdr.token = 0;
  6015. mregion->hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS;
  6016. mregion->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  6017. mregion->num_regions = 1;
  6018. mregion->property_flag = 0x00;
  6019. payload = ((u8 *) mmap_region_cmd +
  6020. sizeof(struct afe_service_cmd_shared_mem_map_regions));
  6021. mregion_pl = (struct afe_service_shared_map_region_payload *)payload;
  6022. mregion_pl->shm_addr_lsw = lower_32_bits(dma_addr_p);
  6023. mregion_pl->shm_addr_msw = msm_audio_populate_upper_32_bits(dma_addr_p);
  6024. mregion_pl->mem_size_bytes = dma_buf_sz;
  6025. ret = afe_apr_send_pkt(mmap_region_cmd, NULL);
  6026. if (ret)
  6027. pr_err("%s: AFE memory map cmd failed %d\n",
  6028. __func__, ret);
  6029. kfree(mmap_region_cmd);
  6030. return ret;
  6031. }
  6032. /**
  6033. * q6afe_audio_client_buf_free_contiguous -
  6034. * frees the shared contiguous memory
  6035. *
  6036. * @dir: RX or TX direction of port
  6037. * @ac: AFE audio client handle
  6038. *
  6039. */
  6040. int q6afe_audio_client_buf_free_contiguous(unsigned int dir,
  6041. struct afe_audio_client *ac)
  6042. {
  6043. struct afe_audio_port_data *port;
  6044. int cnt = 0;
  6045. mutex_lock(&ac->cmd_lock);
  6046. port = &ac->port[dir];
  6047. if (!port->buf) {
  6048. pr_err("%s: buf is null\n", __func__);
  6049. mutex_unlock(&ac->cmd_lock);
  6050. return 0;
  6051. }
  6052. cnt = port->max_buf_cnt - 1;
  6053. if (port->buf[0].data) {
  6054. pr_debug("%s: data[%pK], phys[%pK], dma_buf[%pK]\n",
  6055. __func__,
  6056. port->buf[0].data,
  6057. &port->buf[0].phys,
  6058. port->buf[0].dma_buf);
  6059. msm_audio_ion_free(port->buf[0].dma_buf);
  6060. port->buf[0].dma_buf = NULL;
  6061. }
  6062. while (cnt >= 0) {
  6063. port->buf[cnt].data = NULL;
  6064. port->buf[cnt].phys = 0;
  6065. cnt--;
  6066. }
  6067. port->max_buf_cnt = 0;
  6068. kfree(port->buf);
  6069. port->buf = NULL;
  6070. mutex_unlock(&ac->cmd_lock);
  6071. return 0;
  6072. }
  6073. EXPORT_SYMBOL(q6afe_audio_client_buf_free_contiguous);
  6074. /**
  6075. * q6afe_audio_client_free -
  6076. * frees the audio client from AFE
  6077. *
  6078. * @ac: AFE audio client handle
  6079. *
  6080. */
  6081. void q6afe_audio_client_free(struct afe_audio_client *ac)
  6082. {
  6083. int loopcnt;
  6084. struct afe_audio_port_data *port;
  6085. if (!ac) {
  6086. pr_err("%s: audio client is NULL\n", __func__);
  6087. return;
  6088. }
  6089. for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
  6090. port = &ac->port[loopcnt];
  6091. if (!port->buf)
  6092. continue;
  6093. pr_debug("%s: loopcnt = %d\n", __func__, loopcnt);
  6094. q6afe_audio_client_buf_free_contiguous(loopcnt, ac);
  6095. }
  6096. kfree(ac);
  6097. }
  6098. EXPORT_SYMBOL(q6afe_audio_client_free);
  6099. /**
  6100. * afe_cmd_memory_unmap -
  6101. * command to unmap memory for AFE shared buffer
  6102. *
  6103. * @mem_map_handle: memory map handle to be unmapped
  6104. *
  6105. * Returns 0 on success or error on failure
  6106. */
  6107. int afe_cmd_memory_unmap(u32 mem_map_handle)
  6108. {
  6109. int ret = 0;
  6110. struct afe_service_cmd_shared_mem_unmap_regions mregion;
  6111. int index = 0;
  6112. pr_debug("%s: handle 0x%x\n", __func__, mem_map_handle);
  6113. if (this_afe.apr == NULL) {
  6114. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  6115. 0xFFFFFFFF, &this_afe);
  6116. pr_debug("%s: Register AFE\n", __func__);
  6117. if (this_afe.apr == NULL) {
  6118. pr_err("%s: Unable to register AFE\n", __func__);
  6119. ret = -ENODEV;
  6120. return ret;
  6121. }
  6122. rtac_set_afe_handle(this_afe.apr);
  6123. }
  6124. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  6125. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  6126. mregion.hdr.pkt_size = sizeof(mregion);
  6127. mregion.hdr.src_port = 0;
  6128. mregion.hdr.dest_port = 0;
  6129. mregion.hdr.token = 0;
  6130. mregion.hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS;
  6131. mregion.mem_map_handle = mem_map_handle;
  6132. /* Todo */
  6133. index = mregion.hdr.token = IDX_RSVD_2;
  6134. atomic_set(&this_afe.status, 0);
  6135. ret = afe_apr_send_pkt(&mregion, &this_afe.wait[index]);
  6136. if (ret)
  6137. pr_err("%s: AFE memory unmap cmd failed %d\n",
  6138. __func__, ret);
  6139. return ret;
  6140. }
  6141. EXPORT_SYMBOL(afe_cmd_memory_unmap);
  6142. int afe_cmd_memory_unmap_nowait(u32 mem_map_handle)
  6143. {
  6144. int ret = 0;
  6145. struct afe_service_cmd_shared_mem_unmap_regions mregion;
  6146. pr_debug("%s: handle 0x%x\n", __func__, mem_map_handle);
  6147. if (this_afe.apr == NULL) {
  6148. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  6149. 0xFFFFFFFF, &this_afe);
  6150. pr_debug("%s: Register AFE\n", __func__);
  6151. if (this_afe.apr == NULL) {
  6152. pr_err("%s: Unable to register AFE\n", __func__);
  6153. ret = -ENODEV;
  6154. return ret;
  6155. }
  6156. rtac_set_afe_handle(this_afe.apr);
  6157. }
  6158. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  6159. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  6160. mregion.hdr.pkt_size = sizeof(mregion);
  6161. mregion.hdr.src_port = 0;
  6162. mregion.hdr.dest_port = 0;
  6163. mregion.hdr.token = 0;
  6164. mregion.hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS;
  6165. mregion.mem_map_handle = mem_map_handle;
  6166. ret = afe_apr_send_pkt(&mregion, NULL);
  6167. if (ret)
  6168. pr_err("%s: AFE memory unmap cmd failed %d\n",
  6169. __func__, ret);
  6170. return ret;
  6171. }
  6172. /**
  6173. * afe_register_get_events -
  6174. * register for events from proxy port
  6175. *
  6176. * @port_id: Port ID to register events
  6177. * @cb: callback function to invoke for events from proxy port
  6178. * @private_data: private data to sent back in callback fn
  6179. *
  6180. * Returns 0 on success or error on failure
  6181. */
  6182. int afe_register_get_events(u16 port_id,
  6183. void (*cb)(uint32_t opcode,
  6184. uint32_t token, uint32_t *payload, void *priv),
  6185. void *private_data)
  6186. {
  6187. int ret = 0;
  6188. struct afe_service_cmd_register_rt_port_driver rtproxy;
  6189. pr_debug("%s: port_id: 0x%x\n", __func__, port_id);
  6190. if (this_afe.apr == NULL) {
  6191. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  6192. 0xFFFFFFFF, &this_afe);
  6193. pr_debug("%s: Register AFE\n", __func__);
  6194. if (this_afe.apr == NULL) {
  6195. pr_err("%s: Unable to register AFE\n", __func__);
  6196. ret = -ENODEV;
  6197. return ret;
  6198. }
  6199. rtac_set_afe_handle(this_afe.apr);
  6200. }
  6201. if ((port_id == RT_PROXY_DAI_002_RX) ||
  6202. (port_id == RT_PROXY_DAI_001_TX)) {
  6203. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  6204. } else {
  6205. pr_err("%s: wrong port id 0x%x\n", __func__, port_id);
  6206. return -EINVAL;
  6207. }
  6208. if (port_id == RT_PROXY_PORT_001_TX) {
  6209. this_afe.tx_cb = cb;
  6210. this_afe.tx_private_data = private_data;
  6211. } else if (port_id == RT_PROXY_PORT_001_RX) {
  6212. this_afe.rx_cb = cb;
  6213. this_afe.rx_private_data = private_data;
  6214. }
  6215. rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  6216. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  6217. rtproxy.hdr.pkt_size = sizeof(rtproxy);
  6218. rtproxy.hdr.src_port = 1;
  6219. rtproxy.hdr.dest_port = 1;
  6220. rtproxy.hdr.opcode = AFE_SERVICE_CMD_REGISTER_RT_PORT_DRIVER;
  6221. rtproxy.port_id = port_id;
  6222. rtproxy.reserved = 0;
  6223. ret = afe_apr_send_pkt(&rtproxy, NULL);
  6224. if (ret)
  6225. pr_err("%s: AFE reg. rtproxy_event failed %d\n",
  6226. __func__, ret);
  6227. return ret;
  6228. }
  6229. EXPORT_SYMBOL(afe_register_get_events);
  6230. /**
  6231. * afe_unregister_get_events -
  6232. * unregister for events from proxy port
  6233. *
  6234. * @port_id: Port ID to unregister events
  6235. *
  6236. * Returns 0 on success or error on failure
  6237. */
  6238. int afe_unregister_get_events(u16 port_id)
  6239. {
  6240. int ret = 0;
  6241. struct afe_service_cmd_unregister_rt_port_driver rtproxy;
  6242. int index = 0;
  6243. pr_debug("%s:\n", __func__);
  6244. if (this_afe.apr == NULL) {
  6245. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  6246. 0xFFFFFFFF, &this_afe);
  6247. pr_debug("%s: Register AFE\n", __func__);
  6248. if (this_afe.apr == NULL) {
  6249. pr_err("%s: Unable to register AFE\n", __func__);
  6250. ret = -ENODEV;
  6251. return ret;
  6252. }
  6253. rtac_set_afe_handle(this_afe.apr);
  6254. }
  6255. if ((port_id == RT_PROXY_DAI_002_RX) ||
  6256. (port_id == RT_PROXY_DAI_001_TX)) {
  6257. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  6258. } else {
  6259. pr_err("%s: wrong port id 0x%x\n", __func__, port_id);
  6260. return -EINVAL;
  6261. }
  6262. index = q6audio_get_port_index(port_id);
  6263. if (index < 0 || index >= AFE_MAX_PORTS) {
  6264. pr_err("%s: AFE port index[%d] invalid!\n",
  6265. __func__, index);
  6266. return -EINVAL;
  6267. }
  6268. ret = q6audio_validate_port(port_id);
  6269. if (ret < 0) {
  6270. pr_err("%s: Invalid port 0x%x ret %d", __func__, port_id, ret);
  6271. return -EINVAL;
  6272. }
  6273. rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  6274. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  6275. rtproxy.hdr.pkt_size = sizeof(rtproxy);
  6276. rtproxy.hdr.src_port = 0;
  6277. rtproxy.hdr.dest_port = 0;
  6278. rtproxy.hdr.token = 0;
  6279. rtproxy.hdr.opcode = AFE_SERVICE_CMD_UNREGISTER_RT_PORT_DRIVER;
  6280. rtproxy.port_id = port_id;
  6281. rtproxy.reserved = 0;
  6282. rtproxy.hdr.token = index;
  6283. if (port_id == RT_PROXY_PORT_001_TX) {
  6284. this_afe.tx_cb = NULL;
  6285. this_afe.tx_private_data = NULL;
  6286. } else if (port_id == RT_PROXY_PORT_001_RX) {
  6287. this_afe.rx_cb = NULL;
  6288. this_afe.rx_private_data = NULL;
  6289. }
  6290. ret = afe_apr_send_pkt(&rtproxy, &this_afe.wait[index]);
  6291. if (ret)
  6292. pr_err("%s: AFE enable Unreg. rtproxy_event failed %d\n",
  6293. __func__, ret);
  6294. return ret;
  6295. }
  6296. EXPORT_SYMBOL(afe_unregister_get_events);
  6297. /**
  6298. * afe_rt_proxy_port_write -
  6299. * command for AFE RT proxy port write
  6300. *
  6301. * @buf_addr_p: Physical buffer address with
  6302. * playback data to proxy port
  6303. * @mem_map_handle: memory map handle of write buffer
  6304. * @bytes: number of bytes to write
  6305. *
  6306. * Returns 0 on success or error on failure
  6307. */
  6308. int afe_rt_proxy_port_write(phys_addr_t buf_addr_p,
  6309. u32 mem_map_handle, int bytes)
  6310. {
  6311. int ret = 0;
  6312. struct afe_port_data_cmd_rt_proxy_port_write_v2 afecmd_wr;
  6313. if (this_afe.apr == NULL) {
  6314. pr_err("%s: register to AFE is not done\n", __func__);
  6315. ret = -ENODEV;
  6316. return ret;
  6317. }
  6318. pr_debug("%s: buf_addr_p = 0x%pK bytes = %d\n", __func__,
  6319. &buf_addr_p, bytes);
  6320. afecmd_wr.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  6321. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  6322. afecmd_wr.hdr.pkt_size = sizeof(afecmd_wr);
  6323. afecmd_wr.hdr.src_port = 0;
  6324. afecmd_wr.hdr.dest_port = 0;
  6325. afecmd_wr.hdr.token = 0;
  6326. afecmd_wr.hdr.opcode = AFE_PORT_DATA_CMD_RT_PROXY_PORT_WRITE_V2;
  6327. afecmd_wr.port_id = RT_PROXY_PORT_001_TX;
  6328. afecmd_wr.buffer_address_lsw = lower_32_bits(buf_addr_p);
  6329. afecmd_wr.buffer_address_msw =
  6330. msm_audio_populate_upper_32_bits(buf_addr_p);
  6331. afecmd_wr.mem_map_handle = mem_map_handle;
  6332. afecmd_wr.available_bytes = bytes;
  6333. afecmd_wr.reserved = 0;
  6334. /*
  6335. * Do not call afe_apr_send_pkt() here as it acquires
  6336. * a mutex lock inside and this function gets called in
  6337. * interrupt context leading to scheduler crash
  6338. */
  6339. atomic_set(&this_afe.status, 0);
  6340. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &afecmd_wr);
  6341. if (ret < 0) {
  6342. pr_err("%s: AFE rtproxy write to port 0x%x failed %d\n",
  6343. __func__, afecmd_wr.port_id, ret);
  6344. ret = -EINVAL;
  6345. }
  6346. return ret;
  6347. }
  6348. EXPORT_SYMBOL(afe_rt_proxy_port_write);
  6349. /**
  6350. * afe_rt_proxy_port_read -
  6351. * command for AFE RT proxy port read
  6352. *
  6353. * @buf_addr_p: Physical buffer address to fill read data
  6354. * @mem_map_handle: memory map handle for buffer read
  6355. * @bytes: number of bytes to read
  6356. *
  6357. * Returns 0 on success or error on failure
  6358. */
  6359. int afe_rt_proxy_port_read(phys_addr_t buf_addr_p,
  6360. u32 mem_map_handle, int bytes)
  6361. {
  6362. int ret = 0;
  6363. struct afe_port_data_cmd_rt_proxy_port_read_v2 afecmd_rd;
  6364. if (this_afe.apr == NULL) {
  6365. pr_err("%s: register to AFE is not done\n", __func__);
  6366. ret = -ENODEV;
  6367. return ret;
  6368. }
  6369. pr_debug("%s: buf_addr_p = 0x%pK bytes = %d\n", __func__,
  6370. &buf_addr_p, bytes);
  6371. afecmd_rd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  6372. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  6373. afecmd_rd.hdr.pkt_size = sizeof(afecmd_rd);
  6374. afecmd_rd.hdr.src_port = 0;
  6375. afecmd_rd.hdr.dest_port = 0;
  6376. afecmd_rd.hdr.token = 0;
  6377. afecmd_rd.hdr.opcode = AFE_PORT_DATA_CMD_RT_PROXY_PORT_READ_V2;
  6378. afecmd_rd.port_id = RT_PROXY_PORT_001_RX;
  6379. afecmd_rd.buffer_address_lsw = lower_32_bits(buf_addr_p);
  6380. afecmd_rd.buffer_address_msw =
  6381. msm_audio_populate_upper_32_bits(buf_addr_p);
  6382. afecmd_rd.available_bytes = bytes;
  6383. afecmd_rd.mem_map_handle = mem_map_handle;
  6384. /*
  6385. * Do not call afe_apr_send_pkt() here as it acquires
  6386. * a mutex lock inside and this function gets called in
  6387. * interrupt context leading to scheduler crash
  6388. */
  6389. atomic_set(&this_afe.status, 0);
  6390. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &afecmd_rd);
  6391. if (ret < 0) {
  6392. pr_err("%s: AFE rtproxy read cmd to port 0x%x failed %d\n",
  6393. __func__, afecmd_rd.port_id, ret);
  6394. ret = -EINVAL;
  6395. }
  6396. return ret;
  6397. }
  6398. EXPORT_SYMBOL(afe_rt_proxy_port_read);
  6399. #ifdef CONFIG_DEBUG_FS
  6400. static struct dentry *debugfs_afelb;
  6401. static struct dentry *debugfs_afelb_gain;
  6402. static int afe_debug_open(struct inode *inode, struct file *file)
  6403. {
  6404. file->private_data = inode->i_private;
  6405. pr_info("%s: debug intf %s\n", __func__, (char *) file->private_data);
  6406. return 0;
  6407. }
  6408. static int afe_get_parameters(char *buf, long int *param1, int num_of_par)
  6409. {
  6410. char *token;
  6411. int base, cnt;
  6412. token = strsep(&buf, " ");
  6413. for (cnt = 0; cnt < num_of_par; cnt++) {
  6414. if (token != NULL) {
  6415. if ((token[1] == 'x') || (token[1] == 'X'))
  6416. base = 16;
  6417. else
  6418. base = 10;
  6419. if (kstrtoul(token, base, &param1[cnt]) != 0) {
  6420. pr_err("%s: kstrtoul failed\n",
  6421. __func__);
  6422. return -EINVAL;
  6423. }
  6424. token = strsep(&buf, " ");
  6425. } else {
  6426. pr_err("%s: token NULL\n", __func__);
  6427. return -EINVAL;
  6428. }
  6429. }
  6430. return 0;
  6431. }
  6432. #define AFE_LOOPBACK_ON (1)
  6433. #define AFE_LOOPBACK_OFF (0)
  6434. static ssize_t afe_debug_write(struct file *filp,
  6435. const char __user *ubuf, size_t cnt, loff_t *ppos)
  6436. {
  6437. char *lb_str = filp->private_data;
  6438. char lbuf[32];
  6439. int rc;
  6440. unsigned long param[5];
  6441. if (cnt > sizeof(lbuf) - 1) {
  6442. pr_err("%s: cnt %zd size %zd\n", __func__, cnt, sizeof(lbuf)-1);
  6443. return -EINVAL;
  6444. }
  6445. rc = copy_from_user(lbuf, ubuf, cnt);
  6446. if (rc) {
  6447. pr_err("%s: copy from user failed %d\n", __func__, rc);
  6448. return -EFAULT;
  6449. }
  6450. lbuf[cnt] = '\0';
  6451. if (!strcmp(lb_str, "afe_loopback")) {
  6452. rc = afe_get_parameters(lbuf, param, 3);
  6453. if (!rc) {
  6454. pr_info("%s: %lu %lu %lu\n", lb_str, param[0], param[1],
  6455. param[2]);
  6456. if ((param[0] != AFE_LOOPBACK_ON) && (param[0] !=
  6457. AFE_LOOPBACK_OFF)) {
  6458. pr_err("%s: Error, parameter 0 incorrect\n",
  6459. __func__);
  6460. rc = -EINVAL;
  6461. goto afe_error;
  6462. }
  6463. if ((q6audio_validate_port(param[1]) < 0) ||
  6464. (q6audio_validate_port(param[2])) < 0) {
  6465. pr_err("%s: Error, invalid afe port\n",
  6466. __func__);
  6467. }
  6468. if (this_afe.apr == NULL) {
  6469. pr_err("%s: Error, AFE not opened\n", __func__);
  6470. rc = -EINVAL;
  6471. } else {
  6472. rc = afe_loopback(param[0], param[1], param[2]);
  6473. }
  6474. } else {
  6475. pr_err("%s: Error, invalid parameters\n", __func__);
  6476. rc = -EINVAL;
  6477. }
  6478. } else if (!strcmp(lb_str, "afe_loopback_gain")) {
  6479. rc = afe_get_parameters(lbuf, param, 2);
  6480. if (!rc) {
  6481. pr_info("%s: %s %lu %lu\n",
  6482. __func__, lb_str, param[0], param[1]);
  6483. rc = q6audio_validate_port(param[0]);
  6484. if (rc < 0) {
  6485. pr_err("%s: Error, invalid afe port %d %lu\n",
  6486. __func__, rc, param[0]);
  6487. rc = -EINVAL;
  6488. goto afe_error;
  6489. }
  6490. if (param[1] > 100) {
  6491. pr_err("%s: Error, volume should be 0 to 100 percentage param = %lu\n",
  6492. __func__, param[1]);
  6493. rc = -EINVAL;
  6494. goto afe_error;
  6495. }
  6496. param[1] = (Q6AFE_MAX_VOLUME * param[1]) / 100;
  6497. if (this_afe.apr == NULL) {
  6498. pr_err("%s: Error, AFE not opened\n", __func__);
  6499. rc = -EINVAL;
  6500. } else {
  6501. rc = afe_loopback_gain(param[0], param[1]);
  6502. }
  6503. } else {
  6504. pr_err("%s: Error, invalid parameters\n", __func__);
  6505. rc = -EINVAL;
  6506. }
  6507. }
  6508. afe_error:
  6509. if (rc == 0)
  6510. rc = cnt;
  6511. else
  6512. pr_err("%s: rc = %d\n", __func__, rc);
  6513. return rc;
  6514. }
  6515. static const struct file_operations afe_debug_fops = {
  6516. .open = afe_debug_open,
  6517. .write = afe_debug_write
  6518. };
  6519. static void config_debug_fs_init(void)
  6520. {
  6521. debugfs_afelb = debugfs_create_file("afe_loopback",
  6522. 0664, NULL, (void *) "afe_loopback",
  6523. &afe_debug_fops);
  6524. debugfs_afelb_gain = debugfs_create_file("afe_loopback_gain",
  6525. 0664, NULL, (void *) "afe_loopback_gain",
  6526. &afe_debug_fops);
  6527. }
  6528. static void config_debug_fs_exit(void)
  6529. {
  6530. debugfs_remove(debugfs_afelb);
  6531. debugfs_remove(debugfs_afelb_gain);
  6532. }
  6533. #else
  6534. static void config_debug_fs_init(void)
  6535. {
  6536. }
  6537. static void config_debug_fs_exit(void)
  6538. {
  6539. }
  6540. #endif
  6541. /**
  6542. * afe_set_dtmf_gen_rx_portid -
  6543. * Set port_id for DTMF tone generation
  6544. *
  6545. * @port_id: AFE port id
  6546. * @set: set or reset port id value for dtmf gen
  6547. *
  6548. */
  6549. void afe_set_dtmf_gen_rx_portid(u16 port_id, int set)
  6550. {
  6551. if (set)
  6552. this_afe.dtmf_gen_rx_portid = port_id;
  6553. else if (this_afe.dtmf_gen_rx_portid == port_id)
  6554. this_afe.dtmf_gen_rx_portid = -1;
  6555. }
  6556. EXPORT_SYMBOL(afe_set_dtmf_gen_rx_portid);
  6557. /**
  6558. * afe_dtmf_generate_rx - command to generate AFE DTMF RX
  6559. *
  6560. * @duration_in_ms: Duration in ms for dtmf tone
  6561. * @high_freq: Higher frequency for dtmf
  6562. * @low_freq: lower frequency for dtmf
  6563. * @gain: Gain value for DTMF tone
  6564. *
  6565. * Returns 0 on success, appropriate error code otherwise
  6566. */
  6567. int afe_dtmf_generate_rx(int64_t duration_in_ms,
  6568. uint16_t high_freq,
  6569. uint16_t low_freq, uint16_t gain)
  6570. {
  6571. int ret = 0;
  6572. int index = 0;
  6573. struct afe_dtmf_generation_command cmd_dtmf;
  6574. pr_debug("%s: DTMF AFE Gen\n", __func__);
  6575. if (afe_validate_port(this_afe.dtmf_gen_rx_portid) < 0) {
  6576. pr_err("%s: Failed : Invalid Port id = 0x%x\n",
  6577. __func__, this_afe.dtmf_gen_rx_portid);
  6578. ret = -EINVAL;
  6579. goto fail_cmd;
  6580. }
  6581. if (this_afe.apr == NULL) {
  6582. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  6583. 0xFFFFFFFF, &this_afe);
  6584. pr_debug("%s: Register AFE\n", __func__);
  6585. if (this_afe.apr == NULL) {
  6586. pr_err("%s: Unable to register AFE\n", __func__);
  6587. ret = -ENODEV;
  6588. return ret;
  6589. }
  6590. rtac_set_afe_handle(this_afe.apr);
  6591. }
  6592. pr_debug("%s: dur=%lld: hfreq=%d lfreq=%d gain=%d portid=0x%x\n",
  6593. __func__,
  6594. duration_in_ms, high_freq, low_freq, gain,
  6595. this_afe.dtmf_gen_rx_portid);
  6596. cmd_dtmf.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  6597. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  6598. cmd_dtmf.hdr.pkt_size = sizeof(cmd_dtmf);
  6599. cmd_dtmf.hdr.src_port = 0;
  6600. cmd_dtmf.hdr.dest_port = 0;
  6601. cmd_dtmf.hdr.token = 0;
  6602. cmd_dtmf.hdr.opcode = AFE_PORTS_CMD_DTMF_CTL;
  6603. cmd_dtmf.duration_in_ms = duration_in_ms;
  6604. cmd_dtmf.high_freq = high_freq;
  6605. cmd_dtmf.low_freq = low_freq;
  6606. cmd_dtmf.gain = gain;
  6607. cmd_dtmf.num_ports = 1;
  6608. cmd_dtmf.port_ids = q6audio_get_port_id(this_afe.dtmf_gen_rx_portid);
  6609. index = q6audio_get_port_index(this_afe.dtmf_gen_rx_portid);
  6610. if (index < 0 || index >= AFE_MAX_PORTS) {
  6611. pr_err("%s: AFE port index[%d] invalid!\n",
  6612. __func__, index);
  6613. ret = -EINVAL;
  6614. goto fail_cmd;
  6615. }
  6616. ret = afe_apr_send_pkt((uint32_t *) &cmd_dtmf,
  6617. &this_afe.wait[index]);
  6618. return ret;
  6619. fail_cmd:
  6620. pr_err("%s: failed %d\n", __func__, ret);
  6621. return ret;
  6622. }
  6623. EXPORT_SYMBOL(afe_dtmf_generate_rx);
  6624. static int afe_sidetone_iir(u16 tx_port_id)
  6625. {
  6626. int ret;
  6627. uint16_t size = 0;
  6628. int cal_index = AFE_SIDETONE_IIR_CAL;
  6629. int iir_pregain = 0;
  6630. int iir_num_biquad_stages = 0;
  6631. int iir_enable;
  6632. struct cal_block_data *cal_block;
  6633. int mid;
  6634. struct afe_mod_enable_param enable;
  6635. struct afe_sidetone_iir_filter_config_params filter_data;
  6636. struct param_hdr_v3 param_hdr;
  6637. u8 *packed_param_data = NULL;
  6638. u32 packed_param_size = 0;
  6639. u32 single_param_size = 0;
  6640. struct audio_cal_info_sidetone_iir *st_iir_cal_info = NULL;
  6641. memset(&enable, 0, sizeof(enable));
  6642. memset(&filter_data, 0, sizeof(filter_data));
  6643. memset(&param_hdr, 0, sizeof(param_hdr));
  6644. if (this_afe.cal_data[cal_index] == NULL) {
  6645. pr_err("%s: cal data is NULL\n", __func__);
  6646. ret = -EINVAL;
  6647. goto done;
  6648. }
  6649. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  6650. cal_block = cal_utils_get_only_cal_block(this_afe.cal_data[cal_index]);
  6651. if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
  6652. pr_err("%s: cal_block not found\n ", __func__);
  6653. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  6654. ret = -EINVAL;
  6655. goto done;
  6656. }
  6657. /* Cache data from cal block while inside lock to reduce locked time */
  6658. st_iir_cal_info =
  6659. (struct audio_cal_info_sidetone_iir *) cal_block->cal_info;
  6660. iir_pregain = st_iir_cal_info->pregain;
  6661. iir_enable = st_iir_cal_info->iir_enable;
  6662. iir_num_biquad_stages = st_iir_cal_info->num_biquad_stages;
  6663. mid = st_iir_cal_info->mid;
  6664. /*
  6665. * calculate the actual size of payload based on no of stages
  6666. * enabled in calibration
  6667. */
  6668. size = (MAX_SIDETONE_IIR_DATA_SIZE / MAX_NO_IIR_FILTER_STAGE) *
  6669. iir_num_biquad_stages;
  6670. /*
  6671. * For an odd number of stages, 2 bytes of padding are
  6672. * required at the end of the payload.
  6673. */
  6674. if (iir_num_biquad_stages % 2) {
  6675. pr_debug("%s: adding 2 to size:%d\n", __func__, size);
  6676. size = size + 2;
  6677. }
  6678. memcpy(&filter_data.iir_config, &st_iir_cal_info->iir_config, size);
  6679. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  6680. packed_param_size =
  6681. sizeof(param_hdr) * 2 + sizeof(enable) + sizeof(filter_data);
  6682. packed_param_data = kzalloc(packed_param_size, GFP_KERNEL);
  6683. if (!packed_param_data)
  6684. return -ENOMEM;
  6685. packed_param_size = 0;
  6686. /*
  6687. * Set IIR enable params
  6688. */
  6689. param_hdr.module_id = mid;
  6690. param_hdr.instance_id = INSTANCE_ID_0;
  6691. param_hdr.param_id = AFE_PARAM_ID_ENABLE;
  6692. param_hdr.param_size = sizeof(enable);
  6693. enable.enable = iir_enable;
  6694. ret = q6common_pack_pp_params(packed_param_data, &param_hdr,
  6695. (u8 *) &enable, &single_param_size);
  6696. if (ret) {
  6697. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  6698. ret);
  6699. goto done;
  6700. }
  6701. packed_param_size += single_param_size;
  6702. /*
  6703. * Set IIR filter config params
  6704. */
  6705. param_hdr.module_id = mid;
  6706. param_hdr.instance_id = INSTANCE_ID_0;
  6707. param_hdr.param_id = AFE_PARAM_ID_SIDETONE_IIR_FILTER_CONFIG;
  6708. param_hdr.param_size = sizeof(filter_data.num_biquad_stages) +
  6709. sizeof(filter_data.pregain) + size;
  6710. filter_data.num_biquad_stages = iir_num_biquad_stages;
  6711. filter_data.pregain = iir_pregain;
  6712. ret = q6common_pack_pp_params(packed_param_data + packed_param_size,
  6713. &param_hdr, (u8 *) &filter_data,
  6714. &single_param_size);
  6715. if (ret) {
  6716. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  6717. ret);
  6718. goto done;
  6719. }
  6720. packed_param_size += single_param_size;
  6721. pr_debug("%s: tx(0x%x)mid(0x%x)iir_en(%d)stg(%d)gain(0x%x)size(%d)\n",
  6722. __func__, tx_port_id, mid, enable.enable,
  6723. filter_data.num_biquad_stages, filter_data.pregain,
  6724. param_hdr.param_size);
  6725. ret = q6afe_set_params(tx_port_id, q6audio_get_port_index(tx_port_id),
  6726. NULL, packed_param_data, packed_param_size);
  6727. if (ret)
  6728. pr_err("%s: AFE sidetone failed for tx_port(0x%x)\n",
  6729. __func__, tx_port_id);
  6730. done:
  6731. kfree(packed_param_data);
  6732. return ret;
  6733. }
  6734. static int afe_sidetone(u16 tx_port_id, u16 rx_port_id, bool enable)
  6735. {
  6736. int ret;
  6737. int cal_index = AFE_SIDETONE_CAL;
  6738. int sidetone_gain;
  6739. int sidetone_enable;
  6740. struct cal_block_data *cal_block;
  6741. int mid = 0;
  6742. struct afe_loopback_sidetone_gain gain_data;
  6743. struct loopback_cfg_data cfg_data;
  6744. struct param_hdr_v3 param_hdr;
  6745. u8 *packed_param_data = NULL;
  6746. u32 packed_param_size = 0;
  6747. u32 single_param_size = 0;
  6748. struct audio_cal_info_sidetone *st_cal_info = NULL;
  6749. if (this_afe.cal_data[cal_index] == NULL) {
  6750. pr_err("%s: cal data is NULL\n", __func__);
  6751. ret = -EINVAL;
  6752. goto done;
  6753. }
  6754. memset(&gain_data, 0, sizeof(gain_data));
  6755. memset(&cfg_data, 0, sizeof(cfg_data));
  6756. memset(&param_hdr, 0, sizeof(param_hdr));
  6757. packed_param_size =
  6758. sizeof(param_hdr) * 2 + sizeof(gain_data) + sizeof(cfg_data);
  6759. packed_param_data = kzalloc(packed_param_size, GFP_KERNEL);
  6760. if (!packed_param_data)
  6761. return -ENOMEM;
  6762. packed_param_size = 0;
  6763. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  6764. cal_block = cal_utils_get_only_cal_block(this_afe.cal_data[cal_index]);
  6765. if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
  6766. pr_err("%s: cal_block not found\n", __func__);
  6767. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  6768. ret = -EINVAL;
  6769. goto done;
  6770. }
  6771. /* Cache data from cal block while inside lock to reduce locked time */
  6772. st_cal_info = (struct audio_cal_info_sidetone *) cal_block->cal_info;
  6773. sidetone_gain = st_cal_info->gain;
  6774. sidetone_enable = st_cal_info->enable;
  6775. mid = st_cal_info->mid;
  6776. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  6777. /* Set gain data. */
  6778. param_hdr.module_id = AFE_MODULE_LOOPBACK;
  6779. param_hdr.instance_id = INSTANCE_ID_0;
  6780. param_hdr.param_id = AFE_PARAM_ID_LOOPBACK_GAIN_PER_PATH;
  6781. param_hdr.param_size = sizeof(struct afe_loopback_sidetone_gain);
  6782. gain_data.rx_port_id = rx_port_id;
  6783. gain_data.gain = sidetone_gain;
  6784. ret = q6common_pack_pp_params(packed_param_data, &param_hdr,
  6785. (u8 *) &gain_data, &single_param_size);
  6786. if (ret) {
  6787. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  6788. ret);
  6789. goto done;
  6790. }
  6791. packed_param_size += single_param_size;
  6792. /* Set configuration data. */
  6793. param_hdr.module_id = AFE_MODULE_LOOPBACK;
  6794. param_hdr.instance_id = INSTANCE_ID_0;
  6795. param_hdr.param_id = AFE_PARAM_ID_LOOPBACK_CONFIG;
  6796. param_hdr.param_size = sizeof(struct loopback_cfg_data);
  6797. cfg_data.loopback_cfg_minor_version = AFE_API_VERSION_LOOPBACK_CONFIG;
  6798. cfg_data.dst_port_id = rx_port_id;
  6799. cfg_data.routing_mode = LB_MODE_SIDETONE;
  6800. cfg_data.enable = enable;
  6801. ret = q6common_pack_pp_params(packed_param_data + packed_param_size,
  6802. &param_hdr, (u8 *) &cfg_data,
  6803. &single_param_size);
  6804. if (ret) {
  6805. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  6806. ret);
  6807. goto done;
  6808. }
  6809. packed_param_size += single_param_size;
  6810. pr_debug("%s rx(0x%x) tx(0x%x) enable(%d) mid(0x%x) gain(%d) sidetone_enable(%d)\n",
  6811. __func__, rx_port_id, tx_port_id,
  6812. enable, mid, sidetone_gain, sidetone_enable);
  6813. ret = q6afe_set_params(tx_port_id, q6audio_get_port_index(tx_port_id),
  6814. NULL, packed_param_data, packed_param_size);
  6815. if (ret)
  6816. pr_err("%s: AFE sidetone send failed for tx_port:%d rx_port:%d ret:%d\n",
  6817. __func__, tx_port_id, rx_port_id, ret);
  6818. done:
  6819. kfree(packed_param_data);
  6820. return ret;
  6821. }
  6822. int afe_sidetone_enable(u16 tx_port_id, u16 rx_port_id, bool enable)
  6823. {
  6824. int ret;
  6825. int index;
  6826. index = q6audio_get_port_index(rx_port_id);
  6827. if (index < 0 || index >= AFE_MAX_PORTS) {
  6828. pr_err("%s: AFE port index[%d] invalid!\n",
  6829. __func__, index);
  6830. ret = -EINVAL;
  6831. goto done;
  6832. }
  6833. if (q6audio_validate_port(rx_port_id) < 0) {
  6834. pr_err("%s: Invalid port 0x%x\n",
  6835. __func__, rx_port_id);
  6836. ret = -EINVAL;
  6837. goto done;
  6838. }
  6839. index = q6audio_get_port_index(tx_port_id);
  6840. if (index < 0 || index >= AFE_MAX_PORTS) {
  6841. pr_err("%s: AFE port index[%d] invalid!\n",
  6842. __func__, index);
  6843. ret = -EINVAL;
  6844. goto done;
  6845. }
  6846. if (q6audio_validate_port(tx_port_id) < 0) {
  6847. pr_err("%s: Invalid port 0x%x\n",
  6848. __func__, tx_port_id);
  6849. ret = -EINVAL;
  6850. goto done;
  6851. }
  6852. if (enable) {
  6853. ret = afe_sidetone_iir(tx_port_id);
  6854. if (ret)
  6855. goto done;
  6856. }
  6857. ret = afe_sidetone(tx_port_id, rx_port_id, enable);
  6858. done:
  6859. return ret;
  6860. }
  6861. /**
  6862. * afe_set_display_stream - command to update AFE dp port params
  6863. *
  6864. * @rx_port_id: AFE port id
  6865. * @stream_idx: dp controller stream index
  6866. * @ctl_idx: dp controller index
  6867. *
  6868. * Returns 0 on success, appropriate error code otherwise
  6869. */
  6870. int afe_set_display_stream(u16 rx_port_id, u32 stream_idx, u32 ctl_idx)
  6871. {
  6872. int ret;
  6873. struct param_hdr_v3 param_hdr;
  6874. u32 packed_param_size = 0;
  6875. u8 *packed_param_data = NULL;
  6876. struct afe_display_stream_idx stream_data;
  6877. struct afe_display_ctl_idx ctl_data;
  6878. u32 single_param_size = 0;
  6879. memset(&param_hdr, 0, sizeof(param_hdr));
  6880. memset(&stream_data, 0, sizeof(stream_data));
  6881. memset(&ctl_data, 0, sizeof(ctl_data));
  6882. packed_param_size =
  6883. sizeof(param_hdr) * 2 + sizeof(stream_data) + sizeof(ctl_data);
  6884. packed_param_data = kzalloc(packed_param_size, GFP_KERNEL);
  6885. if (!packed_param_data)
  6886. return -ENOMEM;
  6887. packed_param_size = 0;
  6888. /* Set stream index */
  6889. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  6890. param_hdr.instance_id = INSTANCE_ID_0;
  6891. param_hdr.param_id = AFE_PARAM_ID_HDMI_DP_MST_VID_IDX_CFG;
  6892. param_hdr.param_size = sizeof(struct afe_display_stream_idx);
  6893. stream_data.minor_version = 1;
  6894. stream_data.stream_idx = stream_idx;
  6895. ret = q6common_pack_pp_params(packed_param_data, &param_hdr,
  6896. (u8 *) &stream_data, &single_param_size);
  6897. if (ret) {
  6898. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  6899. ret);
  6900. goto done;
  6901. }
  6902. packed_param_size += single_param_size;
  6903. /* Set controller dptx index */
  6904. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  6905. param_hdr.instance_id = INSTANCE_ID_0;
  6906. param_hdr.param_id = AFE_PARAM_ID_HDMI_DPTX_IDX_CFG;
  6907. param_hdr.param_size = sizeof(struct afe_display_ctl_idx);
  6908. ctl_data.minor_version = 1;
  6909. ctl_data.ctl_idx = ctl_idx;
  6910. ret = q6common_pack_pp_params(packed_param_data + packed_param_size,
  6911. &param_hdr, (u8 *) &ctl_data,
  6912. &single_param_size);
  6913. if (ret) {
  6914. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  6915. ret);
  6916. goto done;
  6917. }
  6918. packed_param_size += single_param_size;
  6919. pr_debug("%s: rx(0x%x) stream(%d) controller(%d)\n",
  6920. __func__, rx_port_id, stream_idx, ctl_idx);
  6921. ret = q6afe_set_params(rx_port_id, q6audio_get_port_index(rx_port_id),
  6922. NULL, packed_param_data, packed_param_size);
  6923. if (ret)
  6924. pr_err("%s: AFE display stream send failed for rx_port:%d ret:%d\n",
  6925. __func__, rx_port_id, ret);
  6926. done:
  6927. kfree(packed_param_data);
  6928. return ret;
  6929. }
  6930. EXPORT_SYMBOL(afe_set_display_stream);
  6931. int afe_validate_port(u16 port_id)
  6932. {
  6933. int ret;
  6934. switch (port_id) {
  6935. case PRIMARY_I2S_RX:
  6936. case PRIMARY_I2S_TX:
  6937. case AFE_PORT_ID_PRIMARY_PCM_RX:
  6938. case AFE_PORT_ID_PRIMARY_PCM_TX:
  6939. case AFE_PORT_ID_SECONDARY_PCM_RX:
  6940. case AFE_PORT_ID_SECONDARY_PCM_TX:
  6941. case AFE_PORT_ID_TERTIARY_PCM_RX:
  6942. case AFE_PORT_ID_TERTIARY_PCM_TX:
  6943. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  6944. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  6945. case AFE_PORT_ID_QUINARY_PCM_RX:
  6946. case AFE_PORT_ID_QUINARY_PCM_TX:
  6947. case AFE_PORT_ID_SENARY_PCM_RX:
  6948. case AFE_PORT_ID_SENARY_PCM_TX:
  6949. case SECONDARY_I2S_RX:
  6950. case SECONDARY_I2S_TX:
  6951. case MI2S_RX:
  6952. case MI2S_TX:
  6953. case HDMI_RX:
  6954. case DISPLAY_PORT_RX:
  6955. case AFE_PORT_ID_PRIMARY_SPDIF_RX:
  6956. case AFE_PORT_ID_PRIMARY_SPDIF_TX:
  6957. case AFE_PORT_ID_SECONDARY_SPDIF_RX:
  6958. case AFE_PORT_ID_SECONDARY_SPDIF_TX:
  6959. case RSVD_2:
  6960. case RSVD_3:
  6961. case DIGI_MIC_TX:
  6962. case VOICE_RECORD_RX:
  6963. case VOICE_RECORD_TX:
  6964. case VOICE_PLAYBACK_TX:
  6965. case VOICE2_PLAYBACK_TX:
  6966. case SLIMBUS_0_RX:
  6967. case SLIMBUS_0_TX:
  6968. case SLIMBUS_1_RX:
  6969. case SLIMBUS_1_TX:
  6970. case SLIMBUS_2_RX:
  6971. case SLIMBUS_2_TX:
  6972. case SLIMBUS_3_RX:
  6973. case INT_BT_SCO_RX:
  6974. case INT_BT_SCO_TX:
  6975. case INT_BT_A2DP_RX:
  6976. case INT_FM_RX:
  6977. case INT_FM_TX:
  6978. case RT_PROXY_PORT_001_RX:
  6979. case RT_PROXY_PORT_001_TX:
  6980. case SLIMBUS_4_RX:
  6981. case SLIMBUS_4_TX:
  6982. case SLIMBUS_5_RX:
  6983. case SLIMBUS_6_RX:
  6984. case SLIMBUS_6_TX:
  6985. case SLIMBUS_7_RX:
  6986. case SLIMBUS_7_TX:
  6987. case SLIMBUS_8_RX:
  6988. case SLIMBUS_8_TX:
  6989. case SLIMBUS_9_RX:
  6990. case SLIMBUS_9_TX:
  6991. case AFE_PORT_ID_USB_RX:
  6992. case AFE_PORT_ID_USB_TX:
  6993. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  6994. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  6995. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  6996. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  6997. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  6998. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  6999. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  7000. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  7001. case AFE_PORT_ID_QUINARY_MI2S_RX:
  7002. case AFE_PORT_ID_QUINARY_MI2S_TX:
  7003. case AFE_PORT_ID_SENARY_MI2S_RX:
  7004. case AFE_PORT_ID_SENARY_MI2S_TX:
  7005. case AFE_PORT_ID_PRIMARY_META_MI2S_RX:
  7006. case AFE_PORT_ID_SECONDARY_META_MI2S_RX:
  7007. case AFE_PORT_ID_PRIMARY_TDM_RX:
  7008. case AFE_PORT_ID_PRIMARY_TDM_TX:
  7009. case AFE_PORT_ID_PRIMARY_TDM_RX_1:
  7010. case AFE_PORT_ID_PRIMARY_TDM_TX_1:
  7011. case AFE_PORT_ID_PRIMARY_TDM_RX_2:
  7012. case AFE_PORT_ID_PRIMARY_TDM_TX_2:
  7013. case AFE_PORT_ID_PRIMARY_TDM_RX_3:
  7014. case AFE_PORT_ID_PRIMARY_TDM_TX_3:
  7015. case AFE_PORT_ID_PRIMARY_TDM_RX_4:
  7016. case AFE_PORT_ID_PRIMARY_TDM_TX_4:
  7017. case AFE_PORT_ID_PRIMARY_TDM_RX_5:
  7018. case AFE_PORT_ID_PRIMARY_TDM_TX_5:
  7019. case AFE_PORT_ID_PRIMARY_TDM_RX_6:
  7020. case AFE_PORT_ID_PRIMARY_TDM_TX_6:
  7021. case AFE_PORT_ID_PRIMARY_TDM_RX_7:
  7022. case AFE_PORT_ID_PRIMARY_TDM_TX_7:
  7023. case AFE_PORT_ID_SECONDARY_TDM_RX:
  7024. case AFE_PORT_ID_SECONDARY_TDM_TX:
  7025. case AFE_PORT_ID_SECONDARY_TDM_RX_1:
  7026. case AFE_PORT_ID_SECONDARY_TDM_TX_1:
  7027. case AFE_PORT_ID_SECONDARY_TDM_RX_2:
  7028. case AFE_PORT_ID_SECONDARY_TDM_TX_2:
  7029. case AFE_PORT_ID_SECONDARY_TDM_RX_3:
  7030. case AFE_PORT_ID_SECONDARY_TDM_TX_3:
  7031. case AFE_PORT_ID_SECONDARY_TDM_RX_4:
  7032. case AFE_PORT_ID_SECONDARY_TDM_TX_4:
  7033. case AFE_PORT_ID_SECONDARY_TDM_RX_5:
  7034. case AFE_PORT_ID_SECONDARY_TDM_TX_5:
  7035. case AFE_PORT_ID_SECONDARY_TDM_RX_6:
  7036. case AFE_PORT_ID_SECONDARY_TDM_TX_6:
  7037. case AFE_PORT_ID_SECONDARY_TDM_RX_7:
  7038. case AFE_PORT_ID_SECONDARY_TDM_TX_7:
  7039. case AFE_PORT_ID_TERTIARY_TDM_RX:
  7040. case AFE_PORT_ID_TERTIARY_TDM_TX:
  7041. case AFE_PORT_ID_TERTIARY_TDM_RX_1:
  7042. case AFE_PORT_ID_TERTIARY_TDM_TX_1:
  7043. case AFE_PORT_ID_TERTIARY_TDM_RX_2:
  7044. case AFE_PORT_ID_TERTIARY_TDM_TX_2:
  7045. case AFE_PORT_ID_TERTIARY_TDM_RX_3:
  7046. case AFE_PORT_ID_TERTIARY_TDM_TX_3:
  7047. case AFE_PORT_ID_TERTIARY_TDM_RX_4:
  7048. case AFE_PORT_ID_TERTIARY_TDM_TX_4:
  7049. case AFE_PORT_ID_TERTIARY_TDM_RX_5:
  7050. case AFE_PORT_ID_TERTIARY_TDM_TX_5:
  7051. case AFE_PORT_ID_TERTIARY_TDM_RX_6:
  7052. case AFE_PORT_ID_TERTIARY_TDM_TX_6:
  7053. case AFE_PORT_ID_TERTIARY_TDM_RX_7:
  7054. case AFE_PORT_ID_TERTIARY_TDM_TX_7:
  7055. case AFE_PORT_ID_QUATERNARY_TDM_RX:
  7056. case AFE_PORT_ID_QUATERNARY_TDM_TX:
  7057. case AFE_PORT_ID_QUATERNARY_TDM_RX_1:
  7058. case AFE_PORT_ID_QUATERNARY_TDM_TX_1:
  7059. case AFE_PORT_ID_QUATERNARY_TDM_RX_2:
  7060. case AFE_PORT_ID_QUATERNARY_TDM_TX_2:
  7061. case AFE_PORT_ID_QUATERNARY_TDM_RX_3:
  7062. case AFE_PORT_ID_QUATERNARY_TDM_TX_3:
  7063. case AFE_PORT_ID_QUATERNARY_TDM_RX_4:
  7064. case AFE_PORT_ID_QUATERNARY_TDM_TX_4:
  7065. case AFE_PORT_ID_QUATERNARY_TDM_RX_5:
  7066. case AFE_PORT_ID_QUATERNARY_TDM_TX_5:
  7067. case AFE_PORT_ID_QUATERNARY_TDM_RX_6:
  7068. case AFE_PORT_ID_QUATERNARY_TDM_TX_6:
  7069. case AFE_PORT_ID_QUATERNARY_TDM_RX_7:
  7070. case AFE_PORT_ID_QUATERNARY_TDM_TX_7:
  7071. case AFE_PORT_ID_QUINARY_TDM_RX:
  7072. case AFE_PORT_ID_QUINARY_TDM_TX:
  7073. case AFE_PORT_ID_QUINARY_TDM_RX_1:
  7074. case AFE_PORT_ID_QUINARY_TDM_TX_1:
  7075. case AFE_PORT_ID_QUINARY_TDM_RX_2:
  7076. case AFE_PORT_ID_QUINARY_TDM_TX_2:
  7077. case AFE_PORT_ID_QUINARY_TDM_RX_3:
  7078. case AFE_PORT_ID_QUINARY_TDM_TX_3:
  7079. case AFE_PORT_ID_QUINARY_TDM_RX_4:
  7080. case AFE_PORT_ID_QUINARY_TDM_TX_4:
  7081. case AFE_PORT_ID_QUINARY_TDM_RX_5:
  7082. case AFE_PORT_ID_QUINARY_TDM_TX_5:
  7083. case AFE_PORT_ID_QUINARY_TDM_RX_6:
  7084. case AFE_PORT_ID_QUINARY_TDM_TX_6:
  7085. case AFE_PORT_ID_QUINARY_TDM_RX_7:
  7086. case AFE_PORT_ID_QUINARY_TDM_TX_7:
  7087. case AFE_PORT_ID_SENARY_TDM_RX:
  7088. case AFE_PORT_ID_SENARY_TDM_TX:
  7089. case AFE_PORT_ID_SENARY_TDM_RX_1:
  7090. case AFE_PORT_ID_SENARY_TDM_TX_1:
  7091. case AFE_PORT_ID_SENARY_TDM_RX_2:
  7092. case AFE_PORT_ID_SENARY_TDM_TX_2:
  7093. case AFE_PORT_ID_SENARY_TDM_RX_3:
  7094. case AFE_PORT_ID_SENARY_TDM_TX_3:
  7095. case AFE_PORT_ID_SENARY_TDM_RX_4:
  7096. case AFE_PORT_ID_SENARY_TDM_TX_4:
  7097. case AFE_PORT_ID_SENARY_TDM_RX_5:
  7098. case AFE_PORT_ID_SENARY_TDM_TX_5:
  7099. case AFE_PORT_ID_SENARY_TDM_RX_6:
  7100. case AFE_PORT_ID_SENARY_TDM_TX_6:
  7101. case AFE_PORT_ID_SENARY_TDM_RX_7:
  7102. case AFE_PORT_ID_SENARY_TDM_TX_7:
  7103. case AFE_PORT_ID_INT0_MI2S_RX:
  7104. case AFE_PORT_ID_INT1_MI2S_RX:
  7105. case AFE_PORT_ID_INT2_MI2S_RX:
  7106. case AFE_PORT_ID_INT3_MI2S_RX:
  7107. case AFE_PORT_ID_INT4_MI2S_RX:
  7108. case AFE_PORT_ID_INT5_MI2S_RX:
  7109. case AFE_PORT_ID_INT6_MI2S_RX:
  7110. case AFE_PORT_ID_INT0_MI2S_TX:
  7111. case AFE_PORT_ID_INT1_MI2S_TX:
  7112. case AFE_PORT_ID_INT2_MI2S_TX:
  7113. case AFE_PORT_ID_INT3_MI2S_TX:
  7114. case AFE_PORT_ID_INT4_MI2S_TX:
  7115. case AFE_PORT_ID_INT5_MI2S_TX:
  7116. case AFE_PORT_ID_INT6_MI2S_TX:
  7117. case AFE_PORT_ID_WSA_CODEC_DMA_RX_0:
  7118. case AFE_PORT_ID_WSA_CODEC_DMA_TX_0:
  7119. case AFE_PORT_ID_WSA_CODEC_DMA_RX_1:
  7120. case AFE_PORT_ID_WSA_CODEC_DMA_TX_1:
  7121. case AFE_PORT_ID_WSA_CODEC_DMA_TX_2:
  7122. case AFE_PORT_ID_VA_CODEC_DMA_TX_0:
  7123. case AFE_PORT_ID_VA_CODEC_DMA_TX_1:
  7124. case AFE_PORT_ID_VA_CODEC_DMA_TX_2:
  7125. case AFE_PORT_ID_RX_CODEC_DMA_RX_0:
  7126. case AFE_PORT_ID_TX_CODEC_DMA_TX_0:
  7127. case AFE_PORT_ID_RX_CODEC_DMA_RX_1:
  7128. case AFE_PORT_ID_TX_CODEC_DMA_TX_1:
  7129. case AFE_PORT_ID_RX_CODEC_DMA_RX_2:
  7130. case AFE_PORT_ID_TX_CODEC_DMA_TX_2:
  7131. case AFE_PORT_ID_RX_CODEC_DMA_RX_3:
  7132. case AFE_PORT_ID_TX_CODEC_DMA_TX_3:
  7133. case AFE_PORT_ID_RX_CODEC_DMA_RX_4:
  7134. case AFE_PORT_ID_TX_CODEC_DMA_TX_4:
  7135. case AFE_PORT_ID_RX_CODEC_DMA_RX_5:
  7136. case AFE_PORT_ID_TX_CODEC_DMA_TX_5:
  7137. case AFE_PORT_ID_RX_CODEC_DMA_RX_6:
  7138. case AFE_PORT_ID_RX_CODEC_DMA_RX_7:
  7139. case RT_PROXY_PORT_002_RX:
  7140. case RT_PROXY_PORT_002_TX:
  7141. {
  7142. ret = 0;
  7143. break;
  7144. }
  7145. default:
  7146. pr_err("%s: default ret 0x%x\n", __func__, port_id);
  7147. ret = -EINVAL;
  7148. }
  7149. return ret;
  7150. }
  7151. int afe_convert_virtual_to_portid(u16 port_id)
  7152. {
  7153. int ret;
  7154. /*
  7155. * if port_id is virtual, convert to physical..
  7156. * if port_id is already physical, return physical
  7157. */
  7158. if (afe_validate_port(port_id) < 0) {
  7159. if (port_id == RT_PROXY_DAI_001_RX ||
  7160. port_id == RT_PROXY_DAI_001_TX ||
  7161. port_id == RT_PROXY_DAI_002_RX ||
  7162. port_id == RT_PROXY_DAI_002_TX) {
  7163. ret = VIRTUAL_ID_TO_PORTID(port_id);
  7164. } else {
  7165. pr_err("%s: wrong port 0x%x\n",
  7166. __func__, port_id);
  7167. ret = -EINVAL;
  7168. }
  7169. } else
  7170. ret = port_id;
  7171. return ret;
  7172. }
  7173. int afe_port_stop_nowait(int port_id)
  7174. {
  7175. struct afe_port_cmd_device_stop stop;
  7176. int ret = 0;
  7177. if (this_afe.apr == NULL) {
  7178. pr_err("%s: AFE is already closed\n", __func__);
  7179. ret = -EINVAL;
  7180. goto fail_cmd;
  7181. }
  7182. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  7183. port_id = q6audio_convert_virtual_to_portid(port_id);
  7184. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  7185. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  7186. stop.hdr.pkt_size = sizeof(stop);
  7187. stop.hdr.src_port = 0;
  7188. stop.hdr.dest_port = 0;
  7189. stop.hdr.token = 0;
  7190. stop.hdr.opcode = AFE_PORT_CMD_DEVICE_STOP;
  7191. stop.port_id = port_id;
  7192. stop.reserved = 0;
  7193. ret = afe_apr_send_pkt(&stop, NULL);
  7194. if (ret)
  7195. pr_err("%s: AFE close failed %d\n", __func__, ret);
  7196. fail_cmd:
  7197. return ret;
  7198. }
  7199. /**
  7200. * afe_close - command to close AFE port
  7201. *
  7202. * @port_id: AFE port id
  7203. *
  7204. * Returns 0 on success, appropriate error code otherwise
  7205. */
  7206. int afe_close(int port_id)
  7207. {
  7208. struct afe_port_cmd_device_stop stop;
  7209. enum afe_mad_type mad_type;
  7210. int ret = 0;
  7211. int index = 0;
  7212. uint16_t port_index;
  7213. if (this_afe.apr == NULL) {
  7214. pr_err("%s: AFE is already closed\n", __func__);
  7215. if ((port_id == RT_PROXY_DAI_001_RX) ||
  7216. (port_id == RT_PROXY_DAI_002_TX))
  7217. pcm_afe_instance[port_id & 0x1] = 0;
  7218. if ((port_id == RT_PROXY_DAI_002_RX) ||
  7219. (port_id == RT_PROXY_DAI_001_TX))
  7220. proxy_afe_instance[port_id & 0x1] = 0;
  7221. afe_close_done[port_id & 0x1] = true;
  7222. ret = -EINVAL;
  7223. goto fail_cmd;
  7224. }
  7225. pr_info("%s: port_id = 0x%x\n", __func__, port_id);
  7226. if ((port_id == RT_PROXY_DAI_001_RX) ||
  7227. (port_id == RT_PROXY_DAI_002_TX)) {
  7228. pr_debug("%s: before decrementing pcm_afe_instance %d\n",
  7229. __func__, pcm_afe_instance[port_id & 0x1]);
  7230. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  7231. pcm_afe_instance[port_id & 0x1]--;
  7232. if ((!(pcm_afe_instance[port_id & 0x1] == 0 &&
  7233. proxy_afe_instance[port_id & 0x1] == 0)) ||
  7234. afe_close_done[port_id & 0x1] == true)
  7235. return 0;
  7236. afe_close_done[port_id & 0x1] = true;
  7237. }
  7238. if ((port_id == RT_PROXY_DAI_002_RX) ||
  7239. (port_id == RT_PROXY_DAI_001_TX)) {
  7240. pr_debug("%s: before decrementing proxy_afe_instance %d\n",
  7241. __func__, proxy_afe_instance[port_id & 0x1]);
  7242. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  7243. proxy_afe_instance[port_id & 0x1]--;
  7244. if ((!(pcm_afe_instance[port_id & 0x1] == 0 &&
  7245. proxy_afe_instance[port_id & 0x1] == 0)) ||
  7246. afe_close_done[port_id & 0x1] == true)
  7247. return 0;
  7248. afe_close_done[port_id & 0x1] = true;
  7249. }
  7250. port_id = q6audio_convert_virtual_to_portid(port_id);
  7251. index = q6audio_get_port_index(port_id);
  7252. if (index < 0 || index >= AFE_MAX_PORTS) {
  7253. pr_err("%s: AFE port index[%d] invalid!\n",
  7254. __func__, index);
  7255. return -EINVAL;
  7256. }
  7257. ret = q6audio_validate_port(port_id);
  7258. if (ret < 0) {
  7259. pr_warn("%s: Not a valid port id 0x%x ret %d\n",
  7260. __func__, port_id, ret);
  7261. return -EINVAL;
  7262. }
  7263. mad_type = afe_port_get_mad_type(port_id);
  7264. pr_debug("%s: port_id 0x%x, mad_type %d\n", __func__, port_id,
  7265. mad_type);
  7266. if (mad_type != MAD_HW_NONE && mad_type != MAD_SW_AUDIO) {
  7267. pr_debug("%s: Turn off MAD\n", __func__);
  7268. ret = afe_turn_onoff_hw_mad(mad_type, false);
  7269. if (ret) {
  7270. pr_err("%s: afe_turn_onoff_hw_mad failed %d\n",
  7271. __func__, ret);
  7272. return ret;
  7273. }
  7274. } else {
  7275. pr_debug("%s: Not a MAD port\n", __func__);
  7276. }
  7277. mutex_lock(&this_afe.afe_cmd_lock);
  7278. port_index = afe_get_port_index(port_id);
  7279. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  7280. this_afe.afe_sample_rates[port_index] = 0;
  7281. this_afe.topology[port_index] = 0;
  7282. this_afe.dev_acdb_id[port_index] = 0;
  7283. } else {
  7284. pr_err("%s: port %d\n", __func__, port_index);
  7285. ret = -EINVAL;
  7286. goto fail_cmd;
  7287. }
  7288. if ((port_id == this_afe.aanc_info.aanc_tx_port) &&
  7289. (this_afe.aanc_info.aanc_active)) {
  7290. memset(&this_afe.aanc_info, 0x00, sizeof(this_afe.aanc_info));
  7291. ret = afe_aanc_mod_enable(this_afe.apr, port_id, 0);
  7292. if (ret)
  7293. pr_err("%s: AFE mod disable failed %d\n",
  7294. __func__, ret);
  7295. }
  7296. /*
  7297. * even if ramp down configuration failed it is not serious enough to
  7298. * warrant bailaing out.
  7299. */
  7300. if (afe_spk_ramp_dn_cfg(port_id) < 0)
  7301. pr_err("%s: ramp down configuration failed\n", __func__);
  7302. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  7303. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  7304. stop.hdr.pkt_size = sizeof(stop);
  7305. stop.hdr.src_port = 0;
  7306. stop.hdr.dest_port = 0;
  7307. stop.hdr.token = index;
  7308. stop.hdr.opcode = AFE_PORT_CMD_DEVICE_STOP;
  7309. stop.port_id = q6audio_get_port_id(port_id);
  7310. stop.reserved = 0;
  7311. ret = afe_apr_send_pkt(&stop, &this_afe.wait[index]);
  7312. if (ret)
  7313. pr_err("%s: AFE close failed %d\n", __func__, ret);
  7314. fail_cmd:
  7315. mutex_unlock(&this_afe.afe_cmd_lock);
  7316. return ret;
  7317. }
  7318. EXPORT_SYMBOL(afe_close);
  7319. int afe_set_digital_codec_core_clock(u16 port_id,
  7320. struct afe_digital_clk_cfg *cfg)
  7321. {
  7322. struct afe_digital_clk_cfg clk_cfg;
  7323. struct param_hdr_v3 param_hdr;
  7324. int ret = 0;
  7325. if (!cfg) {
  7326. pr_err("%s: clock cfg is NULL\n", __func__);
  7327. return -EINVAL;
  7328. }
  7329. memset(&clk_cfg, 0, sizeof(clk_cfg));
  7330. memset(&param_hdr, 0, sizeof(param_hdr));
  7331. /*default rx port is taken to enable the codec digital clock*/
  7332. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  7333. param_hdr.instance_id = INSTANCE_ID_0;
  7334. param_hdr.param_id = AFE_PARAM_ID_INTERNAL_DIGIATL_CDC_CLK_CONFIG;
  7335. param_hdr.param_size = sizeof(struct afe_digital_clk_cfg);
  7336. clk_cfg = *cfg;
  7337. pr_debug("%s: Minor version =0x%x clk val = %d\n"
  7338. "clk root = 0x%x resrv = 0x%x\n",
  7339. __func__, cfg->i2s_cfg_minor_version, cfg->clk_val,
  7340. cfg->clk_root, cfg->reserved);
  7341. ret = q6afe_pack_and_set_param_in_band(port_id,
  7342. q6audio_get_port_index(port_id),
  7343. param_hdr, (u8 *) &clk_cfg);
  7344. if (ret < 0)
  7345. pr_err("%s: AFE enable for port 0x%x ret %d\n", __func__,
  7346. port_id, ret);
  7347. return ret;
  7348. }
  7349. /**
  7350. * afe_set_lpass_clock - Enable AFE lpass clock
  7351. *
  7352. * @port_id: AFE port id
  7353. * @cfg: pointer to clk set struct
  7354. *
  7355. * Returns 0 on success, appropriate error code otherwise
  7356. */
  7357. int afe_set_lpass_clock(u16 port_id, struct afe_clk_cfg *cfg)
  7358. {
  7359. struct afe_clk_cfg clk_cfg;
  7360. struct param_hdr_v3 param_hdr;
  7361. int ret = 0;
  7362. if (!cfg) {
  7363. pr_err("%s: clock cfg is NULL\n", __func__);
  7364. return -EINVAL;
  7365. }
  7366. memset(&clk_cfg, 0, sizeof(clk_cfg));
  7367. memset(&param_hdr, 0, sizeof(param_hdr));
  7368. ret = q6audio_is_digital_pcm_interface(port_id);
  7369. if (ret < 0) {
  7370. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  7371. __func__, ret);
  7372. return -EINVAL;
  7373. }
  7374. mutex_lock(&this_afe.afe_cmd_lock);
  7375. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  7376. param_hdr.instance_id = INSTANCE_ID_0;
  7377. param_hdr.param_id = AFE_PARAM_ID_LPAIF_CLK_CONFIG;
  7378. param_hdr.param_size = sizeof(clk_cfg);
  7379. clk_cfg = *cfg;
  7380. pr_debug("%s: Minor version =0x%x clk val1 = %d\n"
  7381. "clk val2 = %d, clk src = 0x%x\n"
  7382. "clk root = 0x%x clk mode = 0x%x resrv = 0x%x\n"
  7383. "port id = 0x%x\n",
  7384. __func__, cfg->i2s_cfg_minor_version,
  7385. cfg->clk_val1, cfg->clk_val2, cfg->clk_src,
  7386. cfg->clk_root, cfg->clk_set_mode,
  7387. cfg->reserved, q6audio_get_port_id(port_id));
  7388. trace_printk("%s: Minor version =0x%x clk val1 = %d\n"
  7389. "clk val2 = %d, clk src = 0x%x\n"
  7390. "clk root = 0x%x clk mode = 0x%x resrv = 0x%x\n"
  7391. "port id = 0x%x\n",
  7392. __func__, cfg->i2s_cfg_minor_version,
  7393. cfg->clk_val1, cfg->clk_val2, cfg->clk_src,
  7394. cfg->clk_root, cfg->clk_set_mode,
  7395. cfg->reserved, q6audio_get_port_id(port_id));
  7396. ret = q6afe_pack_and_set_param_in_band(port_id,
  7397. q6audio_get_port_index(port_id),
  7398. param_hdr, (u8 *) &clk_cfg);
  7399. if (ret < 0)
  7400. pr_err("%s: AFE enable for port 0x%x ret %d\n",
  7401. __func__, port_id, ret);
  7402. mutex_unlock(&this_afe.afe_cmd_lock);
  7403. return ret;
  7404. }
  7405. EXPORT_SYMBOL(afe_set_lpass_clock);
  7406. /**
  7407. * afe_set_lpass_clk_cfg - Set AFE clk config
  7408. *
  7409. * @index: port index
  7410. * @cfg: pointer to clk set struct
  7411. *
  7412. * Returns 0 on success, appropriate error code otherwise
  7413. */
  7414. int afe_set_lpass_clk_cfg(int index, struct afe_clk_set *cfg)
  7415. {
  7416. struct param_hdr_v3 param_hdr;
  7417. int ret = 0;
  7418. if (!cfg) {
  7419. pr_err("%s: clock cfg is NULL\n", __func__);
  7420. ret = -EINVAL;
  7421. return ret;
  7422. }
  7423. if (index < 0 || index >= AFE_MAX_PORTS) {
  7424. pr_err("%s: index[%d] invalid!\n", __func__, index);
  7425. return -EINVAL;
  7426. }
  7427. memset(&param_hdr, 0, sizeof(param_hdr));
  7428. mutex_lock(&this_afe.afe_clk_lock);
  7429. param_hdr.module_id = AFE_MODULE_CLOCK_SET;
  7430. param_hdr.instance_id = INSTANCE_ID_0;
  7431. param_hdr.param_id = AFE_PARAM_ID_CLOCK_SET;
  7432. param_hdr.param_size = sizeof(struct afe_clk_set);
  7433. pr_debug("%s: Minor version =0x%x clk id = %d\n"
  7434. "clk freq (Hz) = %d, clk attri = 0x%x\n"
  7435. "clk root = 0x%x clk enable = 0x%x\n",
  7436. __func__, cfg->clk_set_minor_version,
  7437. cfg->clk_id, cfg->clk_freq_in_hz, cfg->clk_attri,
  7438. cfg->clk_root, cfg->enable);
  7439. trace_printk("%s: Minor version =0x%x clk id = %d\n"
  7440. "clk freq (Hz) = %d, clk attri = 0x%x\n"
  7441. "clk root = 0x%x clk enable = 0x%x\n",
  7442. __func__, cfg->clk_set_minor_version,
  7443. cfg->clk_id, cfg->clk_freq_in_hz, cfg->clk_attri,
  7444. cfg->clk_root, cfg->enable);
  7445. ret = q6afe_svc_pack_and_set_param_in_band(index, param_hdr,
  7446. (u8 *) cfg);
  7447. if (ret < 0) {
  7448. pr_err_ratelimited("%s: AFE clk cfg failed with ret %d\n",
  7449. __func__, ret);
  7450. trace_printk("%s: AFE clk cfg failed with ret %d\n",
  7451. __func__, ret);
  7452. }
  7453. mutex_unlock(&this_afe.afe_clk_lock);
  7454. return ret;
  7455. }
  7456. EXPORT_SYMBOL(afe_set_lpass_clk_cfg);
  7457. /**
  7458. * afe_set_lpass_clock_v2 - Enable AFE lpass clock
  7459. *
  7460. * @port_id: AFE port id
  7461. * @cfg: pointer to clk set struct
  7462. *
  7463. * Returns 0 on success, appropriate error code otherwise
  7464. */
  7465. int afe_set_lpass_clock_v2(u16 port_id, struct afe_clk_set *cfg)
  7466. {
  7467. int index = 0;
  7468. int ret = 0;
  7469. index = q6audio_get_port_index(port_id);
  7470. if (index < 0 || index >= AFE_MAX_PORTS) {
  7471. pr_err("%s: AFE port index[%d] invalid!\n",
  7472. __func__, index);
  7473. return -EINVAL;
  7474. }
  7475. ret = q6audio_is_digital_pcm_interface(port_id);
  7476. if (ret < 0) {
  7477. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  7478. __func__, ret);
  7479. return -EINVAL;
  7480. }
  7481. ret = afe_set_lpass_clk_cfg(index, cfg);
  7482. if (ret)
  7483. pr_err("%s: afe_set_lpass_clk_cfg_v2 failed %d\n",
  7484. __func__, ret);
  7485. return ret;
  7486. }
  7487. EXPORT_SYMBOL(afe_set_lpass_clock_v2);
  7488. int afe_set_lpass_internal_digital_codec_clock(u16 port_id,
  7489. struct afe_digital_clk_cfg *cfg)
  7490. {
  7491. struct afe_digital_clk_cfg clk_cfg;
  7492. struct param_hdr_v3 param_hdr;
  7493. int ret = 0;
  7494. if (!cfg) {
  7495. pr_err("%s: clock cfg is NULL\n", __func__);
  7496. return -EINVAL;
  7497. }
  7498. memset(&clk_cfg, 0, sizeof(clk_cfg));
  7499. memset(&param_hdr, 0, sizeof(param_hdr));
  7500. ret = q6audio_is_digital_pcm_interface(port_id);
  7501. if (ret < 0) {
  7502. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  7503. __func__, ret);
  7504. return -EINVAL;
  7505. }
  7506. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  7507. param_hdr.instance_id = INSTANCE_ID_0;
  7508. param_hdr.param_id = AFE_PARAM_ID_INTERNAL_DIGIATL_CDC_CLK_CONFIG;
  7509. param_hdr.param_size = sizeof(clk_cfg);
  7510. clk_cfg = *cfg;
  7511. pr_debug("%s: Minor version =0x%x clk val = %d\n"
  7512. "clk root = 0x%x resrv = 0x%x port id = 0x%x\n",
  7513. __func__, cfg->i2s_cfg_minor_version,
  7514. cfg->clk_val, cfg->clk_root, cfg->reserved,
  7515. q6audio_get_port_id(port_id));
  7516. ret = q6afe_pack_and_set_param_in_band(port_id,
  7517. q6audio_get_port_index(port_id),
  7518. param_hdr, (u8 *) &clk_cfg);
  7519. if (ret < 0)
  7520. pr_err("%s: AFE enable for port 0x0x%x ret %d\n",
  7521. __func__, port_id, ret);
  7522. return ret;
  7523. }
  7524. /**
  7525. * afe_enable_lpass_core_shared_clock -
  7526. * Configures the core clk on LPASS.
  7527. * Need on targets where lpass provides
  7528. * clocks
  7529. * @port_id: afe port id
  7530. * @enable: enable or disable clk
  7531. *
  7532. * Returns success or failure of call.
  7533. */
  7534. int afe_enable_lpass_core_shared_clock(u16 port_id, u32 enable)
  7535. {
  7536. struct afe_param_id_lpass_core_shared_clk_cfg clk_cfg;
  7537. struct param_hdr_v3 param_hdr;
  7538. int ret = 0;
  7539. memset(&clk_cfg, 0, sizeof(clk_cfg));
  7540. memset(&param_hdr, 0, sizeof(param_hdr));
  7541. ret = q6audio_is_digital_pcm_interface(port_id);
  7542. if (ret < 0) {
  7543. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  7544. __func__, ret);
  7545. return -EINVAL;
  7546. }
  7547. mutex_lock(&this_afe.afe_cmd_lock);
  7548. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  7549. param_hdr.instance_id = INSTANCE_ID_0;
  7550. param_hdr.param_id = AFE_PARAM_ID_LPASS_CORE_SHARED_CLOCK_CONFIG;
  7551. param_hdr.param_size = sizeof(clk_cfg);
  7552. clk_cfg.lpass_core_shared_clk_cfg_minor_version =
  7553. AFE_API_VERSION_LPASS_CORE_SHARED_CLK_CONFIG;
  7554. clk_cfg.enable = enable;
  7555. pr_debug("%s: port id = %d, enable = %d\n",
  7556. __func__, q6audio_get_port_id(port_id), enable);
  7557. ret = q6afe_pack_and_set_param_in_band(port_id,
  7558. q6audio_get_port_index(port_id),
  7559. param_hdr, (u8 *) &clk_cfg);
  7560. if (ret < 0)
  7561. pr_err("%s: AFE enable for port 0x%x ret %d\n",
  7562. __func__, port_id, ret);
  7563. mutex_unlock(&this_afe.afe_cmd_lock);
  7564. return ret;
  7565. }
  7566. EXPORT_SYMBOL(afe_enable_lpass_core_shared_clock);
  7567. /**
  7568. * q6afe_check_osr_clk_freq -
  7569. * Gets supported OSR CLK frequencies
  7570. *
  7571. * @freq: frequency to check
  7572. *
  7573. * Returns success if freq is supported.
  7574. */
  7575. int q6afe_check_osr_clk_freq(u32 freq)
  7576. {
  7577. int ret = 0;
  7578. switch (freq) {
  7579. case Q6AFE_LPASS_OSR_CLK_12_P288_MHZ:
  7580. case Q6AFE_LPASS_OSR_CLK_9_P600_MHZ:
  7581. case Q6AFE_LPASS_OSR_CLK_8_P192_MHZ:
  7582. case Q6AFE_LPASS_OSR_CLK_6_P144_MHZ:
  7583. case Q6AFE_LPASS_OSR_CLK_4_P096_MHZ:
  7584. case Q6AFE_LPASS_OSR_CLK_3_P072_MHZ:
  7585. case Q6AFE_LPASS_OSR_CLK_2_P048_MHZ:
  7586. case Q6AFE_LPASS_OSR_CLK_1_P536_MHZ:
  7587. case Q6AFE_LPASS_OSR_CLK_1_P024_MHZ:
  7588. case Q6AFE_LPASS_OSR_CLK_768_kHZ:
  7589. case Q6AFE_LPASS_OSR_CLK_512_kHZ:
  7590. break;
  7591. default:
  7592. pr_err("%s: default freq 0x%x\n",
  7593. __func__, freq);
  7594. ret = -EINVAL;
  7595. }
  7596. return ret;
  7597. }
  7598. EXPORT_SYMBOL(q6afe_check_osr_clk_freq);
  7599. static int afe_get_spv4_th_vi_v_vali_data(void *params, uint32_t size)
  7600. {
  7601. struct param_hdr_v3 param_hdr;
  7602. int port = AFE_PORT_ID_WSA_CODEC_DMA_TX_0;
  7603. int ret = -EINVAL;
  7604. u8 *rcvd_params = NULL;
  7605. struct afe_sp_v4_channel_v_vali_params *v_vali_params = NULL;
  7606. if (!params) {
  7607. pr_err("%s: Invalid params\n", __func__);
  7608. goto done;
  7609. }
  7610. if (this_afe.vi_tx_port != -1)
  7611. port = this_afe.vi_tx_port;
  7612. mutex_lock(&this_afe.afe_cmd_lock);
  7613. memset(&param_hdr, 0, sizeof(param_hdr));
  7614. param_hdr.module_id = AFE_MODULE_SPEAKER_PROTECTION_V4_VI;
  7615. param_hdr.instance_id = INSTANCE_ID_0;
  7616. param_hdr.param_id = AFE_PARAM_ID_SP_V4_TH_VI_V_VALI_PARAMS;
  7617. param_hdr.param_size = size;
  7618. ret = q6afe_get_params(port, NULL, &param_hdr);
  7619. if (ret) {
  7620. pr_err("%s: Failed to get TH VI V-Vali data\n", __func__);
  7621. goto get_params_fail;
  7622. }
  7623. rcvd_params = (u8 *)this_afe.spv4_get_param_resp_ptr +
  7624. sizeof(struct afe_sp_v4_gen_get_param_resp);
  7625. memcpy(params, rcvd_params, this_afe.spv4_rcvd_param_size);
  7626. v_vali_params = (struct afe_sp_v4_channel_v_vali_params *)
  7627. (params + sizeof(struct afe_sp_v4_param_th_vi_v_vali_params));
  7628. pr_debug("%s: Vrms %d %d status %d %d\n", __func__,
  7629. v_vali_params[SP_V2_SPKR_1].vrms_q24,
  7630. v_vali_params[SP_V2_SPKR_2].vrms_q24,
  7631. v_vali_params[SP_V2_SPKR_1].status,
  7632. v_vali_params[SP_V2_SPKR_2].status);
  7633. /*using the non-spv4 status varaible to support v_vali debug app. */
  7634. this_afe.th_vi_v_vali_resp.param.status[SP_V2_SPKR_1] =
  7635. v_vali_params[SP_V2_SPKR_1].status;
  7636. this_afe.th_vi_v_vali_resp.param.status[SP_V2_SPKR_2] =
  7637. v_vali_params[SP_V2_SPKR_2].status;
  7638. ret = 0;
  7639. get_params_fail:
  7640. kfree(this_afe.spv4_get_param_resp_ptr);
  7641. mutex_unlock(&this_afe.afe_cmd_lock);
  7642. done:
  7643. return ret;
  7644. }
  7645. static int afe_get_sp_th_vi_v_vali_data(
  7646. struct afe_sp_th_vi_v_vali_get_param *th_vi_v_vali)
  7647. {
  7648. struct param_hdr_v3 param_hdr;
  7649. int port = SLIMBUS_4_TX;
  7650. int ret = -EINVAL;
  7651. if (!th_vi_v_vali) {
  7652. pr_err("%s: Invalid params\n", __func__);
  7653. goto done;
  7654. }
  7655. if (this_afe.vi_tx_port != -1)
  7656. port = this_afe.vi_tx_port;
  7657. mutex_lock(&this_afe.afe_cmd_lock);
  7658. memset(&param_hdr, 0, sizeof(param_hdr));
  7659. param_hdr.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_TH_VI;
  7660. param_hdr.instance_id = INSTANCE_ID_0;
  7661. param_hdr.param_id = AFE_PARAM_ID_SP_V2_TH_VI_V_VALI_PARAMS;
  7662. param_hdr.param_size = sizeof(struct afe_sp_th_vi_v_vali_params);
  7663. ret = q6afe_get_params(port, NULL, &param_hdr);
  7664. if (ret) {
  7665. pr_err("%s: Failed to get TH VI V-Vali data\n", __func__);
  7666. goto get_params_fail;
  7667. }
  7668. th_vi_v_vali->pdata = param_hdr;
  7669. memcpy(&th_vi_v_vali->param, &this_afe.th_vi_v_vali_resp.param,
  7670. sizeof(this_afe.th_vi_v_vali_resp.param));
  7671. pr_debug("%s: Vrms %d %d status %d %d\n", __func__,
  7672. th_vi_v_vali->param.vrms_q24[SP_V2_SPKR_1],
  7673. th_vi_v_vali->param.vrms_q24[SP_V2_SPKR_2],
  7674. th_vi_v_vali->param.status[SP_V2_SPKR_1],
  7675. th_vi_v_vali->param.status[SP_V2_SPKR_2]);
  7676. ret = 0;
  7677. get_params_fail:
  7678. mutex_unlock(&this_afe.afe_cmd_lock);
  7679. done:
  7680. return ret;
  7681. }
  7682. static int afe_get_spv4_th_vi_ftm_data(void *params, uint32_t size)
  7683. {
  7684. struct param_hdr_v3 param_hdr;
  7685. int port = SLIMBUS_4_TX;
  7686. int ret = -EINVAL;
  7687. u8 *rcvd_params = NULL;
  7688. struct afe_sp_v4_channel_ftm_params *th_vi_params;
  7689. if (!params) {
  7690. pr_err("%s: Invalid params\n", __func__);
  7691. goto done;
  7692. }
  7693. if (this_afe.vi_tx_port != -1)
  7694. port = this_afe.vi_tx_port;
  7695. mutex_lock(&this_afe.afe_cmd_lock);
  7696. memset(&param_hdr, 0, sizeof(param_hdr));
  7697. param_hdr.module_id = AFE_MODULE_SPEAKER_PROTECTION_V4_VI;
  7698. param_hdr.instance_id = INSTANCE_ID_0;
  7699. param_hdr.param_id = AFE_PARAM_ID_SP_V4_TH_VI_FTM_PARAMS;
  7700. param_hdr.param_size = size;
  7701. ret = q6afe_get_params(port, NULL, &param_hdr);
  7702. if (ret) {
  7703. pr_err("%s: Failed to get TH VI FTM data\n", __func__);
  7704. goto get_params_fail;
  7705. }
  7706. rcvd_params = (u8 *)this_afe.spv4_get_param_resp_ptr +
  7707. sizeof(struct afe_sp_v4_gen_get_param_resp);
  7708. memcpy(params, rcvd_params, this_afe.spv4_rcvd_param_size);
  7709. th_vi_params = (struct afe_sp_v4_channel_ftm_params *)
  7710. (params + sizeof(struct afe_sp_v4_param_th_vi_ftm_params));
  7711. pr_debug("%s: DC resistance %d %d temp %d %d status %d %d\n",
  7712. __func__, th_vi_params[SP_V2_SPKR_1].dc_res_q24,
  7713. th_vi_params[SP_V2_SPKR_2].dc_res_q24,
  7714. th_vi_params[SP_V2_SPKR_1].temp_q22,
  7715. th_vi_params[SP_V2_SPKR_2].temp_q22,
  7716. th_vi_params[SP_V2_SPKR_1].status,
  7717. th_vi_params[SP_V2_SPKR_2].status);
  7718. ret = 0;
  7719. get_params_fail:
  7720. kfree(this_afe.spv4_get_param_resp_ptr);
  7721. mutex_unlock(&this_afe.afe_cmd_lock);
  7722. done:
  7723. return ret;
  7724. }
  7725. static int afe_get_sp_th_vi_ftm_data(struct afe_sp_th_vi_get_param *th_vi)
  7726. {
  7727. struct param_hdr_v3 param_hdr;
  7728. int port = SLIMBUS_4_TX;
  7729. int ret = -EINVAL;
  7730. if (!th_vi) {
  7731. pr_err("%s: Invalid params\n", __func__);
  7732. goto done;
  7733. }
  7734. if (this_afe.vi_tx_port != -1)
  7735. port = this_afe.vi_tx_port;
  7736. mutex_lock(&this_afe.afe_cmd_lock);
  7737. memset(&param_hdr, 0, sizeof(param_hdr));
  7738. param_hdr.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_TH_VI;
  7739. param_hdr.instance_id = INSTANCE_ID_0;
  7740. param_hdr.param_id = AFE_PARAM_ID_SP_V2_TH_VI_FTM_PARAMS;
  7741. param_hdr.param_size = sizeof(struct afe_sp_th_vi_ftm_params);
  7742. ret = q6afe_get_params(port, NULL, &param_hdr);
  7743. if (ret) {
  7744. pr_err("%s: Failed to get TH VI FTM data\n", __func__);
  7745. goto get_params_fail;
  7746. }
  7747. th_vi->pdata = param_hdr;
  7748. memcpy(&th_vi->param, &this_afe.th_vi_resp.param,
  7749. sizeof(this_afe.th_vi_resp.param));
  7750. pr_debug("%s: DC resistance %d %d temp %d %d status %d %d\n",
  7751. __func__, th_vi->param.dc_res_q24[SP_V2_SPKR_1],
  7752. th_vi->param.dc_res_q24[SP_V2_SPKR_2],
  7753. th_vi->param.temp_q22[SP_V2_SPKR_1],
  7754. th_vi->param.temp_q22[SP_V2_SPKR_2],
  7755. th_vi->param.status[SP_V2_SPKR_1],
  7756. th_vi->param.status[SP_V2_SPKR_2]);
  7757. ret = 0;
  7758. get_params_fail:
  7759. mutex_unlock(&this_afe.afe_cmd_lock);
  7760. done:
  7761. return ret;
  7762. }
  7763. static int afe_get_spv4_ex_vi_ftm_data(void *params, uint32_t size)
  7764. {
  7765. struct param_hdr_v3 param_hdr;
  7766. int port = SLIMBUS_4_TX;
  7767. int ret = -EINVAL;
  7768. u8 *rcvd_params = NULL;
  7769. struct afe_sp_v4_channel_ex_vi_ftm_params *ex_vi_ftm_param;
  7770. if (!params) {
  7771. pr_err("%s: Invalid params\n", __func__);
  7772. goto done;
  7773. }
  7774. if (this_afe.vi_tx_port != -1)
  7775. port = this_afe.vi_tx_port;
  7776. mutex_lock(&this_afe.afe_cmd_lock);
  7777. memset(&param_hdr, 0, sizeof(param_hdr));
  7778. param_hdr.module_id = AFE_MODULE_SPEAKER_PROTECTION_V4_VI;
  7779. param_hdr.instance_id = INSTANCE_ID_0;
  7780. param_hdr.param_id = AFE_PARAM_ID_SP_V4_EX_VI_FTM_PARAMS;
  7781. param_hdr.param_size = size;
  7782. ret = q6afe_get_params(port, NULL, &param_hdr);
  7783. if (ret < 0) {
  7784. pr_err("%s: get param port 0x%x param id[0x%x]failed %d\n",
  7785. __func__, port, param_hdr.param_id, ret);
  7786. goto get_params_fail;
  7787. }
  7788. rcvd_params = (u8 *)this_afe.spv4_get_param_resp_ptr +
  7789. sizeof(struct afe_sp_v4_gen_get_param_resp);
  7790. memcpy(params, rcvd_params, this_afe.spv4_rcvd_param_size);
  7791. ex_vi_ftm_param = (struct afe_sp_v4_channel_ex_vi_ftm_params *)
  7792. (params + sizeof(struct afe_sp_v4_param_ex_vi_ftm_params));
  7793. pr_debug("%s: resistance %d %d force factor %d %d Damping kg/s %d %d\n"
  7794. "stiffness N/mm %d %d freq %d %d Qfactor %d %d status %d %d",
  7795. __func__, ex_vi_ftm_param[SP_V2_SPKR_1].ftm_re_q24,
  7796. ex_vi_ftm_param[SP_V2_SPKR_2].ftm_re_q24,
  7797. ex_vi_ftm_param[SP_V2_SPKR_1].ftm_Bl_q24,
  7798. ex_vi_ftm_param[SP_V2_SPKR_2].ftm_Bl_q24,
  7799. ex_vi_ftm_param[SP_V2_SPKR_1].ftm_Rms_q24,
  7800. ex_vi_ftm_param[SP_V2_SPKR_2].ftm_Rms_q24,
  7801. ex_vi_ftm_param[SP_V2_SPKR_1].ftm_Kms_q24,
  7802. ex_vi_ftm_param[SP_V2_SPKR_2].ftm_Kms_q24,
  7803. ex_vi_ftm_param[SP_V2_SPKR_1].ftm_Fres_q20,
  7804. ex_vi_ftm_param[SP_V2_SPKR_2].ftm_Fres_q20,
  7805. ex_vi_ftm_param[SP_V2_SPKR_1].ftm_Qms_q24,
  7806. ex_vi_ftm_param[SP_V2_SPKR_2].ftm_Qms_q24,
  7807. ex_vi_ftm_param[SP_V2_SPKR_1].status,
  7808. ex_vi_ftm_param[SP_V2_SPKR_2].status);
  7809. ret = 0;
  7810. get_params_fail:
  7811. kfree(this_afe.spv4_get_param_resp_ptr);
  7812. mutex_unlock(&this_afe.afe_cmd_lock);
  7813. done:
  7814. return ret;
  7815. }
  7816. static int afe_get_sp_ex_vi_ftm_data(struct afe_sp_ex_vi_get_param *ex_vi)
  7817. {
  7818. struct param_hdr_v3 param_hdr;
  7819. int port = SLIMBUS_4_TX;
  7820. int ret = -EINVAL;
  7821. if (!ex_vi) {
  7822. pr_err("%s: Invalid params\n", __func__);
  7823. goto done;
  7824. }
  7825. if (this_afe.vi_tx_port != -1)
  7826. port = this_afe.vi_tx_port;
  7827. mutex_lock(&this_afe.afe_cmd_lock);
  7828. memset(&param_hdr, 0, sizeof(param_hdr));
  7829. param_hdr.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_EX_VI;
  7830. param_hdr.instance_id = INSTANCE_ID_0;
  7831. param_hdr.param_id = AFE_PARAM_ID_SP_V2_EX_VI_FTM_PARAMS;
  7832. param_hdr.param_size = sizeof(struct afe_sp_ex_vi_ftm_params);
  7833. ret = q6afe_get_params(port, NULL, &param_hdr);
  7834. if (ret < 0) {
  7835. pr_err("%s: get param port 0x%x param id[0x%x]failed %d\n",
  7836. __func__, port, param_hdr.param_id, ret);
  7837. goto get_params_fail;
  7838. }
  7839. ex_vi->pdata = param_hdr;
  7840. memcpy(&ex_vi->param, &this_afe.ex_vi_resp.param,
  7841. sizeof(this_afe.ex_vi_resp.param));
  7842. pr_debug("%s: freq %d %d resistance %d %d qfactor %d %d state %d %d\n",
  7843. __func__, ex_vi->param.freq_q20[SP_V2_SPKR_1],
  7844. ex_vi->param.freq_q20[SP_V2_SPKR_2],
  7845. ex_vi->param.resis_q24[SP_V2_SPKR_1],
  7846. ex_vi->param.resis_q24[SP_V2_SPKR_2],
  7847. ex_vi->param.qmct_q24[SP_V2_SPKR_1],
  7848. ex_vi->param.qmct_q24[SP_V2_SPKR_2],
  7849. ex_vi->param.status[SP_V2_SPKR_1],
  7850. ex_vi->param.status[SP_V2_SPKR_2]);
  7851. ret = 0;
  7852. get_params_fail:
  7853. mutex_unlock(&this_afe.afe_cmd_lock);
  7854. done:
  7855. return ret;
  7856. }
  7857. static int afe_get_sp_v4_rx_tmax_xmax_logging_data(
  7858. struct afe_sp_rx_tmax_xmax_logging_param *xt_logging,
  7859. u16 port_id)
  7860. {
  7861. struct param_hdr_v3 param_hdr;
  7862. int ret = -EINVAL;
  7863. struct afe_sp_v4_param_tmax_xmax_logging *tmax_xmax_logging;
  7864. struct afe_sp_v4_channel_tmax_xmax_params *tx_channel_params;
  7865. uint32_t i, size = 0;
  7866. if (!xt_logging) {
  7867. pr_err("%s: Invalid params\n", __func__);
  7868. goto done;
  7869. }
  7870. size = sizeof(struct afe_sp_v4_param_tmax_xmax_logging) +
  7871. (SP_V2_NUM_MAX_SPKRS *
  7872. sizeof(struct afe_sp_v4_channel_tmax_xmax_params));
  7873. memset(&param_hdr, 0, sizeof(param_hdr));
  7874. param_hdr.module_id = AFE_MODULE_SPEAKER_PROTECTION_V4_RX;
  7875. param_hdr.instance_id = INSTANCE_ID_0;
  7876. param_hdr.param_id = AFE_PARAM_ID_SP_V4_RX_TMAX_XMAX_LOGGING;
  7877. param_hdr.param_size = size;
  7878. ret = q6afe_get_params(port_id, NULL, &param_hdr);
  7879. if (ret) {
  7880. pr_err("%s: Failed to get Tmax Xmax logging data\n", __func__);
  7881. goto get_params_fail;
  7882. }
  7883. tmax_xmax_logging = (struct afe_sp_v4_param_tmax_xmax_logging *)
  7884. ((u8 *)this_afe.spv4_get_param_resp_ptr +
  7885. sizeof(struct afe_sp_v4_gen_get_param_resp));
  7886. tx_channel_params = (struct afe_sp_v4_channel_tmax_xmax_params *)
  7887. ((u8 *)tmax_xmax_logging +
  7888. sizeof(struct afe_sp_v4_param_tmax_xmax_logging));
  7889. for (i = 0; i < tmax_xmax_logging->num_ch; i++) {
  7890. xt_logging->max_excursion[i] =
  7891. tx_channel_params[i].max_excursion;
  7892. xt_logging->count_exceeded_excursion[i] =
  7893. tx_channel_params[i].count_exceeded_excursion;
  7894. xt_logging->max_temperature[i] =
  7895. tx_channel_params[i].max_temperature;
  7896. xt_logging->count_exceeded_temperature[i] =
  7897. tx_channel_params[i].count_exceeded_temperature;
  7898. }
  7899. ret = 0;
  7900. get_params_fail:
  7901. kfree(this_afe.spv4_get_param_resp_ptr);
  7902. done:
  7903. return ret;
  7904. }
  7905. /**
  7906. * afe_get_sp_rx_tmax_xmax_logging_data -
  7907. * command to get excursion logging data from DSP
  7908. *
  7909. * @xt_logging: excursion logging params
  7910. * @port: AFE port ID
  7911. *
  7912. * Returns 0 on success or error on failure
  7913. */
  7914. int afe_get_sp_rx_tmax_xmax_logging_data(
  7915. struct afe_sp_rx_tmax_xmax_logging_param *xt_logging,
  7916. u16 port_id)
  7917. {
  7918. struct param_hdr_v3 param_hdr;
  7919. int ret = -EINVAL;
  7920. if (!xt_logging) {
  7921. pr_err("%s: Invalid params\n", __func__);
  7922. goto done;
  7923. }
  7924. mutex_lock(&this_afe.afe_cmd_lock);
  7925. if (q6core_get_avcs_api_version_per_service(
  7926. APRV2_IDS_SERVICE_ID_ADSP_AFE_V) >= AFE_API_VERSION_V9) {
  7927. ret = afe_get_sp_v4_rx_tmax_xmax_logging_data(xt_logging,
  7928. port_id);
  7929. } else {
  7930. memset(&param_hdr, 0, sizeof(param_hdr));
  7931. param_hdr.module_id = AFE_MODULE_FB_SPKR_PROT_V2_RX;
  7932. param_hdr.instance_id = INSTANCE_ID_0;
  7933. param_hdr.param_id = AFE_PARAM_ID_SP_RX_TMAX_XMAX_LOGGING;
  7934. param_hdr.param_size =
  7935. sizeof(struct afe_sp_rx_tmax_xmax_logging_param);
  7936. ret = q6afe_get_params(port_id, NULL, &param_hdr);
  7937. if (ret < 0) {
  7938. pr_err(
  7939. "%s: get param port 0x%x param id[0x%x]failed %d\n",
  7940. __func__, port_id, param_hdr.param_id, ret);
  7941. goto get_params_fail;
  7942. }
  7943. memcpy(xt_logging, &this_afe.xt_logging_resp.param,
  7944. sizeof(this_afe.xt_logging_resp.param));
  7945. }
  7946. pr_debug("%s: max_excursion %d %d count_exceeded_excursion %d %d"
  7947. " max_temperature %d %d count_exceeded_temperature %d %d\n",
  7948. __func__, xt_logging->max_excursion[SP_V2_SPKR_1],
  7949. xt_logging->max_excursion[SP_V2_SPKR_2],
  7950. xt_logging->count_exceeded_excursion[SP_V2_SPKR_1],
  7951. xt_logging->count_exceeded_excursion[SP_V2_SPKR_2],
  7952. xt_logging->max_temperature[SP_V2_SPKR_1],
  7953. xt_logging->max_temperature[SP_V2_SPKR_2],
  7954. xt_logging->count_exceeded_temperature[SP_V2_SPKR_1],
  7955. xt_logging->count_exceeded_temperature[SP_V2_SPKR_2]);
  7956. get_params_fail:
  7957. mutex_unlock(&this_afe.afe_cmd_lock);
  7958. done:
  7959. return ret;
  7960. }
  7961. EXPORT_SYMBOL(afe_get_sp_rx_tmax_xmax_logging_data);
  7962. /**
  7963. * afe_get_av_dev_drift -
  7964. * command to retrieve AV drift
  7965. *
  7966. * @timing_stats: timing stats to be updated with AV drift values
  7967. * @port: AFE port ID
  7968. *
  7969. * Returns 0 on success or error on failure
  7970. */
  7971. int afe_get_av_dev_drift(struct afe_param_id_dev_timing_stats *timing_stats,
  7972. u16 port)
  7973. {
  7974. struct param_hdr_v3 param_hdr;
  7975. int ret = -EINVAL;
  7976. if (!timing_stats) {
  7977. pr_err("%s: Invalid params\n", __func__);
  7978. goto exit;
  7979. }
  7980. mutex_lock(&this_afe.afe_cmd_lock);
  7981. memset(&param_hdr, 0, sizeof(param_hdr));
  7982. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  7983. param_hdr.instance_id = INSTANCE_ID_0;
  7984. param_hdr.param_id = AFE_PARAM_ID_DEV_TIMING_STATS;
  7985. param_hdr.param_size = sizeof(struct afe_param_id_dev_timing_stats);
  7986. ret = q6afe_get_params(port, NULL, &param_hdr);
  7987. if (ret < 0) {
  7988. pr_err("%s: get param port 0x%x param id[0x%x] failed %d\n",
  7989. __func__, port, param_hdr.param_id, ret);
  7990. goto get_params_fail;
  7991. }
  7992. memcpy(timing_stats, &this_afe.av_dev_drift_resp.timing_stats,
  7993. param_hdr.param_size);
  7994. ret = 0;
  7995. get_params_fail:
  7996. mutex_unlock(&this_afe.afe_cmd_lock);
  7997. exit:
  7998. return ret;
  7999. }
  8000. EXPORT_SYMBOL(afe_get_av_dev_drift);
  8001. /**
  8002. * afe_get_doa_tracking_mon -
  8003. * command to retrieve doa tracking monitor data
  8004. *
  8005. * @port: AFE port ID
  8006. * @doa_tracking_data: param to be updated with doa tracking data
  8007. *
  8008. * Returns 0 on success or error on failure
  8009. */
  8010. int afe_get_doa_tracking_mon(u16 port,
  8011. struct doa_tracking_mon_param *doa_tracking_data)
  8012. {
  8013. struct param_hdr_v3 param_hdr;
  8014. int ret = -EINVAL, i = 0;
  8015. if (!doa_tracking_data) {
  8016. pr_err("%s: Invalid params\n", __func__);
  8017. goto exit;
  8018. }
  8019. mutex_lock(&this_afe.afe_cmd_lock);
  8020. memset(&param_hdr, 0, sizeof(param_hdr));
  8021. param_hdr.module_id = AUDPROC_MODULE_ID_FFNS;
  8022. param_hdr.instance_id = INSTANCE_ID_0;
  8023. param_hdr.param_id = AUDPROC_PARAM_ID_FFV_DOA_TRACKING_MONITOR;
  8024. param_hdr.param_size = sizeof(struct doa_tracking_mon_param);
  8025. ret = q6afe_get_params(port, NULL, &param_hdr);
  8026. if (ret < 0) {
  8027. pr_err("%s: get param port 0x%x param id[0x%x] failed %d\n",
  8028. __func__, port, param_hdr.param_id, ret);
  8029. goto get_params_fail;
  8030. }
  8031. memcpy(doa_tracking_data, &this_afe.doa_tracking_mon_resp.doa,
  8032. param_hdr.param_size);
  8033. for (i = 0; i < MAX_DOA_TRACKING_ANGLES; i++) {
  8034. pr_debug("%s: target angle[%d] = %d\n",
  8035. __func__, i, doa_tracking_data->target_angle_L16[i]);
  8036. pr_debug("%s: interference angle[%d] = %d\n",
  8037. __func__, i, doa_tracking_data->interf_angle_L16[i]);
  8038. }
  8039. get_params_fail:
  8040. mutex_unlock(&this_afe.afe_cmd_lock);
  8041. exit:
  8042. return ret;
  8043. }
  8044. EXPORT_SYMBOL(afe_get_doa_tracking_mon);
  8045. static int afe_spv4_get_calib_data(
  8046. struct afe_sp_v4_th_vi_calib_resp *calib_resp)
  8047. {
  8048. struct param_hdr_v3 param_hdr;
  8049. int port = SLIMBUS_4_TX;
  8050. int ret = -EINVAL;
  8051. if (!calib_resp) {
  8052. pr_err("%s: Invalid params\n", __func__);
  8053. goto fail_cmd;
  8054. }
  8055. if (this_afe.vi_tx_port != -1)
  8056. port = this_afe.vi_tx_port;
  8057. mutex_lock(&this_afe.afe_cmd_lock);
  8058. memset(&param_hdr, 0, sizeof(param_hdr));
  8059. param_hdr.module_id = AFE_MODULE_SPEAKER_PROTECTION_V4_VI;
  8060. param_hdr.instance_id = INSTANCE_ID_0;
  8061. param_hdr.param_id = AFE_PARAM_ID_SP_V4_CALIB_RES_CFG;
  8062. param_hdr.param_size = sizeof(struct afe_sp_v4_th_vi_calib_resp);
  8063. ret = q6afe_get_params(port, NULL, &param_hdr);
  8064. if (ret < 0) {
  8065. pr_err("%s: get param port 0x%x param id[0x%x]failed %d\n",
  8066. __func__, port, param_hdr.param_id, ret);
  8067. goto get_params_fail;
  8068. }
  8069. memcpy(&calib_resp->res_cfg, &this_afe.spv4_calib_data.res_cfg,
  8070. sizeof(this_afe.calib_data.res_cfg));
  8071. pr_info("%s: state %s resistance %d %d\n", __func__,
  8072. fbsp_state[calib_resp->res_cfg.th_vi_ca_state],
  8073. calib_resp->res_cfg.r0_cali_q24[SP_V2_SPKR_1],
  8074. calib_resp->res_cfg.r0_cali_q24[SP_V2_SPKR_2]);
  8075. ret = 0;
  8076. get_params_fail:
  8077. mutex_unlock(&this_afe.afe_cmd_lock);
  8078. fail_cmd:
  8079. return ret;
  8080. }
  8081. int afe_spk_prot_get_calib_data(struct afe_spkr_prot_get_vi_calib *calib_resp)
  8082. {
  8083. struct param_hdr_v3 param_hdr;
  8084. int port = SLIMBUS_4_TX;
  8085. int ret = -EINVAL;
  8086. if (!calib_resp) {
  8087. pr_err("%s: Invalid params\n", __func__);
  8088. goto fail_cmd;
  8089. }
  8090. if (this_afe.vi_tx_port != -1)
  8091. port = this_afe.vi_tx_port;
  8092. mutex_lock(&this_afe.afe_cmd_lock);
  8093. memset(&param_hdr, 0, sizeof(param_hdr));
  8094. param_hdr.module_id = AFE_MODULE_FB_SPKR_PROT_VI_PROC_V2;
  8095. param_hdr.instance_id = INSTANCE_ID_0;
  8096. param_hdr.param_id = AFE_PARAM_ID_CALIB_RES_CFG_V2;
  8097. param_hdr.param_size = sizeof(struct afe_spkr_prot_get_vi_calib);
  8098. ret = q6afe_get_params(port, NULL, &param_hdr);
  8099. if (ret < 0) {
  8100. pr_err("%s: get param port 0x%x param id[0x%x]failed %d\n",
  8101. __func__, port, param_hdr.param_id, ret);
  8102. goto get_params_fail;
  8103. }
  8104. memcpy(&calib_resp->res_cfg, &this_afe.calib_data.res_cfg,
  8105. sizeof(this_afe.calib_data.res_cfg));
  8106. pr_info("%s: state %s resistance %d %d\n", __func__,
  8107. fbsp_state[calib_resp->res_cfg.th_vi_ca_state],
  8108. calib_resp->res_cfg.r0_cali_q24[SP_V2_SPKR_1],
  8109. calib_resp->res_cfg.r0_cali_q24[SP_V2_SPKR_2]);
  8110. ret = 0;
  8111. get_params_fail:
  8112. mutex_unlock(&this_afe.afe_cmd_lock);
  8113. fail_cmd:
  8114. return ret;
  8115. }
  8116. /**
  8117. * afe_spk_prot_feed_back_cfg -
  8118. * command to setup spk protection feedback config
  8119. *
  8120. * @src_port: source port id
  8121. * @dst_port: destination port id
  8122. * @l_ch: left speaker active or not
  8123. * @r_ch: right speaker active or not
  8124. * @enable: flag to enable or disable
  8125. *
  8126. * Returns 0 on success or error on failure
  8127. */
  8128. int afe_spk_prot_feed_back_cfg(int src_port, int dst_port,
  8129. int l_ch, int r_ch, u32 enable)
  8130. {
  8131. int ret = -EINVAL;
  8132. union afe_spkr_prot_config prot_config;
  8133. int index = 0;
  8134. if (!enable) {
  8135. pr_debug("%s: Disable Feedback tx path", __func__);
  8136. this_afe.vi_tx_port = -1;
  8137. this_afe.vi_rx_port = -1;
  8138. return 0;
  8139. }
  8140. if ((q6audio_validate_port(src_port) < 0) ||
  8141. (q6audio_validate_port(dst_port) < 0)) {
  8142. pr_err("%s: invalid ports src 0x%x dst 0x%x",
  8143. __func__, src_port, dst_port);
  8144. goto fail_cmd;
  8145. }
  8146. if (!l_ch && !r_ch) {
  8147. pr_err("%s: error ch values zero\n", __func__);
  8148. goto fail_cmd;
  8149. }
  8150. pr_debug("%s: src_port 0x%x dst_port 0x%x l_ch %d r_ch %d\n",
  8151. __func__, src_port, dst_port, l_ch, r_ch);
  8152. if (q6core_get_avcs_api_version_per_service(
  8153. APRV2_IDS_SERVICE_ID_ADSP_AFE_V) >= AFE_API_VERSION_V9) {
  8154. if (l_ch) {
  8155. this_afe.v4_ch_map_cfg.chan_info[index++] = 1;
  8156. this_afe.v4_ch_map_cfg.chan_info[index++] = 2;
  8157. }
  8158. if (r_ch) {
  8159. this_afe.v4_ch_map_cfg.chan_info[index++] = 3;
  8160. this_afe.v4_ch_map_cfg.chan_info[index++] = 4;
  8161. }
  8162. this_afe.v4_ch_map_cfg.num_channels = index;
  8163. pr_debug("%s no of channels: %d\n", __func__, index);
  8164. this_afe.vi_tx_port = src_port;
  8165. this_afe.vi_rx_port = dst_port;
  8166. ret = 0;
  8167. } else {
  8168. memset(&prot_config, 0, sizeof(prot_config));
  8169. prot_config.feedback_path_cfg.dst_portid =
  8170. q6audio_get_port_id(dst_port);
  8171. if (l_ch) {
  8172. prot_config.feedback_path_cfg.chan_info[index++] = 1;
  8173. prot_config.feedback_path_cfg.chan_info[index++] = 2;
  8174. }
  8175. if (r_ch) {
  8176. prot_config.feedback_path_cfg.chan_info[index++] = 3;
  8177. prot_config.feedback_path_cfg.chan_info[index++] = 4;
  8178. }
  8179. prot_config.feedback_path_cfg.num_channels = index;
  8180. pr_debug("%s no of channels: %d\n", __func__, index);
  8181. prot_config.feedback_path_cfg.minor_version = 1;
  8182. ret = afe_spk_prot_prepare(src_port, dst_port,
  8183. AFE_PARAM_ID_FEEDBACK_PATH_CFG, &prot_config,
  8184. sizeof(union afe_spkr_prot_config));
  8185. }
  8186. fail_cmd:
  8187. return ret;
  8188. }
  8189. EXPORT_SYMBOL(afe_spk_prot_feed_back_cfg);
  8190. static int get_cal_type_index(int32_t cal_type)
  8191. {
  8192. int ret = -EINVAL;
  8193. switch (cal_type) {
  8194. case AFE_COMMON_RX_CAL_TYPE:
  8195. ret = AFE_COMMON_RX_CAL;
  8196. break;
  8197. case AFE_COMMON_TX_CAL_TYPE:
  8198. ret = AFE_COMMON_TX_CAL;
  8199. break;
  8200. case AFE_LSM_TX_CAL_TYPE:
  8201. ret = AFE_LSM_TX_CAL;
  8202. break;
  8203. case AFE_AANC_CAL_TYPE:
  8204. ret = AFE_AANC_CAL;
  8205. break;
  8206. case AFE_HW_DELAY_CAL_TYPE:
  8207. ret = AFE_HW_DELAY_CAL;
  8208. break;
  8209. case AFE_FB_SPKR_PROT_CAL_TYPE:
  8210. ret = AFE_FB_SPKR_PROT_CAL;
  8211. break;
  8212. case AFE_SIDETONE_CAL_TYPE:
  8213. ret = AFE_SIDETONE_CAL;
  8214. break;
  8215. case AFE_SIDETONE_IIR_CAL_TYPE:
  8216. ret = AFE_SIDETONE_IIR_CAL;
  8217. break;
  8218. case AFE_TOPOLOGY_CAL_TYPE:
  8219. ret = AFE_TOPOLOGY_CAL;
  8220. break;
  8221. case AFE_LSM_TOPOLOGY_CAL_TYPE:
  8222. ret = AFE_LSM_TOPOLOGY_CAL;
  8223. break;
  8224. case AFE_CUST_TOPOLOGY_CAL_TYPE:
  8225. ret = AFE_CUST_TOPOLOGY_CAL;
  8226. break;
  8227. default:
  8228. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  8229. }
  8230. return ret;
  8231. }
  8232. int afe_alloc_cal(int32_t cal_type, size_t data_size,
  8233. void *data)
  8234. {
  8235. int ret = 0;
  8236. int cal_index;
  8237. cal_index = get_cal_type_index(cal_type);
  8238. pr_debug("%s: cal_type = %d cal_index = %d\n",
  8239. __func__, cal_type, cal_index);
  8240. if (cal_index < 0) {
  8241. pr_err("%s: could not get cal index %d!\n",
  8242. __func__, cal_index);
  8243. ret = -EINVAL;
  8244. goto done;
  8245. }
  8246. mutex_lock(&this_afe.afe_cmd_lock);
  8247. ret = cal_utils_alloc_cal(data_size, data,
  8248. this_afe.cal_data[cal_index], 0, NULL);
  8249. if (ret < 0) {
  8250. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  8251. __func__, ret, cal_type);
  8252. ret = -EINVAL;
  8253. mutex_unlock(&this_afe.afe_cmd_lock);
  8254. goto done;
  8255. }
  8256. mutex_unlock(&this_afe.afe_cmd_lock);
  8257. done:
  8258. return ret;
  8259. }
  8260. static int afe_dealloc_cal(int32_t cal_type, size_t data_size,
  8261. void *data)
  8262. {
  8263. int ret = 0;
  8264. int cal_index;
  8265. pr_debug("%s:\n", __func__);
  8266. cal_index = get_cal_type_index(cal_type);
  8267. if (cal_index < 0) {
  8268. pr_err("%s: could not get cal index %d!\n",
  8269. __func__, cal_index);
  8270. ret = -EINVAL;
  8271. goto done;
  8272. }
  8273. ret = cal_utils_dealloc_cal(data_size, data,
  8274. this_afe.cal_data[cal_index]);
  8275. if (ret < 0) {
  8276. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  8277. __func__, ret, cal_type);
  8278. ret = -EINVAL;
  8279. goto done;
  8280. }
  8281. done:
  8282. return ret;
  8283. }
  8284. static int afe_set_cal(int32_t cal_type, size_t data_size,
  8285. void *data)
  8286. {
  8287. int ret = 0;
  8288. int cal_index;
  8289. pr_debug("%s:\n", __func__);
  8290. cal_index = get_cal_type_index(cal_type);
  8291. if (cal_index < 0) {
  8292. pr_err("%s: could not get cal index %d!\n",
  8293. __func__, cal_index);
  8294. ret = -EINVAL;
  8295. goto done;
  8296. }
  8297. ret = cal_utils_set_cal(data_size, data,
  8298. this_afe.cal_data[cal_index], 0, NULL);
  8299. if (ret < 0) {
  8300. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  8301. __func__, ret, cal_type);
  8302. ret = -EINVAL;
  8303. goto done;
  8304. }
  8305. if (cal_index == AFE_CUST_TOPOLOGY_CAL) {
  8306. mutex_lock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  8307. this_afe.set_custom_topology = 1;
  8308. pr_debug("%s:[AFE_CUSTOM_TOPOLOGY] ret = %d, cal type = %d!\n",
  8309. __func__, ret, cal_type);
  8310. mutex_unlock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  8311. }
  8312. done:
  8313. return ret;
  8314. }
  8315. static struct cal_block_data *afe_find_hw_delay_by_path(
  8316. struct cal_type_data *cal_type, int path)
  8317. {
  8318. struct list_head *ptr, *next;
  8319. struct cal_block_data *cal_block = NULL;
  8320. pr_debug("%s:\n", __func__);
  8321. list_for_each_safe(ptr, next,
  8322. &cal_type->cal_blocks) {
  8323. cal_block = list_entry(ptr,
  8324. struct cal_block_data, list);
  8325. if (cal_utils_is_cal_stale(cal_block))
  8326. continue;
  8327. if (((struct audio_cal_info_hw_delay *)cal_block->cal_info)
  8328. ->path == path) {
  8329. return cal_block;
  8330. }
  8331. }
  8332. return NULL;
  8333. }
  8334. static int afe_get_cal_hw_delay(int32_t path,
  8335. struct audio_cal_hw_delay_entry *entry)
  8336. {
  8337. int ret = 0;
  8338. int i;
  8339. struct cal_block_data *cal_block = NULL;
  8340. struct audio_cal_hw_delay_data *hw_delay_info = NULL;
  8341. pr_debug("%s:\n", __func__);
  8342. if (this_afe.cal_data[AFE_HW_DELAY_CAL] == NULL) {
  8343. pr_err("%s: AFE_HW_DELAY_CAL not initialized\n", __func__);
  8344. ret = -EINVAL;
  8345. goto done;
  8346. }
  8347. if (entry == NULL) {
  8348. pr_err("%s: entry is NULL\n", __func__);
  8349. ret = -EINVAL;
  8350. goto done;
  8351. }
  8352. if ((path >= MAX_PATH_TYPE) || (path < 0)) {
  8353. pr_err("%s: bad path: %d\n",
  8354. __func__, path);
  8355. ret = -EINVAL;
  8356. goto done;
  8357. }
  8358. mutex_lock(&this_afe.cal_data[AFE_HW_DELAY_CAL]->lock);
  8359. cal_block = afe_find_hw_delay_by_path(
  8360. this_afe.cal_data[AFE_HW_DELAY_CAL], path);
  8361. if (cal_block == NULL)
  8362. goto unlock;
  8363. hw_delay_info = &((struct audio_cal_info_hw_delay *)
  8364. cal_block->cal_info)->data;
  8365. if (hw_delay_info->num_entries > MAX_HW_DELAY_ENTRIES) {
  8366. pr_err("%s: invalid num entries: %d\n",
  8367. __func__, hw_delay_info->num_entries);
  8368. ret = -EINVAL;
  8369. goto unlock;
  8370. }
  8371. for (i = 0; i < hw_delay_info->num_entries; i++) {
  8372. if (hw_delay_info->entry[i].sample_rate ==
  8373. entry->sample_rate) {
  8374. entry->delay_usec = hw_delay_info->entry[i].delay_usec;
  8375. break;
  8376. }
  8377. }
  8378. if (i == hw_delay_info->num_entries) {
  8379. pr_err("%s: Unable to find delay for sample rate %d\n",
  8380. __func__, entry->sample_rate);
  8381. ret = -EFAULT;
  8382. goto unlock;
  8383. }
  8384. cal_utils_mark_cal_used(cal_block);
  8385. pr_debug("%s: Path = %d samplerate = %u usec = %u status %d\n",
  8386. __func__, path, entry->sample_rate, entry->delay_usec, ret);
  8387. unlock:
  8388. mutex_unlock(&this_afe.cal_data[AFE_HW_DELAY_CAL]->lock);
  8389. done:
  8390. return ret;
  8391. }
  8392. static int afe_set_cal_sp_th_vi_v_vali_cfg(int32_t cal_type, size_t data_size,
  8393. void *data)
  8394. {
  8395. int ret = 0;
  8396. struct audio_cal_type_sp_th_vi_v_vali_cfg *cal_data = data;
  8397. if (cal_data == NULL || data_size != sizeof(*cal_data))
  8398. goto done;
  8399. memcpy(&this_afe.v_vali_cfg, &cal_data->cal_info,
  8400. sizeof(this_afe.v_vali_cfg));
  8401. done:
  8402. return ret;
  8403. }
  8404. static int afe_set_cal_sp_th_vi_ftm_cfg(int32_t cal_type, size_t data_size,
  8405. void *data)
  8406. {
  8407. int ret = 0;
  8408. struct audio_cal_type_sp_th_vi_ftm_cfg *cal_data = data;
  8409. if (cal_data == NULL || data_size != sizeof(*cal_data))
  8410. goto done;
  8411. memcpy(&this_afe.th_ftm_cfg, &cal_data->cal_info,
  8412. sizeof(this_afe.th_ftm_cfg));
  8413. done:
  8414. return ret;
  8415. }
  8416. static int afe_set_cal_sp_th_vi_cfg(int32_t cal_type, size_t data_size,
  8417. void *data)
  8418. {
  8419. int ret = 0;
  8420. struct audio_cal_type_sp_th_vi_ftm_cfg *cal_data = data;
  8421. uint32_t mode;
  8422. if (cal_data == NULL ||
  8423. data_size > sizeof(*cal_data) ||
  8424. this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL)
  8425. goto done;
  8426. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  8427. mode = cal_data->cal_info.mode;
  8428. pr_debug("%s: cal_type = %d, mode = %d\n", __func__, cal_type, mode);
  8429. if (mode == MSM_SPKR_PROT_IN_FTM_MODE) {
  8430. ret = afe_set_cal_sp_th_vi_ftm_cfg(cal_type,
  8431. data_size, data);
  8432. } else if (mode == MSM_SPKR_PROT_IN_V_VALI_MODE) {
  8433. ret = afe_set_cal_sp_th_vi_v_vali_cfg(cal_type,
  8434. data_size, data);
  8435. }
  8436. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  8437. done:
  8438. return ret;
  8439. }
  8440. static int afe_set_cal_sp_ex_vi_ftm_cfg(int32_t cal_type, size_t data_size,
  8441. void *data)
  8442. {
  8443. int ret = 0;
  8444. struct audio_cal_type_sp_ex_vi_ftm_cfg *cal_data = data;
  8445. if (this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL] == NULL ||
  8446. cal_data == NULL ||
  8447. data_size != sizeof(*cal_data))
  8448. goto done;
  8449. pr_debug("%s: cal_type = %d\n", __func__, cal_type);
  8450. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  8451. memcpy(&this_afe.ex_ftm_cfg, &cal_data->cal_info,
  8452. sizeof(this_afe.ex_ftm_cfg));
  8453. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  8454. done:
  8455. return ret;
  8456. }
  8457. static int afe_set_cal_fb_spkr_prot(int32_t cal_type, size_t data_size,
  8458. void *data)
  8459. {
  8460. int ret = 0;
  8461. struct audio_cal_type_fb_spk_prot_cfg *cal_data = data;
  8462. pr_debug("%s:\n", __func__);
  8463. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL)
  8464. goto done;
  8465. if (cal_data == NULL)
  8466. goto done;
  8467. if (data_size != sizeof(*cal_data))
  8468. goto done;
  8469. if (cal_data->cal_info.mode == MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS)
  8470. __pm_wakeup_event(wl.ws, jiffies_to_msecs(WAKELOCK_TIMEOUT));
  8471. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  8472. memcpy(&this_afe.prot_cfg, &cal_data->cal_info,
  8473. sizeof(this_afe.prot_cfg));
  8474. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  8475. done:
  8476. return ret;
  8477. }
  8478. static int afe_get_cal_sp_th_vi_v_vali_param(int32_t cal_type, size_t data_size,
  8479. void *data)
  8480. {
  8481. int i, ret = 0;
  8482. struct audio_cal_type_sp_th_vi_v_vali_param *cal_data = data;
  8483. struct afe_sp_th_vi_v_vali_get_param th_vi_v_vali;
  8484. uint32_t size;
  8485. void *params = NULL;
  8486. struct afe_sp_v4_channel_v_vali_params *v_vali_params;
  8487. if (this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL ||
  8488. cal_data == NULL ||
  8489. data_size != sizeof(*cal_data))
  8490. goto done;
  8491. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  8492. cal_data->cal_info.status[i] = -EINVAL;
  8493. cal_data->cal_info.vrms_q24[i] = -1;
  8494. }
  8495. if (q6core_get_avcs_api_version_per_service(
  8496. APRV2_IDS_SERVICE_ID_ADSP_AFE_V) >= AFE_API_VERSION_V9) {
  8497. size = sizeof(struct afe_sp_v4_param_th_vi_v_vali_params) +
  8498. (SP_V2_NUM_MAX_SPKRS *
  8499. sizeof(struct afe_sp_v4_channel_v_vali_params));
  8500. params = kzalloc(size, GFP_KERNEL);
  8501. if (!params)
  8502. return -ENOMEM;
  8503. v_vali_params =
  8504. (struct afe_sp_v4_channel_v_vali_params *)((u8 *)params +
  8505. sizeof(struct afe_sp_v4_param_th_vi_v_vali_params));
  8506. if (!afe_get_spv4_th_vi_v_vali_data(params, size)) {
  8507. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  8508. pr_debug("%s: ftm param status = %d\n",
  8509. __func__, v_vali_params[i].status);
  8510. if (v_vali_params[i].status ==
  8511. V_VALI_IN_PROGRESS) {
  8512. cal_data->cal_info.status[i] = -EAGAIN;
  8513. } else if (v_vali_params[i].status ==
  8514. V_VALI_SUCCESS) {
  8515. cal_data->cal_info.status[i] =
  8516. V_VALI_SUCCESS;
  8517. cal_data->cal_info.vrms_q24[i] =
  8518. v_vali_params[i].vrms_q24;
  8519. }
  8520. }
  8521. }
  8522. kfree(params);
  8523. } else {
  8524. if (!afe_get_sp_th_vi_v_vali_data(&th_vi_v_vali)) {
  8525. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  8526. pr_debug(
  8527. "%s: v-vali param status = %d\n",
  8528. __func__, th_vi_v_vali.param.status[i]);
  8529. if (th_vi_v_vali.param.status[i] ==
  8530. V_VALI_IN_PROGRESS) {
  8531. cal_data->cal_info.status[i] = -EAGAIN;
  8532. } else if (th_vi_v_vali.param.status[i] ==
  8533. V_VALI_SUCCESS) {
  8534. cal_data->cal_info.status[i] =
  8535. V_VALI_SUCCESS;
  8536. cal_data->cal_info.vrms_q24[i] =
  8537. th_vi_v_vali.param.vrms_q24[i];
  8538. }
  8539. }
  8540. }
  8541. }
  8542. this_afe.v_vali_flag = 0;
  8543. done:
  8544. return ret;
  8545. }
  8546. static int afe_get_cal_sp_th_vi_ftm_param(int32_t cal_type, size_t data_size,
  8547. void *data)
  8548. {
  8549. int i, ret = 0;
  8550. struct audio_cal_type_sp_th_vi_param *cal_data = data;
  8551. struct afe_sp_th_vi_get_param th_vi;
  8552. uint32_t size;
  8553. void *params = NULL;
  8554. struct afe_sp_v4_channel_ftm_params *th_vi_ftm_params = NULL;
  8555. if (this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL ||
  8556. cal_data == NULL ||
  8557. data_size != sizeof(*cal_data))
  8558. goto done;
  8559. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  8560. cal_data->cal_info.status[i] = -EINVAL;
  8561. cal_data->cal_info.r_dc_q24[i] = -1;
  8562. cal_data->cal_info.temp_q22[i] = -1;
  8563. }
  8564. if (q6core_get_avcs_api_version_per_service(
  8565. APRV2_IDS_SERVICE_ID_ADSP_AFE_V) >= AFE_API_VERSION_V9) {
  8566. size = sizeof(struct afe_sp_v4_param_th_vi_ftm_params) +
  8567. (SP_V2_NUM_MAX_SPKRS *
  8568. sizeof(struct afe_sp_v4_channel_ftm_params));
  8569. params = kzalloc(size, GFP_KERNEL);
  8570. if (!params)
  8571. return -ENOMEM;
  8572. th_vi_ftm_params = (struct afe_sp_v4_channel_ftm_params *)
  8573. ((u8 *)params +
  8574. sizeof(struct afe_sp_v4_param_th_vi_ftm_params));
  8575. if (!afe_get_spv4_th_vi_ftm_data(params, size)) {
  8576. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  8577. pr_debug("%s: SP V4 ftm param status = %d\n",
  8578. __func__, th_vi_ftm_params[i].status);
  8579. if (th_vi_ftm_params[i].status ==
  8580. FBSP_IN_PROGRESS) {
  8581. cal_data->cal_info.status[i] = -EAGAIN;
  8582. } else if (th_vi_ftm_params[i].status ==
  8583. FBSP_SUCCESS) {
  8584. cal_data->cal_info.status[i] = 0;
  8585. cal_data->cal_info.r_dc_q24[i] =
  8586. th_vi_ftm_params[i].dc_res_q24;
  8587. cal_data->cal_info.temp_q22[i] =
  8588. th_vi_ftm_params[i].temp_q22;
  8589. }
  8590. }
  8591. }
  8592. kfree(params);
  8593. } else {
  8594. if (!afe_get_sp_th_vi_ftm_data(&th_vi)) {
  8595. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  8596. pr_debug("%s: ftm param status = %d\n",
  8597. __func__, th_vi.param.status[i]);
  8598. if (th_vi.param.status[i] == FBSP_IN_PROGRESS) {
  8599. cal_data->cal_info.status[i] = -EAGAIN;
  8600. } else if (th_vi.param.status[i] ==
  8601. FBSP_SUCCESS) {
  8602. cal_data->cal_info.status[i] = 0;
  8603. cal_data->cal_info.r_dc_q24[i] =
  8604. th_vi.param.dc_res_q24[i];
  8605. cal_data->cal_info.temp_q22[i] =
  8606. th_vi.param.temp_q22[i];
  8607. }
  8608. }
  8609. }
  8610. }
  8611. done:
  8612. return ret;
  8613. }
  8614. static int afe_get_cal_sp_th_vi_param(int32_t cal_type, size_t data_size,
  8615. void *data)
  8616. {
  8617. struct audio_cal_type_sp_th_vi_param *cal_data = data;
  8618. uint32_t mode;
  8619. int ret = 0;
  8620. if (cal_data == NULL ||
  8621. data_size > sizeof(*cal_data) ||
  8622. this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL)
  8623. return 0;
  8624. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  8625. mode = cal_data->cal_info.mode;
  8626. pr_debug("%s: cal_type = %d,mode = %d\n", __func__, cal_type, mode);
  8627. if (mode == MSM_SPKR_PROT_IN_V_VALI_MODE)
  8628. ret = afe_get_cal_sp_th_vi_v_vali_param(cal_type,
  8629. data_size, data);
  8630. else
  8631. ret = afe_get_cal_sp_th_vi_ftm_param(cal_type,
  8632. data_size, data);
  8633. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  8634. return ret;
  8635. }
  8636. static int afe_get_cal_spv4_ex_vi_ftm_param(int32_t cal_type, size_t data_size,
  8637. void *data)
  8638. {
  8639. int i, ret = 0;
  8640. struct audio_cal_type_sp_v4_ex_vi_param *cal_data = data;
  8641. uint32_t size;
  8642. void *params = NULL;
  8643. struct afe_sp_v4_channel_ex_vi_ftm_params *ex_vi_ftm_param;
  8644. pr_debug("%s: cal_type = %d\n", __func__, cal_type);
  8645. if (this_afe.cal_data[AFE_FB_SPKR_PROT_V4_EX_VI_CAL] == NULL ||
  8646. cal_data == NULL ||
  8647. data_size != sizeof(*cal_data))
  8648. goto done;
  8649. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_V4_EX_VI_CAL]->lock);
  8650. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  8651. cal_data->cal_info.status[i] = -EINVAL;
  8652. cal_data->cal_info.ftm_re_q24[i] = -1;
  8653. cal_data->cal_info.ftm_re_q24[i] = -1;
  8654. cal_data->cal_info.ftm_Rms_q24[i] = -1;
  8655. cal_data->cal_info.ftm_Kms_q24[i] = -1;
  8656. cal_data->cal_info.ftm_freq_q20[i] = -1;
  8657. cal_data->cal_info.ftm_Qms_q24[i] = -1;
  8658. }
  8659. size = sizeof(struct afe_sp_v4_param_ex_vi_ftm_params) +
  8660. (SP_V2_NUM_MAX_SPKRS *
  8661. sizeof(struct afe_sp_v4_channel_ex_vi_ftm_params));
  8662. params = kzalloc(size, GFP_KERNEL);
  8663. if (!params) {
  8664. mutex_unlock(
  8665. &this_afe.cal_data[AFE_FB_SPKR_PROT_V4_EX_VI_CAL]->lock);
  8666. return -ENOMEM;
  8667. }
  8668. ex_vi_ftm_param = (struct afe_sp_v4_channel_ex_vi_ftm_params *)
  8669. ((u8 *)params +
  8670. sizeof(struct afe_sp_v4_param_ex_vi_ftm_params));
  8671. if (!afe_get_spv4_ex_vi_ftm_data(params, size)) {
  8672. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  8673. pr_debug("%s: ftm param status = %d\n",
  8674. __func__, ex_vi_ftm_param[i].status);
  8675. if (ex_vi_ftm_param[i].status == FBSP_IN_PROGRESS) {
  8676. cal_data->cal_info.status[i] = -EAGAIN;
  8677. } else if (ex_vi_ftm_param[i].status == FBSP_SUCCESS) {
  8678. cal_data->cal_info.status[i] = 0;
  8679. cal_data->cal_info.ftm_re_q24[i] =
  8680. ex_vi_ftm_param[i].ftm_re_q24;
  8681. cal_data->cal_info.ftm_Bl_q24[i] =
  8682. ex_vi_ftm_param[i].ftm_Bl_q24;
  8683. cal_data->cal_info.ftm_Rms_q24[i] =
  8684. ex_vi_ftm_param[i].ftm_Rms_q24;
  8685. cal_data->cal_info.ftm_Kms_q24[i] =
  8686. ex_vi_ftm_param[i].ftm_Kms_q24;
  8687. cal_data->cal_info.ftm_freq_q20[i] =
  8688. ex_vi_ftm_param[i].ftm_Fres_q20;
  8689. cal_data->cal_info.ftm_Qms_q24[i] =
  8690. ex_vi_ftm_param[i].ftm_Qms_q24;
  8691. }
  8692. }
  8693. }
  8694. kfree(params);
  8695. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_V4_EX_VI_CAL]->lock);
  8696. done:
  8697. return ret;
  8698. }
  8699. static int afe_get_cal_sp_ex_vi_ftm_param(int32_t cal_type, size_t data_size,
  8700. void *data)
  8701. {
  8702. int i, ret = 0;
  8703. struct audio_cal_type_sp_ex_vi_param *cal_data = data;
  8704. struct afe_sp_ex_vi_get_param ex_vi;
  8705. pr_debug("%s: cal_type = %d\n", __func__, cal_type);
  8706. if (this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL] == NULL ||
  8707. cal_data == NULL ||
  8708. data_size != sizeof(*cal_data))
  8709. goto done;
  8710. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  8711. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  8712. cal_data->cal_info.status[i] = -EINVAL;
  8713. cal_data->cal_info.freq_q20[i] = -1;
  8714. cal_data->cal_info.resis_q24[i] = -1;
  8715. cal_data->cal_info.qmct_q24[i] = -1;
  8716. }
  8717. if (!afe_get_sp_ex_vi_ftm_data(&ex_vi)) {
  8718. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  8719. pr_debug("%s: ftm param status = %d\n",
  8720. __func__, ex_vi.param.status[i]);
  8721. if (ex_vi.param.status[i] == FBSP_IN_PROGRESS) {
  8722. cal_data->cal_info.status[i] = -EAGAIN;
  8723. } else if (ex_vi.param.status[i] == FBSP_SUCCESS) {
  8724. cal_data->cal_info.status[i] = 0;
  8725. cal_data->cal_info.freq_q20[i] =
  8726. ex_vi.param.freq_q20[i];
  8727. cal_data->cal_info.resis_q24[i] =
  8728. ex_vi.param.resis_q24[i];
  8729. cal_data->cal_info.qmct_q24[i] =
  8730. ex_vi.param.qmct_q24[i];
  8731. }
  8732. }
  8733. }
  8734. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  8735. done:
  8736. return ret;
  8737. }
  8738. static int afe_get_cal_fb_spkr_prot(int32_t cal_type, size_t data_size,
  8739. void *data)
  8740. {
  8741. int ret = 0;
  8742. struct audio_cal_type_fb_spk_prot_status *cal_data = data;
  8743. struct afe_spkr_prot_get_vi_calib calib_resp;
  8744. struct afe_sp_v4_th_vi_calib_resp spv4_calib_resp;
  8745. pr_debug("%s:\n", __func__);
  8746. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL)
  8747. goto done;
  8748. if (cal_data == NULL)
  8749. goto done;
  8750. if (data_size != sizeof(*cal_data))
  8751. goto done;
  8752. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  8753. if (this_afe.prot_cfg.mode == MSM_SPKR_PROT_CALIBRATED) {
  8754. cal_data->cal_info.r0[SP_V2_SPKR_1] =
  8755. this_afe.prot_cfg.r0[SP_V2_SPKR_1];
  8756. cal_data->cal_info.r0[SP_V2_SPKR_2] =
  8757. this_afe.prot_cfg.r0[SP_V2_SPKR_2];
  8758. cal_data->cal_info.status = 0;
  8759. } else if (this_afe.prot_cfg.mode ==
  8760. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS) {
  8761. /*Call AFE to query the status*/
  8762. cal_data->cal_info.status = -EINVAL;
  8763. cal_data->cal_info.r0[SP_V2_SPKR_1] = -1;
  8764. cal_data->cal_info.r0[SP_V2_SPKR_2] = -1;
  8765. if (this_afe.prot_cfg.sp_version >= AFE_API_VERSION_V9) {
  8766. if (!(q6core_get_avcs_api_version_per_service(
  8767. APRV2_IDS_SERVICE_ID_ADSP_AFE_V) >=
  8768. AFE_API_VERSION_V9)) {
  8769. pr_debug(
  8770. "%s: AFE API version is not supported!\n",
  8771. __func__);
  8772. goto done;
  8773. }
  8774. if (!afe_spv4_get_calib_data(&spv4_calib_resp)) {
  8775. if (spv4_calib_resp.res_cfg.th_vi_ca_state ==
  8776. FBSP_IN_PROGRESS)
  8777. cal_data->cal_info.status = -EAGAIN;
  8778. else if (
  8779. spv4_calib_resp.res_cfg.th_vi_ca_state ==
  8780. FBSP_SUCCESS) {
  8781. cal_data->cal_info.status = 0;
  8782. cal_data->cal_info.r0[SP_V2_SPKR_1] =
  8783. spv4_calib_resp.res_cfg.r0_cali_q24[
  8784. SP_V2_SPKR_1];
  8785. cal_data->cal_info.r0[SP_V2_SPKR_2] =
  8786. spv4_calib_resp.res_cfg.r0_cali_q24[
  8787. SP_V2_SPKR_2];
  8788. }
  8789. }
  8790. } else {
  8791. if (!afe_spk_prot_get_calib_data(&calib_resp)) {
  8792. if (calib_resp.res_cfg.th_vi_ca_state ==
  8793. FBSP_IN_PROGRESS)
  8794. cal_data->cal_info.status = -EAGAIN;
  8795. else if (calib_resp.res_cfg.th_vi_ca_state ==
  8796. FBSP_SUCCESS) {
  8797. cal_data->cal_info.status = 0;
  8798. cal_data->cal_info.r0[SP_V2_SPKR_1] =
  8799. calib_resp.res_cfg.r0_cali_q24[
  8800. SP_V2_SPKR_1];
  8801. cal_data->cal_info.r0[SP_V2_SPKR_2] =
  8802. calib_resp.res_cfg.r0_cali_q24[
  8803. SP_V2_SPKR_2];
  8804. }
  8805. }
  8806. }
  8807. if (!cal_data->cal_info.status) {
  8808. this_afe.prot_cfg.mode =
  8809. MSM_SPKR_PROT_CALIBRATED;
  8810. this_afe.prot_cfg.r0[SP_V2_SPKR_1] =
  8811. cal_data->cal_info.r0[SP_V2_SPKR_1];
  8812. this_afe.prot_cfg.r0[SP_V2_SPKR_2] =
  8813. cal_data->cal_info.r0[SP_V2_SPKR_2];
  8814. }
  8815. } else {
  8816. /*Indicates calibration data is invalid*/
  8817. cal_data->cal_info.status = -EINVAL;
  8818. cal_data->cal_info.r0[SP_V2_SPKR_1] = -1;
  8819. cal_data->cal_info.r0[SP_V2_SPKR_2] = -1;
  8820. }
  8821. this_afe.initial_cal = 0;
  8822. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  8823. __pm_relax(wl.ws);
  8824. done:
  8825. return ret;
  8826. }
  8827. static int afe_map_cal_data(int32_t cal_type,
  8828. struct cal_block_data *cal_block)
  8829. {
  8830. int ret = 0;
  8831. int cal_index;
  8832. pr_debug("%s:\n", __func__);
  8833. cal_index = get_cal_type_index(cal_type);
  8834. if (cal_index < 0) {
  8835. pr_err("%s: could not get cal index %d!\n",
  8836. __func__, cal_index);
  8837. ret = -EINVAL;
  8838. goto done;
  8839. }
  8840. atomic_set(&this_afe.mem_map_cal_index, cal_index);
  8841. ret = afe_cmd_memory_map(cal_block->cal_data.paddr,
  8842. cal_block->map_data.map_size);
  8843. atomic_set(&this_afe.mem_map_cal_index, -1);
  8844. if (ret < 0) {
  8845. pr_err("%s: mmap did not work! size = %zd ret %d\n",
  8846. __func__,
  8847. cal_block->map_data.map_size, ret);
  8848. pr_debug("%s: mmap did not work! addr = 0x%pK, size = %zd\n",
  8849. __func__,
  8850. &cal_block->cal_data.paddr,
  8851. cal_block->map_data.map_size);
  8852. goto done;
  8853. }
  8854. cal_block->map_data.q6map_handle = atomic_read(&this_afe.
  8855. mem_map_cal_handles[cal_index]);
  8856. done:
  8857. return ret;
  8858. }
  8859. static int afe_unmap_cal_data(int32_t cal_type,
  8860. struct cal_block_data *cal_block)
  8861. {
  8862. int ret = 0;
  8863. int cal_index;
  8864. pr_debug("%s:\n", __func__);
  8865. cal_index = get_cal_type_index(cal_type);
  8866. if (cal_index < 0) {
  8867. pr_err("%s: could not get cal index %d!\n",
  8868. __func__, cal_index);
  8869. ret = -EINVAL;
  8870. goto done;
  8871. }
  8872. if (cal_block == NULL) {
  8873. pr_err("%s: Cal block is NULL!\n",
  8874. __func__);
  8875. goto done;
  8876. }
  8877. if (cal_block->map_data.q6map_handle == 0) {
  8878. pr_err("%s: Map handle is NULL, nothing to unmap\n",
  8879. __func__);
  8880. goto done;
  8881. }
  8882. atomic_set(&this_afe.mem_map_cal_handles[cal_index],
  8883. cal_block->map_data.q6map_handle);
  8884. atomic_set(&this_afe.mem_map_cal_index, cal_index);
  8885. ret = afe_cmd_memory_unmap_nowait(
  8886. cal_block->map_data.q6map_handle);
  8887. atomic_set(&this_afe.mem_map_cal_index, -1);
  8888. if (ret < 0) {
  8889. pr_err("%s: unmap did not work! cal_type %i ret %d\n",
  8890. __func__, cal_index, ret);
  8891. }
  8892. cal_block->map_data.q6map_handle = 0;
  8893. done:
  8894. return ret;
  8895. }
  8896. static void afe_delete_cal_data(void)
  8897. {
  8898. pr_debug("%s:\n", __func__);
  8899. cal_utils_destroy_cal_types(MAX_AFE_CAL_TYPES, this_afe.cal_data);
  8900. }
  8901. static int afe_init_cal_data(void)
  8902. {
  8903. int ret = 0;
  8904. struct cal_type_info cal_type_info[] = {
  8905. {{AFE_COMMON_RX_CAL_TYPE,
  8906. {afe_alloc_cal, afe_dealloc_cal, NULL,
  8907. afe_set_cal, NULL, NULL} },
  8908. {afe_map_cal_data, afe_unmap_cal_data,
  8909. cal_utils_match_buf_num} },
  8910. {{AFE_COMMON_TX_CAL_TYPE,
  8911. {afe_alloc_cal, afe_dealloc_cal, NULL,
  8912. afe_set_cal, NULL, NULL} },
  8913. {afe_map_cal_data, afe_unmap_cal_data,
  8914. cal_utils_match_buf_num} },
  8915. {{AFE_LSM_TX_CAL_TYPE,
  8916. {afe_alloc_cal, afe_dealloc_cal, NULL,
  8917. afe_set_cal, NULL, NULL} },
  8918. {afe_map_cal_data, afe_unmap_cal_data,
  8919. cal_utils_match_buf_num} },
  8920. {{AFE_AANC_CAL_TYPE,
  8921. {afe_alloc_cal, afe_dealloc_cal, NULL,
  8922. afe_set_cal, NULL, NULL} },
  8923. {afe_map_cal_data, afe_unmap_cal_data,
  8924. cal_utils_match_buf_num} },
  8925. {{AFE_FB_SPKR_PROT_CAL_TYPE,
  8926. {NULL, NULL, NULL, afe_set_cal_fb_spkr_prot,
  8927. afe_get_cal_fb_spkr_prot, NULL} },
  8928. {NULL, NULL, cal_utils_match_buf_num} },
  8929. {{AFE_HW_DELAY_CAL_TYPE,
  8930. {NULL, NULL, NULL,
  8931. afe_set_cal, NULL, NULL} },
  8932. {NULL, NULL, cal_utils_match_buf_num} },
  8933. {{AFE_SIDETONE_CAL_TYPE,
  8934. {NULL, NULL, NULL,
  8935. afe_set_cal, NULL, NULL} },
  8936. {NULL, NULL, cal_utils_match_buf_num} },
  8937. {{AFE_SIDETONE_IIR_CAL_TYPE,
  8938. {NULL, NULL, NULL,
  8939. afe_set_cal, NULL, NULL} },
  8940. {NULL, NULL, cal_utils_match_buf_num} },
  8941. {{AFE_TOPOLOGY_CAL_TYPE,
  8942. {NULL, NULL, NULL,
  8943. afe_set_cal, NULL, NULL} },
  8944. {NULL, NULL,
  8945. cal_utils_match_buf_num} },
  8946. {{AFE_LSM_TOPOLOGY_CAL_TYPE,
  8947. {NULL, NULL, NULL,
  8948. afe_set_cal, NULL, NULL} },
  8949. {NULL, NULL,
  8950. cal_utils_match_buf_num} },
  8951. {{AFE_CUST_TOPOLOGY_CAL_TYPE,
  8952. {afe_alloc_cal, afe_dealloc_cal, NULL,
  8953. afe_set_cal, NULL, NULL} },
  8954. {afe_map_cal_data, afe_unmap_cal_data,
  8955. cal_utils_match_buf_num} },
  8956. {{AFE_FB_SPKR_PROT_TH_VI_CAL_TYPE,
  8957. {NULL, NULL, NULL, afe_set_cal_sp_th_vi_cfg,
  8958. afe_get_cal_sp_th_vi_param, NULL} },
  8959. {NULL, NULL, cal_utils_match_buf_num} },
  8960. {{AFE_FB_SPKR_PROT_EX_VI_CAL_TYPE,
  8961. {NULL, NULL, NULL, afe_set_cal_sp_ex_vi_ftm_cfg,
  8962. afe_get_cal_sp_ex_vi_ftm_param, NULL} },
  8963. {NULL, NULL, cal_utils_match_buf_num} },
  8964. {{AFE_FB_SPKR_PROT_V4_EX_VI_CAL_TYPE,
  8965. {NULL, NULL, NULL, NULL,
  8966. afe_get_cal_spv4_ex_vi_ftm_param, NULL} },
  8967. {NULL, NULL, cal_utils_match_buf_num} },
  8968. };
  8969. pr_debug("%s:\n", __func__);
  8970. ret = cal_utils_create_cal_types(MAX_AFE_CAL_TYPES, this_afe.cal_data,
  8971. cal_type_info);
  8972. if (ret < 0) {
  8973. pr_err("%s: could not create cal type! %d\n",
  8974. __func__, ret);
  8975. ret = -EINVAL;
  8976. goto err;
  8977. }
  8978. return ret;
  8979. err:
  8980. afe_delete_cal_data();
  8981. return ret;
  8982. }
  8983. int afe_map_rtac_block(struct rtac_cal_block_data *cal_block)
  8984. {
  8985. int result = 0;
  8986. pr_debug("%s:\n", __func__);
  8987. if (cal_block == NULL) {
  8988. pr_err("%s: cal_block is NULL!\n",
  8989. __func__);
  8990. result = -EINVAL;
  8991. goto done;
  8992. }
  8993. if (cal_block->cal_data.paddr == 0) {
  8994. pr_debug("%s: No address to map!\n",
  8995. __func__);
  8996. result = -EINVAL;
  8997. goto done;
  8998. }
  8999. if (cal_block->map_data.map_size == 0) {
  9000. pr_debug("%s: map size is 0!\n",
  9001. __func__);
  9002. result = -EINVAL;
  9003. goto done;
  9004. }
  9005. result = afe_cmd_memory_map(cal_block->cal_data.paddr,
  9006. cal_block->map_data.map_size);
  9007. if (result < 0) {
  9008. pr_err("%s: afe_cmd_memory_map failed for addr = 0x%pK, size = %d, err %d\n",
  9009. __func__, &cal_block->cal_data.paddr,
  9010. cal_block->map_data.map_size, result);
  9011. return result;
  9012. }
  9013. cal_block->map_data.map_handle = this_afe.mmap_handle;
  9014. done:
  9015. return result;
  9016. }
  9017. int afe_unmap_rtac_block(uint32_t *mem_map_handle)
  9018. {
  9019. int result = 0;
  9020. pr_debug("%s:\n", __func__);
  9021. if (mem_map_handle == NULL) {
  9022. pr_err("%s: Map handle is NULL, nothing to unmap\n",
  9023. __func__);
  9024. goto done;
  9025. }
  9026. if (*mem_map_handle == 0) {
  9027. pr_debug("%s: Map handle is 0, nothing to unmap\n",
  9028. __func__);
  9029. goto done;
  9030. }
  9031. result = afe_cmd_memory_unmap(*mem_map_handle);
  9032. if (result) {
  9033. pr_err("%s: AFE memory unmap failed %d, handle 0x%x\n",
  9034. __func__, result, *mem_map_handle);
  9035. goto done;
  9036. } else {
  9037. *mem_map_handle = 0;
  9038. }
  9039. done:
  9040. return result;
  9041. }
  9042. static void afe_release_uevent_data(struct kobject *kobj)
  9043. {
  9044. struct audio_uevent_data *data = container_of(kobj,
  9045. struct audio_uevent_data, kobj);
  9046. kfree(data);
  9047. }
  9048. int __init afe_init(void)
  9049. {
  9050. int i = 0, ret;
  9051. atomic_set(&this_afe.state, 0);
  9052. atomic_set(&this_afe.status, 0);
  9053. atomic_set(&this_afe.clk_state, 0);
  9054. atomic_set(&this_afe.clk_status, 0);
  9055. atomic_set(&this_afe.mem_map_cal_index, -1);
  9056. this_afe.apr = NULL;
  9057. this_afe.dtmf_gen_rx_portid = -1;
  9058. this_afe.mmap_handle = 0;
  9059. this_afe.vi_tx_port = -1;
  9060. this_afe.vi_rx_port = -1;
  9061. for (i = 0; i < AFE_LPASS_CORE_HW_VOTE_MAX; i++)
  9062. this_afe.lpass_hw_core_client_hdl[i] = 0;
  9063. this_afe.prot_cfg.mode = MSM_SPKR_PROT_DISABLED;
  9064. this_afe.th_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  9065. this_afe.ex_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  9066. mutex_init(&this_afe.afe_cmd_lock);
  9067. mutex_init(&this_afe.afe_apr_lock);
  9068. mutex_init(&this_afe.afe_clk_lock);
  9069. for (i = 0; i < AFE_MAX_PORTS; i++) {
  9070. this_afe.afe_cal_mode[i] = AFE_CAL_MODE_DEFAULT;
  9071. this_afe.afe_sample_rates[i] = 0;
  9072. this_afe.dev_acdb_id[i] = 0;
  9073. this_afe.island_mode[i] = 0;
  9074. this_afe.vad_cfg[i].is_enable = 0;
  9075. this_afe.vad_cfg[i].pre_roll = 0;
  9076. init_waitqueue_head(&this_afe.wait[i]);
  9077. }
  9078. init_waitqueue_head(&this_afe.wait_wakeup);
  9079. init_waitqueue_head(&this_afe.lpass_core_hw_wait);
  9080. init_waitqueue_head(&this_afe.clk_wait);
  9081. wl.ws = wakeup_source_register(NULL, "spkr-prot");
  9082. if (!wl.ws)
  9083. return -ENOMEM;
  9084. ret = afe_init_cal_data();
  9085. if (ret)
  9086. pr_err("%s: could not init cal data! %d\n", __func__, ret);
  9087. config_debug_fs_init();
  9088. this_afe.uevent_data = kzalloc(sizeof(*(this_afe.uevent_data)), GFP_KERNEL);
  9089. if (!this_afe.uevent_data) {
  9090. wakeup_source_unregister(wl.ws);
  9091. return -ENOMEM;
  9092. }
  9093. /*
  9094. * Set release function to cleanup memory related to kobject
  9095. * before initializing the kobject.
  9096. */
  9097. this_afe.uevent_data->ktype.release = afe_release_uevent_data;
  9098. q6core_init_uevent_data(this_afe.uevent_data, "q6afe_uevent");
  9099. INIT_WORK(&this_afe.afe_dc_work, afe_notify_dc_presence_work_fn);
  9100. INIT_WORK(&this_afe.afe_spdif_work,
  9101. afe_notify_spdif_fmt_update_work_fn);
  9102. this_afe.event_notifier.notifier_call = afe_aud_event_notify;
  9103. msm_aud_evt_blocking_register_client(&this_afe.event_notifier);
  9104. return 0;
  9105. }
  9106. void afe_exit(void)
  9107. {
  9108. if (this_afe.apr) {
  9109. apr_reset(this_afe.apr);
  9110. atomic_set(&this_afe.state, 0);
  9111. this_afe.apr = NULL;
  9112. rtac_set_afe_handle(this_afe.apr);
  9113. }
  9114. q6core_destroy_uevent_data(this_afe.uevent_data);
  9115. afe_delete_cal_data();
  9116. config_debug_fs_exit();
  9117. mutex_destroy(&this_afe.afe_cmd_lock);
  9118. mutex_destroy(&this_afe.afe_apr_lock);
  9119. mutex_destroy(&this_afe.afe_clk_lock);
  9120. wakeup_source_unregister(wl.ws);
  9121. }
  9122. /*
  9123. * afe_cal_init_hwdep -
  9124. * Initiliaze AFE HW dependent Node
  9125. *
  9126. * @card: pointer to sound card
  9127. *
  9128. */
  9129. int afe_cal_init_hwdep(void *card)
  9130. {
  9131. int ret = 0;
  9132. this_afe.fw_data = kzalloc(sizeof(*(this_afe.fw_data)),
  9133. GFP_KERNEL);
  9134. if (!this_afe.fw_data)
  9135. return -ENOMEM;
  9136. set_bit(Q6AFE_VAD_CORE_CAL, this_afe.fw_data->cal_bit);
  9137. ret = q6afe_cal_create_hwdep(this_afe.fw_data, Q6AFE_HWDEP_NODE, card);
  9138. if (ret < 0) {
  9139. pr_err("%s: couldn't create hwdep for AFE %d\n", __func__, ret);
  9140. return ret;
  9141. }
  9142. return ret;
  9143. }
  9144. EXPORT_SYMBOL(afe_cal_init_hwdep);
  9145. /*
  9146. * afe_vote_lpass_core_hw -
  9147. * Voting for lpass core hardware
  9148. *
  9149. * @hw_block_id: id of the hardware block
  9150. * @client_name: client name
  9151. * @client_handle: client handle
  9152. *
  9153. */
  9154. int afe_vote_lpass_core_hw(uint32_t hw_block_id, char *client_name,
  9155. uint32_t *client_handle)
  9156. {
  9157. struct afe_cmd_remote_lpass_core_hw_vote_request hw_vote_cfg;
  9158. struct afe_cmd_remote_lpass_core_hw_vote_request *cmd_ptr =
  9159. &hw_vote_cfg;
  9160. int ret = 0;
  9161. if (!client_handle) {
  9162. pr_err("%s: Invalid client_handle\n", __func__);
  9163. return -EINVAL;
  9164. }
  9165. if (!client_name) {
  9166. pr_err("%s: Invalid client_name\n", __func__);
  9167. *client_handle = 0;
  9168. return -EINVAL;
  9169. }
  9170. ret = afe_q6_interface_prepare();
  9171. if(ret) {
  9172. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  9173. return ret;
  9174. }
  9175. mutex_lock(&this_afe.afe_clk_lock);
  9176. memset(cmd_ptr, 0, sizeof(hw_vote_cfg));
  9177. cmd_ptr->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  9178. APR_HDR_LEN(APR_HDR_SIZE),
  9179. APR_PKT_VER);
  9180. cmd_ptr->hdr.pkt_size = sizeof(hw_vote_cfg);
  9181. cmd_ptr->hdr.src_port = 0;
  9182. cmd_ptr->hdr.dest_port = 0;
  9183. cmd_ptr->hdr.token = hw_block_id;
  9184. cmd_ptr->hdr.opcode = AFE_CMD_REMOTE_LPASS_CORE_HW_VOTE_REQUEST;
  9185. cmd_ptr->hw_block_id = hw_block_id;
  9186. strlcpy(cmd_ptr->client_name, client_name,
  9187. sizeof(cmd_ptr->client_name));
  9188. pr_debug("%s: lpass core hw vote opcode[0x%x] hw id[0x%x]\n",
  9189. __func__, cmd_ptr->hdr.opcode, cmd_ptr->hw_block_id);
  9190. trace_printk("%s: lpass core hw vote opcode[0x%x] hw id[0x%x]\n",
  9191. __func__, cmd_ptr->hdr.opcode, cmd_ptr->hw_block_id);
  9192. *client_handle = 0;
  9193. ret = afe_apr_send_clk_pkt((uint32_t *)cmd_ptr,
  9194. &this_afe.lpass_core_hw_wait);
  9195. if (ret == 0) {
  9196. *client_handle = this_afe.lpass_hw_core_client_hdl[hw_block_id];
  9197. pr_debug("%s: lpass_hw_core_client_hdl %d\n", __func__,
  9198. this_afe.lpass_hw_core_client_hdl[hw_block_id]);
  9199. }
  9200. mutex_unlock(&this_afe.afe_clk_lock);
  9201. return ret;
  9202. }
  9203. EXPORT_SYMBOL(afe_vote_lpass_core_hw);
  9204. /*
  9205. * afe_unvote_lpass_core_hw -
  9206. * unvoting for lpass core hardware
  9207. *
  9208. * @hw_block_id: id of the hardware block
  9209. * @client_handle: client handle
  9210. *
  9211. */
  9212. int afe_unvote_lpass_core_hw(uint32_t hw_block_id, uint32_t client_handle)
  9213. {
  9214. struct afe_cmd_remote_lpass_core_hw_devote_request hw_vote_cfg;
  9215. struct afe_cmd_remote_lpass_core_hw_devote_request *cmd_ptr =
  9216. &hw_vote_cfg;
  9217. int ret = 0;
  9218. ret = afe_q6_interface_prepare();
  9219. if(ret) {
  9220. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  9221. return ret;
  9222. }
  9223. mutex_lock(&this_afe.afe_clk_lock);
  9224. if (!this_afe.lpass_hw_core_client_hdl[hw_block_id]) {
  9225. pr_debug("%s: SSR in progress, return\n", __func__);
  9226. trace_printk("%s: SSR in progress, return\n", __func__);
  9227. goto done;
  9228. }
  9229. memset(cmd_ptr, 0, sizeof(hw_vote_cfg));
  9230. cmd_ptr->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  9231. APR_HDR_LEN(APR_HDR_SIZE),
  9232. APR_PKT_VER);
  9233. cmd_ptr->hdr.pkt_size = sizeof(hw_vote_cfg);
  9234. cmd_ptr->hdr.src_port = 0;
  9235. cmd_ptr->hdr.dest_port = 0;
  9236. cmd_ptr->hdr.token = 0;
  9237. cmd_ptr->hdr.opcode = AFE_CMD_REMOTE_LPASS_CORE_HW_DEVOTE_REQUEST;
  9238. cmd_ptr->hw_block_id = hw_block_id;
  9239. cmd_ptr->client_handle = client_handle;
  9240. pr_debug("%s: lpass core hw unvote opcode[0x%x] hw id[0x%x]\n",
  9241. __func__, cmd_ptr->hdr.opcode, cmd_ptr->hw_block_id);
  9242. trace_printk("%s: lpass core hw unvote opcode[0x%x] hw id[0x%x]\n",
  9243. __func__, cmd_ptr->hdr.opcode, cmd_ptr->hw_block_id);
  9244. if (cmd_ptr->client_handle <= 0) {
  9245. pr_err("%s: invalid client handle\n", __func__);
  9246. ret = -EINVAL;
  9247. goto done;
  9248. }
  9249. ret = afe_apr_send_clk_pkt((uint32_t *)cmd_ptr,
  9250. &this_afe.lpass_core_hw_wait);
  9251. done:
  9252. mutex_unlock(&this_afe.afe_clk_lock);
  9253. return ret;
  9254. }
  9255. EXPORT_SYMBOL(afe_unvote_lpass_core_hw);