q6afe.c 266 KB

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