q6afe.c 262 KB

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