q6afe.c 252 KB

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