q6afe.c 314 KB

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