q6afe.c 206 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245
  1. /* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kthread.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/wait.h>
  18. #include <linux/wakelock.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/sched.h>
  21. #include <linux/delay.h>
  22. #include <dsp/msm_audio_ion.h>
  23. #include <dsp/apr_audio-v2.h>
  24. #include <dsp/audio_cal_utils.h>
  25. #include <dsp/q6afe-v2.h>
  26. #include <dsp/q6audio-v2.h>
  27. #include <ipc/apr_tal.h>
  28. #include "adsp_err.h"
  29. #define WAKELOCK_TIMEOUT 5000
  30. enum {
  31. AFE_COMMON_RX_CAL = 0,
  32. AFE_COMMON_TX_CAL,
  33. AFE_AANC_CAL,
  34. AFE_FB_SPKR_PROT_CAL,
  35. AFE_HW_DELAY_CAL,
  36. AFE_SIDETONE_CAL,
  37. AFE_SIDETONE_IIR_CAL,
  38. AFE_TOPOLOGY_CAL,
  39. AFE_CUST_TOPOLOGY_CAL,
  40. AFE_FB_SPKR_PROT_TH_VI_CAL,
  41. AFE_FB_SPKR_PROT_EX_VI_CAL,
  42. MAX_AFE_CAL_TYPES
  43. };
  44. enum fbsp_state {
  45. FBSP_INCORRECT_OP_MODE,
  46. FBSP_INACTIVE,
  47. FBSP_WARMUP,
  48. FBSP_IN_PROGRESS,
  49. FBSP_SUCCESS,
  50. FBSP_FAILED,
  51. MAX_FBSP_STATE
  52. };
  53. static char fbsp_state[MAX_FBSP_STATE][50] = {
  54. [FBSP_INCORRECT_OP_MODE] = "incorrect operation mode",
  55. [FBSP_INACTIVE] = "port not started",
  56. [FBSP_WARMUP] = "waiting for warmup",
  57. [FBSP_IN_PROGRESS] = "in progress state",
  58. [FBSP_SUCCESS] = "success",
  59. [FBSP_FAILED] = "failed"
  60. };
  61. enum {
  62. USE_CALIBRATED_R0TO,
  63. USE_SAFE_R0TO
  64. };
  65. enum {
  66. QUICK_CALIB_DISABLE,
  67. QUICK_CALIB_ENABLE
  68. };
  69. enum {
  70. Q6AFE_MSM_SPKR_PROCESSING = 0,
  71. Q6AFE_MSM_SPKR_CALIBRATION,
  72. Q6AFE_MSM_SPKR_FTM_MODE
  73. };
  74. struct wlock {
  75. struct wakeup_source ws;
  76. };
  77. static struct wlock wl;
  78. struct afe_ctl {
  79. void *apr;
  80. atomic_t state;
  81. atomic_t status;
  82. wait_queue_head_t wait[AFE_MAX_PORTS];
  83. struct task_struct *task;
  84. void (*tx_cb)(uint32_t opcode,
  85. uint32_t token, uint32_t *payload, void *priv);
  86. void (*rx_cb)(uint32_t opcode,
  87. uint32_t token, uint32_t *payload, void *priv);
  88. void *tx_private_data;
  89. void *rx_private_data;
  90. uint32_t mmap_handle;
  91. int topology[AFE_MAX_PORTS];
  92. struct cal_type_data *cal_data[MAX_AFE_CAL_TYPES];
  93. atomic_t mem_map_cal_handles[MAX_AFE_CAL_TYPES];
  94. atomic_t mem_map_cal_index;
  95. u32 afe_cal_mode[AFE_MAX_PORTS];
  96. u16 dtmf_gen_rx_portid;
  97. struct audio_cal_info_spk_prot_cfg prot_cfg;
  98. struct afe_spkr_prot_calib_get_resp calib_data;
  99. struct audio_cal_info_sp_th_vi_ftm_cfg th_ftm_cfg;
  100. struct audio_cal_info_sp_ex_vi_ftm_cfg ex_ftm_cfg;
  101. struct afe_sp_th_vi_get_param_resp th_vi_resp;
  102. struct afe_sp_ex_vi_get_param_resp ex_vi_resp;
  103. struct afe_av_dev_drift_get_param_resp av_dev_drift_resp;
  104. int vi_tx_port;
  105. int vi_rx_port;
  106. uint32_t afe_sample_rates[AFE_MAX_PORTS];
  107. struct aanc_data aanc_info;
  108. struct mutex afe_cmd_lock;
  109. int set_custom_topology;
  110. int dev_acdb_id[AFE_MAX_PORTS];
  111. routing_cb rt_cb;
  112. };
  113. static atomic_t afe_ports_mad_type[SLIMBUS_PORT_LAST - SLIMBUS_0_RX];
  114. static unsigned long afe_configured_cmd;
  115. static struct afe_ctl this_afe;
  116. #define TIMEOUT_MS 1000
  117. #define Q6AFE_MAX_VOLUME 0x3FFF
  118. static int pcm_afe_instance[2];
  119. static int proxy_afe_instance[2];
  120. bool afe_close_done[2] = {true, true};
  121. #define SIZEOF_CFG_CMD(y) \
  122. (sizeof(struct apr_hdr) + sizeof(u16) + (sizeof(struct y)))
  123. static int afe_get_cal_hw_delay(int32_t path,
  124. struct audio_cal_hw_delay_entry *entry);
  125. static int remap_cal_data(struct cal_block_data *cal_block, int cal_index);
  126. int afe_get_topology(int port_id)
  127. {
  128. int topology;
  129. int port_index = afe_get_port_index(port_id);
  130. if ((port_index < 0) || (port_index >= AFE_MAX_PORTS)) {
  131. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  132. topology = -EINVAL;
  133. goto done;
  134. }
  135. topology = this_afe.topology[port_index];
  136. done:
  137. return topology;
  138. }
  139. void afe_set_aanc_info(struct aanc_data *q6_aanc_info)
  140. {
  141. this_afe.aanc_info.aanc_active = q6_aanc_info->aanc_active;
  142. this_afe.aanc_info.aanc_rx_port = q6_aanc_info->aanc_rx_port;
  143. this_afe.aanc_info.aanc_tx_port = q6_aanc_info->aanc_tx_port;
  144. pr_debug("%s: aanc active is %d rx port is 0x%x, tx port is 0x%x\n",
  145. __func__,
  146. this_afe.aanc_info.aanc_active,
  147. this_afe.aanc_info.aanc_rx_port,
  148. this_afe.aanc_info.aanc_tx_port);
  149. }
  150. static void afe_callback_debug_print(struct apr_client_data *data)
  151. {
  152. uint32_t *payload;
  153. payload = data->payload;
  154. if (data->payload_size >= 8)
  155. pr_debug("%s: code = 0x%x PL#0[0x%x], PL#1[0x%x], size = %d\n",
  156. __func__, data->opcode, payload[0], payload[1],
  157. data->payload_size);
  158. else if (data->payload_size >= 4)
  159. pr_debug("%s: code = 0x%x PL#0[0x%x], size = %d\n",
  160. __func__, data->opcode, payload[0],
  161. data->payload_size);
  162. else
  163. pr_debug("%s: code = 0x%x, size = %d\n",
  164. __func__, data->opcode, data->payload_size);
  165. }
  166. static void av_dev_drift_afe_cb_handler(uint32_t *payload,
  167. uint32_t payload_size)
  168. {
  169. u32 param_id;
  170. struct afe_av_dev_drift_get_param_resp *resp =
  171. (struct afe_av_dev_drift_get_param_resp *) payload;
  172. if (!(&(resp->pdata))) {
  173. pr_err("%s: Error: resp pdata is NULL\n", __func__);
  174. return;
  175. }
  176. param_id = resp->pdata.param_id;
  177. if (param_id == AFE_PARAM_ID_DEV_TIMING_STATS) {
  178. if (payload_size < sizeof(this_afe.av_dev_drift_resp)) {
  179. pr_err("%s: Error: received size %d, resp size %zu\n",
  180. __func__, payload_size,
  181. sizeof(this_afe.av_dev_drift_resp));
  182. return;
  183. }
  184. memcpy(&this_afe.av_dev_drift_resp, payload,
  185. sizeof(this_afe.av_dev_drift_resp));
  186. if (!this_afe.av_dev_drift_resp.status) {
  187. atomic_set(&this_afe.state, 0);
  188. } else {
  189. pr_debug("%s: av_dev_drift_resp status: %d", __func__,
  190. this_afe.av_dev_drift_resp.status);
  191. atomic_set(&this_afe.state, -1);
  192. }
  193. }
  194. }
  195. static int32_t sp_make_afe_callback(uint32_t *payload, uint32_t payload_size)
  196. {
  197. u32 param_id;
  198. struct afe_spkr_prot_calib_get_resp *resp =
  199. (struct afe_spkr_prot_calib_get_resp *) payload;
  200. if (!(&(resp->pdata))) {
  201. pr_err("%s: Error: resp pdata is NULL\n", __func__);
  202. return -EINVAL;
  203. }
  204. param_id = resp->pdata.param_id;
  205. if (param_id == AFE_PARAM_ID_CALIB_RES_CFG_V2) {
  206. if (payload_size < sizeof(this_afe.calib_data)) {
  207. pr_err("%s: Error: received size %d, calib_data size %zu\n",
  208. __func__, payload_size,
  209. sizeof(this_afe.calib_data));
  210. return -EINVAL;
  211. }
  212. memcpy(&this_afe.calib_data, payload,
  213. sizeof(this_afe.calib_data));
  214. if (!this_afe.calib_data.status) {
  215. atomic_set(&this_afe.state, 0);
  216. } else {
  217. pr_debug("%s: calib resp status: %d", __func__,
  218. this_afe.calib_data.status);
  219. atomic_set(&this_afe.state, -1);
  220. }
  221. }
  222. if (param_id == AFE_PARAM_ID_SP_V2_TH_VI_FTM_PARAMS) {
  223. if (payload_size < sizeof(this_afe.th_vi_resp)) {
  224. pr_err("%s: Error: received size %d, th_vi_resp size %zu\n",
  225. __func__, payload_size,
  226. sizeof(this_afe.th_vi_resp));
  227. return -EINVAL;
  228. }
  229. memcpy(&this_afe.th_vi_resp, payload,
  230. sizeof(this_afe.th_vi_resp));
  231. if (!this_afe.th_vi_resp.status) {
  232. atomic_set(&this_afe.state, 0);
  233. } else {
  234. pr_debug("%s: th vi resp status: %d", __func__,
  235. this_afe.th_vi_resp.status);
  236. atomic_set(&this_afe.state, -1);
  237. }
  238. }
  239. if (param_id == AFE_PARAM_ID_SP_V2_EX_VI_FTM_PARAMS) {
  240. if (payload_size < sizeof(this_afe.ex_vi_resp)) {
  241. pr_err("%s: Error: received size %d, ex_vi_resp size %zu\n",
  242. __func__, payload_size,
  243. sizeof(this_afe.ex_vi_resp));
  244. return -EINVAL;
  245. }
  246. memcpy(&this_afe.ex_vi_resp, payload,
  247. sizeof(this_afe.ex_vi_resp));
  248. if (!this_afe.ex_vi_resp.status) {
  249. atomic_set(&this_afe.state, 0);
  250. } else {
  251. pr_debug("%s: ex vi resp status: %d", __func__,
  252. this_afe.ex_vi_resp.status);
  253. atomic_set(&this_afe.state, -1);
  254. }
  255. }
  256. return 0;
  257. }
  258. static int32_t afe_callback(struct apr_client_data *data, void *priv)
  259. {
  260. if (!data) {
  261. pr_err("%s: Invalid param data\n", __func__);
  262. return -EINVAL;
  263. }
  264. if (data->opcode == RESET_EVENTS) {
  265. pr_debug("%s: reset event = %d %d apr[%pK]\n",
  266. __func__,
  267. data->reset_event, data->reset_proc, this_afe.apr);
  268. cal_utils_clear_cal_block_q6maps(MAX_AFE_CAL_TYPES,
  269. this_afe.cal_data);
  270. /* Reset the custom topology mode: to resend again to AFE. */
  271. mutex_lock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  272. this_afe.set_custom_topology = 1;
  273. mutex_unlock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  274. rtac_clear_mapping(AFE_RTAC_CAL);
  275. if (this_afe.apr) {
  276. apr_reset(this_afe.apr);
  277. atomic_set(&this_afe.state, 0);
  278. this_afe.apr = NULL;
  279. rtac_set_afe_handle(this_afe.apr);
  280. }
  281. /* send info to user */
  282. if (this_afe.task == NULL)
  283. this_afe.task = current;
  284. pr_debug("%s: task_name = %s pid = %d\n",
  285. __func__,
  286. this_afe.task->comm, this_afe.task->pid);
  287. /*
  288. * Pass reset events to proxy driver, if cb is registered
  289. */
  290. if (this_afe.tx_cb) {
  291. this_afe.tx_cb(data->opcode, data->token,
  292. data->payload,
  293. this_afe.tx_private_data);
  294. this_afe.tx_cb = NULL;
  295. }
  296. if (this_afe.rx_cb) {
  297. this_afe.rx_cb(data->opcode, data->token,
  298. data->payload,
  299. this_afe.rx_private_data);
  300. this_afe.rx_cb = NULL;
  301. }
  302. return 0;
  303. }
  304. afe_callback_debug_print(data);
  305. if (data->opcode == AFE_PORT_CMDRSP_GET_PARAM_V2) {
  306. uint32_t *payload = data->payload;
  307. if (!payload || (data->token >= AFE_MAX_PORTS)) {
  308. pr_err("%s: Error: size %d payload %pK token %d\n",
  309. __func__, data->payload_size,
  310. payload, data->token);
  311. return -EINVAL;
  312. }
  313. if (payload[2] == AFE_PARAM_ID_DEV_TIMING_STATS) {
  314. av_dev_drift_afe_cb_handler(data->payload,
  315. data->payload_size);
  316. } else {
  317. if (rtac_make_afe_callback(data->payload,
  318. data->payload_size))
  319. return 0;
  320. if (sp_make_afe_callback(data->payload,
  321. data->payload_size))
  322. return -EINVAL;
  323. }
  324. wake_up(&this_afe.wait[data->token]);
  325. } else if (data->payload_size) {
  326. uint32_t *payload;
  327. uint16_t port_id = 0;
  328. payload = data->payload;
  329. if (data->opcode == APR_BASIC_RSP_RESULT) {
  330. pr_debug("%s:opcode = 0x%x cmd = 0x%x status = 0x%x token=%d\n",
  331. __func__, data->opcode,
  332. payload[0], payload[1], data->token);
  333. /* payload[1] contains the error status for response */
  334. if (payload[1] != 0) {
  335. atomic_set(&this_afe.status, payload[1]);
  336. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  337. __func__, payload[0], payload[1]);
  338. }
  339. switch (payload[0]) {
  340. case AFE_PORT_CMD_SET_PARAM_V2:
  341. if (rtac_make_afe_callback(payload,
  342. data->payload_size))
  343. return 0;
  344. case AFE_PORT_CMD_DEVICE_STOP:
  345. case AFE_PORT_CMD_DEVICE_START:
  346. case AFE_PSEUDOPORT_CMD_START:
  347. case AFE_PSEUDOPORT_CMD_STOP:
  348. case AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS:
  349. case AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS:
  350. case AFE_SERVICE_CMD_UNREGISTER_RT_PORT_DRIVER:
  351. case AFE_PORTS_CMD_DTMF_CTL:
  352. case AFE_SVC_CMD_SET_PARAM:
  353. atomic_set(&this_afe.state, 0);
  354. wake_up(&this_afe.wait[data->token]);
  355. break;
  356. case AFE_SERVICE_CMD_REGISTER_RT_PORT_DRIVER:
  357. break;
  358. case AFE_PORT_DATA_CMD_RT_PROXY_PORT_WRITE_V2:
  359. port_id = RT_PROXY_PORT_001_TX;
  360. break;
  361. case AFE_PORT_DATA_CMD_RT_PROXY_PORT_READ_V2:
  362. port_id = RT_PROXY_PORT_001_RX;
  363. break;
  364. case AFE_CMD_ADD_TOPOLOGIES:
  365. atomic_set(&this_afe.state, 0);
  366. wake_up(&this_afe.wait[data->token]);
  367. pr_debug("%s: AFE_CMD_ADD_TOPOLOGIES cmd 0x%x\n",
  368. __func__, payload[1]);
  369. break;
  370. default:
  371. pr_err("%s: Unknown cmd 0x%x\n", __func__,
  372. payload[0]);
  373. break;
  374. }
  375. } else if (data->opcode ==
  376. AFE_SERVICE_CMDRSP_SHARED_MEM_MAP_REGIONS) {
  377. pr_debug("%s: mmap_handle: 0x%x, cal index %d\n",
  378. __func__, payload[0],
  379. atomic_read(&this_afe.mem_map_cal_index));
  380. if (atomic_read(&this_afe.mem_map_cal_index) != -1)
  381. atomic_set(&this_afe.mem_map_cal_handles[
  382. atomic_read(
  383. &this_afe.mem_map_cal_index)],
  384. (uint32_t)payload[0]);
  385. else
  386. this_afe.mmap_handle = payload[0];
  387. atomic_set(&this_afe.state, 0);
  388. wake_up(&this_afe.wait[data->token]);
  389. } else if (data->opcode == AFE_EVENT_RT_PROXY_PORT_STATUS) {
  390. port_id = (uint16_t)(0x0000FFFF & payload[0]);
  391. }
  392. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  393. switch (port_id) {
  394. case RT_PROXY_PORT_001_TX: {
  395. if (this_afe.tx_cb) {
  396. this_afe.tx_cb(data->opcode, data->token,
  397. data->payload,
  398. this_afe.tx_private_data);
  399. }
  400. break;
  401. }
  402. case RT_PROXY_PORT_001_RX: {
  403. if (this_afe.rx_cb) {
  404. this_afe.rx_cb(data->opcode, data->token,
  405. data->payload,
  406. this_afe.rx_private_data);
  407. }
  408. break;
  409. }
  410. default:
  411. pr_debug("%s: default case 0x%x\n", __func__, port_id);
  412. break;
  413. }
  414. }
  415. return 0;
  416. }
  417. int afe_get_port_type(u16 port_id)
  418. {
  419. int ret;
  420. switch (port_id) {
  421. case PRIMARY_I2S_RX:
  422. case SECONDARY_I2S_RX:
  423. case MI2S_RX:
  424. case HDMI_RX:
  425. case DISPLAY_PORT_RX:
  426. case AFE_PORT_ID_SPDIF_RX:
  427. case SLIMBUS_0_RX:
  428. case SLIMBUS_1_RX:
  429. case SLIMBUS_2_RX:
  430. case SLIMBUS_3_RX:
  431. case SLIMBUS_4_RX:
  432. case SLIMBUS_5_RX:
  433. case SLIMBUS_6_RX:
  434. case SLIMBUS_7_RX:
  435. case SLIMBUS_8_RX:
  436. case INT_BT_SCO_RX:
  437. case INT_BT_A2DP_RX:
  438. case INT_FM_RX:
  439. case VOICE_PLAYBACK_TX:
  440. case VOICE2_PLAYBACK_TX:
  441. case RT_PROXY_PORT_001_RX:
  442. case AUDIO_PORT_ID_I2S_RX:
  443. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  444. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  445. case AFE_PORT_ID_SECONDARY_MI2S_RX_SD1:
  446. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  447. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  448. case AFE_PORT_ID_QUINARY_MI2S_RX:
  449. case AFE_PORT_ID_PRIMARY_PCM_RX:
  450. case AFE_PORT_ID_SECONDARY_PCM_RX:
  451. case AFE_PORT_ID_TERTIARY_PCM_RX:
  452. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  453. case AFE_PORT_ID_QUINARY_PCM_RX:
  454. case AFE_PORT_ID_PRIMARY_TDM_RX:
  455. case AFE_PORT_ID_PRIMARY_TDM_RX_1:
  456. case AFE_PORT_ID_PRIMARY_TDM_RX_2:
  457. case AFE_PORT_ID_PRIMARY_TDM_RX_3:
  458. case AFE_PORT_ID_PRIMARY_TDM_RX_4:
  459. case AFE_PORT_ID_PRIMARY_TDM_RX_5:
  460. case AFE_PORT_ID_PRIMARY_TDM_RX_6:
  461. case AFE_PORT_ID_PRIMARY_TDM_RX_7:
  462. case AFE_PORT_ID_SECONDARY_TDM_RX:
  463. case AFE_PORT_ID_SECONDARY_TDM_RX_1:
  464. case AFE_PORT_ID_SECONDARY_TDM_RX_2:
  465. case AFE_PORT_ID_SECONDARY_TDM_RX_3:
  466. case AFE_PORT_ID_SECONDARY_TDM_RX_4:
  467. case AFE_PORT_ID_SECONDARY_TDM_RX_5:
  468. case AFE_PORT_ID_SECONDARY_TDM_RX_6:
  469. case AFE_PORT_ID_SECONDARY_TDM_RX_7:
  470. case AFE_PORT_ID_TERTIARY_TDM_RX:
  471. case AFE_PORT_ID_TERTIARY_TDM_RX_1:
  472. case AFE_PORT_ID_TERTIARY_TDM_RX_2:
  473. case AFE_PORT_ID_TERTIARY_TDM_RX_3:
  474. case AFE_PORT_ID_TERTIARY_TDM_RX_4:
  475. case AFE_PORT_ID_TERTIARY_TDM_RX_5:
  476. case AFE_PORT_ID_TERTIARY_TDM_RX_6:
  477. case AFE_PORT_ID_TERTIARY_TDM_RX_7:
  478. case AFE_PORT_ID_QUATERNARY_TDM_RX:
  479. case AFE_PORT_ID_QUATERNARY_TDM_RX_1:
  480. case AFE_PORT_ID_QUATERNARY_TDM_RX_2:
  481. case AFE_PORT_ID_QUATERNARY_TDM_RX_3:
  482. case AFE_PORT_ID_QUATERNARY_TDM_RX_4:
  483. case AFE_PORT_ID_QUATERNARY_TDM_RX_5:
  484. case AFE_PORT_ID_QUATERNARY_TDM_RX_6:
  485. case AFE_PORT_ID_QUATERNARY_TDM_RX_7:
  486. case AFE_PORT_ID_QUINARY_TDM_RX:
  487. case AFE_PORT_ID_QUINARY_TDM_RX_1:
  488. case AFE_PORT_ID_QUINARY_TDM_RX_2:
  489. case AFE_PORT_ID_QUINARY_TDM_RX_3:
  490. case AFE_PORT_ID_QUINARY_TDM_RX_4:
  491. case AFE_PORT_ID_QUINARY_TDM_RX_5:
  492. case AFE_PORT_ID_QUINARY_TDM_RX_6:
  493. case AFE_PORT_ID_QUINARY_TDM_RX_7:
  494. case AFE_PORT_ID_USB_RX:
  495. case AFE_PORT_ID_INT0_MI2S_RX:
  496. case AFE_PORT_ID_INT1_MI2S_RX:
  497. case AFE_PORT_ID_INT2_MI2S_RX:
  498. case AFE_PORT_ID_INT3_MI2S_RX:
  499. case AFE_PORT_ID_INT4_MI2S_RX:
  500. case AFE_PORT_ID_INT5_MI2S_RX:
  501. case AFE_PORT_ID_INT6_MI2S_RX:
  502. ret = MSM_AFE_PORT_TYPE_RX;
  503. break;
  504. case PRIMARY_I2S_TX:
  505. case SECONDARY_I2S_TX:
  506. case MI2S_TX:
  507. case DIGI_MIC_TX:
  508. case VOICE_RECORD_TX:
  509. case SLIMBUS_0_TX:
  510. case SLIMBUS_1_TX:
  511. case SLIMBUS_2_TX:
  512. case SLIMBUS_3_TX:
  513. case SLIMBUS_4_TX:
  514. case SLIMBUS_5_TX:
  515. case SLIMBUS_6_TX:
  516. case SLIMBUS_7_TX:
  517. case SLIMBUS_8_TX:
  518. case INT_FM_TX:
  519. case VOICE_RECORD_RX:
  520. case INT_BT_SCO_TX:
  521. case RT_PROXY_PORT_001_TX:
  522. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  523. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  524. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  525. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  526. case AFE_PORT_ID_QUINARY_MI2S_TX:
  527. case AFE_PORT_ID_SENARY_MI2S_TX:
  528. case AFE_PORT_ID_PRIMARY_PCM_TX:
  529. case AFE_PORT_ID_SECONDARY_PCM_TX:
  530. case AFE_PORT_ID_TERTIARY_PCM_TX:
  531. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  532. case AFE_PORT_ID_QUINARY_PCM_TX:
  533. case AFE_PORT_ID_PRIMARY_TDM_TX:
  534. case AFE_PORT_ID_PRIMARY_TDM_TX_1:
  535. case AFE_PORT_ID_PRIMARY_TDM_TX_2:
  536. case AFE_PORT_ID_PRIMARY_TDM_TX_3:
  537. case AFE_PORT_ID_PRIMARY_TDM_TX_4:
  538. case AFE_PORT_ID_PRIMARY_TDM_TX_5:
  539. case AFE_PORT_ID_PRIMARY_TDM_TX_6:
  540. case AFE_PORT_ID_PRIMARY_TDM_TX_7:
  541. case AFE_PORT_ID_SECONDARY_TDM_TX:
  542. case AFE_PORT_ID_SECONDARY_TDM_TX_1:
  543. case AFE_PORT_ID_SECONDARY_TDM_TX_2:
  544. case AFE_PORT_ID_SECONDARY_TDM_TX_3:
  545. case AFE_PORT_ID_SECONDARY_TDM_TX_4:
  546. case AFE_PORT_ID_SECONDARY_TDM_TX_5:
  547. case AFE_PORT_ID_SECONDARY_TDM_TX_6:
  548. case AFE_PORT_ID_SECONDARY_TDM_TX_7:
  549. case AFE_PORT_ID_TERTIARY_TDM_TX:
  550. case AFE_PORT_ID_TERTIARY_TDM_TX_1:
  551. case AFE_PORT_ID_TERTIARY_TDM_TX_2:
  552. case AFE_PORT_ID_TERTIARY_TDM_TX_3:
  553. case AFE_PORT_ID_TERTIARY_TDM_TX_4:
  554. case AFE_PORT_ID_TERTIARY_TDM_TX_5:
  555. case AFE_PORT_ID_TERTIARY_TDM_TX_6:
  556. case AFE_PORT_ID_TERTIARY_TDM_TX_7:
  557. case AFE_PORT_ID_QUATERNARY_TDM_TX:
  558. case AFE_PORT_ID_QUATERNARY_TDM_TX_1:
  559. case AFE_PORT_ID_QUATERNARY_TDM_TX_2:
  560. case AFE_PORT_ID_QUATERNARY_TDM_TX_3:
  561. case AFE_PORT_ID_QUATERNARY_TDM_TX_4:
  562. case AFE_PORT_ID_QUATERNARY_TDM_TX_5:
  563. case AFE_PORT_ID_QUATERNARY_TDM_TX_6:
  564. case AFE_PORT_ID_QUATERNARY_TDM_TX_7:
  565. case AFE_PORT_ID_QUINARY_TDM_TX:
  566. case AFE_PORT_ID_QUINARY_TDM_TX_1:
  567. case AFE_PORT_ID_QUINARY_TDM_TX_2:
  568. case AFE_PORT_ID_QUINARY_TDM_TX_3:
  569. case AFE_PORT_ID_QUINARY_TDM_TX_4:
  570. case AFE_PORT_ID_QUINARY_TDM_TX_5:
  571. case AFE_PORT_ID_QUINARY_TDM_TX_6:
  572. case AFE_PORT_ID_QUINARY_TDM_TX_7:
  573. case AFE_PORT_ID_USB_TX:
  574. case AFE_PORT_ID_INT0_MI2S_TX:
  575. case AFE_PORT_ID_INT1_MI2S_TX:
  576. case AFE_PORT_ID_INT2_MI2S_TX:
  577. case AFE_PORT_ID_INT3_MI2S_TX:
  578. case AFE_PORT_ID_INT4_MI2S_TX:
  579. case AFE_PORT_ID_INT5_MI2S_TX:
  580. case AFE_PORT_ID_INT6_MI2S_TX:
  581. ret = MSM_AFE_PORT_TYPE_TX;
  582. break;
  583. default:
  584. WARN_ON(1);
  585. pr_err("%s: Invalid port id = 0x%x\n",
  586. __func__, port_id);
  587. ret = -EINVAL;
  588. }
  589. return ret;
  590. }
  591. int afe_sizeof_cfg_cmd(u16 port_id)
  592. {
  593. int ret_size;
  594. switch (port_id) {
  595. case PRIMARY_I2S_RX:
  596. case PRIMARY_I2S_TX:
  597. case SECONDARY_I2S_RX:
  598. case SECONDARY_I2S_TX:
  599. case MI2S_RX:
  600. case MI2S_TX:
  601. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  602. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  603. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  604. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  605. case AFE_PORT_ID_QUINARY_MI2S_RX:
  606. case AFE_PORT_ID_QUINARY_MI2S_TX:
  607. ret_size = SIZEOF_CFG_CMD(afe_param_id_i2s_cfg);
  608. break;
  609. case HDMI_RX:
  610. case DISPLAY_PORT_RX:
  611. ret_size =
  612. SIZEOF_CFG_CMD(afe_param_id_hdmi_multi_chan_audio_cfg);
  613. break;
  614. case SLIMBUS_0_RX:
  615. case SLIMBUS_0_TX:
  616. case SLIMBUS_1_RX:
  617. case SLIMBUS_1_TX:
  618. case SLIMBUS_2_RX:
  619. case SLIMBUS_2_TX:
  620. case SLIMBUS_3_RX:
  621. case SLIMBUS_3_TX:
  622. case SLIMBUS_4_RX:
  623. case SLIMBUS_4_TX:
  624. case SLIMBUS_5_RX:
  625. case SLIMBUS_5_TX:
  626. case SLIMBUS_6_RX:
  627. case SLIMBUS_6_TX:
  628. case SLIMBUS_7_RX:
  629. case SLIMBUS_7_TX:
  630. case SLIMBUS_8_RX:
  631. case SLIMBUS_8_TX:
  632. ret_size = SIZEOF_CFG_CMD(afe_param_id_slimbus_cfg);
  633. break;
  634. case VOICE_PLAYBACK_TX:
  635. case VOICE2_PLAYBACK_TX:
  636. case VOICE_RECORD_RX:
  637. case VOICE_RECORD_TX:
  638. ret_size = SIZEOF_CFG_CMD(afe_param_id_pseudo_port_cfg);
  639. break;
  640. case RT_PROXY_PORT_001_RX:
  641. case RT_PROXY_PORT_001_TX:
  642. ret_size = SIZEOF_CFG_CMD(afe_param_id_rt_proxy_port_cfg);
  643. break;
  644. case AFE_PORT_ID_USB_RX:
  645. case AFE_PORT_ID_USB_TX:
  646. ret_size = SIZEOF_CFG_CMD(afe_param_id_usb_audio_cfg);
  647. break;
  648. case AFE_PORT_ID_PRIMARY_PCM_RX:
  649. case AFE_PORT_ID_PRIMARY_PCM_TX:
  650. case AFE_PORT_ID_SECONDARY_PCM_RX:
  651. case AFE_PORT_ID_SECONDARY_PCM_TX:
  652. case AFE_PORT_ID_TERTIARY_PCM_RX:
  653. case AFE_PORT_ID_TERTIARY_PCM_TX:
  654. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  655. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  656. case AFE_PORT_ID_QUINARY_PCM_RX:
  657. case AFE_PORT_ID_QUINARY_PCM_TX:
  658. default:
  659. pr_debug("%s: default case 0x%x\n", __func__, port_id);
  660. ret_size = SIZEOF_CFG_CMD(afe_param_id_pcm_cfg);
  661. break;
  662. }
  663. return ret_size;
  664. }
  665. int afe_q6_interface_prepare(void)
  666. {
  667. int ret = 0;
  668. pr_debug("%s:\n", __func__);
  669. if (this_afe.apr == NULL) {
  670. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  671. 0xFFFFFFFF, &this_afe);
  672. if (this_afe.apr == NULL) {
  673. pr_err("%s: Unable to register AFE\n", __func__);
  674. ret = -ENODEV;
  675. }
  676. rtac_set_afe_handle(this_afe.apr);
  677. }
  678. return ret;
  679. }
  680. /*
  681. * afe_apr_send_pkt : returns 0 on success, negative otherwise.
  682. */
  683. static int afe_apr_send_pkt(void *data, wait_queue_head_t *wait)
  684. {
  685. int ret;
  686. if (wait)
  687. atomic_set(&this_afe.state, 1);
  688. atomic_set(&this_afe.status, 0);
  689. ret = apr_send_pkt(this_afe.apr, data);
  690. if (ret > 0) {
  691. if (wait) {
  692. ret = wait_event_timeout(*wait,
  693. (atomic_read(&this_afe.state) == 0),
  694. msecs_to_jiffies(TIMEOUT_MS));
  695. if (!ret) {
  696. ret = -ETIMEDOUT;
  697. } else if (atomic_read(&this_afe.status) > 0) {
  698. pr_err("%s: DSP returned error[%s]\n", __func__,
  699. adsp_err_get_err_str(atomic_read(
  700. &this_afe.status)));
  701. ret = adsp_err_get_lnx_err_code(
  702. atomic_read(&this_afe.status));
  703. } else {
  704. ret = 0;
  705. }
  706. } else {
  707. ret = 0;
  708. }
  709. } else if (ret == 0) {
  710. pr_err("%s: packet not transmitted\n", __func__);
  711. /* apr_send_pkt can return 0 when nothing is transmitted */
  712. ret = -EINVAL;
  713. }
  714. pr_debug("%s: leave %d\n", __func__, ret);
  715. return ret;
  716. }
  717. static int afe_send_cal_block(u16 port_id, struct cal_block_data *cal_block)
  718. {
  719. int result = 0;
  720. int index = 0;
  721. struct afe_audioif_config_command_no_payload afe_cal;
  722. if (!cal_block) {
  723. pr_debug("%s: No AFE cal to send!\n", __func__);
  724. result = -EINVAL;
  725. goto done;
  726. }
  727. if (cal_block->cal_data.size <= 0) {
  728. pr_debug("%s: AFE cal has invalid size!\n", __func__);
  729. result = -EINVAL;
  730. goto done;
  731. }
  732. index = q6audio_get_port_index(port_id);
  733. if (index < 0 || index >= AFE_MAX_PORTS) {
  734. pr_err("%s: AFE port index[%d] invalid!\n",
  735. __func__, index);
  736. result = -EINVAL;
  737. goto done;
  738. }
  739. afe_cal.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  740. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  741. afe_cal.hdr.pkt_size = sizeof(afe_cal);
  742. afe_cal.hdr.src_port = 0;
  743. afe_cal.hdr.dest_port = 0;
  744. afe_cal.hdr.token = index;
  745. afe_cal.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  746. afe_cal.param.port_id = port_id;
  747. afe_cal.param.payload_size = cal_block->cal_data.size;
  748. afe_cal.param.payload_address_lsw =
  749. lower_32_bits(cal_block->cal_data.paddr);
  750. afe_cal.param.payload_address_msw =
  751. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  752. afe_cal.param.mem_map_handle = cal_block->map_data.q6map_handle;
  753. pr_debug("%s: AFE cal sent for device port = 0x%x, cal size = %zd, cal addr = 0x%pK\n",
  754. __func__, port_id,
  755. cal_block->cal_data.size, &cal_block->cal_data.paddr);
  756. result = afe_apr_send_pkt(&afe_cal, &this_afe.wait[index]);
  757. if (result)
  758. pr_err("%s: AFE cal for port 0x%x failed %d\n",
  759. __func__, port_id, result);
  760. done:
  761. return result;
  762. }
  763. static int afe_send_custom_topology_block(struct cal_block_data *cal_block)
  764. {
  765. int result = 0;
  766. int index = 0;
  767. struct cmd_set_topologies afe_cal;
  768. if (!cal_block) {
  769. pr_err("%s: No AFE SVC cal to send!\n", __func__);
  770. return -EINVAL;
  771. }
  772. if (cal_block->cal_data.size <= 0) {
  773. pr_err("%s: AFE SVC cal has invalid size: %zd!\n",
  774. __func__, cal_block->cal_data.size);
  775. return -EINVAL;
  776. }
  777. afe_cal.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  778. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  779. afe_cal.hdr.pkt_size = sizeof(afe_cal);
  780. afe_cal.hdr.src_port = 0;
  781. afe_cal.hdr.dest_port = 0;
  782. afe_cal.hdr.token = index;
  783. afe_cal.hdr.opcode = AFE_CMD_ADD_TOPOLOGIES;
  784. afe_cal.payload_size = cal_block->cal_data.size;
  785. afe_cal.payload_addr_lsw =
  786. lower_32_bits(cal_block->cal_data.paddr);
  787. afe_cal.payload_addr_msw =
  788. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  789. afe_cal.mem_map_handle = cal_block->map_data.q6map_handle;
  790. pr_debug("%s:cmd_id:0x%x calsize:%zd memmap_hdl:0x%x caladdr:0x%pK",
  791. __func__, AFE_CMD_ADD_TOPOLOGIES, cal_block->cal_data.size,
  792. afe_cal.mem_map_handle, &cal_block->cal_data.paddr);
  793. result = afe_apr_send_pkt(&afe_cal, &this_afe.wait[index]);
  794. if (result)
  795. pr_err("%s: AFE send topology for command 0x%x failed %d\n",
  796. __func__, AFE_CMD_ADD_TOPOLOGIES, result);
  797. return result;
  798. }
  799. static void afe_send_custom_topology(void)
  800. {
  801. struct cal_block_data *cal_block = NULL;
  802. int cal_index = AFE_CUST_TOPOLOGY_CAL;
  803. int ret;
  804. if (this_afe.cal_data[cal_index] == NULL) {
  805. pr_err("%s: cal_index %d not allocated!\n",
  806. __func__, cal_index);
  807. return;
  808. }
  809. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  810. if (!this_afe.set_custom_topology)
  811. goto unlock;
  812. this_afe.set_custom_topology = 0;
  813. cal_block = cal_utils_get_only_cal_block(this_afe.cal_data[cal_index]);
  814. if (cal_block == NULL) {
  815. pr_err("%s cal_block not found!!\n", __func__);
  816. goto unlock;
  817. }
  818. pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
  819. ret = remap_cal_data(cal_block, cal_index);
  820. if (ret) {
  821. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  822. __func__, cal_index);
  823. goto unlock;
  824. }
  825. ret = afe_send_custom_topology_block(cal_block);
  826. if (ret < 0) {
  827. pr_err("%s: No cal sent for cal_index %d! ret %d\n",
  828. __func__, cal_index, ret);
  829. goto unlock;
  830. }
  831. pr_debug("%s:sent custom topology for AFE\n", __func__);
  832. unlock:
  833. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  834. }
  835. static int afe_spk_ramp_dn_cfg(int port)
  836. {
  837. int ret = -EINVAL;
  838. int index = 0;
  839. struct afe_spkr_prot_config_command config;
  840. if (afe_get_port_type(port) != MSM_AFE_PORT_TYPE_RX) {
  841. pr_debug("%s: port doesn't match 0x%x\n", __func__, port);
  842. return 0;
  843. }
  844. if (this_afe.prot_cfg.mode == MSM_SPKR_PROT_DISABLED ||
  845. (this_afe.vi_rx_port != port)) {
  846. pr_debug("%s: spkr protection disabled port 0x%x %d 0x%x\n",
  847. __func__, port, ret, this_afe.vi_rx_port);
  848. return 0;
  849. }
  850. memset(&config, 0, sizeof(config));
  851. ret = q6audio_validate_port(port);
  852. if (ret < 0) {
  853. pr_err("%s: Invalid port 0x%x ret %d", __func__, port, ret);
  854. ret = -EINVAL;
  855. goto fail_cmd;
  856. }
  857. index = q6audio_get_port_index(port);
  858. if (index < 0 || index >= AFE_MAX_PORTS) {
  859. pr_err("%s: AFE port index[%d] invalid!\n",
  860. __func__, index);
  861. ret = -EINVAL;
  862. goto fail_cmd;
  863. }
  864. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  865. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  866. config.hdr.pkt_size = sizeof(config);
  867. config.hdr.src_port = 0;
  868. config.hdr.dest_port = 0;
  869. config.hdr.token = index;
  870. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  871. config.param.port_id = q6audio_get_port_id(port);
  872. config.param.payload_size =
  873. sizeof(config) - sizeof(config.hdr) - sizeof(config.param)
  874. - sizeof(config.prot_config);
  875. config.pdata.module_id = AFE_MODULE_FB_SPKR_PROT_V2_RX;
  876. config.pdata.param_id = AFE_PARAM_ID_FBSP_PTONE_RAMP_CFG;
  877. config.pdata.param_size = 0;
  878. atomic_set(&this_afe.state, 1);
  879. atomic_set(&this_afe.status, 0);
  880. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
  881. if (ret < 0) {
  882. pr_err("%s: port = 0x%x param = 0x%x failed %d\n",
  883. __func__, port, config.pdata.param_id, ret);
  884. goto fail_cmd;
  885. }
  886. ret = wait_event_timeout(this_afe.wait[index],
  887. (atomic_read(&this_afe.state) == 0),
  888. msecs_to_jiffies(TIMEOUT_MS));
  889. if (!ret) {
  890. pr_err("%s: wait_event timeout\n", __func__);
  891. ret = -EINVAL;
  892. goto fail_cmd;
  893. }
  894. if (atomic_read(&this_afe.status) > 0) {
  895. pr_err("%s: config cmd failed [%s]\n",
  896. __func__, adsp_err_get_err_str(
  897. atomic_read(&this_afe.status)));
  898. ret = adsp_err_get_lnx_err_code(
  899. atomic_read(&this_afe.status));
  900. goto fail_cmd;
  901. }
  902. /* dsp needs atleast 15ms to ramp down pilot tone*/
  903. usleep_range(15000, 15010);
  904. ret = 0;
  905. fail_cmd:
  906. pr_debug("%s: config.pdata.param_id 0x%x status %d\n",
  907. __func__, config.pdata.param_id, ret);
  908. return ret;
  909. }
  910. static int afe_spk_prot_prepare(int src_port, int dst_port, int param_id,
  911. union afe_spkr_prot_config *prot_config)
  912. {
  913. int ret = -EINVAL;
  914. int index = 0;
  915. struct afe_spkr_prot_config_command config;
  916. memset(&config, 0, sizeof(config));
  917. if (!prot_config) {
  918. pr_err("%s: Invalid params\n", __func__);
  919. goto fail_cmd;
  920. }
  921. ret = q6audio_validate_port(src_port);
  922. if (ret < 0) {
  923. pr_err("%s: Invalid src port 0x%x ret %d",
  924. __func__, src_port, ret);
  925. ret = -EINVAL;
  926. goto fail_cmd;
  927. }
  928. ret = q6audio_validate_port(dst_port);
  929. if (ret < 0) {
  930. pr_err("%s: Invalid dst port 0x%x ret %d", __func__,
  931. dst_port, ret);
  932. ret = -EINVAL;
  933. goto fail_cmd;
  934. }
  935. index = q6audio_get_port_index(src_port);
  936. if (index < 0 || index >= AFE_MAX_PORTS) {
  937. pr_err("%s: AFE port index[%d] invalid!\n",
  938. __func__, index);
  939. ret = -EINVAL;
  940. goto fail_cmd;
  941. }
  942. switch (param_id) {
  943. case AFE_PARAM_ID_FBSP_MODE_RX_CFG:
  944. config.pdata.module_id = AFE_MODULE_FB_SPKR_PROT_V2_RX;
  945. break;
  946. case AFE_PARAM_ID_FEEDBACK_PATH_CFG:
  947. this_afe.vi_tx_port = src_port;
  948. this_afe.vi_rx_port = dst_port;
  949. config.pdata.module_id = AFE_MODULE_FEEDBACK;
  950. break;
  951. /*
  952. * AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG_V2 is same as
  953. * AFE_PARAM_ID_SP_V2_TH_VI_MODE_CFG
  954. */
  955. case AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG_V2:
  956. case AFE_PARAM_ID_SP_V2_TH_VI_FTM_CFG:
  957. config.pdata.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_TH_VI;
  958. break;
  959. case AFE_PARAM_ID_SP_V2_EX_VI_MODE_CFG:
  960. case AFE_PARAM_ID_SP_V2_EX_VI_FTM_CFG:
  961. config.pdata.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_EX_VI;
  962. break;
  963. default:
  964. pr_err("%s: default case 0x%x\n", __func__, param_id);
  965. goto fail_cmd;
  966. }
  967. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  968. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  969. config.hdr.pkt_size = sizeof(config);
  970. config.hdr.src_port = 0;
  971. config.hdr.dest_port = 0;
  972. config.hdr.token = index;
  973. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  974. config.param.port_id = q6audio_get_port_id(src_port);
  975. config.param.payload_size = sizeof(config) - sizeof(config.hdr)
  976. - sizeof(config.param);
  977. config.pdata.param_id = param_id;
  978. config.pdata.param_size = sizeof(config.prot_config);
  979. config.prot_config = *prot_config;
  980. atomic_set(&this_afe.state, 1);
  981. atomic_set(&this_afe.status, 0);
  982. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
  983. if (ret < 0) {
  984. pr_err("%s: port = 0x%x param = 0x%x failed %d\n",
  985. __func__, src_port, param_id, ret);
  986. goto fail_cmd;
  987. }
  988. ret = wait_event_timeout(this_afe.wait[index],
  989. (atomic_read(&this_afe.state) == 0),
  990. msecs_to_jiffies(TIMEOUT_MS));
  991. if (!ret) {
  992. pr_err("%s: wait_event timeout\n", __func__);
  993. ret = -EINVAL;
  994. goto fail_cmd;
  995. }
  996. if (atomic_read(&this_afe.status) > 0) {
  997. pr_err("%s: config cmd failed [%s]\n",
  998. __func__, adsp_err_get_err_str(
  999. atomic_read(&this_afe.status)));
  1000. ret = adsp_err_get_lnx_err_code(
  1001. atomic_read(&this_afe.status));
  1002. goto fail_cmd;
  1003. }
  1004. ret = 0;
  1005. fail_cmd:
  1006. pr_debug("%s: config.pdata.param_id 0x%x status %d 0x%x\n",
  1007. __func__, config.pdata.param_id, ret, src_port);
  1008. return ret;
  1009. }
  1010. static void afe_send_cal_spkr_prot_tx(int port_id)
  1011. {
  1012. union afe_spkr_prot_config afe_spk_config;
  1013. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL ||
  1014. this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL ||
  1015. this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL] == NULL)
  1016. return;
  1017. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  1018. if ((this_afe.prot_cfg.mode != MSM_SPKR_PROT_DISABLED) &&
  1019. (this_afe.vi_tx_port == port_id)) {
  1020. if (this_afe.prot_cfg.mode ==
  1021. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS) {
  1022. afe_spk_config.vi_proc_cfg.operation_mode =
  1023. Q6AFE_MSM_SPKR_CALIBRATION;
  1024. afe_spk_config.vi_proc_cfg.quick_calib_flag =
  1025. this_afe.prot_cfg.quick_calib_flag;
  1026. } else {
  1027. afe_spk_config.vi_proc_cfg.operation_mode =
  1028. Q6AFE_MSM_SPKR_PROCESSING;
  1029. }
  1030. if (this_afe.th_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE)
  1031. afe_spk_config.vi_proc_cfg.operation_mode =
  1032. Q6AFE_MSM_SPKR_FTM_MODE;
  1033. afe_spk_config.vi_proc_cfg.minor_version = 1;
  1034. afe_spk_config.vi_proc_cfg.r0_cali_q24[SP_V2_SPKR_1] =
  1035. (uint32_t) this_afe.prot_cfg.r0[SP_V2_SPKR_1];
  1036. afe_spk_config.vi_proc_cfg.r0_cali_q24[SP_V2_SPKR_2] =
  1037. (uint32_t) this_afe.prot_cfg.r0[SP_V2_SPKR_2];
  1038. afe_spk_config.vi_proc_cfg.t0_cali_q6[SP_V2_SPKR_1] =
  1039. (uint32_t) this_afe.prot_cfg.t0[SP_V2_SPKR_1];
  1040. afe_spk_config.vi_proc_cfg.t0_cali_q6[SP_V2_SPKR_2] =
  1041. (uint32_t) this_afe.prot_cfg.t0[SP_V2_SPKR_2];
  1042. if (this_afe.prot_cfg.mode != MSM_SPKR_PROT_NOT_CALIBRATED) {
  1043. struct asm_spkr_calib_vi_proc_cfg *vi_proc_cfg;
  1044. vi_proc_cfg = &afe_spk_config.vi_proc_cfg;
  1045. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_1] =
  1046. USE_CALIBRATED_R0TO;
  1047. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_2] =
  1048. USE_CALIBRATED_R0TO;
  1049. } else {
  1050. struct asm_spkr_calib_vi_proc_cfg *vi_proc_cfg;
  1051. vi_proc_cfg = &afe_spk_config.vi_proc_cfg;
  1052. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_1] =
  1053. USE_SAFE_R0TO;
  1054. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_2] =
  1055. USE_SAFE_R0TO;
  1056. }
  1057. if (afe_spk_prot_prepare(port_id, 0,
  1058. AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG_V2,
  1059. &afe_spk_config))
  1060. pr_err("%s: SPKR_CALIB_VI_PROC_CFG failed\n",
  1061. __func__);
  1062. }
  1063. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  1064. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  1065. if ((this_afe.th_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE) &&
  1066. (this_afe.vi_tx_port == port_id)) {
  1067. afe_spk_config.th_vi_ftm_cfg.minor_version = 1;
  1068. afe_spk_config.th_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_1] =
  1069. this_afe.th_ftm_cfg.wait_time[SP_V2_SPKR_1];
  1070. afe_spk_config.th_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_2] =
  1071. this_afe.th_ftm_cfg.wait_time[SP_V2_SPKR_2];
  1072. afe_spk_config.th_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_1] =
  1073. this_afe.th_ftm_cfg.ftm_time[SP_V2_SPKR_1];
  1074. afe_spk_config.th_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_2] =
  1075. this_afe.th_ftm_cfg.ftm_time[SP_V2_SPKR_2];
  1076. if (afe_spk_prot_prepare(port_id, 0,
  1077. AFE_PARAM_ID_SP_V2_TH_VI_FTM_CFG,
  1078. &afe_spk_config))
  1079. pr_err("%s: th vi ftm cfg failed\n", __func__);
  1080. this_afe.th_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  1081. }
  1082. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  1083. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  1084. if ((this_afe.ex_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE) &&
  1085. (this_afe.vi_tx_port == port_id)) {
  1086. afe_spk_config.ex_vi_mode_cfg.minor_version = 1;
  1087. afe_spk_config.ex_vi_mode_cfg.operation_mode =
  1088. Q6AFE_MSM_SPKR_FTM_MODE;
  1089. if (afe_spk_prot_prepare(port_id, 0,
  1090. AFE_PARAM_ID_SP_V2_EX_VI_MODE_CFG,
  1091. &afe_spk_config))
  1092. pr_err("%s: ex vi mode cfg failed\n", __func__);
  1093. afe_spk_config.ex_vi_ftm_cfg.minor_version = 1;
  1094. afe_spk_config.ex_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_1] =
  1095. this_afe.ex_ftm_cfg.wait_time[SP_V2_SPKR_1];
  1096. afe_spk_config.ex_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_2] =
  1097. this_afe.ex_ftm_cfg.wait_time[SP_V2_SPKR_2];
  1098. afe_spk_config.ex_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_1] =
  1099. this_afe.ex_ftm_cfg.ftm_time[SP_V2_SPKR_1];
  1100. afe_spk_config.ex_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_2] =
  1101. this_afe.ex_ftm_cfg.ftm_time[SP_V2_SPKR_2];
  1102. if (afe_spk_prot_prepare(port_id, 0,
  1103. AFE_PARAM_ID_SP_V2_EX_VI_FTM_CFG,
  1104. &afe_spk_config))
  1105. pr_err("%s: ex vi ftm cfg failed\n", __func__);
  1106. this_afe.ex_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  1107. }
  1108. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  1109. }
  1110. static void afe_send_cal_spkr_prot_rx(int port_id)
  1111. {
  1112. union afe_spkr_prot_config afe_spk_config;
  1113. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL)
  1114. goto done;
  1115. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  1116. if (this_afe.prot_cfg.mode != MSM_SPKR_PROT_DISABLED &&
  1117. (this_afe.vi_rx_port == port_id)) {
  1118. if (this_afe.prot_cfg.mode ==
  1119. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS)
  1120. afe_spk_config.mode_rx_cfg.mode =
  1121. Q6AFE_MSM_SPKR_CALIBRATION;
  1122. else
  1123. afe_spk_config.mode_rx_cfg.mode =
  1124. Q6AFE_MSM_SPKR_PROCESSING;
  1125. afe_spk_config.mode_rx_cfg.minor_version = 1;
  1126. if (afe_spk_prot_prepare(port_id, 0,
  1127. AFE_PARAM_ID_FBSP_MODE_RX_CFG,
  1128. &afe_spk_config))
  1129. pr_err("%s: RX MODE_VI_PROC_CFG failed\n",
  1130. __func__);
  1131. }
  1132. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  1133. done:
  1134. return;
  1135. }
  1136. static int afe_send_hw_delay(u16 port_id, u32 rate)
  1137. {
  1138. struct audio_cal_hw_delay_entry delay_entry;
  1139. struct afe_audioif_config_command config;
  1140. int index = 0;
  1141. int ret = -EINVAL;
  1142. pr_debug("%s:\n", __func__);
  1143. memset(&delay_entry, 0, sizeof(delay_entry));
  1144. delay_entry.sample_rate = rate;
  1145. if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX)
  1146. ret = afe_get_cal_hw_delay(TX_DEVICE, &delay_entry);
  1147. else if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_RX)
  1148. ret = afe_get_cal_hw_delay(RX_DEVICE, &delay_entry);
  1149. /*
  1150. * HW delay is only used for IMS calls to sync audio with video
  1151. * It is only needed for devices & sample rates used for IMS video
  1152. * calls. Values are received from ACDB calbration files
  1153. */
  1154. if (ret != 0) {
  1155. pr_debug("%s: debug: HW delay info not available %d\n",
  1156. __func__, ret);
  1157. goto fail_cmd;
  1158. }
  1159. index = q6audio_get_port_index(port_id);
  1160. if (index < 0 || index >= AFE_MAX_PORTS) {
  1161. pr_err("%s: AFE port index[%d] invalid!\n",
  1162. __func__, index);
  1163. ret = -EINVAL;
  1164. goto fail_cmd;
  1165. }
  1166. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1167. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1168. config.hdr.pkt_size = sizeof(config);
  1169. config.hdr.src_port = 0;
  1170. config.hdr.dest_port = 0;
  1171. config.hdr.token = index;
  1172. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1173. config.param.port_id = q6audio_get_port_id(port_id);
  1174. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  1175. sizeof(config.param);
  1176. config.param.payload_address_lsw = 0x00;
  1177. config.param.payload_address_msw = 0x00;
  1178. config.param.mem_map_handle = 0x00;
  1179. config.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  1180. config.pdata.param_id = AFE_PARAM_ID_DEVICE_HW_DELAY;
  1181. config.pdata.param_size = sizeof(config.port);
  1182. config.port.hw_delay.delay_in_us = delay_entry.delay_usec;
  1183. config.port.hw_delay.device_hw_delay_minor_version =
  1184. AFE_API_VERSION_DEVICE_HW_DELAY;
  1185. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  1186. if (ret) {
  1187. pr_err("%s: AFE hw delay for port 0x%x failed %d\n",
  1188. __func__, port_id, ret);
  1189. goto fail_cmd;
  1190. }
  1191. fail_cmd:
  1192. pr_debug("%s: port_id 0x%x rate %u delay_usec %d status %d\n",
  1193. __func__, port_id, rate, delay_entry.delay_usec, ret);
  1194. return ret;
  1195. }
  1196. static struct cal_block_data *afe_find_cal_topo_id_by_port(
  1197. struct cal_type_data *cal_type, u16 port_id)
  1198. {
  1199. struct list_head *ptr, *next;
  1200. struct cal_block_data *cal_block = NULL;
  1201. int32_t path;
  1202. struct audio_cal_info_afe_top *afe_top;
  1203. int afe_port_index = q6audio_get_port_index(port_id);
  1204. if (afe_port_index < 0)
  1205. goto err_exit;
  1206. list_for_each_safe(ptr, next,
  1207. &cal_type->cal_blocks) {
  1208. cal_block = list_entry(ptr,
  1209. struct cal_block_data, list);
  1210. path = ((afe_get_port_type(port_id) ==
  1211. MSM_AFE_PORT_TYPE_TX)?(TX_DEVICE):(RX_DEVICE));
  1212. afe_top =
  1213. (struct audio_cal_info_afe_top *)cal_block->cal_info;
  1214. if (afe_top->path == path) {
  1215. if (this_afe.dev_acdb_id[afe_port_index] > 0) {
  1216. if (afe_top->acdb_id ==
  1217. this_afe.dev_acdb_id[afe_port_index]) {
  1218. pr_debug("%s: top_id:%x acdb_id:%d afe_port_id:%d\n",
  1219. __func__, afe_top->topology,
  1220. afe_top->acdb_id,
  1221. q6audio_get_port_id(port_id));
  1222. return cal_block;
  1223. }
  1224. } else {
  1225. pr_debug("%s: top_id:%x acdb_id:%d afe_port:%d\n",
  1226. __func__, afe_top->topology, afe_top->acdb_id,
  1227. q6audio_get_port_id(port_id));
  1228. return cal_block;
  1229. }
  1230. }
  1231. }
  1232. err_exit:
  1233. return NULL;
  1234. }
  1235. static int afe_get_cal_topology_id(u16 port_id, u32 *topology_id)
  1236. {
  1237. int ret = 0;
  1238. struct cal_block_data *cal_block = NULL;
  1239. struct audio_cal_info_afe_top *afe_top_info = NULL;
  1240. if (this_afe.cal_data[AFE_TOPOLOGY_CAL] == NULL) {
  1241. pr_err("%s: [AFE_TOPOLOGY_CAL] not initialized\n", __func__);
  1242. return -EINVAL;
  1243. }
  1244. if (topology_id == NULL) {
  1245. pr_err("%s: topology_id is NULL\n", __func__);
  1246. return -EINVAL;
  1247. }
  1248. *topology_id = 0;
  1249. mutex_lock(&this_afe.cal_data[AFE_TOPOLOGY_CAL]->lock);
  1250. cal_block = afe_find_cal_topo_id_by_port(
  1251. this_afe.cal_data[AFE_TOPOLOGY_CAL], port_id);
  1252. if (cal_block == NULL) {
  1253. pr_err("%s: [AFE_TOPOLOGY_CAL] not initialized for this port %d\n",
  1254. __func__, port_id);
  1255. ret = -EINVAL;
  1256. goto unlock;
  1257. }
  1258. afe_top_info = ((struct audio_cal_info_afe_top *)
  1259. cal_block->cal_info);
  1260. if (!afe_top_info->topology) {
  1261. pr_err("%s: invalid topology id : [%d, %d]\n",
  1262. __func__, afe_top_info->acdb_id, afe_top_info->topology);
  1263. ret = -EINVAL;
  1264. goto unlock;
  1265. }
  1266. *topology_id = (u32)afe_top_info->topology;
  1267. pr_debug("%s: port_id = %u acdb_id = %d topology_id = %u ret=%d\n",
  1268. __func__, port_id, afe_top_info->acdb_id,
  1269. afe_top_info->topology, ret);
  1270. unlock:
  1271. mutex_unlock(&this_afe.cal_data[AFE_TOPOLOGY_CAL]->lock);
  1272. return ret;
  1273. }
  1274. static int afe_send_port_topology_id(u16 port_id)
  1275. {
  1276. struct afe_audioif_config_command config;
  1277. int index = 0;
  1278. int ret = 0;
  1279. u32 topology_id = 0;
  1280. index = q6audio_get_port_index(port_id);
  1281. if (index < 0 || index >= AFE_MAX_PORTS) {
  1282. pr_err("%s: AFE port index[%d] invalid!\n",
  1283. __func__, index);
  1284. return -EINVAL;
  1285. }
  1286. ret = afe_get_cal_topology_id(port_id, &topology_id);
  1287. if (ret || !topology_id) {
  1288. pr_debug("%s: AFE port[%d] get_cal_topology[%d] invalid!\n",
  1289. __func__, port_id, topology_id);
  1290. goto done;
  1291. }
  1292. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1293. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1294. config.hdr.pkt_size = sizeof(config);
  1295. config.hdr.src_port = 0;
  1296. config.hdr.dest_port = 0;
  1297. config.hdr.token = index;
  1298. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1299. config.param.port_id = q6audio_get_port_id(port_id);
  1300. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  1301. sizeof(config.param);
  1302. config.param.payload_address_lsw = 0x00;
  1303. config.param.payload_address_msw = 0x00;
  1304. config.param.mem_map_handle = 0x00;
  1305. config.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  1306. config.pdata.param_id = AFE_PARAM_ID_SET_TOPOLOGY;
  1307. config.pdata.param_size = sizeof(config.port);
  1308. config.port.topology.minor_version = AFE_API_VERSION_TOPOLOGY_V1;
  1309. config.port.topology.topology_id = topology_id;
  1310. pr_debug("%s: param PL size=%d iparam_size[%d][%zd %zd %zd %zd] param_id[0x%x]\n",
  1311. __func__, config.param.payload_size, config.pdata.param_size,
  1312. sizeof(config), sizeof(config.param), sizeof(config.port),
  1313. sizeof(struct apr_hdr), config.pdata.param_id);
  1314. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  1315. if (ret) {
  1316. pr_err("%s: AFE set topology id enable for port 0x%x failed %d\n",
  1317. __func__, port_id, ret);
  1318. goto done;
  1319. }
  1320. this_afe.topology[index] = topology_id;
  1321. rtac_update_afe_topology(port_id);
  1322. done:
  1323. pr_debug("%s: AFE set topology id 0x%x enable for port 0x%x ret %d\n",
  1324. __func__, topology_id, port_id, ret);
  1325. return ret;
  1326. }
  1327. static int remap_cal_data(struct cal_block_data *cal_block, int cal_index)
  1328. {
  1329. int ret = 0;
  1330. if (cal_block->map_data.ion_client == NULL) {
  1331. pr_err("%s: No ION allocation for cal index %d!\n",
  1332. __func__, cal_index);
  1333. ret = -EINVAL;
  1334. goto done;
  1335. }
  1336. if ((cal_block->map_data.map_size > 0) &&
  1337. (cal_block->map_data.q6map_handle == 0)) {
  1338. atomic_set(&this_afe.mem_map_cal_index, cal_index);
  1339. ret = afe_cmd_memory_map(cal_block->cal_data.paddr,
  1340. cal_block->map_data.map_size);
  1341. atomic_set(&this_afe.mem_map_cal_index, -1);
  1342. if (ret < 0) {
  1343. pr_err("%s: mmap did not work! size = %zd ret %d\n",
  1344. __func__,
  1345. cal_block->map_data.map_size, ret);
  1346. pr_debug("%s: mmap did not work! addr = 0x%pK, size = %zd\n",
  1347. __func__,
  1348. &cal_block->cal_data.paddr,
  1349. cal_block->map_data.map_size);
  1350. goto done;
  1351. }
  1352. cal_block->map_data.q6map_handle = atomic_read(&this_afe.
  1353. mem_map_cal_handles[cal_index]);
  1354. }
  1355. done:
  1356. return ret;
  1357. }
  1358. static struct cal_block_data *afe_find_cal(int cal_index, int port_id)
  1359. {
  1360. struct list_head *ptr, *next;
  1361. struct cal_block_data *cal_block = NULL;
  1362. struct audio_cal_info_afe *afe_cal_info = NULL;
  1363. int afe_port_index = q6audio_get_port_index(port_id);
  1364. pr_debug("%s: cal_index %d port_id %d port_index %d\n", __func__,
  1365. cal_index, port_id, afe_port_index);
  1366. if (afe_port_index < 0) {
  1367. pr_err("%s: Error getting AFE port index %d\n",
  1368. __func__, afe_port_index);
  1369. goto exit;
  1370. }
  1371. list_for_each_safe(ptr, next,
  1372. &this_afe.cal_data[cal_index]->cal_blocks) {
  1373. cal_block = list_entry(ptr, struct cal_block_data, list);
  1374. afe_cal_info = cal_block->cal_info;
  1375. if ((afe_cal_info->acdb_id ==
  1376. this_afe.dev_acdb_id[afe_port_index]) &&
  1377. (afe_cal_info->sample_rate ==
  1378. this_afe.afe_sample_rates[afe_port_index])) {
  1379. pr_debug("%s: cal block is a match, size is %zd\n",
  1380. __func__, cal_block->cal_data.size);
  1381. goto exit;
  1382. }
  1383. }
  1384. pr_err("%s: no matching cal_block found\n", __func__);
  1385. cal_block = NULL;
  1386. exit:
  1387. return cal_block;
  1388. }
  1389. static void send_afe_cal_type(int cal_index, int port_id)
  1390. {
  1391. struct cal_block_data *cal_block = NULL;
  1392. int ret;
  1393. int afe_port_index = q6audio_get_port_index(port_id);
  1394. pr_debug("%s:\n", __func__);
  1395. if (this_afe.cal_data[cal_index] == NULL) {
  1396. pr_warn("%s: cal_index %d not allocated!\n",
  1397. __func__, cal_index);
  1398. goto done;
  1399. }
  1400. if (afe_port_index < 0) {
  1401. pr_err("%s: Error getting AFE port index %d\n",
  1402. __func__, afe_port_index);
  1403. goto done;
  1404. }
  1405. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  1406. if (((cal_index == AFE_COMMON_RX_CAL) ||
  1407. (cal_index == AFE_COMMON_TX_CAL)) &&
  1408. (this_afe.dev_acdb_id[afe_port_index] > 0))
  1409. cal_block = afe_find_cal(cal_index, port_id);
  1410. else
  1411. cal_block = cal_utils_get_only_cal_block(
  1412. this_afe.cal_data[cal_index]);
  1413. if (cal_block == NULL) {
  1414. pr_err("%s cal_block not found!!\n", __func__);
  1415. goto unlock;
  1416. }
  1417. pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
  1418. ret = remap_cal_data(cal_block, cal_index);
  1419. if (ret) {
  1420. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  1421. __func__, cal_index);
  1422. goto unlock;
  1423. }
  1424. ret = afe_send_cal_block(port_id, cal_block);
  1425. if (ret < 0)
  1426. pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d\n",
  1427. __func__, cal_index, port_id, ret);
  1428. unlock:
  1429. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  1430. done:
  1431. return;
  1432. }
  1433. void afe_send_cal(u16 port_id)
  1434. {
  1435. pr_debug("%s: port_id=0x%x\n", __func__, port_id);
  1436. if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) {
  1437. afe_send_cal_spkr_prot_tx(port_id);
  1438. send_afe_cal_type(AFE_COMMON_TX_CAL, port_id);
  1439. } else if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_RX) {
  1440. afe_send_cal_spkr_prot_rx(port_id);
  1441. send_afe_cal_type(AFE_COMMON_RX_CAL, port_id);
  1442. }
  1443. }
  1444. int afe_turn_onoff_hw_mad(u16 mad_type, u16 enable)
  1445. {
  1446. int ret;
  1447. struct afe_cmd_hw_mad_ctrl config;
  1448. pr_debug("%s: enter\n", __func__);
  1449. memset(&config, 0, sizeof(config));
  1450. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1451. APR_HDR_LEN(APR_HDR_SIZE),
  1452. APR_PKT_VER);
  1453. config.hdr.pkt_size = sizeof(config);
  1454. config.hdr.src_port = 0;
  1455. config.hdr.dest_port = 0;
  1456. config.hdr.token = IDX_GLOBAL_CFG;
  1457. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1458. config.param.port_id = SLIMBUS_5_TX;
  1459. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  1460. sizeof(config.param);
  1461. config.param.payload_address_lsw = 0x00;
  1462. config.param.payload_address_msw = 0x00;
  1463. config.param.mem_map_handle = 0x00;
  1464. config.pdata.module_id = AFE_MODULE_HW_MAD;
  1465. config.pdata.param_id = AFE_PARAM_ID_HW_MAD_CTRL;
  1466. config.pdata.param_size = sizeof(config.payload);
  1467. config.payload.minor_version = 1;
  1468. config.payload.mad_type = mad_type;
  1469. config.payload.mad_enable = enable;
  1470. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  1471. if (ret)
  1472. pr_err("%s: AFE_PARAM_ID_HW_MAD_CTRL failed %d\n", __func__,
  1473. ret);
  1474. return ret;
  1475. }
  1476. static int afe_send_slimbus_slave_cfg(
  1477. struct afe_param_cdc_slimbus_slave_cfg *sb_slave_cfg)
  1478. {
  1479. int ret;
  1480. struct afe_svc_cmd_sb_slave_cfg config;
  1481. pr_debug("%s: enter\n", __func__);
  1482. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1483. APR_HDR_LEN(APR_HDR_SIZE),
  1484. APR_PKT_VER);
  1485. config.hdr.pkt_size = sizeof(config);
  1486. config.hdr.src_port = 0;
  1487. config.hdr.dest_port = 0;
  1488. config.hdr.token = IDX_GLOBAL_CFG;
  1489. config.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  1490. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  1491. sizeof(config.param);
  1492. config.param.payload_address_lsw = 0x00;
  1493. config.param.payload_address_msw = 0x00;
  1494. config.param.mem_map_handle = 0x00;
  1495. config.pdata.module_id = AFE_MODULE_CDC_DEV_CFG;
  1496. config.pdata.param_id = AFE_PARAM_ID_CDC_SLIMBUS_SLAVE_CFG;
  1497. config.pdata.param_size =
  1498. sizeof(struct afe_param_cdc_slimbus_slave_cfg);
  1499. config.sb_slave_cfg = *sb_slave_cfg;
  1500. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  1501. if (ret)
  1502. pr_err("%s: AFE_PARAM_ID_CDC_SLIMBUS_SLAVE_CFG failed %d\n",
  1503. __func__, ret);
  1504. pr_debug("%s: leave %d\n", __func__, ret);
  1505. return ret;
  1506. }
  1507. static int afe_send_codec_reg_page_config(
  1508. struct afe_param_cdc_reg_page_cfg *cdc_reg_page_cfg)
  1509. {
  1510. struct afe_svc_cmd_cdc_reg_page_cfg config;
  1511. int ret;
  1512. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1513. APR_HDR_LEN(APR_HDR_SIZE),
  1514. APR_PKT_VER);
  1515. config.hdr.pkt_size = sizeof(config);
  1516. config.hdr.src_port = 0;
  1517. config.hdr.dest_port = 0;
  1518. config.hdr.token = IDX_GLOBAL_CFG;
  1519. config.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  1520. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  1521. sizeof(config.param);
  1522. config.param.payload_address_lsw = 0x00;
  1523. config.param.payload_address_msw = 0x00;
  1524. config.param.mem_map_handle = 0x00;
  1525. config.pdata.module_id = AFE_MODULE_CDC_DEV_CFG;
  1526. config.pdata.param_id = AFE_PARAM_ID_CDC_REG_PAGE_CFG;
  1527. config.pdata.param_size =
  1528. sizeof(struct afe_param_cdc_reg_page_cfg);
  1529. config.cdc_reg_page_cfg = *cdc_reg_page_cfg;
  1530. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  1531. if (ret)
  1532. pr_err("%s: AFE_PARAM_ID_CDC_REG_PAGE_CFG failed %d\n",
  1533. __func__, ret);
  1534. return ret;
  1535. }
  1536. static int afe_send_codec_reg_config(
  1537. struct afe_param_cdc_reg_cfg_data *cdc_reg_cfg)
  1538. {
  1539. int i, j, ret = -EINVAL;
  1540. int pkt_size, payload_size, reg_per_pkt, num_pkts, num_regs;
  1541. struct afe_svc_cmd_cdc_reg_cfg *config;
  1542. struct afe_svc_cmd_set_param *param;
  1543. reg_per_pkt = (APR_MAX_BUF - sizeof(*config)) /
  1544. sizeof(struct afe_param_cdc_reg_cfg_payload);
  1545. if (reg_per_pkt > 0) {
  1546. num_pkts = (cdc_reg_cfg->num_registers / reg_per_pkt) +
  1547. (cdc_reg_cfg->num_registers % reg_per_pkt == 0 ? 0 : 1);
  1548. } else {
  1549. pr_err("%s: Failed to build codec reg config APR packet\n",
  1550. __func__);
  1551. return -EINVAL;
  1552. }
  1553. for (j = 0; j < num_pkts; ++j) {
  1554. /*
  1555. * num_regs is set to reg_per_pkt on each pass through the loop
  1556. * except the last, when it is set to the number of registers
  1557. * remaining from the total
  1558. */
  1559. num_regs = (j < (num_pkts - 1) ? reg_per_pkt :
  1560. cdc_reg_cfg->num_registers - (reg_per_pkt * j));
  1561. payload_size = sizeof(struct afe_param_cdc_reg_cfg_payload) *
  1562. num_regs;
  1563. pkt_size = sizeof(*config) + payload_size;
  1564. pr_debug("%s: pkt_size %d, payload_size %d\n", __func__,
  1565. pkt_size, payload_size);
  1566. config = kzalloc(pkt_size, GFP_KERNEL);
  1567. if (!config)
  1568. return -ENOMEM;
  1569. config->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1570. APR_HDR_LEN(APR_HDR_SIZE),
  1571. APR_PKT_VER);
  1572. config->hdr.pkt_size = pkt_size;
  1573. config->hdr.src_port = 0;
  1574. config->hdr.dest_port = 0;
  1575. config->hdr.token = IDX_GLOBAL_CFG;
  1576. config->hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  1577. param = &config->param;
  1578. param->payload_size = payload_size;
  1579. param->payload_address_lsw = 0x00;
  1580. param->payload_address_msw = 0x00;
  1581. param->mem_map_handle = 0x00;
  1582. for (i = 0; i < num_regs; i++) {
  1583. config->reg_data[i].common.module_id =
  1584. AFE_MODULE_CDC_DEV_CFG;
  1585. config->reg_data[i].common.param_id =
  1586. AFE_PARAM_ID_CDC_REG_CFG;
  1587. config->reg_data[i].common.param_size =
  1588. sizeof(config->reg_data[i].reg_cfg);
  1589. config->reg_data[i].reg_cfg =
  1590. cdc_reg_cfg->reg_data[i + (j * reg_per_pkt)];
  1591. }
  1592. ret = afe_apr_send_pkt(config, &this_afe.wait[IDX_GLOBAL_CFG]);
  1593. if (ret) {
  1594. pr_err("%s: AFE_PARAM_ID_CDC_REG_CFG failed %d\n",
  1595. __func__, ret);
  1596. kfree(config);
  1597. break;
  1598. }
  1599. kfree(config);
  1600. }
  1601. return ret;
  1602. }
  1603. static int afe_init_cdc_reg_config(void)
  1604. {
  1605. int ret;
  1606. struct afe_svc_cmd_init_cdc_reg_cfg config;
  1607. pr_debug("%s: enter\n", __func__);
  1608. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1609. APR_HDR_LEN(APR_HDR_SIZE),
  1610. APR_PKT_VER);
  1611. config.hdr.pkt_size = sizeof(config);
  1612. config.hdr.src_port = 0;
  1613. config.hdr.dest_port = 0;
  1614. config.hdr.token = IDX_GLOBAL_CFG;
  1615. config.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  1616. config.param.payload_size = sizeof(struct afe_port_param_data_v2);
  1617. config.param.payload_address_lsw = 0x00;
  1618. config.param.payload_address_msw = 0x00;
  1619. config.param.mem_map_handle = 0x00;
  1620. config.init.module_id = AFE_MODULE_CDC_DEV_CFG;
  1621. config.init.param_id = AFE_PARAM_ID_CDC_REG_CFG_INIT;
  1622. config.init.param_size = 0;
  1623. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  1624. if (ret) {
  1625. pr_err("%s: AFE_PARAM_ID_CDC_INIT_REG_CFG failed %d\n",
  1626. __func__, ret);
  1627. }
  1628. return ret;
  1629. }
  1630. static int afe_send_slimbus_slave_port_cfg(
  1631. struct afe_param_slimbus_slave_port_cfg *port_config, u16 port_id)
  1632. {
  1633. int ret, index;
  1634. struct afe_cmd_hw_mad_slimbus_slave_port_cfg config;
  1635. pr_debug("%s: enter, port_id = 0x%x\n", __func__, port_id);
  1636. index = q6audio_get_port_index(port_id);
  1637. if (index < 0 || index >= AFE_MAX_PORTS) {
  1638. pr_err("%s: AFE port index[%d] invalid!\n",
  1639. __func__, index);
  1640. return -EINVAL;
  1641. }
  1642. ret = q6audio_validate_port(port_id);
  1643. if (ret < 0) {
  1644. pr_err("%s: port id = 0x%x ret %d\n", __func__, port_id, ret);
  1645. return -EINVAL;
  1646. }
  1647. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1648. APR_HDR_LEN(APR_HDR_SIZE),
  1649. APR_PKT_VER);
  1650. config.hdr.pkt_size = sizeof(config);
  1651. config.hdr.src_port = 0;
  1652. config.hdr.dest_port = 0;
  1653. config.hdr.token = index;
  1654. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1655. config.param.port_id = port_id;
  1656. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  1657. sizeof(config.param);
  1658. config.param.payload_address_lsw = 0x00;
  1659. config.param.payload_address_msw = 0x00;
  1660. config.param.mem_map_handle = 0x00;
  1661. config.pdata.module_id = AFE_MODULE_HW_MAD;
  1662. config.pdata.param_id = AFE_PARAM_ID_SLIMBUS_SLAVE_PORT_CFG;
  1663. config.pdata.param_size = sizeof(*port_config);
  1664. config.sb_port_cfg = *port_config;
  1665. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  1666. if (ret) {
  1667. pr_err("%s: AFE_PARAM_ID_SLIMBUS_SLAVE_PORT_CFG failed %d\n",
  1668. __func__, ret);
  1669. }
  1670. pr_debug("%s: leave %d\n", __func__, ret);
  1671. return ret;
  1672. }
  1673. static int afe_aanc_port_cfg(void *apr, uint16_t tx_port, uint16_t rx_port)
  1674. {
  1675. struct afe_port_cmd_set_aanc_param cfg;
  1676. int ret = 0;
  1677. int index = 0;
  1678. pr_debug("%s: tx_port 0x%x, rx_port 0x%x\n",
  1679. __func__, tx_port, rx_port);
  1680. ret = afe_q6_interface_prepare();
  1681. if (ret != 0) {
  1682. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  1683. return -EINVAL;
  1684. }
  1685. index = q6audio_get_port_index(tx_port);
  1686. if (index < 0 || index >= AFE_MAX_PORTS) {
  1687. pr_err("%s: AFE port index[%d] invalid!\n",
  1688. __func__, index);
  1689. return -EINVAL;
  1690. }
  1691. ret = q6audio_validate_port(tx_port);
  1692. if (ret < 0) {
  1693. pr_err("%s: port id: 0x%x ret %d\n", __func__, tx_port, ret);
  1694. return -EINVAL;
  1695. }
  1696. pr_debug("%s: AANC sample rate tx rate: %d rx rate %d\n",
  1697. __func__, this_afe.aanc_info.aanc_tx_port_sample_rate,
  1698. this_afe.aanc_info.aanc_rx_port_sample_rate);
  1699. /*
  1700. * If aanc tx sample rate or rx sample rate is zero, skip aanc
  1701. * configuration as AFE resampler will fail for invalid sample
  1702. * rates.
  1703. */
  1704. if (!this_afe.aanc_info.aanc_tx_port_sample_rate ||
  1705. !this_afe.aanc_info.aanc_rx_port_sample_rate) {
  1706. return -EINVAL;
  1707. }
  1708. cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1709. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1710. cfg.hdr.pkt_size = sizeof(cfg);
  1711. cfg.hdr.src_port = 0;
  1712. cfg.hdr.dest_port = 0;
  1713. cfg.hdr.token = index;
  1714. cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1715. cfg.param.port_id = tx_port;
  1716. cfg.param.payload_size = sizeof(struct afe_port_param_data_v2) +
  1717. sizeof(struct afe_param_aanc_port_cfg);
  1718. cfg.param.payload_address_lsw = 0;
  1719. cfg.param.payload_address_msw = 0;
  1720. cfg.param.mem_map_handle = 0;
  1721. cfg.pdata.module_id = AFE_MODULE_AANC;
  1722. cfg.pdata.param_id = AFE_PARAM_ID_AANC_PORT_CONFIG;
  1723. cfg.pdata.param_size = sizeof(struct afe_param_aanc_port_cfg);
  1724. cfg.pdata.reserved = 0;
  1725. cfg.data.aanc_port_cfg.aanc_port_cfg_minor_version =
  1726. AFE_API_VERSION_AANC_PORT_CONFIG;
  1727. cfg.data.aanc_port_cfg.tx_port_sample_rate =
  1728. this_afe.aanc_info.aanc_tx_port_sample_rate;
  1729. cfg.data.aanc_port_cfg.tx_port_channel_map[0] = AANC_TX_VOICE_MIC;
  1730. cfg.data.aanc_port_cfg.tx_port_channel_map[1] = AANC_TX_NOISE_MIC;
  1731. cfg.data.aanc_port_cfg.tx_port_channel_map[2] = AANC_TX_ERROR_MIC;
  1732. cfg.data.aanc_port_cfg.tx_port_channel_map[3] = AANC_TX_MIC_UNUSED;
  1733. cfg.data.aanc_port_cfg.tx_port_channel_map[4] = AANC_TX_MIC_UNUSED;
  1734. cfg.data.aanc_port_cfg.tx_port_channel_map[5] = AANC_TX_MIC_UNUSED;
  1735. cfg.data.aanc_port_cfg.tx_port_channel_map[6] = AANC_TX_MIC_UNUSED;
  1736. cfg.data.aanc_port_cfg.tx_port_channel_map[7] = AANC_TX_MIC_UNUSED;
  1737. cfg.data.aanc_port_cfg.tx_port_num_channels = 3;
  1738. cfg.data.aanc_port_cfg.rx_path_ref_port_id = rx_port;
  1739. cfg.data.aanc_port_cfg.ref_port_sample_rate =
  1740. this_afe.aanc_info.aanc_rx_port_sample_rate;
  1741. ret = afe_apr_send_pkt((uint32_t *) &cfg, &this_afe.wait[index]);
  1742. if (ret) {
  1743. pr_err("%s: AFE AANC port config failed for tx_port 0x%x, rx_port 0x%x ret %d\n",
  1744. __func__, tx_port, rx_port, ret);
  1745. }
  1746. return ret;
  1747. }
  1748. static int afe_aanc_mod_enable(void *apr, uint16_t tx_port, uint16_t enable)
  1749. {
  1750. struct afe_port_cmd_set_aanc_param cfg;
  1751. int ret = 0;
  1752. int index = 0;
  1753. pr_debug("%s: tx_port 0x%x\n",
  1754. __func__, tx_port);
  1755. ret = afe_q6_interface_prepare();
  1756. if (ret != 0) {
  1757. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  1758. return -EINVAL;
  1759. }
  1760. index = q6audio_get_port_index(tx_port);
  1761. if (index < 0 || index >= AFE_MAX_PORTS) {
  1762. pr_err("%s: AFE port index[%d] invalid!\n",
  1763. __func__, index);
  1764. return -EINVAL;
  1765. }
  1766. ret = q6audio_validate_port(tx_port);
  1767. if (ret < 0) {
  1768. pr_err("%s: port id: 0x%x ret %d\n", __func__, tx_port, ret);
  1769. return -EINVAL;
  1770. }
  1771. cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1772. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1773. cfg.hdr.pkt_size = sizeof(cfg);
  1774. cfg.hdr.src_port = 0;
  1775. cfg.hdr.dest_port = 0;
  1776. cfg.hdr.token = index;
  1777. cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1778. cfg.param.port_id = tx_port;
  1779. cfg.param.payload_size = sizeof(struct afe_port_param_data_v2) +
  1780. sizeof(struct afe_mod_enable_param);
  1781. cfg.param.payload_address_lsw = 0;
  1782. cfg.param.payload_address_lsw = 0;
  1783. cfg.param.mem_map_handle = 0;
  1784. cfg.pdata.module_id = AFE_MODULE_AANC;
  1785. cfg.pdata.param_id = AFE_PARAM_ID_ENABLE;
  1786. cfg.pdata.param_size = sizeof(struct afe_mod_enable_param);
  1787. cfg.pdata.reserved = 0;
  1788. cfg.data.mod_enable.enable = enable;
  1789. cfg.data.mod_enable.reserved = 0;
  1790. ret = afe_apr_send_pkt((uint32_t *) &cfg, &this_afe.wait[index]);
  1791. if (ret) {
  1792. pr_err("%s: AFE AANC enable failed for tx_port 0x%x ret %d\n",
  1793. __func__, tx_port, ret);
  1794. }
  1795. return ret;
  1796. }
  1797. static int afe_send_bank_selection_clip(
  1798. struct afe_param_id_clip_bank_sel *param)
  1799. {
  1800. int ret;
  1801. struct afe_svc_cmd_set_clip_bank_selection config;
  1802. if (!param) {
  1803. pr_err("%s: Invalid params", __func__);
  1804. return -EINVAL;
  1805. }
  1806. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1807. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1808. config.hdr.pkt_size = sizeof(config);
  1809. config.hdr.src_port = 0;
  1810. config.hdr.dest_port = 0;
  1811. config.hdr.token = IDX_GLOBAL_CFG;
  1812. config.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  1813. config.param.payload_size = sizeof(struct afe_port_param_data_v2) +
  1814. sizeof(struct afe_param_id_clip_bank_sel);
  1815. config.param.payload_address_lsw = 0x00;
  1816. config.param.payload_address_msw = 0x00;
  1817. config.param.mem_map_handle = 0x00;
  1818. config.pdata.module_id = AFE_MODULE_CDC_DEV_CFG;
  1819. config.pdata.param_id = AFE_PARAM_ID_CLIP_BANK_SEL_CFG;
  1820. config.pdata.param_size =
  1821. sizeof(struct afe_param_id_clip_bank_sel);
  1822. config.bank_sel = *param;
  1823. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  1824. if (ret) {
  1825. pr_err("%s: AFE_PARAM_ID_CLIP_BANK_SEL_CFG failed %d\n",
  1826. __func__, ret);
  1827. }
  1828. return ret;
  1829. }
  1830. int afe_send_aanc_version(
  1831. struct afe_param_id_cdc_aanc_version *version_cfg)
  1832. {
  1833. int ret;
  1834. struct afe_svc_cmd_cdc_aanc_version config;
  1835. pr_debug("%s: enter\n", __func__);
  1836. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1837. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1838. config.hdr.pkt_size = sizeof(config);
  1839. config.hdr.src_port = 0;
  1840. config.hdr.dest_port = 0;
  1841. config.hdr.token = IDX_GLOBAL_CFG;
  1842. config.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  1843. config.param.payload_size = sizeof(struct afe_port_param_data_v2) +
  1844. sizeof(struct afe_param_id_cdc_aanc_version);
  1845. config.param.payload_address_lsw = 0x00;
  1846. config.param.payload_address_msw = 0x00;
  1847. config.param.mem_map_handle = 0x00;
  1848. config.pdata.module_id = AFE_MODULE_CDC_DEV_CFG;
  1849. config.pdata.param_id = AFE_PARAM_ID_CDC_AANC_VERSION;
  1850. config.pdata.param_size =
  1851. sizeof(struct afe_param_id_cdc_aanc_version);
  1852. config.version = *version_cfg;
  1853. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  1854. if (ret) {
  1855. pr_err("%s: AFE_PARAM_ID_CDC_AANC_VERSION failed %d\n",
  1856. __func__, ret);
  1857. }
  1858. return ret;
  1859. }
  1860. int afe_port_set_mad_type(u16 port_id, enum afe_mad_type mad_type)
  1861. {
  1862. int i;
  1863. if (port_id == AFE_PORT_ID_TERTIARY_MI2S_TX ||
  1864. port_id == AFE_PORT_ID_INT3_MI2S_TX) {
  1865. mad_type = MAD_SW_AUDIO;
  1866. return 0;
  1867. }
  1868. i = port_id - SLIMBUS_0_RX;
  1869. if (i < 0 || i >= ARRAY_SIZE(afe_ports_mad_type)) {
  1870. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  1871. return -EINVAL;
  1872. }
  1873. atomic_set(&afe_ports_mad_type[i], mad_type);
  1874. return 0;
  1875. }
  1876. enum afe_mad_type afe_port_get_mad_type(u16 port_id)
  1877. {
  1878. int i;
  1879. if (port_id == AFE_PORT_ID_TERTIARY_MI2S_TX ||
  1880. port_id == AFE_PORT_ID_INT3_MI2S_TX)
  1881. return MAD_SW_AUDIO;
  1882. i = port_id - SLIMBUS_0_RX;
  1883. if (i < 0 || i >= ARRAY_SIZE(afe_ports_mad_type)) {
  1884. pr_debug("%s: Non Slimbus port_id 0x%x\n", __func__, port_id);
  1885. return MAD_HW_NONE;
  1886. }
  1887. return (enum afe_mad_type) atomic_read(&afe_ports_mad_type[i]);
  1888. }
  1889. int afe_set_config(enum afe_config_type config_type, void *config_data, int arg)
  1890. {
  1891. int ret;
  1892. pr_debug("%s: enter config_type %d\n", __func__, config_type);
  1893. ret = afe_q6_interface_prepare();
  1894. if (ret) {
  1895. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  1896. return ret;
  1897. }
  1898. switch (config_type) {
  1899. case AFE_SLIMBUS_SLAVE_CONFIG:
  1900. ret = afe_send_slimbus_slave_cfg(config_data);
  1901. if (!ret)
  1902. ret = afe_init_cdc_reg_config();
  1903. else
  1904. pr_err("%s: Sending slimbus slave config failed %d\n",
  1905. __func__, ret);
  1906. break;
  1907. case AFE_CDC_REGISTERS_CONFIG:
  1908. ret = afe_send_codec_reg_config(config_data);
  1909. break;
  1910. case AFE_SLIMBUS_SLAVE_PORT_CONFIG:
  1911. ret = afe_send_slimbus_slave_port_cfg(config_data, arg);
  1912. break;
  1913. case AFE_AANC_VERSION:
  1914. ret = afe_send_aanc_version(config_data);
  1915. break;
  1916. case AFE_CLIP_BANK_SEL:
  1917. ret = afe_send_bank_selection_clip(config_data);
  1918. break;
  1919. case AFE_CDC_CLIP_REGISTERS_CONFIG:
  1920. ret = afe_send_codec_reg_config(config_data);
  1921. break;
  1922. case AFE_CDC_REGISTER_PAGE_CONFIG:
  1923. ret = afe_send_codec_reg_page_config(config_data);
  1924. break;
  1925. default:
  1926. pr_err("%s: unknown configuration type %d",
  1927. __func__, config_type);
  1928. ret = -EINVAL;
  1929. }
  1930. if (!ret)
  1931. set_bit(config_type, &afe_configured_cmd);
  1932. return ret;
  1933. }
  1934. EXPORT_SYMBOL(afe_set_config);
  1935. /*
  1936. * afe_clear_config - If SSR happens ADSP loses AFE configs, let AFE driver know
  1937. * about the state so client driver can wait until AFE is
  1938. * reconfigured.
  1939. */
  1940. void afe_clear_config(enum afe_config_type config)
  1941. {
  1942. clear_bit(config, &afe_configured_cmd);
  1943. }
  1944. EXPORT_SYMBOL(afe_clear_config);
  1945. bool afe_has_config(enum afe_config_type config)
  1946. {
  1947. return !!test_bit(config, &afe_configured_cmd);
  1948. }
  1949. int afe_send_spdif_clk_cfg(struct afe_param_id_spdif_clk_cfg *cfg,
  1950. u16 port_id)
  1951. {
  1952. struct afe_spdif_clk_config_command clk_cfg;
  1953. int ret = 0;
  1954. int index = 0;
  1955. if (!cfg) {
  1956. pr_err("%s: Error, no configuration data\n", __func__);
  1957. ret = -EINVAL;
  1958. return ret;
  1959. }
  1960. index = q6audio_get_port_index(port_id);
  1961. if (index < 0 || index >= AFE_MAX_PORTS) {
  1962. pr_err("%s: AFE port index[%d] invalid!\n",
  1963. __func__, index);
  1964. return -EINVAL;
  1965. }
  1966. ret = q6audio_validate_port(port_id);
  1967. if (ret < 0) {
  1968. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  1969. return -EINVAL;
  1970. }
  1971. ret = afe_q6_interface_prepare();
  1972. if (ret) {
  1973. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  1974. return ret;
  1975. }
  1976. clk_cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1977. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1978. clk_cfg.hdr.pkt_size = sizeof(clk_cfg);
  1979. clk_cfg.hdr.src_port = 0;
  1980. clk_cfg.hdr.dest_port = 0;
  1981. clk_cfg.hdr.token = index;
  1982. clk_cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1983. clk_cfg.param.port_id = q6audio_get_port_id(port_id);
  1984. clk_cfg.param.payload_address_lsw = 0x00;
  1985. clk_cfg.param.payload_address_msw = 0x00;
  1986. clk_cfg.param.mem_map_handle = 0x00;
  1987. clk_cfg.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  1988. clk_cfg.pdata.param_id = AFE_PARAM_ID_SPDIF_CLK_CONFIG;
  1989. clk_cfg.pdata.param_size = sizeof(clk_cfg.clk_cfg);
  1990. clk_cfg.param.payload_size = sizeof(clk_cfg) - sizeof(struct apr_hdr)
  1991. - sizeof(clk_cfg.param);
  1992. clk_cfg.clk_cfg = *cfg;
  1993. pr_debug("%s: Minor version = 0x%x clk val = %d\n"
  1994. "clk root = 0x%x\n port id = 0x%x\n",
  1995. __func__, cfg->clk_cfg_minor_version,
  1996. cfg->clk_value, cfg->clk_root,
  1997. q6audio_get_port_id(port_id));
  1998. atomic_set(&this_afe.state, 1);
  1999. atomic_set(&this_afe.status, 0);
  2000. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &clk_cfg);
  2001. if (ret < 0) {
  2002. pr_err("%s: AFE send clock config for port 0x%x failed ret = %d\n",
  2003. __func__, port_id, ret);
  2004. ret = -EINVAL;
  2005. goto fail_cmd;
  2006. }
  2007. ret = wait_event_timeout(this_afe.wait[index],
  2008. (atomic_read(&this_afe.state) == 0),
  2009. msecs_to_jiffies(TIMEOUT_MS));
  2010. if (!ret) {
  2011. pr_err("%s: wait_event timeout\n",
  2012. __func__);
  2013. ret = -EINVAL;
  2014. goto fail_cmd;
  2015. }
  2016. if (atomic_read(&this_afe.status) > 0) {
  2017. pr_err("%s: config cmd failed [%s]\n",
  2018. __func__, adsp_err_get_err_str(
  2019. atomic_read(&this_afe.status)));
  2020. ret = adsp_err_get_lnx_err_code(
  2021. atomic_read(&this_afe.status));
  2022. goto fail_cmd;
  2023. }
  2024. fail_cmd:
  2025. return ret;
  2026. }
  2027. int afe_send_spdif_ch_status_cfg(struct afe_param_id_spdif_ch_status_cfg
  2028. *ch_status_cfg, u16 port_id)
  2029. {
  2030. struct afe_spdif_chstatus_config_command ch_status;
  2031. int ret = 0;
  2032. int index = 0;
  2033. if (!ch_status_cfg) {
  2034. pr_err("%s: Error, no configuration data\n", __func__);
  2035. ret = -EINVAL;
  2036. return ret;
  2037. }
  2038. index = q6audio_get_port_index(port_id);
  2039. if (index < 0 || index >= AFE_MAX_PORTS) {
  2040. pr_err("%s: AFE port index[%d] invalid!\n",
  2041. __func__, index);
  2042. return -EINVAL;
  2043. }
  2044. ret = q6audio_validate_port(port_id);
  2045. if (ret < 0) {
  2046. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  2047. return -EINVAL;
  2048. }
  2049. ret = afe_q6_interface_prepare();
  2050. if (ret != 0) {
  2051. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  2052. return ret;
  2053. }
  2054. ch_status.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2055. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2056. ch_status.hdr.pkt_size = sizeof(ch_status_cfg);
  2057. ch_status.hdr.src_port = 0;
  2058. ch_status.hdr.dest_port = 0;
  2059. ch_status.hdr.token = index;
  2060. ch_status.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  2061. ch_status.param.port_id = q6audio_get_port_id(port_id);
  2062. ch_status.param.payload_address_lsw = 0x00;
  2063. ch_status.param.payload_address_msw = 0x00;
  2064. ch_status.param.mem_map_handle = 0x00;
  2065. ch_status.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2066. ch_status.pdata.param_id = AFE_PARAM_ID_SPDIF_CLK_CONFIG;
  2067. ch_status.pdata.param_size = sizeof(ch_status.ch_status);
  2068. ch_status.param.payload_size = sizeof(ch_status)
  2069. - sizeof(struct apr_hdr) - sizeof(ch_status.param);
  2070. ch_status.ch_status = *ch_status_cfg;
  2071. atomic_set(&this_afe.state, 1);
  2072. atomic_set(&this_afe.status, 0);
  2073. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &ch_status);
  2074. if (ret < 0) {
  2075. pr_err("%s: AFE send channel status for port 0x%x failed ret = %d\n",
  2076. __func__, port_id, ret);
  2077. ret = -EINVAL;
  2078. goto fail_cmd;
  2079. }
  2080. ret = wait_event_timeout(this_afe.wait[index],
  2081. (atomic_read(&this_afe.state) == 0),
  2082. msecs_to_jiffies(TIMEOUT_MS));
  2083. if (!ret) {
  2084. pr_err("%s: wait_event timeout\n",
  2085. __func__);
  2086. ret = -EINVAL;
  2087. goto fail_cmd;
  2088. }
  2089. if (atomic_read(&this_afe.status) > 0) {
  2090. pr_err("%s: config cmd failed [%s]\n",
  2091. __func__, adsp_err_get_err_str(
  2092. atomic_read(&this_afe.status)));
  2093. ret = adsp_err_get_lnx_err_code(
  2094. atomic_read(&this_afe.status));
  2095. goto fail_cmd;
  2096. }
  2097. fail_cmd:
  2098. return ret;
  2099. }
  2100. static int afe_send_cmd_port_start(u16 port_id)
  2101. {
  2102. struct afe_port_cmd_device_start start;
  2103. int ret, index;
  2104. pr_debug("%s: enter\n", __func__);
  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 = q6audio_validate_port(port_id);
  2112. if (ret < 0) {
  2113. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  2114. return -EINVAL;
  2115. }
  2116. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2117. APR_HDR_LEN(APR_HDR_SIZE),
  2118. APR_PKT_VER);
  2119. start.hdr.pkt_size = sizeof(start);
  2120. start.hdr.src_port = 0;
  2121. start.hdr.dest_port = 0;
  2122. start.hdr.token = index;
  2123. start.hdr.opcode = AFE_PORT_CMD_DEVICE_START;
  2124. start.port_id = q6audio_get_port_id(port_id);
  2125. pr_debug("%s: cmd device start opcode[0x%x] port id[0x%x]\n",
  2126. __func__, start.hdr.opcode, start.port_id);
  2127. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  2128. if (ret) {
  2129. pr_err("%s: AFE enable for port 0x%x failed %d\n", __func__,
  2130. port_id, ret);
  2131. } else if (this_afe.task != current) {
  2132. this_afe.task = current;
  2133. pr_debug("task_name = %s pid = %d\n",
  2134. this_afe.task->comm, this_afe.task->pid);
  2135. }
  2136. return ret;
  2137. }
  2138. static int afe_aanc_start(uint16_t tx_port_id, uint16_t rx_port_id)
  2139. {
  2140. int ret;
  2141. pr_debug("%s: Tx port is 0x%x, Rx port is 0x%x\n",
  2142. __func__, tx_port_id, rx_port_id);
  2143. ret = afe_aanc_port_cfg(this_afe.apr, tx_port_id, rx_port_id);
  2144. if (ret) {
  2145. pr_err("%s: Send AANC Port Config failed %d\n",
  2146. __func__, ret);
  2147. goto fail_cmd;
  2148. }
  2149. send_afe_cal_type(AFE_AANC_CAL, tx_port_id);
  2150. fail_cmd:
  2151. return ret;
  2152. }
  2153. int afe_spdif_port_start(u16 port_id, struct afe_spdif_port_config *spdif_port,
  2154. u32 rate)
  2155. {
  2156. struct afe_audioif_config_command config;
  2157. int ret = 0;
  2158. int index = 0;
  2159. uint16_t port_index;
  2160. if (!spdif_port) {
  2161. pr_err("%s: Error, no configuration data\n", __func__);
  2162. ret = -EINVAL;
  2163. return ret;
  2164. }
  2165. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  2166. index = q6audio_get_port_index(port_id);
  2167. if (index < 0 || index >= AFE_MAX_PORTS) {
  2168. pr_err("%s: AFE port index[%d] invalid!\n",
  2169. __func__, index);
  2170. return -EINVAL;
  2171. }
  2172. ret = q6audio_validate_port(port_id);
  2173. if (ret < 0) {
  2174. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  2175. return -EINVAL;
  2176. }
  2177. afe_send_cal(port_id);
  2178. afe_send_hw_delay(port_id, rate);
  2179. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2180. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2181. config.hdr.pkt_size = sizeof(config);
  2182. config.hdr.src_port = 0;
  2183. config.hdr.dest_port = 0;
  2184. config.hdr.token = index;
  2185. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  2186. config.param.port_id = q6audio_get_port_id(port_id);
  2187. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  2188. sizeof(config.param);
  2189. config.param.payload_address_lsw = 0x00;
  2190. config.param.payload_address_msw = 0x00;
  2191. config.param.mem_map_handle = 0x00;
  2192. config.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2193. config.pdata.param_id = AFE_PARAM_ID_SPDIF_CONFIG;
  2194. config.pdata.param_size = sizeof(config.port);
  2195. config.port.spdif = spdif_port->cfg;
  2196. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  2197. if (ret) {
  2198. pr_err("%s: AFE enable for port 0x%x failed ret = %d\n",
  2199. __func__, port_id, ret);
  2200. goto fail_cmd;
  2201. }
  2202. port_index = afe_get_port_index(port_id);
  2203. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  2204. this_afe.afe_sample_rates[port_index] = rate;
  2205. } else {
  2206. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  2207. ret = -EINVAL;
  2208. goto fail_cmd;
  2209. }
  2210. ret = afe_send_spdif_ch_status_cfg(&spdif_port->ch_status, port_id);
  2211. if (ret < 0) {
  2212. pr_err("%s: afe send failed %d\n", __func__, ret);
  2213. goto fail_cmd;
  2214. }
  2215. return afe_send_cmd_port_start(port_id);
  2216. fail_cmd:
  2217. return ret;
  2218. }
  2219. int afe_send_slot_mapping_cfg(
  2220. struct afe_param_id_slot_mapping_cfg *slot_mapping_cfg,
  2221. u16 port_id)
  2222. {
  2223. struct afe_slot_mapping_config_command config;
  2224. int ret = 0;
  2225. int index = 0;
  2226. if (!slot_mapping_cfg) {
  2227. pr_err("%s: Error, no configuration data\n", __func__);
  2228. return -EINVAL;
  2229. }
  2230. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  2231. index = q6audio_get_port_index(port_id);
  2232. if (index < 0 || index >= AFE_MAX_PORTS) {
  2233. pr_err("%s: AFE port index[%d] invalid!\n",
  2234. __func__, index);
  2235. return -EINVAL;
  2236. }
  2237. ret = q6audio_validate_port(port_id);
  2238. if (ret < 0) {
  2239. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  2240. return -EINVAL;
  2241. }
  2242. memset(&config, 0, sizeof(config));
  2243. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2244. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2245. config.hdr.pkt_size = sizeof(config);
  2246. config.hdr.src_port = 0;
  2247. config.hdr.dest_port = 0;
  2248. config.hdr.token = index;
  2249. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  2250. config.param.port_id = q6audio_get_port_id(port_id);
  2251. config.param.payload_size = sizeof(config)
  2252. - sizeof(struct apr_hdr) - sizeof(config.param);
  2253. config.param.payload_address_lsw = 0x00;
  2254. config.param.payload_address_msw = 0x00;
  2255. config.param.mem_map_handle = 0x00;
  2256. config.pdata.module_id = AFE_MODULE_TDM;
  2257. config.pdata.param_id = AFE_PARAM_ID_PORT_SLOT_MAPPING_CONFIG;
  2258. config.pdata.param_size = sizeof(config.slot_mapping);
  2259. config.slot_mapping = *slot_mapping_cfg;
  2260. atomic_set(&this_afe.state, 1);
  2261. atomic_set(&this_afe.status, 0);
  2262. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
  2263. if (ret < 0) {
  2264. pr_err("%s: AFE send slot mapping for port 0x%x failed ret = %d\n",
  2265. __func__, port_id, ret);
  2266. ret = -EINVAL;
  2267. goto fail_cmd;
  2268. }
  2269. ret = wait_event_timeout(this_afe.wait[index],
  2270. (atomic_read(&this_afe.state) == 0),
  2271. msecs_to_jiffies(TIMEOUT_MS));
  2272. if (!ret) {
  2273. pr_err("%s: wait_event timeout\n",
  2274. __func__);
  2275. ret = -EINVAL;
  2276. goto fail_cmd;
  2277. }
  2278. if (atomic_read(&this_afe.status) > 0) {
  2279. pr_err("%s: config cmd failed [%s]\n",
  2280. __func__, adsp_err_get_err_str(
  2281. atomic_read(&this_afe.status)));
  2282. ret = adsp_err_get_lnx_err_code(
  2283. atomic_read(&this_afe.status));
  2284. goto fail_cmd;
  2285. }
  2286. fail_cmd:
  2287. return ret;
  2288. }
  2289. int afe_send_custom_tdm_header_cfg(
  2290. struct afe_param_id_custom_tdm_header_cfg *custom_tdm_header_cfg,
  2291. u16 port_id)
  2292. {
  2293. struct afe_custom_tdm_header_config_command config;
  2294. int ret = 0;
  2295. int index = 0;
  2296. if (!custom_tdm_header_cfg) {
  2297. pr_err("%s: Error, no configuration data\n", __func__);
  2298. return -EINVAL;
  2299. }
  2300. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  2301. index = q6audio_get_port_index(port_id);
  2302. if (index < 0 || index >= AFE_MAX_PORTS) {
  2303. pr_err("%s: AFE port index[%d] invalid!\n",
  2304. __func__, index);
  2305. return -EINVAL;
  2306. }
  2307. ret = q6audio_validate_port(port_id);
  2308. if (ret < 0) {
  2309. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  2310. return -EINVAL;
  2311. }
  2312. memset(&config, 0, sizeof(config));
  2313. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2314. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2315. config.hdr.pkt_size = sizeof(config);
  2316. config.hdr.src_port = 0;
  2317. config.hdr.dest_port = 0;
  2318. config.hdr.token = index;
  2319. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  2320. config.param.port_id = q6audio_get_port_id(port_id);
  2321. config.param.payload_size = sizeof(config)
  2322. - sizeof(struct apr_hdr) - sizeof(config.param);
  2323. config.param.payload_address_lsw = 0x00;
  2324. config.param.payload_address_msw = 0x00;
  2325. config.param.mem_map_handle = 0x00;
  2326. config.pdata.module_id = AFE_MODULE_TDM;
  2327. config.pdata.param_id = AFE_PARAM_ID_CUSTOM_TDM_HEADER_CONFIG;
  2328. config.pdata.param_size = sizeof(config.custom_tdm_header);
  2329. config.custom_tdm_header = *custom_tdm_header_cfg;
  2330. atomic_set(&this_afe.state, 1);
  2331. atomic_set(&this_afe.status, 0);
  2332. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
  2333. if (ret < 0) {
  2334. pr_err("%s: AFE send custom tdm header for port 0x%x failed ret = %d\n",
  2335. __func__, port_id, ret);
  2336. ret = -EINVAL;
  2337. goto fail_cmd;
  2338. }
  2339. ret = wait_event_timeout(this_afe.wait[index],
  2340. (atomic_read(&this_afe.state) == 0),
  2341. msecs_to_jiffies(TIMEOUT_MS));
  2342. if (!ret) {
  2343. pr_err("%s: wait_event timeout\n",
  2344. __func__);
  2345. ret = -EINVAL;
  2346. goto fail_cmd;
  2347. }
  2348. if (atomic_read(&this_afe.status) > 0) {
  2349. pr_err("%s: config cmd failed [%s]\n",
  2350. __func__, adsp_err_get_err_str(
  2351. atomic_read(&this_afe.status)));
  2352. ret = adsp_err_get_lnx_err_code(
  2353. atomic_read(&this_afe.status));
  2354. goto fail_cmd;
  2355. }
  2356. fail_cmd:
  2357. return ret;
  2358. }
  2359. int afe_tdm_port_start(u16 port_id, struct afe_tdm_port_config *tdm_port,
  2360. u32 rate, u16 num_groups)
  2361. {
  2362. struct afe_audioif_config_command config;
  2363. int ret = 0;
  2364. int index = 0;
  2365. uint16_t port_index = 0;
  2366. enum afe_mad_type mad_type = MAD_HW_NONE;
  2367. if (!tdm_port) {
  2368. pr_err("%s: Error, no configuration data\n", __func__);
  2369. return -EINVAL;
  2370. }
  2371. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  2372. index = q6audio_get_port_index(port_id);
  2373. if (index < 0 || index >= AFE_MAX_PORTS) {
  2374. pr_err("%s: AFE port index[%d] invalid!\n",
  2375. __func__, index);
  2376. return -EINVAL;
  2377. }
  2378. ret = q6audio_validate_port(port_id);
  2379. if (ret < 0) {
  2380. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  2381. return -EINVAL;
  2382. }
  2383. ret = afe_q6_interface_prepare();
  2384. if (ret != 0) {
  2385. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  2386. return ret;
  2387. }
  2388. if ((index >= 0) && (index < AFE_MAX_PORTS)) {
  2389. this_afe.afe_sample_rates[index] = rate;
  2390. if (this_afe.rt_cb)
  2391. this_afe.dev_acdb_id[index] = this_afe.rt_cb(port_id);
  2392. }
  2393. /* Also send the topology id here: */
  2394. port_index = afe_get_port_index(port_id);
  2395. if (!(this_afe.afe_cal_mode[port_index] == AFE_CAL_MODE_NONE)) {
  2396. /* One time call: only for first time */
  2397. afe_send_custom_topology();
  2398. afe_send_port_topology_id(port_id);
  2399. afe_send_cal(port_id);
  2400. afe_send_hw_delay(port_id, rate);
  2401. }
  2402. /* Start SW MAD module */
  2403. mad_type = afe_port_get_mad_type(port_id);
  2404. pr_debug("%s: port_id 0x%x, mad_type %d\n", __func__, port_id,
  2405. mad_type);
  2406. if (mad_type != MAD_HW_NONE && mad_type != MAD_SW_AUDIO) {
  2407. if (!afe_has_config(AFE_CDC_REGISTERS_CONFIG) ||
  2408. !afe_has_config(AFE_SLIMBUS_SLAVE_CONFIG)) {
  2409. pr_err("%s: AFE isn't configured yet for\n"
  2410. "HW MAD try Again\n", __func__);
  2411. ret = -EAGAIN;
  2412. goto fail_cmd;
  2413. }
  2414. ret = afe_turn_onoff_hw_mad(mad_type, true);
  2415. if (ret) {
  2416. pr_err("%s: afe_turn_onoff_hw_mad failed %d\n",
  2417. __func__, ret);
  2418. goto fail_cmd;
  2419. }
  2420. }
  2421. memset(&config, 0, sizeof(config));
  2422. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2423. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2424. config.hdr.pkt_size = sizeof(config);
  2425. config.hdr.src_port = 0;
  2426. config.hdr.dest_port = 0;
  2427. config.hdr.token = index;
  2428. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  2429. config.param.port_id = q6audio_get_port_id(port_id);
  2430. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  2431. sizeof(config.param);
  2432. config.param.payload_address_lsw = 0x00;
  2433. config.param.payload_address_msw = 0x00;
  2434. config.param.mem_map_handle = 0x00;
  2435. config.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2436. config.pdata.param_id = AFE_PARAM_ID_TDM_CONFIG;
  2437. config.pdata.param_size = sizeof(config.port);
  2438. config.port.tdm = tdm_port->tdm;
  2439. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  2440. if (ret) {
  2441. pr_err("%s: AFE enable for port 0x%x failed ret = %d\n",
  2442. __func__, port_id, ret);
  2443. goto fail_cmd;
  2444. }
  2445. port_index = afe_get_port_index(port_id);
  2446. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  2447. this_afe.afe_sample_rates[port_index] = rate;
  2448. } else {
  2449. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  2450. ret = -EINVAL;
  2451. goto fail_cmd;
  2452. }
  2453. ret = afe_send_slot_mapping_cfg(&tdm_port->slot_mapping,
  2454. port_id);
  2455. if (ret < 0) {
  2456. pr_err("%s: afe send failed %d\n", __func__, ret);
  2457. goto fail_cmd;
  2458. }
  2459. if (tdm_port->custom_tdm_header.header_type) {
  2460. ret = afe_send_custom_tdm_header_cfg(
  2461. &tdm_port->custom_tdm_header, port_id);
  2462. if (ret < 0) {
  2463. pr_err("%s: afe send failed %d\n", __func__, ret);
  2464. goto fail_cmd;
  2465. }
  2466. }
  2467. ret = afe_send_cmd_port_start(port_id);
  2468. fail_cmd:
  2469. return ret;
  2470. }
  2471. void afe_set_cal_mode(u16 port_id, enum afe_cal_mode afe_cal_mode)
  2472. {
  2473. uint16_t port_index;
  2474. port_index = afe_get_port_index(port_id);
  2475. this_afe.afe_cal_mode[port_index] = afe_cal_mode;
  2476. }
  2477. void afe_set_routing_callback(routing_cb cb)
  2478. {
  2479. this_afe.rt_cb = cb;
  2480. }
  2481. int afe_port_send_usb_dev_param(u16 port_id, union afe_port_config *afe_config)
  2482. {
  2483. struct afe_usb_audio_dev_param_command config;
  2484. int ret = 0, index = 0;
  2485. if (!afe_config) {
  2486. pr_err("%s: Error, no configuration data\n", __func__);
  2487. ret = -EINVAL;
  2488. goto exit;
  2489. }
  2490. index = q6audio_get_port_index(port_id);
  2491. if (index < 0 || index >= AFE_MAX_PORTS) {
  2492. pr_err("%s: AFE port index[%d] invalid! for port ID 0x%x\n",
  2493. __func__, index, port_id);
  2494. ret = -EINVAL;
  2495. goto exit;
  2496. }
  2497. memset(&config, 0, sizeof(config));
  2498. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2499. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2500. config.hdr.pkt_size = sizeof(config);
  2501. config.hdr.src_port = 0;
  2502. config.hdr.dest_port = 0;
  2503. config.hdr.token = index;
  2504. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  2505. config.param.port_id = q6audio_get_port_id(port_id);
  2506. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  2507. sizeof(config.param);
  2508. config.param.payload_address_lsw = 0x00;
  2509. config.param.payload_address_msw = 0x00;
  2510. config.param.mem_map_handle = 0x00;
  2511. config.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2512. config.pdata.param_id = AFE_PARAM_ID_USB_AUDIO_DEV_PARAMS;
  2513. config.pdata.param_size = sizeof(config.usb_dev);
  2514. config.usb_dev.cfg_minor_version =
  2515. AFE_API_MINIOR_VERSION_USB_AUDIO_CONFIG;
  2516. config.usb_dev.dev_token = afe_config->usb_audio.dev_token;
  2517. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  2518. if (ret) {
  2519. pr_err("%s: AFE device param cmd failed %d\n",
  2520. __func__, ret);
  2521. ret = -EINVAL;
  2522. goto exit;
  2523. }
  2524. config.pdata.param_id = AFE_PARAM_ID_USB_AUDIO_DEV_LPCM_FMT;
  2525. config.pdata.param_size = sizeof(config.lpcm_fmt);
  2526. config.lpcm_fmt.cfg_minor_version =
  2527. AFE_API_MINIOR_VERSION_USB_AUDIO_CONFIG;
  2528. config.lpcm_fmt.endian = afe_config->usb_audio.endian;
  2529. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  2530. if (ret) {
  2531. pr_err("%s: AFE device param cmd LPCM_FMT failed %d\n",
  2532. __func__, ret);
  2533. ret = -EINVAL;
  2534. goto exit;
  2535. }
  2536. exit:
  2537. return ret;
  2538. }
  2539. static int q6afe_send_enc_config(u16 port_id,
  2540. union afe_enc_config_data *cfg, u32 format,
  2541. union afe_port_config afe_config,
  2542. u16 afe_in_channels, u16 afe_in_bit_width)
  2543. {
  2544. struct afe_audioif_config_command config;
  2545. int index;
  2546. int ret;
  2547. int payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  2548. sizeof(config.param) - sizeof(config.port);
  2549. pr_debug("%s:update DSP for enc format = %d\n", __func__, format);
  2550. if (format != ASM_MEDIA_FMT_SBC && format != ASM_MEDIA_FMT_AAC_V2 &&
  2551. format != ASM_MEDIA_FMT_APTX && format != ASM_MEDIA_FMT_APTX_HD &&
  2552. format != ASM_MEDIA_FMT_CELT) {
  2553. pr_err("%s:Unsuppported format Ignore AFE config\n", __func__);
  2554. return 0;
  2555. }
  2556. memset(&config, 0, sizeof(config));
  2557. index = q6audio_get_port_index(port_id);
  2558. if (index < 0) {
  2559. pr_err("%s: Invalid index number: %d\n", __func__, index);
  2560. return -EINVAL;
  2561. }
  2562. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2563. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2564. config.hdr.pkt_size = sizeof(config);
  2565. config.hdr.src_port = 0;
  2566. config.hdr.dest_port = 0;
  2567. config.hdr.token = index;
  2568. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  2569. config.param.port_id = q6audio_get_port_id(port_id);
  2570. config.param.payload_size = payload_size + sizeof(config.port.enc_fmt);
  2571. config.param.payload_address_lsw = 0x00;
  2572. config.param.payload_address_msw = 0x00;
  2573. config.param.mem_map_handle = 0x00;
  2574. config.pdata.module_id = AFE_MODULE_ID_ENCODER;
  2575. config.pdata.param_id = AFE_ENCODER_PARAM_ID_ENC_FMT_ID;
  2576. config.pdata.param_size = sizeof(config.port.enc_fmt);
  2577. config.port.enc_fmt.fmt_id = format;
  2578. pr_debug("%s:sending AFE_ENCODER_PARAM_ID_ENC_FMT_ID payload: %d\n",
  2579. __func__, config.param.payload_size);
  2580. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  2581. if (ret) {
  2582. pr_err("%s:unable to send AFE_ENCODER_PARAM_ID_ENC_FMT_ID",
  2583. __func__);
  2584. goto exit;
  2585. }
  2586. config.param.payload_size = payload_size
  2587. + sizeof(config.port.enc_blk_param);
  2588. pr_debug("%s:send AFE_ENCODER_PARAM_ID_ENC_CFG_BLK to DSP payload:%d\n",
  2589. __func__, config.param.payload_size);
  2590. config.pdata.param_id = AFE_ENCODER_PARAM_ID_ENC_CFG_BLK;
  2591. config.pdata.param_size = sizeof(config.port.enc_blk_param);
  2592. config.port.enc_blk_param.enc_cfg_blk_size =
  2593. sizeof(config.port.enc_blk_param.enc_blk_config);
  2594. config.port.enc_blk_param.enc_blk_config = *cfg;
  2595. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  2596. if (ret) {
  2597. pr_err("%s: AFE_ENCODER_PARAM_ID_ENC_CFG_BLK for port 0x%x failed %d\n",
  2598. __func__, port_id, ret);
  2599. goto exit;
  2600. }
  2601. if (format == ASM_MEDIA_FMT_APTX) {
  2602. config.param.payload_size =
  2603. payload_size + sizeof(config.port.sync_mode_param);
  2604. pr_debug("%s: sending AFE_PARAM_ID_APTX_SYNC_MODE to DSP",
  2605. __func__);
  2606. config.pdata.param_id = AFE_PARAM_ID_APTX_SYNC_MODE;
  2607. config.pdata.param_size = sizeof(config.port.sync_mode_param);
  2608. config.port.sync_mode_param.sync_mode =
  2609. config.port.enc_blk_param.enc_blk_config.aptx_config.
  2610. aptx_v2_cfg.sync_mode;
  2611. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  2612. if (ret) {
  2613. pr_err("%s: AFE_PARAM_ID_APTX_SYNC_MODE for port 0x%x failed %d\n",
  2614. __func__, port_id, ret);
  2615. goto exit;
  2616. }
  2617. }
  2618. config.param.payload_size =
  2619. payload_size + sizeof(config.port.enc_pkt_id_param);
  2620. pr_debug("%s:sending AFE_ENCODER_PARAM_ID_PACKETIZER to DSP payload = %d",
  2621. __func__, config.param.payload_size);
  2622. config.pdata.param_id = AFE_ENCODER_PARAM_ID_PACKETIZER_ID;
  2623. config.pdata.param_size = sizeof(config.port.enc_pkt_id_param);
  2624. config.port.enc_pkt_id_param.enc_packetizer_id =
  2625. AFE_MODULE_ID_PACKETIZER_COP;
  2626. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  2627. if (ret) {
  2628. pr_err("%s: AFE_ENCODER_PARAM_ID_PACKETIZER for port 0x%x failed %d\n",
  2629. __func__, port_id, ret);
  2630. goto exit;
  2631. }
  2632. config.param.payload_size =
  2633. payload_size + sizeof(config.port.media_type);
  2634. config.pdata.param_size = sizeof(config.port.media_type);
  2635. pr_debug("%s:Sending AFE_API_VERSION_PORT_MEDIA_TYPE to DSP", __func__);
  2636. config.pdata.module_id = AFE_MODULE_PORT;
  2637. config.pdata.param_id = AFE_PARAM_ID_PORT_MEDIA_TYPE;
  2638. config.port.media_type.minor_version = AFE_API_VERSION_PORT_MEDIA_TYPE;
  2639. config.port.media_type.sample_rate = afe_config.slim_sch.sample_rate;
  2640. if (afe_in_bit_width)
  2641. config.port.media_type.bit_width = afe_in_bit_width;
  2642. else
  2643. config.port.media_type.bit_width =
  2644. afe_config.slim_sch.bit_width;
  2645. if (afe_in_channels)
  2646. config.port.media_type.num_channels = afe_in_channels;
  2647. else
  2648. config.port.media_type.num_channels =
  2649. afe_config.slim_sch.num_channels;
  2650. config.port.media_type.data_format = AFE_PORT_DATA_FORMAT_PCM;
  2651. config.port.media_type.reserved = 0;
  2652. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  2653. if (ret) {
  2654. pr_err("%s: AFE_API_VERSION_PORT_MEDIA_TYPE for port 0x%x failed %d\n",
  2655. __func__, port_id, ret);
  2656. goto exit;
  2657. }
  2658. exit:
  2659. return ret;
  2660. }
  2661. static int __afe_port_start(u16 port_id, union afe_port_config *afe_config,
  2662. u32 rate, u16 afe_in_channels, u16 afe_in_bit_width,
  2663. union afe_enc_config_data *cfg, u32 enc_format)
  2664. {
  2665. struct afe_audioif_config_command config;
  2666. int ret = 0;
  2667. int cfg_type;
  2668. int index = 0;
  2669. enum afe_mad_type mad_type;
  2670. uint16_t port_index;
  2671. if (!afe_config) {
  2672. pr_err("%s: Error, no configuration data\n", __func__);
  2673. ret = -EINVAL;
  2674. return ret;
  2675. }
  2676. if ((port_id == RT_PROXY_DAI_001_RX) ||
  2677. (port_id == RT_PROXY_DAI_002_TX)) {
  2678. pr_debug("%s: before incrementing pcm_afe_instance %d port_id 0x%x\n",
  2679. __func__,
  2680. pcm_afe_instance[port_id & 0x1], port_id);
  2681. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  2682. pcm_afe_instance[port_id & 0x1]++;
  2683. return 0;
  2684. }
  2685. if ((port_id == RT_PROXY_DAI_002_RX) ||
  2686. (port_id == RT_PROXY_DAI_001_TX)) {
  2687. pr_debug("%s: before incrementing proxy_afe_instance %d port_id 0x%x\n",
  2688. __func__,
  2689. proxy_afe_instance[port_id & 0x1], port_id);
  2690. if (!afe_close_done[port_id & 0x1]) {
  2691. /*close pcm dai corresponding to the proxy dai*/
  2692. afe_close(port_id - 0x10);
  2693. pcm_afe_instance[port_id & 0x1]++;
  2694. pr_debug("%s: reconfigure afe port again\n", __func__);
  2695. }
  2696. proxy_afe_instance[port_id & 0x1]++;
  2697. afe_close_done[port_id & 0x1] = false;
  2698. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  2699. }
  2700. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  2701. index = q6audio_get_port_index(port_id);
  2702. if (index < 0 || index >= AFE_MAX_PORTS) {
  2703. pr_err("%s: AFE port index[%d] invalid!\n",
  2704. __func__, index);
  2705. return -EINVAL;
  2706. }
  2707. ret = q6audio_validate_port(port_id);
  2708. if (ret < 0) {
  2709. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  2710. return -EINVAL;
  2711. }
  2712. ret = afe_q6_interface_prepare();
  2713. if (ret != 0) {
  2714. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  2715. return ret;
  2716. }
  2717. if ((index >= 0) && (index < AFE_MAX_PORTS)) {
  2718. this_afe.afe_sample_rates[index] = rate;
  2719. if (this_afe.rt_cb)
  2720. this_afe.dev_acdb_id[index] = this_afe.rt_cb(port_id);
  2721. }
  2722. mutex_lock(&this_afe.afe_cmd_lock);
  2723. /* Also send the topology id here: */
  2724. port_index = afe_get_port_index(port_id);
  2725. if (!(this_afe.afe_cal_mode[port_index] == AFE_CAL_MODE_NONE)) {
  2726. /* One time call: only for first time */
  2727. afe_send_custom_topology();
  2728. afe_send_port_topology_id(port_id);
  2729. afe_send_cal(port_id);
  2730. afe_send_hw_delay(port_id, rate);
  2731. }
  2732. /* Start SW MAD module */
  2733. mad_type = afe_port_get_mad_type(port_id);
  2734. pr_debug("%s: port_id 0x%x, mad_type %d\n", __func__, port_id,
  2735. mad_type);
  2736. if (mad_type != MAD_HW_NONE && mad_type != MAD_SW_AUDIO) {
  2737. if (!afe_has_config(AFE_CDC_REGISTERS_CONFIG) ||
  2738. !afe_has_config(AFE_SLIMBUS_SLAVE_CONFIG)) {
  2739. pr_err("%s: AFE isn't configured yet for\n"
  2740. "HW MAD try Again\n", __func__);
  2741. ret = -EAGAIN;
  2742. goto fail_cmd;
  2743. }
  2744. ret = afe_turn_onoff_hw_mad(mad_type, true);
  2745. if (ret) {
  2746. pr_err("%s: afe_turn_onoff_hw_mad failed %d\n",
  2747. __func__, ret);
  2748. goto fail_cmd;
  2749. }
  2750. }
  2751. if ((this_afe.aanc_info.aanc_active) &&
  2752. (this_afe.aanc_info.aanc_tx_port == port_id)) {
  2753. this_afe.aanc_info.aanc_tx_port_sample_rate = rate;
  2754. port_index =
  2755. afe_get_port_index(this_afe.aanc_info.aanc_rx_port);
  2756. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  2757. this_afe.aanc_info.aanc_rx_port_sample_rate =
  2758. this_afe.afe_sample_rates[port_index];
  2759. } else {
  2760. pr_err("%s: Invalid port index %d\n",
  2761. __func__, port_index);
  2762. ret = -EINVAL;
  2763. goto fail_cmd;
  2764. }
  2765. ret = afe_aanc_start(this_afe.aanc_info.aanc_tx_port,
  2766. this_afe.aanc_info.aanc_rx_port);
  2767. pr_debug("%s: afe_aanc_start ret %d\n", __func__, ret);
  2768. }
  2769. if ((port_id == AFE_PORT_ID_USB_RX) ||
  2770. (port_id == AFE_PORT_ID_USB_TX)) {
  2771. ret = afe_port_send_usb_dev_param(port_id, afe_config);
  2772. if (ret) {
  2773. pr_err("%s: AFE device param for port 0x%x failed %d\n",
  2774. __func__, port_id, ret);
  2775. ret = -EINVAL;
  2776. goto fail_cmd;
  2777. }
  2778. }
  2779. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2780. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2781. config.hdr.pkt_size = sizeof(config);
  2782. config.hdr.src_port = 0;
  2783. config.hdr.dest_port = 0;
  2784. config.hdr.token = index;
  2785. switch (port_id) {
  2786. case AFE_PORT_ID_PRIMARY_PCM_RX:
  2787. case AFE_PORT_ID_PRIMARY_PCM_TX:
  2788. case AFE_PORT_ID_SECONDARY_PCM_RX:
  2789. case AFE_PORT_ID_SECONDARY_PCM_TX:
  2790. case AFE_PORT_ID_TERTIARY_PCM_RX:
  2791. case AFE_PORT_ID_TERTIARY_PCM_TX:
  2792. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  2793. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  2794. case AFE_PORT_ID_QUINARY_PCM_RX:
  2795. case AFE_PORT_ID_QUINARY_PCM_TX:
  2796. cfg_type = AFE_PARAM_ID_PCM_CONFIG;
  2797. break;
  2798. case PRIMARY_I2S_RX:
  2799. case PRIMARY_I2S_TX:
  2800. case SECONDARY_I2S_RX:
  2801. case SECONDARY_I2S_TX:
  2802. case MI2S_RX:
  2803. case MI2S_TX:
  2804. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  2805. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  2806. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  2807. case AFE_PORT_ID_SECONDARY_MI2S_RX_SD1:
  2808. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  2809. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  2810. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  2811. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  2812. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  2813. case AFE_PORT_ID_QUINARY_MI2S_RX:
  2814. case AFE_PORT_ID_QUINARY_MI2S_TX:
  2815. case AFE_PORT_ID_SENARY_MI2S_TX:
  2816. case AFE_PORT_ID_INT0_MI2S_RX:
  2817. case AFE_PORT_ID_INT0_MI2S_TX:
  2818. case AFE_PORT_ID_INT1_MI2S_RX:
  2819. case AFE_PORT_ID_INT1_MI2S_TX:
  2820. case AFE_PORT_ID_INT2_MI2S_RX:
  2821. case AFE_PORT_ID_INT2_MI2S_TX:
  2822. case AFE_PORT_ID_INT3_MI2S_RX:
  2823. case AFE_PORT_ID_INT3_MI2S_TX:
  2824. case AFE_PORT_ID_INT4_MI2S_RX:
  2825. case AFE_PORT_ID_INT4_MI2S_TX:
  2826. case AFE_PORT_ID_INT5_MI2S_RX:
  2827. case AFE_PORT_ID_INT5_MI2S_TX:
  2828. case AFE_PORT_ID_INT6_MI2S_RX:
  2829. case AFE_PORT_ID_INT6_MI2S_TX:
  2830. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  2831. break;
  2832. case HDMI_RX:
  2833. case DISPLAY_PORT_RX:
  2834. cfg_type = AFE_PARAM_ID_HDMI_CONFIG;
  2835. break;
  2836. case VOICE_PLAYBACK_TX:
  2837. case VOICE2_PLAYBACK_TX:
  2838. case VOICE_RECORD_RX:
  2839. case VOICE_RECORD_TX:
  2840. cfg_type = AFE_PARAM_ID_PSEUDO_PORT_CONFIG;
  2841. break;
  2842. case SLIMBUS_0_RX:
  2843. case SLIMBUS_0_TX:
  2844. case SLIMBUS_1_RX:
  2845. case SLIMBUS_1_TX:
  2846. case SLIMBUS_2_RX:
  2847. case SLIMBUS_2_TX:
  2848. case SLIMBUS_3_RX:
  2849. case SLIMBUS_3_TX:
  2850. case SLIMBUS_4_RX:
  2851. case SLIMBUS_4_TX:
  2852. case SLIMBUS_5_RX:
  2853. case SLIMBUS_5_TX:
  2854. case SLIMBUS_6_RX:
  2855. case SLIMBUS_6_TX:
  2856. case SLIMBUS_7_RX:
  2857. case SLIMBUS_7_TX:
  2858. case SLIMBUS_8_RX:
  2859. case SLIMBUS_8_TX:
  2860. cfg_type = AFE_PARAM_ID_SLIMBUS_CONFIG;
  2861. break;
  2862. case AFE_PORT_ID_USB_RX:
  2863. case AFE_PORT_ID_USB_TX:
  2864. cfg_type = AFE_PARAM_ID_USB_AUDIO_CONFIG;
  2865. break;
  2866. case RT_PROXY_PORT_001_RX:
  2867. case RT_PROXY_PORT_001_TX:
  2868. cfg_type = AFE_PARAM_ID_RT_PROXY_CONFIG;
  2869. break;
  2870. case INT_BT_SCO_RX:
  2871. case INT_BT_A2DP_RX:
  2872. case INT_BT_SCO_TX:
  2873. case INT_FM_RX:
  2874. case INT_FM_TX:
  2875. cfg_type = AFE_PARAM_ID_INTERNAL_BT_FM_CONFIG;
  2876. break;
  2877. default:
  2878. pr_err("%s: Invalid port id 0x%x\n", __func__, port_id);
  2879. ret = -EINVAL;
  2880. goto fail_cmd;
  2881. }
  2882. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  2883. config.param.port_id = q6audio_get_port_id(port_id);
  2884. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  2885. sizeof(config.param);
  2886. config.param.payload_address_lsw = 0x00;
  2887. config.param.payload_address_msw = 0x00;
  2888. config.param.mem_map_handle = 0x00;
  2889. config.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2890. config.pdata.param_id = cfg_type;
  2891. config.pdata.param_size = sizeof(config.port);
  2892. config.port = *afe_config;
  2893. if ((enc_format != ASM_MEDIA_FMT_NONE) &&
  2894. (cfg_type == AFE_PARAM_ID_SLIMBUS_CONFIG)) {
  2895. config.port.slim_sch.data_format =
  2896. AFE_SB_DATA_FORMAT_GENERIC_COMPRESSED;
  2897. }
  2898. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  2899. if (ret) {
  2900. pr_err("%s: AFE enable for port 0x%x failed %d\n",
  2901. __func__, port_id, ret);
  2902. goto fail_cmd;
  2903. }
  2904. if ((enc_format != ASM_MEDIA_FMT_NONE) &&
  2905. (cfg_type == AFE_PARAM_ID_SLIMBUS_CONFIG)) {
  2906. pr_debug("%s: Found AFE encoder support for SLIMBUS enc_format = %d\n",
  2907. __func__, enc_format);
  2908. ret = q6afe_send_enc_config(port_id, cfg, enc_format,
  2909. *afe_config, afe_in_channels,
  2910. afe_in_bit_width);
  2911. if (ret) {
  2912. pr_err("%s: AFE encoder config for port 0x%x failed %d\n",
  2913. __func__, port_id, ret);
  2914. goto fail_cmd;
  2915. }
  2916. }
  2917. port_index = afe_get_port_index(port_id);
  2918. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  2919. /*
  2920. * If afe_port_start() for tx port called before
  2921. * rx port, then aanc rx sample rate is zero. So,
  2922. * AANC state machine in AFE will not get triggered.
  2923. * Make sure to check whether aanc is active during
  2924. * afe_port_start() for rx port and if aanc rx
  2925. * sample rate is zero, call afe_aanc_start to configure
  2926. * aanc with valid sample rates.
  2927. */
  2928. if (this_afe.aanc_info.aanc_active &&
  2929. !this_afe.aanc_info.aanc_rx_port_sample_rate) {
  2930. this_afe.aanc_info.aanc_rx_port_sample_rate =
  2931. this_afe.afe_sample_rates[port_index];
  2932. ret = afe_aanc_start(this_afe.aanc_info.aanc_tx_port,
  2933. this_afe.aanc_info.aanc_rx_port);
  2934. pr_debug("%s: afe_aanc_start ret %d\n", __func__, ret);
  2935. }
  2936. } else {
  2937. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  2938. ret = -EINVAL;
  2939. goto fail_cmd;
  2940. }
  2941. ret = afe_send_cmd_port_start(port_id);
  2942. fail_cmd:
  2943. mutex_unlock(&this_afe.afe_cmd_lock);
  2944. return ret;
  2945. }
  2946. /**
  2947. * afe_port_start - to configure AFE session with
  2948. * specified port configuration
  2949. *
  2950. * @port_id: AFE port id number
  2951. * @afe_config: port configutation
  2952. * @rate: sampling rate of port
  2953. *
  2954. * Returns 0 on success or error value on port start failure.
  2955. */
  2956. int afe_port_start(u16 port_id, union afe_port_config *afe_config,
  2957. u32 rate)
  2958. {
  2959. return __afe_port_start(port_id, afe_config, rate,
  2960. 0, 0, NULL, ASM_MEDIA_FMT_NONE);
  2961. }
  2962. EXPORT_SYMBOL(afe_port_start);
  2963. /**
  2964. * afe_port_start_v2 - to configure AFE session with
  2965. * specified port configuration and encoder params
  2966. *
  2967. * @port_id: AFE port id number
  2968. * @afe_config: port configutation
  2969. * @rate: sampling rate of port
  2970. * @cfg: AFE encoder configuration information to setup encoder
  2971. * @afe_in_channels: AFE input channel configuration, this needs
  2972. * update only if input channel is differ from AFE output
  2973. *
  2974. * Returns 0 on success or error value on port start failure.
  2975. */
  2976. int afe_port_start_v2(u16 port_id, union afe_port_config *afe_config,
  2977. u32 rate, u16 afe_in_channels, u16 afe_in_bit_width,
  2978. struct afe_enc_config *enc_cfg)
  2979. {
  2980. return __afe_port_start(port_id, afe_config, rate,
  2981. afe_in_channels, afe_in_bit_width,
  2982. &enc_cfg->data, enc_cfg->format);
  2983. }
  2984. EXPORT_SYMBOL(afe_port_start_v2);
  2985. int afe_get_port_index(u16 port_id)
  2986. {
  2987. switch (port_id) {
  2988. case PRIMARY_I2S_RX: return IDX_PRIMARY_I2S_RX;
  2989. case PRIMARY_I2S_TX: return IDX_PRIMARY_I2S_TX;
  2990. case AFE_PORT_ID_PRIMARY_PCM_RX:
  2991. return IDX_AFE_PORT_ID_PRIMARY_PCM_RX;
  2992. case AFE_PORT_ID_PRIMARY_PCM_TX:
  2993. return IDX_AFE_PORT_ID_PRIMARY_PCM_TX;
  2994. case AFE_PORT_ID_SECONDARY_PCM_RX:
  2995. return IDX_AFE_PORT_ID_SECONDARY_PCM_RX;
  2996. case AFE_PORT_ID_SECONDARY_PCM_TX:
  2997. return IDX_AFE_PORT_ID_SECONDARY_PCM_TX;
  2998. case AFE_PORT_ID_TERTIARY_PCM_RX:
  2999. return IDX_AFE_PORT_ID_TERTIARY_PCM_RX;
  3000. case AFE_PORT_ID_TERTIARY_PCM_TX:
  3001. return IDX_AFE_PORT_ID_TERTIARY_PCM_TX;
  3002. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  3003. return IDX_AFE_PORT_ID_QUATERNARY_PCM_RX;
  3004. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  3005. return IDX_AFE_PORT_ID_QUATERNARY_PCM_TX;
  3006. case AFE_PORT_ID_QUINARY_PCM_RX:
  3007. return IDX_AFE_PORT_ID_QUINARY_PCM_RX;
  3008. case AFE_PORT_ID_QUINARY_PCM_TX:
  3009. return IDX_AFE_PORT_ID_QUINARY_PCM_TX;
  3010. case SECONDARY_I2S_RX: return IDX_SECONDARY_I2S_RX;
  3011. case SECONDARY_I2S_TX: return IDX_SECONDARY_I2S_TX;
  3012. case MI2S_RX: return IDX_MI2S_RX;
  3013. case MI2S_TX: return IDX_MI2S_TX;
  3014. case HDMI_RX: return IDX_HDMI_RX;
  3015. case DISPLAY_PORT_RX: return IDX_DISPLAY_PORT_RX;
  3016. case AFE_PORT_ID_SPDIF_RX: return IDX_SPDIF_RX;
  3017. case RSVD_2: return IDX_RSVD_2;
  3018. case RSVD_3: return IDX_RSVD_3;
  3019. case DIGI_MIC_TX: return IDX_DIGI_MIC_TX;
  3020. case VOICE_RECORD_RX: return IDX_VOICE_RECORD_RX;
  3021. case VOICE_RECORD_TX: return IDX_VOICE_RECORD_TX;
  3022. case VOICE_PLAYBACK_TX: return IDX_VOICE_PLAYBACK_TX;
  3023. case VOICE2_PLAYBACK_TX: return IDX_VOICE2_PLAYBACK_TX;
  3024. case SLIMBUS_0_RX: return IDX_SLIMBUS_0_RX;
  3025. case SLIMBUS_0_TX: return IDX_SLIMBUS_0_TX;
  3026. case SLIMBUS_1_RX: return IDX_SLIMBUS_1_RX;
  3027. case SLIMBUS_1_TX: return IDX_SLIMBUS_1_TX;
  3028. case SLIMBUS_2_RX: return IDX_SLIMBUS_2_RX;
  3029. case SLIMBUS_2_TX: return IDX_SLIMBUS_2_TX;
  3030. case SLIMBUS_3_RX: return IDX_SLIMBUS_3_RX;
  3031. case SLIMBUS_3_TX: return IDX_SLIMBUS_3_TX;
  3032. case INT_BT_SCO_RX: return IDX_INT_BT_SCO_RX;
  3033. case INT_BT_SCO_TX: return IDX_INT_BT_SCO_TX;
  3034. case INT_BT_A2DP_RX: return IDX_INT_BT_A2DP_RX;
  3035. case INT_FM_RX: return IDX_INT_FM_RX;
  3036. case INT_FM_TX: return IDX_INT_FM_TX;
  3037. case RT_PROXY_PORT_001_RX: return IDX_RT_PROXY_PORT_001_RX;
  3038. case RT_PROXY_PORT_001_TX: return IDX_RT_PROXY_PORT_001_TX;
  3039. case SLIMBUS_4_RX: return IDX_SLIMBUS_4_RX;
  3040. case SLIMBUS_4_TX: return IDX_SLIMBUS_4_TX;
  3041. case SLIMBUS_5_RX: return IDX_SLIMBUS_5_RX;
  3042. case SLIMBUS_5_TX: return IDX_SLIMBUS_5_TX;
  3043. case SLIMBUS_6_RX: return IDX_SLIMBUS_6_RX;
  3044. case SLIMBUS_6_TX: return IDX_SLIMBUS_6_TX;
  3045. case SLIMBUS_7_RX: return IDX_SLIMBUS_7_RX;
  3046. case SLIMBUS_7_TX: return IDX_SLIMBUS_7_TX;
  3047. case SLIMBUS_8_RX: return IDX_SLIMBUS_8_RX;
  3048. case SLIMBUS_8_TX: return IDX_SLIMBUS_8_TX;
  3049. case AFE_PORT_ID_USB_RX: return IDX_AFE_PORT_ID_USB_RX;
  3050. case AFE_PORT_ID_USB_TX: return IDX_AFE_PORT_ID_USB_TX;
  3051. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  3052. return IDX_AFE_PORT_ID_PRIMARY_MI2S_RX;
  3053. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  3054. return IDX_AFE_PORT_ID_PRIMARY_MI2S_TX;
  3055. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  3056. return IDX_AFE_PORT_ID_QUATERNARY_MI2S_RX;
  3057. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  3058. return IDX_AFE_PORT_ID_QUATERNARY_MI2S_TX;
  3059. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  3060. return IDX_AFE_PORT_ID_SECONDARY_MI2S_RX;
  3061. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  3062. return IDX_AFE_PORT_ID_SECONDARY_MI2S_TX;
  3063. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  3064. return IDX_AFE_PORT_ID_TERTIARY_MI2S_RX;
  3065. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  3066. return IDX_AFE_PORT_ID_TERTIARY_MI2S_TX;
  3067. case AFE_PORT_ID_SECONDARY_MI2S_RX_SD1:
  3068. return IDX_AFE_PORT_ID_SECONDARY_MI2S_RX_SD1;
  3069. case AFE_PORT_ID_QUINARY_MI2S_RX:
  3070. return IDX_AFE_PORT_ID_QUINARY_MI2S_RX;
  3071. case AFE_PORT_ID_QUINARY_MI2S_TX:
  3072. return IDX_AFE_PORT_ID_QUINARY_MI2S_TX;
  3073. case AFE_PORT_ID_SENARY_MI2S_TX:
  3074. return IDX_AFE_PORT_ID_SENARY_MI2S_TX;
  3075. case AFE_PORT_ID_PRIMARY_TDM_RX:
  3076. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_0;
  3077. case AFE_PORT_ID_PRIMARY_TDM_TX:
  3078. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_0;
  3079. case AFE_PORT_ID_PRIMARY_TDM_RX_1:
  3080. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_1;
  3081. case AFE_PORT_ID_PRIMARY_TDM_TX_1:
  3082. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_1;
  3083. case AFE_PORT_ID_PRIMARY_TDM_RX_2:
  3084. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_2;
  3085. case AFE_PORT_ID_PRIMARY_TDM_TX_2:
  3086. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_2;
  3087. case AFE_PORT_ID_PRIMARY_TDM_RX_3:
  3088. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_3;
  3089. case AFE_PORT_ID_PRIMARY_TDM_TX_3:
  3090. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_3;
  3091. case AFE_PORT_ID_PRIMARY_TDM_RX_4:
  3092. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_4;
  3093. case AFE_PORT_ID_PRIMARY_TDM_TX_4:
  3094. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_4;
  3095. case AFE_PORT_ID_PRIMARY_TDM_RX_5:
  3096. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_5;
  3097. case AFE_PORT_ID_PRIMARY_TDM_TX_5:
  3098. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_5;
  3099. case AFE_PORT_ID_PRIMARY_TDM_RX_6:
  3100. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_6;
  3101. case AFE_PORT_ID_PRIMARY_TDM_TX_6:
  3102. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_6;
  3103. case AFE_PORT_ID_PRIMARY_TDM_RX_7:
  3104. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_7;
  3105. case AFE_PORT_ID_PRIMARY_TDM_TX_7:
  3106. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_7;
  3107. case AFE_PORT_ID_SECONDARY_TDM_RX:
  3108. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_0;
  3109. case AFE_PORT_ID_SECONDARY_TDM_TX:
  3110. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_0;
  3111. case AFE_PORT_ID_SECONDARY_TDM_RX_1:
  3112. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_1;
  3113. case AFE_PORT_ID_SECONDARY_TDM_TX_1:
  3114. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_1;
  3115. case AFE_PORT_ID_SECONDARY_TDM_RX_2:
  3116. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_2;
  3117. case AFE_PORT_ID_SECONDARY_TDM_TX_2:
  3118. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_2;
  3119. case AFE_PORT_ID_SECONDARY_TDM_RX_3:
  3120. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_3;
  3121. case AFE_PORT_ID_SECONDARY_TDM_TX_3:
  3122. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_3;
  3123. case AFE_PORT_ID_SECONDARY_TDM_RX_4:
  3124. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_4;
  3125. case AFE_PORT_ID_SECONDARY_TDM_TX_4:
  3126. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_4;
  3127. case AFE_PORT_ID_SECONDARY_TDM_RX_5:
  3128. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_5;
  3129. case AFE_PORT_ID_SECONDARY_TDM_TX_5:
  3130. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_5;
  3131. case AFE_PORT_ID_SECONDARY_TDM_RX_6:
  3132. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_6;
  3133. case AFE_PORT_ID_SECONDARY_TDM_TX_6:
  3134. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_6;
  3135. case AFE_PORT_ID_SECONDARY_TDM_RX_7:
  3136. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_7;
  3137. case AFE_PORT_ID_SECONDARY_TDM_TX_7:
  3138. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_7;
  3139. case AFE_PORT_ID_TERTIARY_TDM_RX:
  3140. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_0;
  3141. case AFE_PORT_ID_TERTIARY_TDM_TX:
  3142. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_0;
  3143. case AFE_PORT_ID_TERTIARY_TDM_RX_1:
  3144. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_1;
  3145. case AFE_PORT_ID_TERTIARY_TDM_TX_1:
  3146. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_1;
  3147. case AFE_PORT_ID_TERTIARY_TDM_RX_2:
  3148. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_2;
  3149. case AFE_PORT_ID_TERTIARY_TDM_TX_2:
  3150. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_2;
  3151. case AFE_PORT_ID_TERTIARY_TDM_RX_3:
  3152. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_3;
  3153. case AFE_PORT_ID_TERTIARY_TDM_TX_3:
  3154. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_3;
  3155. case AFE_PORT_ID_TERTIARY_TDM_RX_4:
  3156. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_4;
  3157. case AFE_PORT_ID_TERTIARY_TDM_TX_4:
  3158. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_4;
  3159. case AFE_PORT_ID_TERTIARY_TDM_RX_5:
  3160. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_5;
  3161. case AFE_PORT_ID_TERTIARY_TDM_TX_5:
  3162. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_5;
  3163. case AFE_PORT_ID_TERTIARY_TDM_RX_6:
  3164. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_6;
  3165. case AFE_PORT_ID_TERTIARY_TDM_TX_6:
  3166. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_6;
  3167. case AFE_PORT_ID_TERTIARY_TDM_RX_7:
  3168. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_7;
  3169. case AFE_PORT_ID_TERTIARY_TDM_TX_7:
  3170. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_7;
  3171. case AFE_PORT_ID_QUATERNARY_TDM_RX:
  3172. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_0;
  3173. case AFE_PORT_ID_QUATERNARY_TDM_TX:
  3174. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_0;
  3175. case AFE_PORT_ID_QUATERNARY_TDM_RX_1:
  3176. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_1;
  3177. case AFE_PORT_ID_QUATERNARY_TDM_TX_1:
  3178. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_1;
  3179. case AFE_PORT_ID_QUATERNARY_TDM_RX_2:
  3180. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_2;
  3181. case AFE_PORT_ID_QUATERNARY_TDM_TX_2:
  3182. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_2;
  3183. case AFE_PORT_ID_QUATERNARY_TDM_RX_3:
  3184. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_3;
  3185. case AFE_PORT_ID_QUATERNARY_TDM_TX_3:
  3186. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_3;
  3187. case AFE_PORT_ID_QUATERNARY_TDM_RX_4:
  3188. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_4;
  3189. case AFE_PORT_ID_QUATERNARY_TDM_TX_4:
  3190. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_4;
  3191. case AFE_PORT_ID_QUATERNARY_TDM_RX_5:
  3192. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_5;
  3193. case AFE_PORT_ID_QUATERNARY_TDM_TX_5:
  3194. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_5;
  3195. case AFE_PORT_ID_QUATERNARY_TDM_RX_6:
  3196. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_6;
  3197. case AFE_PORT_ID_QUATERNARY_TDM_TX_6:
  3198. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_6;
  3199. case AFE_PORT_ID_QUATERNARY_TDM_RX_7:
  3200. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_7;
  3201. case AFE_PORT_ID_QUATERNARY_TDM_TX_7:
  3202. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_7;
  3203. case AFE_PORT_ID_QUINARY_TDM_RX:
  3204. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_0;
  3205. case AFE_PORT_ID_QUINARY_TDM_TX:
  3206. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_0;
  3207. case AFE_PORT_ID_QUINARY_TDM_RX_1:
  3208. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_1;
  3209. case AFE_PORT_ID_QUINARY_TDM_TX_1:
  3210. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_1;
  3211. case AFE_PORT_ID_QUINARY_TDM_RX_2:
  3212. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_2;
  3213. case AFE_PORT_ID_QUINARY_TDM_TX_2:
  3214. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_2;
  3215. case AFE_PORT_ID_QUINARY_TDM_RX_3:
  3216. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_3;
  3217. case AFE_PORT_ID_QUINARY_TDM_TX_3:
  3218. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_3;
  3219. case AFE_PORT_ID_QUINARY_TDM_RX_4:
  3220. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_4;
  3221. case AFE_PORT_ID_QUINARY_TDM_TX_4:
  3222. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_4;
  3223. case AFE_PORT_ID_QUINARY_TDM_RX_5:
  3224. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_5;
  3225. case AFE_PORT_ID_QUINARY_TDM_TX_5:
  3226. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_5;
  3227. case AFE_PORT_ID_QUINARY_TDM_RX_6:
  3228. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_6;
  3229. case AFE_PORT_ID_QUINARY_TDM_TX_6:
  3230. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_6;
  3231. case AFE_PORT_ID_QUINARY_TDM_RX_7:
  3232. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_7;
  3233. case AFE_PORT_ID_QUINARY_TDM_TX_7:
  3234. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_7;
  3235. case AFE_PORT_ID_INT0_MI2S_RX:
  3236. return IDX_AFE_PORT_ID_INT0_MI2S_RX;
  3237. case AFE_PORT_ID_INT0_MI2S_TX:
  3238. return IDX_AFE_PORT_ID_INT0_MI2S_TX;
  3239. case AFE_PORT_ID_INT1_MI2S_RX:
  3240. return IDX_AFE_PORT_ID_INT1_MI2S_RX;
  3241. case AFE_PORT_ID_INT1_MI2S_TX:
  3242. return IDX_AFE_PORT_ID_INT1_MI2S_TX;
  3243. case AFE_PORT_ID_INT2_MI2S_RX:
  3244. return IDX_AFE_PORT_ID_INT2_MI2S_RX;
  3245. case AFE_PORT_ID_INT2_MI2S_TX:
  3246. return IDX_AFE_PORT_ID_INT2_MI2S_TX;
  3247. case AFE_PORT_ID_INT3_MI2S_RX:
  3248. return IDX_AFE_PORT_ID_INT3_MI2S_RX;
  3249. case AFE_PORT_ID_INT3_MI2S_TX:
  3250. return IDX_AFE_PORT_ID_INT3_MI2S_TX;
  3251. case AFE_PORT_ID_INT4_MI2S_RX:
  3252. return IDX_AFE_PORT_ID_INT4_MI2S_RX;
  3253. case AFE_PORT_ID_INT4_MI2S_TX:
  3254. return IDX_AFE_PORT_ID_INT4_MI2S_TX;
  3255. case AFE_PORT_ID_INT5_MI2S_RX:
  3256. return IDX_AFE_PORT_ID_INT5_MI2S_RX;
  3257. case AFE_PORT_ID_INT5_MI2S_TX:
  3258. return IDX_AFE_PORT_ID_INT5_MI2S_TX;
  3259. case AFE_PORT_ID_INT6_MI2S_RX:
  3260. return IDX_AFE_PORT_ID_INT6_MI2S_RX;
  3261. case AFE_PORT_ID_INT6_MI2S_TX:
  3262. return IDX_AFE_PORT_ID_INT6_MI2S_TX;
  3263. default:
  3264. pr_err("%s: port 0x%x\n", __func__, port_id);
  3265. return -EINVAL;
  3266. }
  3267. }
  3268. int afe_open(u16 port_id,
  3269. union afe_port_config *afe_config, int rate)
  3270. {
  3271. struct afe_port_cmd_device_start start;
  3272. struct afe_audioif_config_command config;
  3273. int ret = 0;
  3274. int cfg_type;
  3275. int index = 0;
  3276. if (!afe_config) {
  3277. pr_err("%s: Error, no configuration data\n", __func__);
  3278. ret = -EINVAL;
  3279. return ret;
  3280. }
  3281. pr_err("%s: port_id 0x%x rate %d\n", __func__, port_id, rate);
  3282. index = q6audio_get_port_index(port_id);
  3283. if (index < 0 || index >= AFE_MAX_PORTS) {
  3284. pr_err("%s: AFE port index[%d] invalid!\n",
  3285. __func__, index);
  3286. return -EINVAL;
  3287. }
  3288. ret = q6audio_validate_port(port_id);
  3289. if (ret < 0) {
  3290. pr_err("%s: Invalid port 0x%x ret %d", __func__, port_id, ret);
  3291. return -EINVAL;
  3292. }
  3293. if ((port_id == RT_PROXY_DAI_001_RX) ||
  3294. (port_id == RT_PROXY_DAI_002_TX)) {
  3295. pr_err("%s: wrong port 0x%x\n", __func__, port_id);
  3296. return -EINVAL;
  3297. }
  3298. if ((port_id == RT_PROXY_DAI_002_RX) ||
  3299. (port_id == RT_PROXY_DAI_001_TX))
  3300. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  3301. ret = afe_q6_interface_prepare();
  3302. if (ret != 0) {
  3303. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  3304. return -EINVAL;
  3305. }
  3306. if ((index >= 0) && (index < AFE_MAX_PORTS)) {
  3307. this_afe.afe_sample_rates[index] = rate;
  3308. if (this_afe.rt_cb)
  3309. this_afe.dev_acdb_id[index] = this_afe.rt_cb(port_id);
  3310. }
  3311. /* Also send the topology id here: */
  3312. afe_send_custom_topology(); /* One time call: only for first time */
  3313. afe_send_port_topology_id(port_id);
  3314. ret = q6audio_validate_port(port_id);
  3315. if (ret < 0) {
  3316. pr_err("%s: Failed : Invalid Port id = 0x%x ret %d\n",
  3317. __func__, port_id, ret);
  3318. return -EINVAL;
  3319. }
  3320. mutex_lock(&this_afe.afe_cmd_lock);
  3321. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3322. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3323. config.hdr.pkt_size = sizeof(config);
  3324. config.hdr.src_port = 0;
  3325. config.hdr.dest_port = 0;
  3326. config.hdr.token = index;
  3327. switch (port_id) {
  3328. case PRIMARY_I2S_RX:
  3329. case PRIMARY_I2S_TX:
  3330. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  3331. break;
  3332. case AFE_PORT_ID_PRIMARY_PCM_RX:
  3333. case AFE_PORT_ID_PRIMARY_PCM_TX:
  3334. case AFE_PORT_ID_SECONDARY_PCM_RX:
  3335. case AFE_PORT_ID_SECONDARY_PCM_TX:
  3336. case AFE_PORT_ID_TERTIARY_PCM_RX:
  3337. case AFE_PORT_ID_TERTIARY_PCM_TX:
  3338. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  3339. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  3340. case AFE_PORT_ID_QUINARY_PCM_RX:
  3341. case AFE_PORT_ID_QUINARY_PCM_TX:
  3342. cfg_type = AFE_PARAM_ID_PCM_CONFIG;
  3343. break;
  3344. case SECONDARY_I2S_RX:
  3345. case SECONDARY_I2S_TX:
  3346. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  3347. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  3348. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  3349. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  3350. case MI2S_RX:
  3351. case MI2S_TX:
  3352. case AFE_PORT_ID_QUINARY_MI2S_RX:
  3353. case AFE_PORT_ID_QUINARY_MI2S_TX:
  3354. case AFE_PORT_ID_SENARY_MI2S_TX:
  3355. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  3356. break;
  3357. case HDMI_RX:
  3358. case DISPLAY_PORT_RX:
  3359. cfg_type = AFE_PARAM_ID_HDMI_CONFIG;
  3360. break;
  3361. case SLIMBUS_0_RX:
  3362. case SLIMBUS_0_TX:
  3363. case SLIMBUS_1_RX:
  3364. case SLIMBUS_1_TX:
  3365. case SLIMBUS_2_RX:
  3366. case SLIMBUS_2_TX:
  3367. case SLIMBUS_3_RX:
  3368. case SLIMBUS_3_TX:
  3369. case SLIMBUS_4_RX:
  3370. case SLIMBUS_4_TX:
  3371. case SLIMBUS_5_RX:
  3372. case SLIMBUS_6_RX:
  3373. case SLIMBUS_6_TX:
  3374. case SLIMBUS_7_RX:
  3375. case SLIMBUS_7_TX:
  3376. case SLIMBUS_8_RX:
  3377. case SLIMBUS_8_TX:
  3378. cfg_type = AFE_PARAM_ID_SLIMBUS_CONFIG;
  3379. break;
  3380. case AFE_PORT_ID_USB_RX:
  3381. case AFE_PORT_ID_USB_TX:
  3382. cfg_type = AFE_PARAM_ID_USB_AUDIO_CONFIG;
  3383. break;
  3384. default:
  3385. pr_err("%s: Invalid port id 0x%x\n",
  3386. __func__, port_id);
  3387. ret = -EINVAL;
  3388. goto fail_cmd;
  3389. }
  3390. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  3391. config.param.port_id = q6audio_get_port_id(port_id);
  3392. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr)
  3393. - sizeof(config.param);
  3394. config.param.payload_address_lsw = 0x00;
  3395. config.param.payload_address_msw = 0x00;
  3396. config.param.mem_map_handle = 0x00;
  3397. config.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  3398. config.pdata.param_id = cfg_type;
  3399. config.pdata.param_size = sizeof(config.port);
  3400. config.port = *afe_config;
  3401. pr_debug("%s: param PL size=%d iparam_size[%d][%zd %zd %zd %zd] param_id[0x%x]\n",
  3402. __func__, config.param.payload_size, config.pdata.param_size,
  3403. sizeof(config), sizeof(config.param), sizeof(config.port),
  3404. sizeof(struct apr_hdr), config.pdata.param_id);
  3405. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  3406. if (ret) {
  3407. pr_err("%s: AFE enable for port 0x%x opcode[0x%x]failed %d\n",
  3408. __func__, port_id, cfg_type, ret);
  3409. goto fail_cmd;
  3410. }
  3411. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3412. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3413. start.hdr.pkt_size = sizeof(start);
  3414. start.hdr.src_port = 0;
  3415. start.hdr.dest_port = 0;
  3416. start.hdr.token = index;
  3417. start.hdr.opcode = AFE_PORT_CMD_DEVICE_START;
  3418. start.port_id = q6audio_get_port_id(port_id);
  3419. pr_debug("%s: cmd device start opcode[0x%x] port id[0x%x]\n",
  3420. __func__, start.hdr.opcode, start.port_id);
  3421. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  3422. if (ret) {
  3423. pr_err("%s: AFE enable for port 0x%x failed %d\n", __func__,
  3424. port_id, ret);
  3425. goto fail_cmd;
  3426. }
  3427. fail_cmd:
  3428. mutex_unlock(&this_afe.afe_cmd_lock);
  3429. return ret;
  3430. }
  3431. int afe_loopback(u16 enable, u16 rx_port, u16 tx_port)
  3432. {
  3433. struct afe_loopback_cfg_v1 lb_cmd;
  3434. int ret = 0;
  3435. int index = 0;
  3436. if (rx_port == MI2S_RX)
  3437. rx_port = AFE_PORT_ID_PRIMARY_MI2S_RX;
  3438. if (tx_port == MI2S_TX)
  3439. tx_port = AFE_PORT_ID_PRIMARY_MI2S_TX;
  3440. ret = afe_q6_interface_prepare();
  3441. if (ret != 0) {
  3442. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  3443. return ret;
  3444. }
  3445. index = q6audio_get_port_index(rx_port);
  3446. if (index < 0 || index >= AFE_MAX_PORTS) {
  3447. pr_err("%s: AFE port index[%d] invalid!\n",
  3448. __func__, index);
  3449. return -EINVAL;
  3450. }
  3451. ret = q6audio_validate_port(rx_port);
  3452. if (ret < 0) {
  3453. pr_err("%s: Invalid port 0x%x ret %d", __func__, rx_port, ret);
  3454. return -EINVAL;
  3455. }
  3456. lb_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3457. APR_HDR_LEN(20), APR_PKT_VER);
  3458. lb_cmd.hdr.pkt_size = sizeof(lb_cmd);
  3459. lb_cmd.hdr.src_port = 0;
  3460. lb_cmd.hdr.dest_port = 0;
  3461. lb_cmd.hdr.token = index;
  3462. lb_cmd.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  3463. lb_cmd.param.port_id = tx_port;
  3464. lb_cmd.param.payload_size = (sizeof(lb_cmd) - sizeof(struct apr_hdr) -
  3465. sizeof(struct afe_port_cmd_set_param_v2));
  3466. lb_cmd.param.payload_address_lsw = 0x00;
  3467. lb_cmd.param.payload_address_msw = 0x00;
  3468. lb_cmd.param.mem_map_handle = 0x00;
  3469. lb_cmd.pdata.module_id = AFE_MODULE_LOOPBACK;
  3470. lb_cmd.pdata.param_id = AFE_PARAM_ID_LOOPBACK_CONFIG;
  3471. lb_cmd.pdata.param_size = lb_cmd.param.payload_size -
  3472. sizeof(struct afe_port_param_data_v2);
  3473. lb_cmd.dst_port_id = rx_port;
  3474. lb_cmd.routing_mode = LB_MODE_DEFAULT;
  3475. lb_cmd.enable = (enable ? 1 : 0);
  3476. lb_cmd.loopback_cfg_minor_version = AFE_API_VERSION_LOOPBACK_CONFIG;
  3477. ret = afe_apr_send_pkt(&lb_cmd, &this_afe.wait[index]);
  3478. if (ret)
  3479. pr_err("%s: AFE loopback failed %d\n", __func__, ret);
  3480. return ret;
  3481. }
  3482. int afe_loopback_gain(u16 port_id, u16 volume)
  3483. {
  3484. struct afe_loopback_gain_per_path_param set_param;
  3485. int ret = 0;
  3486. int index = 0;
  3487. if (this_afe.apr == NULL) {
  3488. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  3489. 0xFFFFFFFF, &this_afe);
  3490. pr_debug("%s: Register AFE\n", __func__);
  3491. if (this_afe.apr == NULL) {
  3492. pr_err("%s: Unable to register AFE\n", __func__);
  3493. ret = -ENODEV;
  3494. return ret;
  3495. }
  3496. rtac_set_afe_handle(this_afe.apr);
  3497. }
  3498. ret = q6audio_validate_port(port_id);
  3499. if (ret < 0) {
  3500. pr_err("%s: Failed : Invalid Port id = 0x%x ret %d\n",
  3501. __func__, port_id, ret);
  3502. ret = -EINVAL;
  3503. goto fail_cmd;
  3504. }
  3505. index = q6audio_get_port_index(port_id);
  3506. if (index < 0 || index >= AFE_MAX_PORTS) {
  3507. pr_err("%s: AFE port index[%d] invalid!\n",
  3508. __func__, index);
  3509. return -EINVAL;
  3510. }
  3511. ret = q6audio_validate_port(port_id);
  3512. if (ret < 0) {
  3513. pr_err("%s: Invalid port 0x%x ret %d",
  3514. __func__, port_id, ret);
  3515. return -EINVAL;
  3516. }
  3517. /* RX ports numbers are even .TX ports numbers are odd. */
  3518. if (port_id % 2 == 0) {
  3519. pr_err("%s: Failed : afe loopback gain only for TX ports. port_id %d\n",
  3520. __func__, port_id);
  3521. ret = -EINVAL;
  3522. goto fail_cmd;
  3523. }
  3524. pr_debug("%s: port 0x%x volume %d\n", __func__, port_id, volume);
  3525. set_param.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3526. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3527. set_param.hdr.pkt_size = sizeof(set_param);
  3528. set_param.hdr.src_port = 0;
  3529. set_param.hdr.dest_port = 0;
  3530. set_param.hdr.token = index;
  3531. set_param.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  3532. set_param.param.port_id = port_id;
  3533. set_param.param.payload_size =
  3534. (sizeof(struct afe_loopback_gain_per_path_param) -
  3535. sizeof(struct apr_hdr) - sizeof(struct afe_port_cmd_set_param_v2));
  3536. set_param.param.payload_address_lsw = 0;
  3537. set_param.param.payload_address_msw = 0;
  3538. set_param.param.mem_map_handle = 0;
  3539. set_param.pdata.module_id = AFE_MODULE_LOOPBACK;
  3540. set_param.pdata.param_id = AFE_PARAM_ID_LOOPBACK_GAIN_PER_PATH;
  3541. set_param.pdata.param_size =
  3542. (set_param.param.payload_size -
  3543. sizeof(struct afe_port_param_data_v2));
  3544. set_param.rx_port_id = port_id;
  3545. set_param.gain = volume;
  3546. ret = afe_apr_send_pkt(&set_param, &this_afe.wait[index]);
  3547. if (ret) {
  3548. pr_err("%s: AFE param set failed for port 0x%x ret %d\n",
  3549. __func__, port_id, ret);
  3550. goto fail_cmd;
  3551. }
  3552. fail_cmd:
  3553. return ret;
  3554. }
  3555. int afe_pseudo_port_start_nowait(u16 port_id)
  3556. {
  3557. struct afe_pseudoport_start_command start;
  3558. int ret = 0;
  3559. pr_debug("%s: port_id=0x%x\n", __func__, port_id);
  3560. if (this_afe.apr == NULL) {
  3561. pr_err("%s: AFE APR is not registered\n", __func__);
  3562. return -ENODEV;
  3563. }
  3564. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3565. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3566. start.hdr.pkt_size = sizeof(start);
  3567. start.hdr.src_port = 0;
  3568. start.hdr.dest_port = 0;
  3569. start.hdr.token = 0;
  3570. start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
  3571. start.port_id = port_id;
  3572. start.timing = 1;
  3573. ret = afe_apr_send_pkt(&start, NULL);
  3574. if (ret) {
  3575. pr_err("%s: AFE enable for port 0x%x failed %d\n",
  3576. __func__, port_id, ret);
  3577. return ret;
  3578. }
  3579. return 0;
  3580. }
  3581. int afe_start_pseudo_port(u16 port_id)
  3582. {
  3583. int ret = 0;
  3584. struct afe_pseudoport_start_command start;
  3585. int index = 0;
  3586. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  3587. ret = afe_q6_interface_prepare();
  3588. if (ret != 0) {
  3589. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  3590. return ret;
  3591. }
  3592. index = q6audio_get_port_index(port_id);
  3593. if (index < 0 || index >= AFE_MAX_PORTS) {
  3594. pr_err("%s: AFE port index[%d] invalid!\n",
  3595. __func__, index);
  3596. return -EINVAL;
  3597. }
  3598. ret = q6audio_validate_port(port_id);
  3599. if (ret < 0) {
  3600. pr_err("%s: Invalid port 0x%x ret %d",
  3601. __func__, port_id, ret);
  3602. return -EINVAL;
  3603. }
  3604. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3605. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3606. start.hdr.pkt_size = sizeof(start);
  3607. start.hdr.src_port = 0;
  3608. start.hdr.dest_port = 0;
  3609. start.hdr.token = 0;
  3610. start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
  3611. start.port_id = port_id;
  3612. start.timing = 1;
  3613. start.hdr.token = index;
  3614. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  3615. if (ret)
  3616. pr_err("%s: AFE enable for port 0x%x failed %d\n",
  3617. __func__, port_id, ret);
  3618. return ret;
  3619. }
  3620. int afe_pseudo_port_stop_nowait(u16 port_id)
  3621. {
  3622. int ret = 0;
  3623. struct afe_pseudoport_stop_command stop;
  3624. int index = 0;
  3625. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  3626. if (this_afe.apr == NULL) {
  3627. pr_err("%s: AFE is already closed\n", __func__);
  3628. return -EINVAL;
  3629. }
  3630. index = q6audio_get_port_index(port_id);
  3631. if (index < 0 || index >= AFE_MAX_PORTS) {
  3632. pr_err("%s: AFE port index[%d] invalid!\n",
  3633. __func__, index);
  3634. return -EINVAL;
  3635. }
  3636. ret = q6audio_validate_port(port_id);
  3637. if (ret < 0) {
  3638. pr_err("%s: Invalid port 0x%x ret %d",
  3639. __func__, port_id, ret);
  3640. return -EINVAL;
  3641. }
  3642. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3643. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3644. stop.hdr.pkt_size = sizeof(stop);
  3645. stop.hdr.src_port = 0;
  3646. stop.hdr.dest_port = 0;
  3647. stop.hdr.token = 0;
  3648. stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
  3649. stop.port_id = port_id;
  3650. stop.reserved = 0;
  3651. stop.hdr.token = index;
  3652. ret = afe_apr_send_pkt(&stop, NULL);
  3653. if (ret)
  3654. pr_err("%s: AFE close failed %d\n", __func__, ret);
  3655. return ret;
  3656. }
  3657. int afe_port_group_set_param(u16 group_id,
  3658. union afe_port_group_config *afe_group_config)
  3659. {
  3660. int ret;
  3661. struct afe_port_group_create config;
  3662. int cfg_type;
  3663. if (!afe_group_config) {
  3664. pr_err("%s: Error, no configuration data\n", __func__);
  3665. return -EINVAL;
  3666. }
  3667. pr_debug("%s: group id: 0x%x\n", __func__, group_id);
  3668. ret = afe_q6_interface_prepare();
  3669. if (ret != 0) {
  3670. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  3671. return ret;
  3672. }
  3673. switch (group_id) {
  3674. case AFE_GROUP_DEVICE_ID_PRIMARY_TDM_RX:
  3675. case AFE_GROUP_DEVICE_ID_PRIMARY_TDM_TX:
  3676. case AFE_GROUP_DEVICE_ID_SECONDARY_TDM_RX:
  3677. case AFE_GROUP_DEVICE_ID_SECONDARY_TDM_TX:
  3678. case AFE_GROUP_DEVICE_ID_TERTIARY_TDM_RX:
  3679. case AFE_GROUP_DEVICE_ID_TERTIARY_TDM_TX:
  3680. case AFE_GROUP_DEVICE_ID_QUATERNARY_TDM_RX:
  3681. case AFE_GROUP_DEVICE_ID_QUATERNARY_TDM_TX:
  3682. case AFE_GROUP_DEVICE_ID_QUINARY_TDM_RX:
  3683. case AFE_GROUP_DEVICE_ID_QUINARY_TDM_TX:
  3684. cfg_type = AFE_PARAM_ID_GROUP_DEVICE_TDM_CONFIG;
  3685. break;
  3686. default:
  3687. pr_err("%s: Invalid group id 0x%x\n", __func__, group_id);
  3688. return -EINVAL;
  3689. }
  3690. memset(&config, 0, sizeof(config));
  3691. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3692. APR_HDR_LEN(APR_HDR_SIZE),
  3693. APR_PKT_VER);
  3694. config.hdr.pkt_size = sizeof(config);
  3695. config.hdr.src_port = 0;
  3696. config.hdr.dest_port = 0;
  3697. config.hdr.token = IDX_GLOBAL_CFG;
  3698. config.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  3699. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  3700. sizeof(config.param);
  3701. config.param.payload_address_lsw = 0x00;
  3702. config.param.payload_address_msw = 0x00;
  3703. config.param.mem_map_handle = 0x00;
  3704. config.pdata.module_id = AFE_MODULE_GROUP_DEVICE;
  3705. config.pdata.param_id = cfg_type;
  3706. config.pdata.param_size = sizeof(config.data);
  3707. config.data = *afe_group_config;
  3708. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  3709. if (ret)
  3710. pr_err("%s: AFE_PARAM_ID_GROUP_DEVICE_CFG failed %d\n",
  3711. __func__, ret);
  3712. return ret;
  3713. }
  3714. int afe_port_group_enable(u16 group_id,
  3715. union afe_port_group_config *afe_group_config,
  3716. u16 enable)
  3717. {
  3718. int ret;
  3719. struct afe_port_group_create config;
  3720. pr_debug("%s: group id: 0x%x enable: %d\n", __func__,
  3721. group_id, enable);
  3722. ret = afe_q6_interface_prepare();
  3723. if (ret != 0) {
  3724. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  3725. return ret;
  3726. }
  3727. if (enable) {
  3728. ret = afe_port_group_set_param(group_id, afe_group_config);
  3729. if (ret < 0) {
  3730. pr_err("%s: afe send failed %d\n", __func__, ret);
  3731. return ret;
  3732. }
  3733. }
  3734. memset(&config, 0, sizeof(config));
  3735. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3736. APR_HDR_LEN(APR_HDR_SIZE),
  3737. APR_PKT_VER);
  3738. config.hdr.pkt_size = sizeof(config);
  3739. config.hdr.src_port = 0;
  3740. config.hdr.dest_port = 0;
  3741. config.hdr.token = IDX_GLOBAL_CFG;
  3742. config.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  3743. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  3744. sizeof(config.param);
  3745. config.param.payload_address_lsw = 0x00;
  3746. config.param.payload_address_msw = 0x00;
  3747. config.param.mem_map_handle = 0x00;
  3748. config.pdata.module_id = AFE_MODULE_GROUP_DEVICE;
  3749. config.pdata.param_id = AFE_PARAM_ID_GROUP_DEVICE_ENABLE;
  3750. config.pdata.param_size = sizeof(config.data);
  3751. config.data.group_enable.group_id = group_id;
  3752. config.data.group_enable.enable = enable;
  3753. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  3754. if (ret)
  3755. pr_err("%s: AFE_PARAM_ID_GROUP_DEVICE_ENABLE failed %d\n",
  3756. __func__, ret);
  3757. return ret;
  3758. }
  3759. int afe_stop_pseudo_port(u16 port_id)
  3760. {
  3761. int ret = 0;
  3762. struct afe_pseudoport_stop_command stop;
  3763. int index = 0;
  3764. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  3765. if (this_afe.apr == NULL) {
  3766. pr_err("%s: AFE is already closed\n", __func__);
  3767. return -EINVAL;
  3768. }
  3769. index = q6audio_get_port_index(port_id);
  3770. if (index < 0 || index >= AFE_MAX_PORTS) {
  3771. pr_err("%s: AFE port index[%d] invalid!\n",
  3772. __func__, index);
  3773. return -EINVAL;
  3774. }
  3775. ret = q6audio_validate_port(port_id);
  3776. if (ret < 0) {
  3777. pr_err("%s: Invalid port 0x%x ret %d\n",
  3778. __func__, port_id, ret);
  3779. return -EINVAL;
  3780. }
  3781. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3782. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3783. stop.hdr.pkt_size = sizeof(stop);
  3784. stop.hdr.src_port = 0;
  3785. stop.hdr.dest_port = 0;
  3786. stop.hdr.token = 0;
  3787. stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
  3788. stop.port_id = port_id;
  3789. stop.reserved = 0;
  3790. stop.hdr.token = index;
  3791. ret = afe_apr_send_pkt(&stop, &this_afe.wait[index]);
  3792. if (ret)
  3793. pr_err("%s: AFE close failed %d\n", __func__, ret);
  3794. return ret;
  3795. }
  3796. uint32_t afe_req_mmap_handle(struct afe_audio_client *ac)
  3797. {
  3798. return ac->mem_map_handle;
  3799. }
  3800. struct afe_audio_client *q6afe_audio_client_alloc(void *priv)
  3801. {
  3802. struct afe_audio_client *ac;
  3803. int lcnt = 0;
  3804. ac = kzalloc(sizeof(struct afe_audio_client), GFP_KERNEL);
  3805. if (!ac)
  3806. return NULL;
  3807. ac->priv = priv;
  3808. init_waitqueue_head(&ac->cmd_wait);
  3809. INIT_LIST_HEAD(&ac->port[0].mem_map_handle);
  3810. INIT_LIST_HEAD(&ac->port[1].mem_map_handle);
  3811. pr_debug("%s: mem_map_handle list init'ed\n", __func__);
  3812. mutex_init(&ac->cmd_lock);
  3813. for (lcnt = 0; lcnt <= OUT; lcnt++) {
  3814. mutex_init(&ac->port[lcnt].lock);
  3815. spin_lock_init(&ac->port[lcnt].dsp_lock);
  3816. }
  3817. atomic_set(&ac->cmd_state, 0);
  3818. return ac;
  3819. }
  3820. int q6afe_audio_client_buf_alloc_contiguous(unsigned int dir,
  3821. struct afe_audio_client *ac,
  3822. unsigned int bufsz,
  3823. unsigned int bufcnt)
  3824. {
  3825. int cnt = 0;
  3826. int rc = 0;
  3827. struct afe_audio_buffer *buf;
  3828. size_t len;
  3829. if (!(ac) || ((dir != IN) && (dir != OUT))) {
  3830. pr_err("%s: ac %pK dir %d\n", __func__, ac, dir);
  3831. return -EINVAL;
  3832. }
  3833. pr_debug("%s: bufsz[%d]bufcnt[%d]\n",
  3834. __func__,
  3835. bufsz, bufcnt);
  3836. if (ac->port[dir].buf) {
  3837. pr_debug("%s: buffer already allocated\n", __func__);
  3838. return 0;
  3839. }
  3840. mutex_lock(&ac->cmd_lock);
  3841. buf = kzalloc(((sizeof(struct afe_audio_buffer))*bufcnt),
  3842. GFP_KERNEL);
  3843. if (!buf) {
  3844. pr_err("%s: null buf\n", __func__);
  3845. mutex_unlock(&ac->cmd_lock);
  3846. goto fail;
  3847. }
  3848. ac->port[dir].buf = buf;
  3849. rc = msm_audio_ion_alloc("afe_client", &buf[0].client,
  3850. &buf[0].handle, bufsz*bufcnt,
  3851. &buf[0].phys, &len,
  3852. &buf[0].data);
  3853. if (rc) {
  3854. pr_err("%s: audio ION alloc failed, rc = %d\n",
  3855. __func__, rc);
  3856. mutex_unlock(&ac->cmd_lock);
  3857. goto fail;
  3858. }
  3859. buf[0].used = dir ^ 1;
  3860. buf[0].size = bufsz;
  3861. buf[0].actual_size = bufsz;
  3862. cnt = 1;
  3863. while (cnt < bufcnt) {
  3864. if (bufsz > 0) {
  3865. buf[cnt].data = buf[0].data + (cnt * bufsz);
  3866. buf[cnt].phys = buf[0].phys + (cnt * bufsz);
  3867. if (!buf[cnt].data) {
  3868. pr_err("%s: Buf alloc failed\n",
  3869. __func__);
  3870. mutex_unlock(&ac->cmd_lock);
  3871. goto fail;
  3872. }
  3873. buf[cnt].used = dir ^ 1;
  3874. buf[cnt].size = bufsz;
  3875. buf[cnt].actual_size = bufsz;
  3876. pr_debug("%s: data[%pK]phys[%pK][%pK]\n", __func__,
  3877. buf[cnt].data,
  3878. &buf[cnt].phys,
  3879. &buf[cnt].phys);
  3880. }
  3881. cnt++;
  3882. }
  3883. ac->port[dir].max_buf_cnt = cnt;
  3884. mutex_unlock(&ac->cmd_lock);
  3885. return 0;
  3886. fail:
  3887. pr_err("%s: jump fail\n", __func__);
  3888. q6afe_audio_client_buf_free_contiguous(dir, ac);
  3889. return -EINVAL;
  3890. }
  3891. int afe_memory_map(phys_addr_t dma_addr_p, u32 dma_buf_sz,
  3892. struct afe_audio_client *ac)
  3893. {
  3894. int ret = 0;
  3895. mutex_lock(&this_afe.afe_cmd_lock);
  3896. ac->mem_map_handle = 0;
  3897. ret = afe_cmd_memory_map(dma_addr_p, dma_buf_sz);
  3898. if (ret < 0) {
  3899. pr_err("%s: afe_cmd_memory_map failed %d\n",
  3900. __func__, ret);
  3901. mutex_unlock(&this_afe.afe_cmd_lock);
  3902. return ret;
  3903. }
  3904. ac->mem_map_handle = this_afe.mmap_handle;
  3905. mutex_unlock(&this_afe.afe_cmd_lock);
  3906. return ret;
  3907. }
  3908. int afe_cmd_memory_map(phys_addr_t dma_addr_p, u32 dma_buf_sz)
  3909. {
  3910. int ret = 0;
  3911. int cmd_size = 0;
  3912. void *payload = NULL;
  3913. void *mmap_region_cmd = NULL;
  3914. struct afe_service_cmd_shared_mem_map_regions *mregion = NULL;
  3915. struct afe_service_shared_map_region_payload *mregion_pl = NULL;
  3916. int index = 0;
  3917. pr_debug("%s:\n", __func__);
  3918. if (this_afe.apr == NULL) {
  3919. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  3920. 0xFFFFFFFF, &this_afe);
  3921. pr_debug("%s: Register AFE\n", __func__);
  3922. if (this_afe.apr == NULL) {
  3923. pr_err("%s: Unable to register AFE\n", __func__);
  3924. ret = -ENODEV;
  3925. return ret;
  3926. }
  3927. rtac_set_afe_handle(this_afe.apr);
  3928. }
  3929. if (dma_buf_sz % SZ_4K != 0) {
  3930. /*
  3931. * The memory allocated by msm_audio_ion_alloc is always 4kB
  3932. * aligned, ADSP expects the size to be 4kB aligned as well
  3933. * so re-adjusts the buffer size before passing to ADSP.
  3934. */
  3935. dma_buf_sz = PAGE_ALIGN(dma_buf_sz);
  3936. }
  3937. cmd_size = sizeof(struct afe_service_cmd_shared_mem_map_regions)
  3938. + sizeof(struct afe_service_shared_map_region_payload);
  3939. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  3940. if (!mmap_region_cmd)
  3941. return -ENOMEM;
  3942. mregion = (struct afe_service_cmd_shared_mem_map_regions *)
  3943. mmap_region_cmd;
  3944. mregion->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3945. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3946. mregion->hdr.pkt_size = cmd_size;
  3947. mregion->hdr.src_port = 0;
  3948. mregion->hdr.dest_port = 0;
  3949. mregion->hdr.token = 0;
  3950. mregion->hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS;
  3951. mregion->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  3952. mregion->num_regions = 1;
  3953. mregion->property_flag = 0x00;
  3954. /* Todo */
  3955. index = mregion->hdr.token = IDX_RSVD_2;
  3956. payload = ((u8 *) mmap_region_cmd +
  3957. sizeof(struct afe_service_cmd_shared_mem_map_regions));
  3958. mregion_pl = (struct afe_service_shared_map_region_payload *)payload;
  3959. mregion_pl->shm_addr_lsw = lower_32_bits(dma_addr_p);
  3960. mregion_pl->shm_addr_msw = msm_audio_populate_upper_32_bits(dma_addr_p);
  3961. mregion_pl->mem_size_bytes = dma_buf_sz;
  3962. pr_debug("%s: dma_addr_p 0x%pK , size %d\n", __func__,
  3963. &dma_addr_p, dma_buf_sz);
  3964. atomic_set(&this_afe.state, 1);
  3965. atomic_set(&this_afe.status, 0);
  3966. this_afe.mmap_handle = 0;
  3967. ret = apr_send_pkt(this_afe.apr, (uint32_t *) mmap_region_cmd);
  3968. if (ret < 0) {
  3969. pr_err("%s: AFE memory map cmd failed %d\n",
  3970. __func__, ret);
  3971. ret = -EINVAL;
  3972. goto fail_cmd;
  3973. }
  3974. ret = wait_event_timeout(this_afe.wait[index],
  3975. (atomic_read(&this_afe.state) == 0),
  3976. msecs_to_jiffies(TIMEOUT_MS));
  3977. if (!ret) {
  3978. pr_err("%s: wait_event timeout\n", __func__);
  3979. ret = -EINVAL;
  3980. goto fail_cmd;
  3981. }
  3982. if (atomic_read(&this_afe.status) > 0) {
  3983. pr_err("%s: config cmd failed [%s]\n",
  3984. __func__, adsp_err_get_err_str(
  3985. atomic_read(&this_afe.status)));
  3986. ret = adsp_err_get_lnx_err_code(
  3987. atomic_read(&this_afe.status));
  3988. goto fail_cmd;
  3989. }
  3990. kfree(mmap_region_cmd);
  3991. return 0;
  3992. fail_cmd:
  3993. kfree(mmap_region_cmd);
  3994. pr_err("%s: fail_cmd\n", __func__);
  3995. return ret;
  3996. }
  3997. int afe_cmd_memory_map_nowait(int port_id, phys_addr_t dma_addr_p,
  3998. u32 dma_buf_sz)
  3999. {
  4000. int ret = 0;
  4001. int cmd_size = 0;
  4002. void *payload = NULL;
  4003. void *mmap_region_cmd = NULL;
  4004. struct afe_service_cmd_shared_mem_map_regions *mregion = NULL;
  4005. struct afe_service_shared_map_region_payload *mregion_pl = NULL;
  4006. int index = 0;
  4007. pr_debug("%s:\n", __func__);
  4008. if (this_afe.apr == NULL) {
  4009. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4010. 0xFFFFFFFF, &this_afe);
  4011. pr_debug("%s: Register AFE\n", __func__);
  4012. if (this_afe.apr == NULL) {
  4013. pr_err("%s: Unable to register AFE\n", __func__);
  4014. ret = -ENODEV;
  4015. return ret;
  4016. }
  4017. rtac_set_afe_handle(this_afe.apr);
  4018. }
  4019. index = q6audio_get_port_index(port_id);
  4020. if (index < 0 || index >= AFE_MAX_PORTS) {
  4021. pr_err("%s: AFE port index[%d] invalid!\n",
  4022. __func__, index);
  4023. return -EINVAL;
  4024. }
  4025. ret = q6audio_validate_port(port_id);
  4026. if (ret < 0) {
  4027. pr_err("%s: Invalid port 0x%x ret %d",
  4028. __func__, port_id, ret);
  4029. return -EINVAL;
  4030. }
  4031. cmd_size = sizeof(struct afe_service_cmd_shared_mem_map_regions)
  4032. + sizeof(struct afe_service_shared_map_region_payload);
  4033. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  4034. if (!mmap_region_cmd)
  4035. return -ENOMEM;
  4036. mregion = (struct afe_service_cmd_shared_mem_map_regions *)
  4037. mmap_region_cmd;
  4038. mregion->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4039. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4040. mregion->hdr.pkt_size = sizeof(mregion);
  4041. mregion->hdr.src_port = 0;
  4042. mregion->hdr.dest_port = 0;
  4043. mregion->hdr.token = 0;
  4044. mregion->hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS;
  4045. mregion->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  4046. mregion->num_regions = 1;
  4047. mregion->property_flag = 0x00;
  4048. payload = ((u8 *) mmap_region_cmd +
  4049. sizeof(struct afe_service_cmd_shared_mem_map_regions));
  4050. mregion_pl = (struct afe_service_shared_map_region_payload *)payload;
  4051. mregion_pl->shm_addr_lsw = lower_32_bits(dma_addr_p);
  4052. mregion_pl->shm_addr_msw = msm_audio_populate_upper_32_bits(dma_addr_p);
  4053. mregion_pl->mem_size_bytes = dma_buf_sz;
  4054. ret = afe_apr_send_pkt(mmap_region_cmd, NULL);
  4055. if (ret)
  4056. pr_err("%s: AFE memory map cmd failed %d\n",
  4057. __func__, ret);
  4058. kfree(mmap_region_cmd);
  4059. return ret;
  4060. }
  4061. int q6afe_audio_client_buf_free_contiguous(unsigned int dir,
  4062. struct afe_audio_client *ac)
  4063. {
  4064. struct afe_audio_port_data *port;
  4065. int cnt = 0;
  4066. mutex_lock(&ac->cmd_lock);
  4067. port = &ac->port[dir];
  4068. if (!port->buf) {
  4069. pr_err("%s: buf is null\n", __func__);
  4070. mutex_unlock(&ac->cmd_lock);
  4071. return 0;
  4072. }
  4073. cnt = port->max_buf_cnt - 1;
  4074. if (port->buf[0].data) {
  4075. pr_debug("%s: data[%pK]phys[%pK][%pK] , client[%pK] handle[%pK]\n",
  4076. __func__,
  4077. port->buf[0].data,
  4078. &port->buf[0].phys,
  4079. &port->buf[0].phys,
  4080. port->buf[0].client,
  4081. port->buf[0].handle);
  4082. msm_audio_ion_free(port->buf[0].client, port->buf[0].handle);
  4083. port->buf[0].client = NULL;
  4084. port->buf[0].handle = NULL;
  4085. }
  4086. while (cnt >= 0) {
  4087. port->buf[cnt].data = NULL;
  4088. port->buf[cnt].phys = 0;
  4089. cnt--;
  4090. }
  4091. port->max_buf_cnt = 0;
  4092. kfree(port->buf);
  4093. port->buf = NULL;
  4094. mutex_unlock(&ac->cmd_lock);
  4095. return 0;
  4096. }
  4097. void q6afe_audio_client_free(struct afe_audio_client *ac)
  4098. {
  4099. int loopcnt;
  4100. struct afe_audio_port_data *port;
  4101. if (!ac) {
  4102. pr_err("%s: audio client is NULL\n", __func__);
  4103. return;
  4104. }
  4105. for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
  4106. port = &ac->port[loopcnt];
  4107. if (!port->buf)
  4108. continue;
  4109. pr_debug("%s: loopcnt = %d\n", __func__, loopcnt);
  4110. q6afe_audio_client_buf_free_contiguous(loopcnt, ac);
  4111. }
  4112. kfree(ac);
  4113. }
  4114. int afe_cmd_memory_unmap(u32 mem_map_handle)
  4115. {
  4116. int ret = 0;
  4117. struct afe_service_cmd_shared_mem_unmap_regions mregion;
  4118. int index = 0;
  4119. pr_debug("%s: handle 0x%x\n", __func__, mem_map_handle);
  4120. if (this_afe.apr == NULL) {
  4121. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4122. 0xFFFFFFFF, &this_afe);
  4123. pr_debug("%s: Register AFE\n", __func__);
  4124. if (this_afe.apr == NULL) {
  4125. pr_err("%s: Unable to register AFE\n", __func__);
  4126. ret = -ENODEV;
  4127. return ret;
  4128. }
  4129. rtac_set_afe_handle(this_afe.apr);
  4130. }
  4131. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4132. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4133. mregion.hdr.pkt_size = sizeof(mregion);
  4134. mregion.hdr.src_port = 0;
  4135. mregion.hdr.dest_port = 0;
  4136. mregion.hdr.token = 0;
  4137. mregion.hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS;
  4138. mregion.mem_map_handle = mem_map_handle;
  4139. /* Todo */
  4140. index = mregion.hdr.token = IDX_RSVD_2;
  4141. atomic_set(&this_afe.status, 0);
  4142. ret = afe_apr_send_pkt(&mregion, &this_afe.wait[index]);
  4143. if (ret)
  4144. pr_err("%s: AFE memory unmap cmd failed %d\n",
  4145. __func__, ret);
  4146. return ret;
  4147. }
  4148. int afe_cmd_memory_unmap_nowait(u32 mem_map_handle)
  4149. {
  4150. int ret = 0;
  4151. struct afe_service_cmd_shared_mem_unmap_regions mregion;
  4152. pr_debug("%s: handle 0x%x\n", __func__, mem_map_handle);
  4153. if (this_afe.apr == NULL) {
  4154. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4155. 0xFFFFFFFF, &this_afe);
  4156. pr_debug("%s: Register AFE\n", __func__);
  4157. if (this_afe.apr == NULL) {
  4158. pr_err("%s: Unable to register AFE\n", __func__);
  4159. ret = -ENODEV;
  4160. return ret;
  4161. }
  4162. rtac_set_afe_handle(this_afe.apr);
  4163. }
  4164. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4165. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4166. mregion.hdr.pkt_size = sizeof(mregion);
  4167. mregion.hdr.src_port = 0;
  4168. mregion.hdr.dest_port = 0;
  4169. mregion.hdr.token = 0;
  4170. mregion.hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS;
  4171. mregion.mem_map_handle = mem_map_handle;
  4172. ret = afe_apr_send_pkt(&mregion, NULL);
  4173. if (ret)
  4174. pr_err("%s: AFE memory unmap cmd failed %d\n",
  4175. __func__, ret);
  4176. return ret;
  4177. }
  4178. int afe_register_get_events(u16 port_id,
  4179. void (*cb)(uint32_t opcode,
  4180. uint32_t token, uint32_t *payload, void *priv),
  4181. void *private_data)
  4182. {
  4183. int ret = 0;
  4184. struct afe_service_cmd_register_rt_port_driver rtproxy;
  4185. pr_debug("%s: port_id: 0x%x\n", __func__, port_id);
  4186. if (this_afe.apr == NULL) {
  4187. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4188. 0xFFFFFFFF, &this_afe);
  4189. pr_debug("%s: Register AFE\n", __func__);
  4190. if (this_afe.apr == NULL) {
  4191. pr_err("%s: Unable to register AFE\n", __func__);
  4192. ret = -ENODEV;
  4193. return ret;
  4194. }
  4195. rtac_set_afe_handle(this_afe.apr);
  4196. }
  4197. if ((port_id == RT_PROXY_DAI_002_RX) ||
  4198. (port_id == RT_PROXY_DAI_001_TX)) {
  4199. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  4200. } else {
  4201. pr_err("%s: wrong port id 0x%x\n", __func__, port_id);
  4202. return -EINVAL;
  4203. }
  4204. if (port_id == RT_PROXY_PORT_001_TX) {
  4205. this_afe.tx_cb = cb;
  4206. this_afe.tx_private_data = private_data;
  4207. } else if (port_id == RT_PROXY_PORT_001_RX) {
  4208. this_afe.rx_cb = cb;
  4209. this_afe.rx_private_data = private_data;
  4210. }
  4211. rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4212. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4213. rtproxy.hdr.pkt_size = sizeof(rtproxy);
  4214. rtproxy.hdr.src_port = 1;
  4215. rtproxy.hdr.dest_port = 1;
  4216. rtproxy.hdr.opcode = AFE_SERVICE_CMD_REGISTER_RT_PORT_DRIVER;
  4217. rtproxy.port_id = port_id;
  4218. rtproxy.reserved = 0;
  4219. ret = afe_apr_send_pkt(&rtproxy, NULL);
  4220. if (ret)
  4221. pr_err("%s: AFE reg. rtproxy_event failed %d\n",
  4222. __func__, ret);
  4223. return ret;
  4224. }
  4225. int afe_unregister_get_events(u16 port_id)
  4226. {
  4227. int ret = 0;
  4228. struct afe_service_cmd_unregister_rt_port_driver rtproxy;
  4229. int index = 0;
  4230. pr_debug("%s:\n", __func__);
  4231. if (this_afe.apr == NULL) {
  4232. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4233. 0xFFFFFFFF, &this_afe);
  4234. pr_debug("%s: Register AFE\n", __func__);
  4235. if (this_afe.apr == NULL) {
  4236. pr_err("%s: Unable to register AFE\n", __func__);
  4237. ret = -ENODEV;
  4238. return ret;
  4239. }
  4240. rtac_set_afe_handle(this_afe.apr);
  4241. }
  4242. if ((port_id == RT_PROXY_DAI_002_RX) ||
  4243. (port_id == RT_PROXY_DAI_001_TX)) {
  4244. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  4245. } else {
  4246. pr_err("%s: wrong port id 0x%x\n", __func__, port_id);
  4247. return -EINVAL;
  4248. }
  4249. index = q6audio_get_port_index(port_id);
  4250. if (index < 0 || index >= AFE_MAX_PORTS) {
  4251. pr_err("%s: AFE port index[%d] invalid!\n",
  4252. __func__, index);
  4253. return -EINVAL;
  4254. }
  4255. ret = q6audio_validate_port(port_id);
  4256. if (ret < 0) {
  4257. pr_err("%s: Invalid port 0x%x ret %d", __func__, port_id, ret);
  4258. return -EINVAL;
  4259. }
  4260. rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4261. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4262. rtproxy.hdr.pkt_size = sizeof(rtproxy);
  4263. rtproxy.hdr.src_port = 0;
  4264. rtproxy.hdr.dest_port = 0;
  4265. rtproxy.hdr.token = 0;
  4266. rtproxy.hdr.opcode = AFE_SERVICE_CMD_UNREGISTER_RT_PORT_DRIVER;
  4267. rtproxy.port_id = port_id;
  4268. rtproxy.reserved = 0;
  4269. rtproxy.hdr.token = index;
  4270. if (port_id == RT_PROXY_PORT_001_TX) {
  4271. this_afe.tx_cb = NULL;
  4272. this_afe.tx_private_data = NULL;
  4273. } else if (port_id == RT_PROXY_PORT_001_RX) {
  4274. this_afe.rx_cb = NULL;
  4275. this_afe.rx_private_data = NULL;
  4276. }
  4277. ret = afe_apr_send_pkt(&rtproxy, &this_afe.wait[index]);
  4278. if (ret)
  4279. pr_err("%s: AFE enable Unreg. rtproxy_event failed %d\n",
  4280. __func__, ret);
  4281. return ret;
  4282. }
  4283. int afe_rt_proxy_port_write(phys_addr_t buf_addr_p,
  4284. u32 mem_map_handle, int bytes)
  4285. {
  4286. int ret = 0;
  4287. struct afe_port_data_cmd_rt_proxy_port_write_v2 afecmd_wr;
  4288. if (this_afe.apr == NULL) {
  4289. pr_err("%s: register to AFE is not done\n", __func__);
  4290. ret = -ENODEV;
  4291. return ret;
  4292. }
  4293. pr_debug("%s: buf_addr_p = 0x%pK bytes = %d\n", __func__,
  4294. &buf_addr_p, bytes);
  4295. afecmd_wr.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4296. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4297. afecmd_wr.hdr.pkt_size = sizeof(afecmd_wr);
  4298. afecmd_wr.hdr.src_port = 0;
  4299. afecmd_wr.hdr.dest_port = 0;
  4300. afecmd_wr.hdr.token = 0;
  4301. afecmd_wr.hdr.opcode = AFE_PORT_DATA_CMD_RT_PROXY_PORT_WRITE_V2;
  4302. afecmd_wr.port_id = RT_PROXY_PORT_001_TX;
  4303. afecmd_wr.buffer_address_lsw = lower_32_bits(buf_addr_p);
  4304. afecmd_wr.buffer_address_msw =
  4305. msm_audio_populate_upper_32_bits(buf_addr_p);
  4306. afecmd_wr.mem_map_handle = mem_map_handle;
  4307. afecmd_wr.available_bytes = bytes;
  4308. afecmd_wr.reserved = 0;
  4309. ret = afe_apr_send_pkt(&afecmd_wr, NULL);
  4310. if (ret)
  4311. pr_err("%s: AFE rtproxy write to port 0x%x failed %d\n",
  4312. __func__, afecmd_wr.port_id, ret);
  4313. return ret;
  4314. }
  4315. int afe_rt_proxy_port_read(phys_addr_t buf_addr_p,
  4316. u32 mem_map_handle, int bytes)
  4317. {
  4318. int ret = 0;
  4319. struct afe_port_data_cmd_rt_proxy_port_read_v2 afecmd_rd;
  4320. if (this_afe.apr == NULL) {
  4321. pr_err("%s: register to AFE is not done\n", __func__);
  4322. ret = -ENODEV;
  4323. return ret;
  4324. }
  4325. pr_debug("%s: buf_addr_p = 0x%pK bytes = %d\n", __func__,
  4326. &buf_addr_p, bytes);
  4327. afecmd_rd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4328. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4329. afecmd_rd.hdr.pkt_size = sizeof(afecmd_rd);
  4330. afecmd_rd.hdr.src_port = 0;
  4331. afecmd_rd.hdr.dest_port = 0;
  4332. afecmd_rd.hdr.token = 0;
  4333. afecmd_rd.hdr.opcode = AFE_PORT_DATA_CMD_RT_PROXY_PORT_READ_V2;
  4334. afecmd_rd.port_id = RT_PROXY_PORT_001_RX;
  4335. afecmd_rd.buffer_address_lsw = lower_32_bits(buf_addr_p);
  4336. afecmd_rd.buffer_address_msw =
  4337. msm_audio_populate_upper_32_bits(buf_addr_p);
  4338. afecmd_rd.available_bytes = bytes;
  4339. afecmd_rd.mem_map_handle = mem_map_handle;
  4340. ret = afe_apr_send_pkt(&afecmd_rd, NULL);
  4341. if (ret)
  4342. pr_err("%s: AFE rtproxy read cmd to port 0x%x failed %d\n",
  4343. __func__, afecmd_rd.port_id, ret);
  4344. return ret;
  4345. }
  4346. #ifdef CONFIG_DEBUG_FS
  4347. static struct dentry *debugfs_afelb;
  4348. static struct dentry *debugfs_afelb_gain;
  4349. static int afe_debug_open(struct inode *inode, struct file *file)
  4350. {
  4351. file->private_data = inode->i_private;
  4352. pr_info("%s: debug intf %s\n", __func__, (char *) file->private_data);
  4353. return 0;
  4354. }
  4355. static int afe_get_parameters(char *buf, long int *param1, int num_of_par)
  4356. {
  4357. char *token;
  4358. int base, cnt;
  4359. token = strsep(&buf, " ");
  4360. for (cnt = 0; cnt < num_of_par; cnt++) {
  4361. if (token != NULL) {
  4362. if ((token[1] == 'x') || (token[1] == 'X'))
  4363. base = 16;
  4364. else
  4365. base = 10;
  4366. if (kstrtoul(token, base, &param1[cnt]) != 0) {
  4367. pr_err("%s: kstrtoul failed\n",
  4368. __func__);
  4369. return -EINVAL;
  4370. }
  4371. token = strsep(&buf, " ");
  4372. } else {
  4373. pr_err("%s: token NULL\n", __func__);
  4374. return -EINVAL;
  4375. }
  4376. }
  4377. return 0;
  4378. }
  4379. #define AFE_LOOPBACK_ON (1)
  4380. #define AFE_LOOPBACK_OFF (0)
  4381. static ssize_t afe_debug_write(struct file *filp,
  4382. const char __user *ubuf, size_t cnt, loff_t *ppos)
  4383. {
  4384. char *lb_str = filp->private_data;
  4385. char lbuf[32];
  4386. int rc;
  4387. unsigned long param[5];
  4388. if (cnt > sizeof(lbuf) - 1) {
  4389. pr_err("%s: cnt %zd size %zd\n", __func__, cnt, sizeof(lbuf)-1);
  4390. return -EINVAL;
  4391. }
  4392. rc = copy_from_user(lbuf, ubuf, cnt);
  4393. if (rc) {
  4394. pr_err("%s: copy from user failed %d\n", __func__, rc);
  4395. return -EFAULT;
  4396. }
  4397. lbuf[cnt] = '\0';
  4398. if (!strcmp(lb_str, "afe_loopback")) {
  4399. rc = afe_get_parameters(lbuf, param, 3);
  4400. if (!rc) {
  4401. pr_info("%s: %lu %lu %lu\n", lb_str, param[0], param[1],
  4402. param[2]);
  4403. if ((param[0] != AFE_LOOPBACK_ON) && (param[0] !=
  4404. AFE_LOOPBACK_OFF)) {
  4405. pr_err("%s: Error, parameter 0 incorrect\n",
  4406. __func__);
  4407. rc = -EINVAL;
  4408. goto afe_error;
  4409. }
  4410. if ((q6audio_validate_port(param[1]) < 0) ||
  4411. (q6audio_validate_port(param[2])) < 0) {
  4412. pr_err("%s: Error, invalid afe port\n",
  4413. __func__);
  4414. }
  4415. if (this_afe.apr == NULL) {
  4416. pr_err("%s: Error, AFE not opened\n", __func__);
  4417. rc = -EINVAL;
  4418. } else {
  4419. rc = afe_loopback(param[0], param[1], param[2]);
  4420. }
  4421. } else {
  4422. pr_err("%s: Error, invalid parameters\n", __func__);
  4423. rc = -EINVAL;
  4424. }
  4425. } else if (!strcmp(lb_str, "afe_loopback_gain")) {
  4426. rc = afe_get_parameters(lbuf, param, 2);
  4427. if (!rc) {
  4428. pr_info("%s: %s %lu %lu\n",
  4429. __func__, lb_str, param[0], param[1]);
  4430. rc = q6audio_validate_port(param[0]);
  4431. if (rc < 0) {
  4432. pr_err("%s: Error, invalid afe port %d %lu\n",
  4433. __func__, rc, param[0]);
  4434. rc = -EINVAL;
  4435. goto afe_error;
  4436. }
  4437. if (param[1] > 100) {
  4438. pr_err("%s: Error, volume should be 0 to 100 percentage param = %lu\n",
  4439. __func__, param[1]);
  4440. rc = -EINVAL;
  4441. goto afe_error;
  4442. }
  4443. param[1] = (Q6AFE_MAX_VOLUME * param[1]) / 100;
  4444. if (this_afe.apr == NULL) {
  4445. pr_err("%s: Error, AFE not opened\n", __func__);
  4446. rc = -EINVAL;
  4447. } else {
  4448. rc = afe_loopback_gain(param[0], param[1]);
  4449. }
  4450. } else {
  4451. pr_err("%s: Error, invalid parameters\n", __func__);
  4452. rc = -EINVAL;
  4453. }
  4454. }
  4455. afe_error:
  4456. if (rc == 0)
  4457. rc = cnt;
  4458. else
  4459. pr_err("%s: rc = %d\n", __func__, rc);
  4460. return rc;
  4461. }
  4462. static const struct file_operations afe_debug_fops = {
  4463. .open = afe_debug_open,
  4464. .write = afe_debug_write
  4465. };
  4466. static void config_debug_fs_init(void)
  4467. {
  4468. debugfs_afelb = debugfs_create_file("afe_loopback",
  4469. 0664, NULL, (void *) "afe_loopback",
  4470. &afe_debug_fops);
  4471. debugfs_afelb_gain = debugfs_create_file("afe_loopback_gain",
  4472. 0664, NULL, (void *) "afe_loopback_gain",
  4473. &afe_debug_fops);
  4474. }
  4475. static void config_debug_fs_exit(void)
  4476. {
  4477. debugfs_remove(debugfs_afelb);
  4478. debugfs_remove(debugfs_afelb_gain);
  4479. }
  4480. #else
  4481. static void config_debug_fs_init(void)
  4482. {
  4483. }
  4484. static void config_debug_fs_exit(void)
  4485. {
  4486. }
  4487. #endif
  4488. void afe_set_dtmf_gen_rx_portid(u16 port_id, int set)
  4489. {
  4490. if (set)
  4491. this_afe.dtmf_gen_rx_portid = port_id;
  4492. else if (this_afe.dtmf_gen_rx_portid == port_id)
  4493. this_afe.dtmf_gen_rx_portid = -1;
  4494. }
  4495. int afe_dtmf_generate_rx(int64_t duration_in_ms,
  4496. uint16_t high_freq,
  4497. uint16_t low_freq, uint16_t gain)
  4498. {
  4499. int ret = 0;
  4500. int index = 0;
  4501. struct afe_dtmf_generation_command cmd_dtmf;
  4502. pr_debug("%s: DTMF AFE Gen\n", __func__);
  4503. if (afe_validate_port(this_afe.dtmf_gen_rx_portid) < 0) {
  4504. pr_err("%s: Failed : Invalid Port id = 0x%x\n",
  4505. __func__, this_afe.dtmf_gen_rx_portid);
  4506. ret = -EINVAL;
  4507. goto fail_cmd;
  4508. }
  4509. if (this_afe.apr == NULL) {
  4510. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4511. 0xFFFFFFFF, &this_afe);
  4512. pr_debug("%s: Register AFE\n", __func__);
  4513. if (this_afe.apr == NULL) {
  4514. pr_err("%s: Unable to register AFE\n", __func__);
  4515. ret = -ENODEV;
  4516. return ret;
  4517. }
  4518. rtac_set_afe_handle(this_afe.apr);
  4519. }
  4520. pr_debug("%s: dur=%lld: hfreq=%d lfreq=%d gain=%d portid=0x%x\n",
  4521. __func__,
  4522. duration_in_ms, high_freq, low_freq, gain,
  4523. this_afe.dtmf_gen_rx_portid);
  4524. cmd_dtmf.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4525. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4526. cmd_dtmf.hdr.pkt_size = sizeof(cmd_dtmf);
  4527. cmd_dtmf.hdr.src_port = 0;
  4528. cmd_dtmf.hdr.dest_port = 0;
  4529. cmd_dtmf.hdr.token = 0;
  4530. cmd_dtmf.hdr.opcode = AFE_PORTS_CMD_DTMF_CTL;
  4531. cmd_dtmf.duration_in_ms = duration_in_ms;
  4532. cmd_dtmf.high_freq = high_freq;
  4533. cmd_dtmf.low_freq = low_freq;
  4534. cmd_dtmf.gain = gain;
  4535. cmd_dtmf.num_ports = 1;
  4536. cmd_dtmf.port_ids = q6audio_get_port_id(this_afe.dtmf_gen_rx_portid);
  4537. atomic_set(&this_afe.state, 1);
  4538. atomic_set(&this_afe.status, 0);
  4539. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &cmd_dtmf);
  4540. if (ret < 0) {
  4541. pr_err("%s: AFE DTMF failed for num_ports:%d ids:0x%x\n",
  4542. __func__, cmd_dtmf.num_ports, cmd_dtmf.port_ids);
  4543. ret = -EINVAL;
  4544. goto fail_cmd;
  4545. }
  4546. index = q6audio_get_port_index(this_afe.dtmf_gen_rx_portid);
  4547. if (index < 0 || index >= AFE_MAX_PORTS) {
  4548. pr_err("%s: AFE port index[%d] invalid!\n",
  4549. __func__, index);
  4550. ret = -EINVAL;
  4551. goto fail_cmd;
  4552. }
  4553. ret = wait_event_timeout(this_afe.wait[index],
  4554. (atomic_read(&this_afe.state) == 0),
  4555. msecs_to_jiffies(TIMEOUT_MS));
  4556. if (!ret) {
  4557. pr_err("%s: wait_event timeout\n", __func__);
  4558. ret = -EINVAL;
  4559. goto fail_cmd;
  4560. }
  4561. if (atomic_read(&this_afe.status) > 0) {
  4562. pr_err("%s: config cmd failed [%s]\n",
  4563. __func__, adsp_err_get_err_str(
  4564. atomic_read(&this_afe.status)));
  4565. ret = adsp_err_get_lnx_err_code(
  4566. atomic_read(&this_afe.status));
  4567. goto fail_cmd;
  4568. }
  4569. return 0;
  4570. fail_cmd:
  4571. pr_err("%s: failed %d\n", __func__, ret);
  4572. return ret;
  4573. }
  4574. static int afe_sidetone_iir(u16 tx_port_id)
  4575. {
  4576. struct afe_loopback_iir_cfg_v2 iir_sidetone;
  4577. int ret;
  4578. int index = 0;
  4579. uint16_t size = 0;
  4580. int cal_index = AFE_SIDETONE_IIR_CAL;
  4581. int iir_pregain = 0;
  4582. int iir_num_biquad_stages = 0;
  4583. int iir_enable;
  4584. struct cal_block_data *cal_block;
  4585. int mid;
  4586. memset(&iir_sidetone, 0, sizeof(iir_sidetone));
  4587. index = q6audio_get_port_index(tx_port_id);
  4588. iir_sidetone.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4589. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4590. iir_sidetone.hdr.pkt_size = sizeof(iir_sidetone);
  4591. iir_sidetone.hdr.src_port = 0;
  4592. iir_sidetone.hdr.dest_port = 0;
  4593. iir_sidetone.hdr.token = index;
  4594. iir_sidetone.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  4595. iir_sidetone.param.port_id = tx_port_id;
  4596. iir_sidetone.param.payload_address_lsw = 0x00;
  4597. iir_sidetone.param.payload_address_msw = 0x00;
  4598. iir_sidetone.param.mem_map_handle = 0x00;
  4599. if (this_afe.cal_data[cal_index] == NULL) {
  4600. pr_err("%s: cal data is NULL\n", __func__);
  4601. ret = -EINVAL;
  4602. goto done;
  4603. }
  4604. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  4605. cal_block = cal_utils_get_only_cal_block(this_afe.cal_data[cal_index]);
  4606. if (cal_block == NULL) {
  4607. pr_err("%s: cal_block not found\n ", __func__);
  4608. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  4609. ret = -EINVAL;
  4610. goto done;
  4611. }
  4612. iir_pregain = ((struct audio_cal_info_sidetone_iir *)
  4613. cal_block->cal_info)->pregain;
  4614. iir_enable = ((struct audio_cal_info_sidetone_iir *)
  4615. cal_block->cal_info)->iir_enable;
  4616. iir_num_biquad_stages = ((struct audio_cal_info_sidetone_iir *)
  4617. cal_block->cal_info)->num_biquad_stages;
  4618. mid = ((struct audio_cal_info_sidetone_iir *)
  4619. cal_block->cal_info)->mid;
  4620. /*
  4621. * calculate the actual size of payload based on no of stages
  4622. * enabled in calibration
  4623. */
  4624. size = (MAX_SIDETONE_IIR_DATA_SIZE / MAX_NO_IIR_FILTER_STAGE) *
  4625. iir_num_biquad_stages;
  4626. /*
  4627. * For an odd number of stages, 2 bytes of padding are
  4628. * required at the end of the payload.
  4629. */
  4630. if (iir_num_biquad_stages % 2) {
  4631. pr_debug("%s: adding 2 to size:%d\n", __func__, size);
  4632. size = size + 2;
  4633. }
  4634. memcpy(&iir_sidetone.st_iir_filter_config_data.iir_config,
  4635. &((struct audio_cal_info_sidetone_iir *)
  4636. cal_block->cal_info)->iir_config,
  4637. sizeof(iir_sidetone.st_iir_filter_config_data.iir_config));
  4638. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  4639. /*
  4640. * Calculate the payload size for setparams command
  4641. */
  4642. iir_sidetone.param.payload_size = (sizeof(iir_sidetone) -
  4643. sizeof(struct apr_hdr) -
  4644. sizeof(struct afe_port_cmd_set_param_v2) -
  4645. (MAX_SIDETONE_IIR_DATA_SIZE - size));
  4646. pr_debug("%s: payload size :%d\n", __func__,
  4647. iir_sidetone.param.payload_size);
  4648. /*
  4649. * Set IIR enable params
  4650. */
  4651. iir_sidetone.st_iir_enable_pdata.module_id = mid;
  4652. iir_sidetone.st_iir_enable_pdata.param_id =
  4653. AFE_PARAM_ID_ENABLE;
  4654. iir_sidetone.st_iir_enable_pdata.param_size =
  4655. sizeof(iir_sidetone.st_iir_mode_enable_data);
  4656. iir_sidetone.st_iir_mode_enable_data.enable = iir_enable;
  4657. /*
  4658. * Set IIR filter config params
  4659. */
  4660. iir_sidetone.st_iir_filter_config_pdata.module_id = mid;
  4661. iir_sidetone.st_iir_filter_config_pdata.param_id =
  4662. AFE_PARAM_ID_SIDETONE_IIR_FILTER_CONFIG;
  4663. iir_sidetone.st_iir_filter_config_pdata.param_size =
  4664. sizeof(iir_sidetone.st_iir_filter_config_data.num_biquad_stages)
  4665. +
  4666. sizeof(iir_sidetone.st_iir_filter_config_data.pregain) + size;
  4667. iir_sidetone.st_iir_filter_config_pdata.reserved = 0;
  4668. iir_sidetone.st_iir_filter_config_data.num_biquad_stages =
  4669. iir_num_biquad_stages;
  4670. iir_sidetone.st_iir_filter_config_data.pregain = iir_pregain;
  4671. pr_debug("%s: tx(0x%x)mid(0x%x)iir_en(%d)stg(%d)gain(0x%x)size(%d)\n",
  4672. __func__, tx_port_id, mid,
  4673. iir_sidetone.st_iir_mode_enable_data.enable,
  4674. iir_sidetone.st_iir_filter_config_data.num_biquad_stages,
  4675. iir_sidetone.st_iir_filter_config_data.pregain,
  4676. iir_sidetone.st_iir_filter_config_pdata.param_size);
  4677. ret = afe_apr_send_pkt(&iir_sidetone, &this_afe.wait[index]);
  4678. if (ret)
  4679. pr_err("%s: AFE sidetone failed for tx_port(0x%x)\n",
  4680. __func__, tx_port_id);
  4681. done:
  4682. return ret;
  4683. }
  4684. static int afe_sidetone(u16 tx_port_id, u16 rx_port_id, bool enable)
  4685. {
  4686. struct afe_st_loopback_cfg_v1 cmd_sidetone;
  4687. int ret;
  4688. int index;
  4689. int cal_index = AFE_SIDETONE_CAL;
  4690. int sidetone_gain;
  4691. int sidetone_enable;
  4692. struct cal_block_data *cal_block;
  4693. int mid = 0;
  4694. memset(&cmd_sidetone, 0, sizeof(cmd_sidetone));
  4695. if (this_afe.cal_data[cal_index] == NULL) {
  4696. pr_err("%s: cal data is NULL\n", __func__);
  4697. ret = -EINVAL;
  4698. goto done;
  4699. }
  4700. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  4701. cal_block = cal_utils_get_only_cal_block(this_afe.cal_data[cal_index]);
  4702. if (cal_block == NULL) {
  4703. pr_err("%s: cal_block not found\n", __func__);
  4704. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  4705. ret = -EINVAL;
  4706. goto done;
  4707. }
  4708. sidetone_gain = ((struct audio_cal_info_sidetone *)
  4709. cal_block->cal_info)->gain;
  4710. sidetone_enable = ((struct audio_cal_info_sidetone *)
  4711. cal_block->cal_info)->enable;
  4712. mid = ((struct audio_cal_info_sidetone *)
  4713. cal_block->cal_info)->mid;
  4714. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  4715. index = q6audio_get_port_index(tx_port_id);
  4716. cmd_sidetone.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4717. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4718. cmd_sidetone.hdr.pkt_size = sizeof(cmd_sidetone);
  4719. cmd_sidetone.hdr.src_port = 0;
  4720. cmd_sidetone.hdr.dest_port = 0;
  4721. cmd_sidetone.hdr.token = index;
  4722. cmd_sidetone.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  4723. cmd_sidetone.param.port_id = tx_port_id;
  4724. cmd_sidetone.param.payload_size = (sizeof(cmd_sidetone) -
  4725. sizeof(struct apr_hdr) -
  4726. sizeof(struct afe_port_cmd_set_param_v2));
  4727. cmd_sidetone.param.payload_address_lsw = 0x00;
  4728. cmd_sidetone.param.payload_address_msw = 0x00;
  4729. cmd_sidetone.param.mem_map_handle = 0x00;
  4730. cmd_sidetone.gain_pdata.module_id = AFE_MODULE_LOOPBACK;
  4731. cmd_sidetone.gain_pdata.param_id = AFE_PARAM_ID_LOOPBACK_GAIN_PER_PATH;
  4732. /*
  4733. * size of actual payload only
  4734. */
  4735. cmd_sidetone.gain_pdata.param_size = sizeof(
  4736. struct afe_loopback_sidetone_gain);
  4737. cmd_sidetone.gain_data.rx_port_id = rx_port_id;
  4738. cmd_sidetone.gain_data.gain = sidetone_gain;
  4739. cmd_sidetone.cfg_pdata.module_id = AFE_MODULE_LOOPBACK;
  4740. cmd_sidetone.cfg_pdata.param_id = AFE_PARAM_ID_LOOPBACK_CONFIG;
  4741. /*
  4742. * size of actual payload only
  4743. */
  4744. cmd_sidetone.cfg_pdata.param_size = sizeof(struct loopback_cfg_data);
  4745. cmd_sidetone.cfg_data.loopback_cfg_minor_version =
  4746. AFE_API_VERSION_LOOPBACK_CONFIG;
  4747. cmd_sidetone.cfg_data.dst_port_id = rx_port_id;
  4748. cmd_sidetone.cfg_data.routing_mode = LB_MODE_SIDETONE;
  4749. cmd_sidetone.cfg_data.enable = enable;
  4750. pr_debug("%s rx(0x%x) tx(0x%x) enable(%d) mid(0x%x) gain(%d) sidetone_enable(%d)\n",
  4751. __func__, rx_port_id, tx_port_id,
  4752. enable, mid, sidetone_gain, sidetone_enable);
  4753. ret = afe_apr_send_pkt(&cmd_sidetone, &this_afe.wait[index]);
  4754. if (ret)
  4755. pr_err("%s: AFE sidetone send failed for tx_port:%d rx_port:%d ret:%d\n",
  4756. __func__, tx_port_id, rx_port_id, ret);
  4757. done:
  4758. return ret;
  4759. }
  4760. int afe_sidetone_enable(u16 tx_port_id, u16 rx_port_id, bool enable)
  4761. {
  4762. int ret;
  4763. int index;
  4764. index = q6audio_get_port_index(rx_port_id);
  4765. if (index < 0 || index >= AFE_MAX_PORTS) {
  4766. pr_err("%s: AFE port index[%d] invalid!\n",
  4767. __func__, index);
  4768. ret = -EINVAL;
  4769. goto done;
  4770. }
  4771. if (q6audio_validate_port(rx_port_id) < 0) {
  4772. pr_err("%s: Invalid port 0x%x\n",
  4773. __func__, rx_port_id);
  4774. ret = -EINVAL;
  4775. goto done;
  4776. }
  4777. index = q6audio_get_port_index(tx_port_id);
  4778. if (index < 0 || index >= AFE_MAX_PORTS) {
  4779. pr_err("%s: AFE port index[%d] invalid!\n",
  4780. __func__, index);
  4781. ret = -EINVAL;
  4782. goto done;
  4783. }
  4784. if (q6audio_validate_port(tx_port_id) < 0) {
  4785. pr_err("%s: Invalid port 0x%x\n",
  4786. __func__, tx_port_id);
  4787. ret = -EINVAL;
  4788. goto done;
  4789. }
  4790. if (enable) {
  4791. ret = afe_sidetone_iir(tx_port_id);
  4792. if (ret)
  4793. goto done;
  4794. }
  4795. ret = afe_sidetone(tx_port_id, rx_port_id, enable);
  4796. done:
  4797. return ret;
  4798. }
  4799. int afe_validate_port(u16 port_id)
  4800. {
  4801. int ret;
  4802. switch (port_id) {
  4803. case PRIMARY_I2S_RX:
  4804. case PRIMARY_I2S_TX:
  4805. case AFE_PORT_ID_PRIMARY_PCM_RX:
  4806. case AFE_PORT_ID_PRIMARY_PCM_TX:
  4807. case AFE_PORT_ID_SECONDARY_PCM_RX:
  4808. case AFE_PORT_ID_SECONDARY_PCM_TX:
  4809. case AFE_PORT_ID_TERTIARY_PCM_RX:
  4810. case AFE_PORT_ID_TERTIARY_PCM_TX:
  4811. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  4812. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  4813. case AFE_PORT_ID_QUINARY_PCM_RX:
  4814. case AFE_PORT_ID_QUINARY_PCM_TX:
  4815. case SECONDARY_I2S_RX:
  4816. case SECONDARY_I2S_TX:
  4817. case MI2S_RX:
  4818. case MI2S_TX:
  4819. case HDMI_RX:
  4820. case DISPLAY_PORT_RX:
  4821. case AFE_PORT_ID_SPDIF_RX:
  4822. case RSVD_2:
  4823. case RSVD_3:
  4824. case DIGI_MIC_TX:
  4825. case VOICE_RECORD_RX:
  4826. case VOICE_RECORD_TX:
  4827. case VOICE_PLAYBACK_TX:
  4828. case VOICE2_PLAYBACK_TX:
  4829. case SLIMBUS_0_RX:
  4830. case SLIMBUS_0_TX:
  4831. case SLIMBUS_1_RX:
  4832. case SLIMBUS_1_TX:
  4833. case SLIMBUS_2_RX:
  4834. case SLIMBUS_2_TX:
  4835. case SLIMBUS_3_RX:
  4836. case INT_BT_SCO_RX:
  4837. case INT_BT_SCO_TX:
  4838. case INT_BT_A2DP_RX:
  4839. case INT_FM_RX:
  4840. case INT_FM_TX:
  4841. case RT_PROXY_PORT_001_RX:
  4842. case RT_PROXY_PORT_001_TX:
  4843. case SLIMBUS_4_RX:
  4844. case SLIMBUS_4_TX:
  4845. case SLIMBUS_5_RX:
  4846. case SLIMBUS_6_RX:
  4847. case SLIMBUS_6_TX:
  4848. case SLIMBUS_7_RX:
  4849. case SLIMBUS_7_TX:
  4850. case SLIMBUS_8_RX:
  4851. case SLIMBUS_8_TX:
  4852. case AFE_PORT_ID_USB_RX:
  4853. case AFE_PORT_ID_USB_TX:
  4854. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  4855. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  4856. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  4857. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  4858. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  4859. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  4860. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  4861. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  4862. case AFE_PORT_ID_QUINARY_MI2S_RX:
  4863. case AFE_PORT_ID_QUINARY_MI2S_TX:
  4864. case AFE_PORT_ID_SENARY_MI2S_TX:
  4865. case AFE_PORT_ID_PRIMARY_TDM_RX:
  4866. case AFE_PORT_ID_PRIMARY_TDM_TX:
  4867. case AFE_PORT_ID_PRIMARY_TDM_RX_1:
  4868. case AFE_PORT_ID_PRIMARY_TDM_TX_1:
  4869. case AFE_PORT_ID_PRIMARY_TDM_RX_2:
  4870. case AFE_PORT_ID_PRIMARY_TDM_TX_2:
  4871. case AFE_PORT_ID_PRIMARY_TDM_RX_3:
  4872. case AFE_PORT_ID_PRIMARY_TDM_TX_3:
  4873. case AFE_PORT_ID_PRIMARY_TDM_RX_4:
  4874. case AFE_PORT_ID_PRIMARY_TDM_TX_4:
  4875. case AFE_PORT_ID_PRIMARY_TDM_RX_5:
  4876. case AFE_PORT_ID_PRIMARY_TDM_TX_5:
  4877. case AFE_PORT_ID_PRIMARY_TDM_RX_6:
  4878. case AFE_PORT_ID_PRIMARY_TDM_TX_6:
  4879. case AFE_PORT_ID_PRIMARY_TDM_RX_7:
  4880. case AFE_PORT_ID_PRIMARY_TDM_TX_7:
  4881. case AFE_PORT_ID_SECONDARY_TDM_RX:
  4882. case AFE_PORT_ID_SECONDARY_TDM_TX:
  4883. case AFE_PORT_ID_SECONDARY_TDM_RX_1:
  4884. case AFE_PORT_ID_SECONDARY_TDM_TX_1:
  4885. case AFE_PORT_ID_SECONDARY_TDM_RX_2:
  4886. case AFE_PORT_ID_SECONDARY_TDM_TX_2:
  4887. case AFE_PORT_ID_SECONDARY_TDM_RX_3:
  4888. case AFE_PORT_ID_SECONDARY_TDM_TX_3:
  4889. case AFE_PORT_ID_SECONDARY_TDM_RX_4:
  4890. case AFE_PORT_ID_SECONDARY_TDM_TX_4:
  4891. case AFE_PORT_ID_SECONDARY_TDM_RX_5:
  4892. case AFE_PORT_ID_SECONDARY_TDM_TX_5:
  4893. case AFE_PORT_ID_SECONDARY_TDM_RX_6:
  4894. case AFE_PORT_ID_SECONDARY_TDM_TX_6:
  4895. case AFE_PORT_ID_SECONDARY_TDM_RX_7:
  4896. case AFE_PORT_ID_SECONDARY_TDM_TX_7:
  4897. case AFE_PORT_ID_TERTIARY_TDM_RX:
  4898. case AFE_PORT_ID_TERTIARY_TDM_TX:
  4899. case AFE_PORT_ID_TERTIARY_TDM_RX_1:
  4900. case AFE_PORT_ID_TERTIARY_TDM_TX_1:
  4901. case AFE_PORT_ID_TERTIARY_TDM_RX_2:
  4902. case AFE_PORT_ID_TERTIARY_TDM_TX_2:
  4903. case AFE_PORT_ID_TERTIARY_TDM_RX_3:
  4904. case AFE_PORT_ID_TERTIARY_TDM_TX_3:
  4905. case AFE_PORT_ID_TERTIARY_TDM_RX_4:
  4906. case AFE_PORT_ID_TERTIARY_TDM_TX_4:
  4907. case AFE_PORT_ID_TERTIARY_TDM_RX_5:
  4908. case AFE_PORT_ID_TERTIARY_TDM_TX_5:
  4909. case AFE_PORT_ID_TERTIARY_TDM_RX_6:
  4910. case AFE_PORT_ID_TERTIARY_TDM_TX_6:
  4911. case AFE_PORT_ID_TERTIARY_TDM_RX_7:
  4912. case AFE_PORT_ID_TERTIARY_TDM_TX_7:
  4913. case AFE_PORT_ID_QUATERNARY_TDM_RX:
  4914. case AFE_PORT_ID_QUATERNARY_TDM_TX:
  4915. case AFE_PORT_ID_QUATERNARY_TDM_RX_1:
  4916. case AFE_PORT_ID_QUATERNARY_TDM_TX_1:
  4917. case AFE_PORT_ID_QUATERNARY_TDM_RX_2:
  4918. case AFE_PORT_ID_QUATERNARY_TDM_TX_2:
  4919. case AFE_PORT_ID_QUATERNARY_TDM_RX_3:
  4920. case AFE_PORT_ID_QUATERNARY_TDM_TX_3:
  4921. case AFE_PORT_ID_QUATERNARY_TDM_RX_4:
  4922. case AFE_PORT_ID_QUATERNARY_TDM_TX_4:
  4923. case AFE_PORT_ID_QUATERNARY_TDM_RX_5:
  4924. case AFE_PORT_ID_QUATERNARY_TDM_TX_5:
  4925. case AFE_PORT_ID_QUATERNARY_TDM_RX_6:
  4926. case AFE_PORT_ID_QUATERNARY_TDM_TX_6:
  4927. case AFE_PORT_ID_QUATERNARY_TDM_RX_7:
  4928. case AFE_PORT_ID_QUATERNARY_TDM_TX_7:
  4929. case AFE_PORT_ID_QUINARY_TDM_RX:
  4930. case AFE_PORT_ID_QUINARY_TDM_TX:
  4931. case AFE_PORT_ID_QUINARY_TDM_RX_1:
  4932. case AFE_PORT_ID_QUINARY_TDM_TX_1:
  4933. case AFE_PORT_ID_QUINARY_TDM_RX_2:
  4934. case AFE_PORT_ID_QUINARY_TDM_TX_2:
  4935. case AFE_PORT_ID_QUINARY_TDM_RX_3:
  4936. case AFE_PORT_ID_QUINARY_TDM_TX_3:
  4937. case AFE_PORT_ID_QUINARY_TDM_RX_4:
  4938. case AFE_PORT_ID_QUINARY_TDM_TX_4:
  4939. case AFE_PORT_ID_QUINARY_TDM_RX_5:
  4940. case AFE_PORT_ID_QUINARY_TDM_TX_5:
  4941. case AFE_PORT_ID_QUINARY_TDM_RX_6:
  4942. case AFE_PORT_ID_QUINARY_TDM_TX_6:
  4943. case AFE_PORT_ID_QUINARY_TDM_RX_7:
  4944. case AFE_PORT_ID_QUINARY_TDM_TX_7:
  4945. case AFE_PORT_ID_INT0_MI2S_RX:
  4946. case AFE_PORT_ID_INT1_MI2S_RX:
  4947. case AFE_PORT_ID_INT2_MI2S_RX:
  4948. case AFE_PORT_ID_INT3_MI2S_RX:
  4949. case AFE_PORT_ID_INT4_MI2S_RX:
  4950. case AFE_PORT_ID_INT5_MI2S_RX:
  4951. case AFE_PORT_ID_INT6_MI2S_RX:
  4952. case AFE_PORT_ID_INT0_MI2S_TX:
  4953. case AFE_PORT_ID_INT1_MI2S_TX:
  4954. case AFE_PORT_ID_INT2_MI2S_TX:
  4955. case AFE_PORT_ID_INT3_MI2S_TX:
  4956. case AFE_PORT_ID_INT4_MI2S_TX:
  4957. case AFE_PORT_ID_INT5_MI2S_TX:
  4958. case AFE_PORT_ID_INT6_MI2S_TX:
  4959. {
  4960. ret = 0;
  4961. break;
  4962. }
  4963. default:
  4964. pr_err("%s: default ret 0x%x\n", __func__, port_id);
  4965. ret = -EINVAL;
  4966. }
  4967. return ret;
  4968. }
  4969. int afe_convert_virtual_to_portid(u16 port_id)
  4970. {
  4971. int ret;
  4972. /*
  4973. * if port_id is virtual, convert to physical..
  4974. * if port_id is already physical, return physical
  4975. */
  4976. if (afe_validate_port(port_id) < 0) {
  4977. if (port_id == RT_PROXY_DAI_001_RX ||
  4978. port_id == RT_PROXY_DAI_001_TX ||
  4979. port_id == RT_PROXY_DAI_002_RX ||
  4980. port_id == RT_PROXY_DAI_002_TX) {
  4981. ret = VIRTUAL_ID_TO_PORTID(port_id);
  4982. } else {
  4983. pr_err("%s: wrong port 0x%x\n",
  4984. __func__, port_id);
  4985. ret = -EINVAL;
  4986. }
  4987. } else
  4988. ret = port_id;
  4989. return ret;
  4990. }
  4991. int afe_port_stop_nowait(int port_id)
  4992. {
  4993. struct afe_port_cmd_device_stop stop;
  4994. int ret = 0;
  4995. if (this_afe.apr == NULL) {
  4996. pr_err("%s: AFE is already closed\n", __func__);
  4997. ret = -EINVAL;
  4998. goto fail_cmd;
  4999. }
  5000. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  5001. port_id = q6audio_convert_virtual_to_portid(port_id);
  5002. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5003. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5004. stop.hdr.pkt_size = sizeof(stop);
  5005. stop.hdr.src_port = 0;
  5006. stop.hdr.dest_port = 0;
  5007. stop.hdr.token = 0;
  5008. stop.hdr.opcode = AFE_PORT_CMD_DEVICE_STOP;
  5009. stop.port_id = port_id;
  5010. stop.reserved = 0;
  5011. ret = afe_apr_send_pkt(&stop, NULL);
  5012. if (ret)
  5013. pr_err("%s: AFE close failed %d\n", __func__, ret);
  5014. fail_cmd:
  5015. return ret;
  5016. }
  5017. int afe_close(int port_id)
  5018. {
  5019. struct afe_port_cmd_device_stop stop;
  5020. enum afe_mad_type mad_type;
  5021. int ret = 0;
  5022. int index = 0;
  5023. uint16_t port_index;
  5024. if (this_afe.apr == NULL) {
  5025. pr_err("%s: AFE is already closed\n", __func__);
  5026. if ((port_id == RT_PROXY_DAI_001_RX) ||
  5027. (port_id == RT_PROXY_DAI_002_TX))
  5028. pcm_afe_instance[port_id & 0x1] = 0;
  5029. if ((port_id == RT_PROXY_DAI_002_RX) ||
  5030. (port_id == RT_PROXY_DAI_001_TX))
  5031. proxy_afe_instance[port_id & 0x1] = 0;
  5032. afe_close_done[port_id & 0x1] = true;
  5033. ret = -EINVAL;
  5034. goto fail_cmd;
  5035. }
  5036. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  5037. if ((port_id == RT_PROXY_DAI_001_RX) ||
  5038. (port_id == RT_PROXY_DAI_002_TX)) {
  5039. pr_debug("%s: before decrementing pcm_afe_instance %d\n",
  5040. __func__, pcm_afe_instance[port_id & 0x1]);
  5041. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  5042. pcm_afe_instance[port_id & 0x1]--;
  5043. if ((!(pcm_afe_instance[port_id & 0x1] == 0 &&
  5044. proxy_afe_instance[port_id & 0x1] == 0)) ||
  5045. afe_close_done[port_id & 0x1] == true)
  5046. return 0;
  5047. afe_close_done[port_id & 0x1] = true;
  5048. }
  5049. if ((port_id == RT_PROXY_DAI_002_RX) ||
  5050. (port_id == RT_PROXY_DAI_001_TX)) {
  5051. pr_debug("%s: before decrementing proxy_afe_instance %d\n",
  5052. __func__, proxy_afe_instance[port_id & 0x1]);
  5053. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  5054. proxy_afe_instance[port_id & 0x1]--;
  5055. if ((!(pcm_afe_instance[port_id & 0x1] == 0 &&
  5056. proxy_afe_instance[port_id & 0x1] == 0)) ||
  5057. afe_close_done[port_id & 0x1] == true)
  5058. return 0;
  5059. afe_close_done[port_id & 0x1] = true;
  5060. }
  5061. port_id = q6audio_convert_virtual_to_portid(port_id);
  5062. index = q6audio_get_port_index(port_id);
  5063. if (index < 0 || index >= AFE_MAX_PORTS) {
  5064. pr_err("%s: AFE port index[%d] invalid!\n",
  5065. __func__, index);
  5066. return -EINVAL;
  5067. }
  5068. ret = q6audio_validate_port(port_id);
  5069. if (ret < 0) {
  5070. pr_warn("%s: Not a valid port id 0x%x ret %d\n",
  5071. __func__, port_id, ret);
  5072. return -EINVAL;
  5073. }
  5074. mad_type = afe_port_get_mad_type(port_id);
  5075. pr_debug("%s: port_id 0x%x, mad_type %d\n", __func__, port_id,
  5076. mad_type);
  5077. if (mad_type != MAD_HW_NONE && mad_type != MAD_SW_AUDIO) {
  5078. pr_debug("%s: Turn off MAD\n", __func__);
  5079. ret = afe_turn_onoff_hw_mad(mad_type, false);
  5080. if (ret) {
  5081. pr_err("%s: afe_turn_onoff_hw_mad failed %d\n",
  5082. __func__, ret);
  5083. return ret;
  5084. }
  5085. } else {
  5086. pr_debug("%s: Not a MAD port\n", __func__);
  5087. }
  5088. port_index = afe_get_port_index(port_id);
  5089. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  5090. this_afe.afe_sample_rates[port_index] = 0;
  5091. this_afe.topology[port_index] = 0;
  5092. this_afe.dev_acdb_id[port_index] = 0;
  5093. } else {
  5094. pr_err("%s: port %d\n", __func__, port_index);
  5095. ret = -EINVAL;
  5096. goto fail_cmd;
  5097. }
  5098. if ((port_id == this_afe.aanc_info.aanc_tx_port) &&
  5099. (this_afe.aanc_info.aanc_active)) {
  5100. memset(&this_afe.aanc_info, 0x00, sizeof(this_afe.aanc_info));
  5101. ret = afe_aanc_mod_enable(this_afe.apr, port_id, 0);
  5102. if (ret)
  5103. pr_err("%s: AFE mod disable failed %d\n",
  5104. __func__, ret);
  5105. }
  5106. /*
  5107. * even if ramp down configuration failed it is not serious enough to
  5108. * warrant bailaing out.
  5109. */
  5110. if (afe_spk_ramp_dn_cfg(port_id) < 0)
  5111. pr_err("%s: ramp down configuration failed\n", __func__);
  5112. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5113. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5114. stop.hdr.pkt_size = sizeof(stop);
  5115. stop.hdr.src_port = 0;
  5116. stop.hdr.dest_port = 0;
  5117. stop.hdr.token = index;
  5118. stop.hdr.opcode = AFE_PORT_CMD_DEVICE_STOP;
  5119. stop.port_id = q6audio_get_port_id(port_id);
  5120. stop.reserved = 0;
  5121. ret = afe_apr_send_pkt(&stop, &this_afe.wait[index]);
  5122. if (ret)
  5123. pr_err("%s: AFE close failed %d\n", __func__, ret);
  5124. fail_cmd:
  5125. return ret;
  5126. }
  5127. int afe_set_digital_codec_core_clock(u16 port_id,
  5128. struct afe_digital_clk_cfg *cfg)
  5129. {
  5130. struct afe_lpass_digital_clk_config_command clk_cfg;
  5131. int index = 0;
  5132. int ret = 0;
  5133. if (!cfg) {
  5134. pr_err("%s: clock cfg is NULL\n", __func__);
  5135. ret = -EINVAL;
  5136. return ret;
  5137. }
  5138. ret = afe_q6_interface_prepare();
  5139. if (ret != 0) {
  5140. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  5141. return ret;
  5142. }
  5143. clk_cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5144. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5145. clk_cfg.hdr.pkt_size = sizeof(clk_cfg);
  5146. clk_cfg.hdr.src_port = 0;
  5147. clk_cfg.hdr.dest_port = 0;
  5148. clk_cfg.hdr.token = index;
  5149. clk_cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  5150. /*default rx port is taken to enable the codec digital clock*/
  5151. clk_cfg.param.port_id = q6audio_get_port_id(port_id);
  5152. clk_cfg.param.payload_size = sizeof(clk_cfg) - sizeof(struct apr_hdr)
  5153. - sizeof(clk_cfg.param);
  5154. clk_cfg.param.payload_address_lsw = 0x00;
  5155. clk_cfg.param.payload_address_msw = 0x00;
  5156. clk_cfg.param.mem_map_handle = 0x00;
  5157. clk_cfg.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5158. clk_cfg.pdata.param_id = AFE_PARAM_ID_INTERNAL_DIGIATL_CDC_CLK_CONFIG;
  5159. clk_cfg.pdata.param_size = sizeof(clk_cfg.clk_cfg);
  5160. clk_cfg.clk_cfg = *cfg;
  5161. pr_debug("%s: Minor version =0x%x clk val = %d\n"
  5162. "clk root = 0x%x resrv = 0x%x\n",
  5163. __func__, cfg->i2s_cfg_minor_version,
  5164. cfg->clk_val, cfg->clk_root, cfg->reserved);
  5165. atomic_set(&this_afe.state, 1);
  5166. atomic_set(&this_afe.status, 0);
  5167. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &clk_cfg);
  5168. if (ret < 0) {
  5169. pr_err("%s: AFE enable for port 0x%x ret %d\n",
  5170. __func__, port_id, ret);
  5171. ret = -EINVAL;
  5172. goto fail_cmd;
  5173. }
  5174. ret = wait_event_timeout(this_afe.wait[index],
  5175. (atomic_read(&this_afe.state) == 0),
  5176. msecs_to_jiffies(TIMEOUT_MS));
  5177. if (!ret) {
  5178. pr_err("%s: wait_event timeout\n", __func__);
  5179. ret = -EINVAL;
  5180. goto fail_cmd;
  5181. }
  5182. if (atomic_read(&this_afe.status) > 0) {
  5183. pr_err("%s: config cmd failed [%s]\n",
  5184. __func__, adsp_err_get_err_str(
  5185. atomic_read(&this_afe.status)));
  5186. ret = adsp_err_get_lnx_err_code(
  5187. atomic_read(&this_afe.status));
  5188. goto fail_cmd;
  5189. }
  5190. fail_cmd:
  5191. return ret;
  5192. }
  5193. int afe_set_lpass_clock(u16 port_id, struct afe_clk_cfg *cfg)
  5194. {
  5195. struct afe_lpass_clk_config_command clk_cfg;
  5196. int index = 0;
  5197. int ret = 0;
  5198. if (!cfg) {
  5199. pr_err("%s: clock cfg is NULL\n", __func__);
  5200. ret = -EINVAL;
  5201. return ret;
  5202. }
  5203. index = q6audio_get_port_index(port_id);
  5204. if (index < 0 || index >= AFE_MAX_PORTS) {
  5205. pr_err("%s: AFE port index[%d] invalid!\n",
  5206. __func__, index);
  5207. return -EINVAL;
  5208. }
  5209. ret = q6audio_is_digital_pcm_interface(port_id);
  5210. if (ret < 0) {
  5211. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  5212. __func__, ret);
  5213. return -EINVAL;
  5214. }
  5215. ret = afe_q6_interface_prepare();
  5216. if (ret != 0) {
  5217. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  5218. return ret;
  5219. }
  5220. mutex_lock(&this_afe.afe_cmd_lock);
  5221. clk_cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5222. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5223. clk_cfg.hdr.pkt_size = sizeof(clk_cfg);
  5224. clk_cfg.hdr.src_port = 0;
  5225. clk_cfg.hdr.dest_port = 0;
  5226. clk_cfg.hdr.token = index;
  5227. clk_cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  5228. clk_cfg.param.port_id = q6audio_get_port_id(port_id);
  5229. clk_cfg.param.payload_size = sizeof(clk_cfg) - sizeof(struct apr_hdr)
  5230. - sizeof(clk_cfg.param);
  5231. clk_cfg.param.payload_address_lsw = 0x00;
  5232. clk_cfg.param.payload_address_msw = 0x00;
  5233. clk_cfg.param.mem_map_handle = 0x00;
  5234. clk_cfg.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5235. clk_cfg.pdata.param_id = AFE_PARAM_ID_LPAIF_CLK_CONFIG;
  5236. clk_cfg.pdata.param_size = sizeof(clk_cfg.clk_cfg);
  5237. clk_cfg.clk_cfg = *cfg;
  5238. pr_debug("%s: Minor version =0x%x clk val1 = %d\n"
  5239. "clk val2 = %d, clk src = 0x%x\n"
  5240. "clk root = 0x%x clk mode = 0x%x resrv = 0x%x\n"
  5241. "port id = 0x%x\n",
  5242. __func__, cfg->i2s_cfg_minor_version,
  5243. cfg->clk_val1, cfg->clk_val2, cfg->clk_src,
  5244. cfg->clk_root, cfg->clk_set_mode,
  5245. cfg->reserved, q6audio_get_port_id(port_id));
  5246. atomic_set(&this_afe.state, 1);
  5247. atomic_set(&this_afe.status, 0);
  5248. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &clk_cfg);
  5249. if (ret < 0) {
  5250. pr_err("%s: AFE enable for port 0x%x ret %d\n",
  5251. __func__, port_id, ret);
  5252. ret = -EINVAL;
  5253. goto fail_cmd;
  5254. }
  5255. ret = wait_event_timeout(this_afe.wait[index],
  5256. (atomic_read(&this_afe.state) == 0),
  5257. msecs_to_jiffies(TIMEOUT_MS));
  5258. if (!ret) {
  5259. pr_err("%s: wait_event timeout\n", __func__);
  5260. ret = -EINVAL;
  5261. goto fail_cmd;
  5262. }
  5263. if (atomic_read(&this_afe.status) > 0) {
  5264. pr_err("%s: config cmd failed [%s]\n",
  5265. __func__, adsp_err_get_err_str(
  5266. atomic_read(&this_afe.status)));
  5267. ret = adsp_err_get_lnx_err_code(
  5268. atomic_read(&this_afe.status));
  5269. goto fail_cmd;
  5270. }
  5271. fail_cmd:
  5272. mutex_unlock(&this_afe.afe_cmd_lock);
  5273. return ret;
  5274. }
  5275. /**
  5276. * afe_set_lpass_clk_cfg - Set AFE clk config
  5277. *
  5278. * @index: port index
  5279. * @cfg: pointer to clk set struct
  5280. *
  5281. * Returns 0 on success, appropriate error code otherwise
  5282. */
  5283. int afe_set_lpass_clk_cfg(int index, struct afe_clk_set *cfg)
  5284. {
  5285. struct afe_lpass_clk_config_command_v2 clk_cfg;
  5286. int ret = 0;
  5287. if (!cfg) {
  5288. pr_err("%s: clock cfg is NULL\n", __func__);
  5289. ret = -EINVAL;
  5290. return ret;
  5291. }
  5292. if (index < 0 || index >= AFE_MAX_PORTS) {
  5293. pr_err("%s: index[%d] invalid!\n", __func__, index);
  5294. return -EINVAL;
  5295. }
  5296. ret = afe_q6_interface_prepare();
  5297. if (ret != 0) {
  5298. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  5299. return ret;
  5300. }
  5301. mutex_lock(&this_afe.afe_cmd_lock);
  5302. clk_cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5303. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5304. clk_cfg.hdr.pkt_size = sizeof(clk_cfg);
  5305. clk_cfg.hdr.src_port = 0;
  5306. clk_cfg.hdr.dest_port = 0;
  5307. clk_cfg.hdr.token = index;
  5308. clk_cfg.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  5309. clk_cfg.param.payload_size = sizeof(clk_cfg) - sizeof(struct apr_hdr)
  5310. - sizeof(clk_cfg.param);
  5311. clk_cfg.param.payload_address_lsw = 0x00;
  5312. clk_cfg.param.payload_address_msw = 0x00;
  5313. clk_cfg.param.mem_map_handle = 0x00;
  5314. clk_cfg.pdata.module_id = AFE_MODULE_CLOCK_SET;
  5315. clk_cfg.pdata.param_id = AFE_PARAM_ID_CLOCK_SET;
  5316. clk_cfg.pdata.param_size = sizeof(clk_cfg.clk_cfg);
  5317. clk_cfg.clk_cfg = *cfg;
  5318. pr_debug("%s: Minor version =0x%x clk id = %d\n"
  5319. "clk freq (Hz) = %d, clk attri = 0x%x\n"
  5320. "clk root = 0x%x clk enable = 0x%x\n",
  5321. __func__, cfg->clk_set_minor_version,
  5322. cfg->clk_id, cfg->clk_freq_in_hz, cfg->clk_attri,
  5323. cfg->clk_root, cfg->enable);
  5324. atomic_set(&this_afe.state, 1);
  5325. atomic_set(&this_afe.status, 0);
  5326. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &clk_cfg);
  5327. if (ret < 0) {
  5328. pr_err("%s: AFE clk cfg failed with ret %d\n",
  5329. __func__, ret);
  5330. ret = -EINVAL;
  5331. goto fail_cmd;
  5332. }
  5333. ret = wait_event_timeout(this_afe.wait[index],
  5334. (atomic_read(&this_afe.state) == 0),
  5335. msecs_to_jiffies(TIMEOUT_MS));
  5336. if (!ret) {
  5337. pr_err("%s: wait_event timeout\n", __func__);
  5338. ret = -EINVAL;
  5339. goto fail_cmd;
  5340. } else {
  5341. /* set ret to 0 as no timeout happened */
  5342. ret = 0;
  5343. }
  5344. if (atomic_read(&this_afe.status) != 0) {
  5345. pr_err("%s: config cmd failed\n", __func__);
  5346. ret = -EINVAL;
  5347. goto fail_cmd;
  5348. }
  5349. fail_cmd:
  5350. mutex_unlock(&this_afe.afe_cmd_lock);
  5351. return ret;
  5352. }
  5353. EXPORT_SYMBOL(afe_set_lpass_clk_cfg);
  5354. /**
  5355. * afe_set_lpass_clock_v2 - Enable AFE lpass clock
  5356. *
  5357. * @port_id: AFE port id
  5358. * @cfg: pointer to clk set struct
  5359. *
  5360. * Returns 0 on success, appropriate error code otherwise
  5361. */
  5362. int afe_set_lpass_clock_v2(u16 port_id, struct afe_clk_set *cfg)
  5363. {
  5364. int index = 0;
  5365. int ret = 0;
  5366. index = q6audio_get_port_index(port_id);
  5367. if (index < 0 || index >= AFE_MAX_PORTS) {
  5368. pr_err("%s: AFE port index[%d] invalid!\n",
  5369. __func__, index);
  5370. return -EINVAL;
  5371. }
  5372. ret = q6audio_is_digital_pcm_interface(port_id);
  5373. if (ret < 0) {
  5374. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  5375. __func__, ret);
  5376. return -EINVAL;
  5377. }
  5378. ret = afe_set_lpass_clk_cfg(index, cfg);
  5379. if (ret)
  5380. pr_err("%s: afe_set_lpass_clk_cfg_v2 failed %d\n",
  5381. __func__, ret);
  5382. return ret;
  5383. }
  5384. EXPORT_SYMBOL(afe_set_lpass_clock_v2);
  5385. int afe_set_lpass_internal_digital_codec_clock(u16 port_id,
  5386. struct afe_digital_clk_cfg *cfg)
  5387. {
  5388. struct afe_lpass_digital_clk_config_command clk_cfg;
  5389. int index = 0;
  5390. int ret = 0;
  5391. if (!cfg) {
  5392. pr_err("%s: clock cfg is NULL\n", __func__);
  5393. ret = -EINVAL;
  5394. return ret;
  5395. }
  5396. index = q6audio_get_port_index(port_id);
  5397. if (index < 0 || index >= AFE_MAX_PORTS) {
  5398. pr_err("%s: AFE port index[%d] invalid!\n",
  5399. __func__, index);
  5400. return -EINVAL;
  5401. }
  5402. ret = q6audio_is_digital_pcm_interface(port_id);
  5403. if (ret < 0) {
  5404. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  5405. __func__, ret);
  5406. return -EINVAL;
  5407. }
  5408. ret = afe_q6_interface_prepare();
  5409. if (ret != 0) {
  5410. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  5411. return ret;
  5412. }
  5413. clk_cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5414. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5415. clk_cfg.hdr.pkt_size = sizeof(clk_cfg);
  5416. clk_cfg.hdr.src_port = 0;
  5417. clk_cfg.hdr.dest_port = 0;
  5418. clk_cfg.hdr.token = index;
  5419. clk_cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  5420. clk_cfg.param.port_id = q6audio_get_port_id(port_id);
  5421. clk_cfg.param.payload_size = sizeof(clk_cfg) - sizeof(struct apr_hdr)
  5422. - sizeof(clk_cfg.param);
  5423. clk_cfg.param.payload_address_lsw = 0x00;
  5424. clk_cfg.param.payload_address_msw = 0x00;
  5425. clk_cfg.param.mem_map_handle = 0x00;
  5426. clk_cfg.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5427. clk_cfg.pdata.param_id = AFE_PARAM_ID_INTERNAL_DIGIATL_CDC_CLK_CONFIG;
  5428. clk_cfg.pdata.param_size = sizeof(clk_cfg.clk_cfg);
  5429. clk_cfg.clk_cfg = *cfg;
  5430. pr_debug("%s: Minor version =0x%x clk val = %d\n"
  5431. "clk root = 0x%x resrv = 0x%x port id = 0x%x\n",
  5432. __func__, cfg->i2s_cfg_minor_version,
  5433. cfg->clk_val, cfg->clk_root, cfg->reserved,
  5434. q6audio_get_port_id(port_id));
  5435. atomic_set(&this_afe.state, 1);
  5436. atomic_set(&this_afe.status, 0);
  5437. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &clk_cfg);
  5438. if (ret < 0) {
  5439. pr_err("%s: AFE enable for port 0x0x%x ret %d\n",
  5440. __func__, port_id, ret);
  5441. ret = -EINVAL;
  5442. goto fail_cmd;
  5443. }
  5444. ret = wait_event_timeout(this_afe.wait[index],
  5445. (atomic_read(&this_afe.state) == 0),
  5446. msecs_to_jiffies(TIMEOUT_MS));
  5447. if (!ret) {
  5448. pr_err("%s: wait_event timeout\n", __func__);
  5449. ret = -EINVAL;
  5450. goto fail_cmd;
  5451. }
  5452. if (atomic_read(&this_afe.status) > 0) {
  5453. pr_err("%s: config cmd failed [%s]\n",
  5454. __func__, adsp_err_get_err_str(
  5455. atomic_read(&this_afe.status)));
  5456. ret = adsp_err_get_lnx_err_code(
  5457. atomic_read(&this_afe.status));
  5458. goto fail_cmd;
  5459. }
  5460. fail_cmd:
  5461. return ret;
  5462. }
  5463. int afe_enable_lpass_core_shared_clock(u16 port_id, u32 enable)
  5464. {
  5465. struct afe_lpass_core_shared_clk_config_command clk_cfg;
  5466. int index = 0;
  5467. int ret = 0;
  5468. index = q6audio_get_port_index(port_id);
  5469. if (index < 0 || index >= AFE_MAX_PORTS) {
  5470. pr_err("%s: AFE port index[%d] invalid!\n",
  5471. __func__, index);
  5472. return -EINVAL;
  5473. }
  5474. ret = q6audio_is_digital_pcm_interface(port_id);
  5475. if (ret < 0) {
  5476. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  5477. __func__, ret);
  5478. return -EINVAL;
  5479. }
  5480. ret = afe_q6_interface_prepare();
  5481. if (ret != 0) {
  5482. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  5483. return ret;
  5484. }
  5485. mutex_lock(&this_afe.afe_cmd_lock);
  5486. clk_cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5487. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5488. clk_cfg.hdr.pkt_size = sizeof(clk_cfg);
  5489. clk_cfg.hdr.src_port = 0;
  5490. clk_cfg.hdr.dest_port = 0;
  5491. clk_cfg.hdr.token = index;
  5492. clk_cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  5493. clk_cfg.param.port_id = q6audio_get_port_id(port_id);
  5494. clk_cfg.param.payload_size = sizeof(clk_cfg) - sizeof(struct apr_hdr)
  5495. - sizeof(clk_cfg.param);
  5496. clk_cfg.param.payload_address_lsw = 0x00;
  5497. clk_cfg.param.payload_address_msw = 0x00;
  5498. clk_cfg.param.mem_map_handle = 0x00;
  5499. clk_cfg.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5500. clk_cfg.pdata.param_id = AFE_PARAM_ID_LPASS_CORE_SHARED_CLOCK_CONFIG;
  5501. clk_cfg.pdata.param_size = sizeof(clk_cfg.clk_cfg);
  5502. clk_cfg.clk_cfg.lpass_core_shared_clk_cfg_minor_version =
  5503. AFE_API_VERSION_LPASS_CORE_SHARED_CLK_CONFIG;
  5504. clk_cfg.clk_cfg.enable = enable;
  5505. pr_debug("%s: port id = %d, enable = %d\n",
  5506. __func__, q6audio_get_port_id(port_id), enable);
  5507. atomic_set(&this_afe.state, 1);
  5508. atomic_set(&this_afe.status, 0);
  5509. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &clk_cfg);
  5510. if (ret < 0) {
  5511. pr_err("%s: AFE enable for port 0x%x ret %d\n",
  5512. __func__, port_id, ret);
  5513. ret = -EINVAL;
  5514. goto fail_cmd;
  5515. }
  5516. ret = wait_event_timeout(this_afe.wait[index],
  5517. (atomic_read(&this_afe.state) == 0),
  5518. msecs_to_jiffies(TIMEOUT_MS));
  5519. if (!ret) {
  5520. pr_err("%s: wait_event timeout\n", __func__);
  5521. ret = -EINVAL;
  5522. goto fail_cmd;
  5523. }
  5524. if (atomic_read(&this_afe.status) > 0) {
  5525. pr_err("%s: config cmd failed [%s]\n",
  5526. __func__, adsp_err_get_err_str(
  5527. atomic_read(&this_afe.status)));
  5528. ret = adsp_err_get_lnx_err_code(
  5529. atomic_read(&this_afe.status));
  5530. goto fail_cmd;
  5531. }
  5532. fail_cmd:
  5533. mutex_unlock(&this_afe.afe_cmd_lock);
  5534. return ret;
  5535. }
  5536. int q6afe_check_osr_clk_freq(u32 freq)
  5537. {
  5538. int ret = 0;
  5539. switch (freq) {
  5540. case Q6AFE_LPASS_OSR_CLK_12_P288_MHZ:
  5541. case Q6AFE_LPASS_OSR_CLK_8_P192_MHZ:
  5542. case Q6AFE_LPASS_OSR_CLK_6_P144_MHZ:
  5543. case Q6AFE_LPASS_OSR_CLK_4_P096_MHZ:
  5544. case Q6AFE_LPASS_OSR_CLK_3_P072_MHZ:
  5545. case Q6AFE_LPASS_OSR_CLK_2_P048_MHZ:
  5546. case Q6AFE_LPASS_OSR_CLK_1_P536_MHZ:
  5547. case Q6AFE_LPASS_OSR_CLK_1_P024_MHZ:
  5548. case Q6AFE_LPASS_OSR_CLK_768_kHZ:
  5549. case Q6AFE_LPASS_OSR_CLK_512_kHZ:
  5550. break;
  5551. default:
  5552. pr_err("%s: deafault freq 0x%x\n",
  5553. __func__, freq);
  5554. ret = -EINVAL;
  5555. }
  5556. return ret;
  5557. }
  5558. int afe_get_sp_th_vi_ftm_data(struct afe_sp_th_vi_get_param *th_vi)
  5559. {
  5560. int ret = -EINVAL;
  5561. int index = 0, port = SLIMBUS_4_TX;
  5562. if (!th_vi) {
  5563. pr_err("%s: Invalid params\n", __func__);
  5564. goto done;
  5565. }
  5566. if (this_afe.vi_tx_port != -1)
  5567. port = this_afe.vi_tx_port;
  5568. ret = q6audio_validate_port(port);
  5569. if (ret < 0) {
  5570. pr_err("%s: invalid port 0x%x ret %d\n", __func__, port, ret);
  5571. goto done;
  5572. }
  5573. index = q6audio_get_port_index(port);
  5574. if (index < 0) {
  5575. pr_err("%s: invalid port 0x%x, index %d\n",
  5576. __func__, port, index);
  5577. ret = -EINVAL;
  5578. goto done;
  5579. }
  5580. th_vi->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5581. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5582. th_vi->hdr.pkt_size = sizeof(*th_vi);
  5583. th_vi->hdr.src_port = 0;
  5584. th_vi->hdr.dest_port = 0;
  5585. th_vi->hdr.token = index;
  5586. th_vi->hdr.opcode = AFE_PORT_CMD_GET_PARAM_V2;
  5587. th_vi->get_param.mem_map_handle = 0;
  5588. th_vi->get_param.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_TH_VI;
  5589. th_vi->get_param.param_id = AFE_PARAM_ID_SP_V2_TH_VI_FTM_PARAMS;
  5590. th_vi->get_param.payload_address_lsw = 0;
  5591. th_vi->get_param.payload_address_msw = 0;
  5592. th_vi->get_param.payload_size = sizeof(*th_vi)
  5593. - sizeof(th_vi->get_param) - sizeof(th_vi->hdr);
  5594. th_vi->get_param.port_id = q6audio_get_port_id(port);
  5595. th_vi->pdata.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_TH_VI;
  5596. th_vi->pdata.param_id = AFE_PARAM_ID_SP_V2_TH_VI_FTM_PARAMS;
  5597. th_vi->pdata.param_size = sizeof(th_vi->param);
  5598. atomic_set(&this_afe.status, 0);
  5599. atomic_set(&this_afe.state, 1);
  5600. ret = apr_send_pkt(this_afe.apr, (uint32_t *)th_vi);
  5601. if (ret < 0) {
  5602. pr_err("%s: get param port 0x%x param id[0x%x]failed %d\n",
  5603. __func__, port, th_vi->get_param.param_id, ret);
  5604. goto done;
  5605. }
  5606. ret = wait_event_timeout(this_afe.wait[index],
  5607. (atomic_read(&this_afe.state) == 0),
  5608. msecs_to_jiffies(TIMEOUT_MS));
  5609. if (!ret) {
  5610. pr_err("%s: wait_event timeout\n", __func__);
  5611. ret = -EINVAL;
  5612. goto done;
  5613. }
  5614. if (atomic_read(&this_afe.status) > 0) {
  5615. pr_err("%s: config cmd failed [%s]\n",
  5616. __func__, adsp_err_get_err_str(
  5617. atomic_read(&this_afe.status)));
  5618. ret = adsp_err_get_lnx_err_code(atomic_read(&this_afe.status));
  5619. goto done;
  5620. }
  5621. memcpy(&th_vi->param, &this_afe.th_vi_resp.param,
  5622. sizeof(this_afe.th_vi_resp.param));
  5623. pr_debug("%s: DC resistance %d %d temp %d %d status %d %d\n",
  5624. __func__, th_vi->param.dc_res_q24[SP_V2_SPKR_1],
  5625. th_vi->param.dc_res_q24[SP_V2_SPKR_2],
  5626. th_vi->param.temp_q22[SP_V2_SPKR_1],
  5627. th_vi->param.temp_q22[SP_V2_SPKR_2],
  5628. th_vi->param.status[SP_V2_SPKR_1],
  5629. th_vi->param.status[SP_V2_SPKR_2]);
  5630. ret = 0;
  5631. done:
  5632. return ret;
  5633. }
  5634. int afe_get_sp_ex_vi_ftm_data(struct afe_sp_ex_vi_get_param *ex_vi)
  5635. {
  5636. int ret = -EINVAL;
  5637. int index = 0, port = SLIMBUS_4_TX;
  5638. if (!ex_vi) {
  5639. pr_err("%s: Invalid params\n", __func__);
  5640. goto done;
  5641. }
  5642. if (this_afe.vi_tx_port != -1)
  5643. port = this_afe.vi_tx_port;
  5644. ret = q6audio_validate_port(port);
  5645. if (ret < 0) {
  5646. pr_err("%s: invalid port 0x%x ret %d\n", __func__, port, ret);
  5647. goto done;
  5648. }
  5649. index = q6audio_get_port_index(port);
  5650. if (index < 0) {
  5651. pr_err("%s: invalid index %d port 0x%x\n", __func__,
  5652. index, port);
  5653. ret = -EINVAL;
  5654. goto done;
  5655. }
  5656. ex_vi->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5657. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5658. ex_vi->hdr.pkt_size = sizeof(*ex_vi);
  5659. ex_vi->hdr.src_port = 0;
  5660. ex_vi->hdr.dest_port = 0;
  5661. ex_vi->hdr.token = index;
  5662. ex_vi->hdr.opcode = AFE_PORT_CMD_GET_PARAM_V2;
  5663. ex_vi->get_param.mem_map_handle = 0;
  5664. ex_vi->get_param.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_EX_VI;
  5665. ex_vi->get_param.param_id = AFE_PARAM_ID_SP_V2_EX_VI_FTM_PARAMS;
  5666. ex_vi->get_param.payload_address_lsw = 0;
  5667. ex_vi->get_param.payload_address_msw = 0;
  5668. ex_vi->get_param.payload_size = sizeof(*ex_vi)
  5669. - sizeof(ex_vi->get_param) - sizeof(ex_vi->hdr);
  5670. ex_vi->get_param.port_id = q6audio_get_port_id(port);
  5671. ex_vi->pdata.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_EX_VI;
  5672. ex_vi->pdata.param_id = AFE_PARAM_ID_SP_V2_EX_VI_FTM_PARAMS;
  5673. ex_vi->pdata.param_size = sizeof(ex_vi->param);
  5674. atomic_set(&this_afe.status, 0);
  5675. atomic_set(&this_afe.state, 1);
  5676. ret = apr_send_pkt(this_afe.apr, (uint32_t *)ex_vi);
  5677. if (ret < 0) {
  5678. pr_err("%s: get param port 0x%x param id[0x%x]failed %d\n",
  5679. __func__, port, ex_vi->get_param.param_id, ret);
  5680. goto done;
  5681. }
  5682. ret = wait_event_timeout(this_afe.wait[index],
  5683. (atomic_read(&this_afe.state) == 0),
  5684. msecs_to_jiffies(TIMEOUT_MS));
  5685. if (!ret) {
  5686. pr_err("%s: wait_event timeout\n", __func__);
  5687. ret = -EINVAL;
  5688. goto done;
  5689. }
  5690. if (atomic_read(&this_afe.status) > 0) {
  5691. pr_err("%s: config cmd failed [%s]\n",
  5692. __func__, adsp_err_get_err_str(
  5693. atomic_read(&this_afe.status)));
  5694. ret = adsp_err_get_lnx_err_code(atomic_read(&this_afe.status));
  5695. goto done;
  5696. }
  5697. memcpy(&ex_vi->param, &this_afe.ex_vi_resp.param,
  5698. sizeof(this_afe.ex_vi_resp.param));
  5699. pr_debug("%s: freq %d %d resistance %d %d qfactor %d %d state %d %d\n",
  5700. __func__, ex_vi->param.freq_q20[SP_V2_SPKR_1],
  5701. ex_vi->param.freq_q20[SP_V2_SPKR_2],
  5702. ex_vi->param.resis_q24[SP_V2_SPKR_1],
  5703. ex_vi->param.resis_q24[SP_V2_SPKR_2],
  5704. ex_vi->param.qmct_q24[SP_V2_SPKR_1],
  5705. ex_vi->param.qmct_q24[SP_V2_SPKR_2],
  5706. ex_vi->param.status[SP_V2_SPKR_1],
  5707. ex_vi->param.status[SP_V2_SPKR_2]);
  5708. ret = 0;
  5709. done:
  5710. return ret;
  5711. }
  5712. int afe_get_av_dev_drift(struct afe_param_id_dev_timing_stats *timing_stats,
  5713. u16 port)
  5714. {
  5715. int ret = -EINVAL;
  5716. int index = 0;
  5717. struct afe_av_dev_drift_get_param av_dev_drift;
  5718. if (!timing_stats) {
  5719. pr_err("%s: Invalid params\n", __func__);
  5720. goto exit;
  5721. }
  5722. ret = q6audio_validate_port(port);
  5723. if (ret < 0) {
  5724. pr_err("%s: invalid port 0x%x ret %d\n", __func__, port, ret);
  5725. ret = -EINVAL;
  5726. goto exit;
  5727. }
  5728. index = q6audio_get_port_index(port);
  5729. if (index < 0 || index >= AFE_MAX_PORTS) {
  5730. pr_err("%s: Invalid AFE port index[%d]\n",
  5731. __func__, index);
  5732. ret = -EINVAL;
  5733. goto exit;
  5734. }
  5735. memset(&av_dev_drift, 0, sizeof(struct afe_av_dev_drift_get_param));
  5736. av_dev_drift.hdr.hdr_field =
  5737. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5738. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5739. av_dev_drift.hdr.pkt_size = sizeof(av_dev_drift);
  5740. av_dev_drift.hdr.src_port = 0;
  5741. av_dev_drift.hdr.dest_port = 0;
  5742. av_dev_drift.hdr.token = index;
  5743. av_dev_drift.hdr.opcode = AFE_PORT_CMD_GET_PARAM_V2;
  5744. av_dev_drift.get_param.mem_map_handle = 0;
  5745. av_dev_drift.get_param.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5746. av_dev_drift.get_param.param_id = AFE_PARAM_ID_DEV_TIMING_STATS;
  5747. av_dev_drift.get_param.payload_address_lsw = 0;
  5748. av_dev_drift.get_param.payload_address_msw = 0;
  5749. av_dev_drift.get_param.payload_size = sizeof(av_dev_drift)
  5750. - sizeof(av_dev_drift.get_param) - sizeof(av_dev_drift.hdr);
  5751. av_dev_drift.get_param.port_id = q6audio_get_port_id(port);
  5752. av_dev_drift.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5753. av_dev_drift.pdata.param_id = AFE_PARAM_ID_DEV_TIMING_STATS;
  5754. av_dev_drift.pdata.param_size = sizeof(av_dev_drift.timing_stats);
  5755. atomic_set(&this_afe.status, 0);
  5756. atomic_set(&this_afe.state, 1);
  5757. ret = apr_send_pkt(this_afe.apr, (uint32_t *)&av_dev_drift);
  5758. if (ret < 0) {
  5759. pr_err("%s: get param port 0x%x param id[0x%x] failed %d\n",
  5760. __func__, port, av_dev_drift.get_param.param_id, ret);
  5761. goto exit;
  5762. }
  5763. ret = wait_event_timeout(this_afe.wait[index],
  5764. (atomic_read(&this_afe.state) == 0),
  5765. msecs_to_jiffies(TIMEOUT_MS));
  5766. if (!ret) {
  5767. pr_err("%s: wait_event timeout\n", __func__);
  5768. ret = -EINVAL;
  5769. goto exit;
  5770. }
  5771. if (atomic_read(&this_afe.status) > 0) {
  5772. pr_err("%s: config cmd failed [%s]\n",
  5773. __func__, adsp_err_get_err_str(
  5774. atomic_read(&this_afe.status)));
  5775. ret = adsp_err_get_lnx_err_code(
  5776. atomic_read(&this_afe.status));
  5777. goto exit;
  5778. }
  5779. memcpy(timing_stats, &this_afe.av_dev_drift_resp.timing_stats,
  5780. sizeof(this_afe.av_dev_drift_resp.timing_stats));
  5781. ret = 0;
  5782. exit:
  5783. return ret;
  5784. }
  5785. int afe_spk_prot_get_calib_data(struct afe_spkr_prot_get_vi_calib *calib_resp)
  5786. {
  5787. int ret = -EINVAL;
  5788. int index = 0, port = SLIMBUS_4_TX;
  5789. if (!calib_resp) {
  5790. pr_err("%s: Invalid params\n", __func__);
  5791. goto fail_cmd;
  5792. }
  5793. if (this_afe.vi_tx_port != -1)
  5794. port = this_afe.vi_tx_port;
  5795. ret = q6audio_validate_port(port);
  5796. if (ret < 0) {
  5797. pr_err("%s: invalid port 0x%x ret %d\n", __func__, port, ret);
  5798. ret = -EINVAL;
  5799. goto fail_cmd;
  5800. }
  5801. index = q6audio_get_port_index(port);
  5802. if (index < 0 || index >= AFE_MAX_PORTS) {
  5803. pr_err("%s: AFE port index[%d] invalid!\n",
  5804. __func__, index);
  5805. ret = -EINVAL;
  5806. goto fail_cmd;
  5807. }
  5808. calib_resp->hdr.hdr_field =
  5809. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5810. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5811. calib_resp->hdr.pkt_size = sizeof(*calib_resp);
  5812. calib_resp->hdr.src_port = 0;
  5813. calib_resp->hdr.dest_port = 0;
  5814. calib_resp->hdr.token = index;
  5815. calib_resp->hdr.opcode = AFE_PORT_CMD_GET_PARAM_V2;
  5816. calib_resp->get_param.mem_map_handle = 0;
  5817. calib_resp->get_param.module_id = AFE_MODULE_FB_SPKR_PROT_VI_PROC_V2;
  5818. calib_resp->get_param.param_id = AFE_PARAM_ID_CALIB_RES_CFG_V2;
  5819. calib_resp->get_param.payload_address_lsw = 0;
  5820. calib_resp->get_param.payload_address_msw = 0;
  5821. calib_resp->get_param.payload_size = sizeof(*calib_resp)
  5822. - sizeof(calib_resp->get_param) - sizeof(calib_resp->hdr);
  5823. calib_resp->get_param.port_id = q6audio_get_port_id(port);
  5824. calib_resp->pdata.module_id = AFE_MODULE_FB_SPKR_PROT_VI_PROC_V2;
  5825. calib_resp->pdata.param_id = AFE_PARAM_ID_CALIB_RES_CFG_V2;
  5826. calib_resp->pdata.param_size = sizeof(calib_resp->res_cfg);
  5827. atomic_set(&this_afe.status, 0);
  5828. atomic_set(&this_afe.state, 1);
  5829. ret = apr_send_pkt(this_afe.apr, (uint32_t *)calib_resp);
  5830. if (ret < 0) {
  5831. pr_err("%s: get param port 0x%x param id[0x%x]failed %d\n",
  5832. __func__, port, calib_resp->get_param.param_id, ret);
  5833. goto fail_cmd;
  5834. }
  5835. ret = wait_event_timeout(this_afe.wait[index],
  5836. (atomic_read(&this_afe.state) == 0),
  5837. msecs_to_jiffies(TIMEOUT_MS));
  5838. if (!ret) {
  5839. pr_err("%s: wait_event timeout\n", __func__);
  5840. ret = -EINVAL;
  5841. goto fail_cmd;
  5842. }
  5843. if (atomic_read(&this_afe.status) > 0) {
  5844. pr_err("%s: config cmd failed [%s]\n",
  5845. __func__, adsp_err_get_err_str(
  5846. atomic_read(&this_afe.status)));
  5847. ret = adsp_err_get_lnx_err_code(
  5848. atomic_read(&this_afe.status));
  5849. goto fail_cmd;
  5850. }
  5851. memcpy(&calib_resp->res_cfg, &this_afe.calib_data.res_cfg,
  5852. sizeof(this_afe.calib_data.res_cfg));
  5853. pr_info("%s: state %s resistance %d %d\n", __func__,
  5854. fbsp_state[calib_resp->res_cfg.th_vi_ca_state],
  5855. calib_resp->res_cfg.r0_cali_q24[SP_V2_SPKR_1],
  5856. calib_resp->res_cfg.r0_cali_q24[SP_V2_SPKR_2]);
  5857. ret = 0;
  5858. fail_cmd:
  5859. return ret;
  5860. }
  5861. int afe_spk_prot_feed_back_cfg(int src_port, int dst_port,
  5862. int l_ch, int r_ch, u32 enable)
  5863. {
  5864. int ret = -EINVAL;
  5865. union afe_spkr_prot_config prot_config;
  5866. int index = 0;
  5867. if (!enable) {
  5868. pr_debug("%s: Disable Feedback tx path", __func__);
  5869. this_afe.vi_tx_port = -1;
  5870. this_afe.vi_rx_port = -1;
  5871. return 0;
  5872. }
  5873. if ((q6audio_validate_port(src_port) < 0) ||
  5874. (q6audio_validate_port(dst_port) < 0)) {
  5875. pr_err("%s: invalid ports src 0x%x dst 0x%x",
  5876. __func__, src_port, dst_port);
  5877. goto fail_cmd;
  5878. }
  5879. if (!l_ch && !r_ch) {
  5880. pr_err("%s: error ch values zero\n", __func__);
  5881. goto fail_cmd;
  5882. }
  5883. pr_debug("%s: src_port 0x%x dst_port 0x%x l_ch %d r_ch %d\n",
  5884. __func__, src_port, dst_port, l_ch, r_ch);
  5885. memset(&prot_config, 0, sizeof(prot_config));
  5886. prot_config.feedback_path_cfg.dst_portid =
  5887. q6audio_get_port_id(dst_port);
  5888. if (l_ch) {
  5889. prot_config.feedback_path_cfg.chan_info[index++] = 1;
  5890. prot_config.feedback_path_cfg.chan_info[index++] = 2;
  5891. }
  5892. if (r_ch) {
  5893. prot_config.feedback_path_cfg.chan_info[index++] = 3;
  5894. prot_config.feedback_path_cfg.chan_info[index++] = 4;
  5895. }
  5896. prot_config.feedback_path_cfg.num_channels = index;
  5897. pr_debug("%s no of channels: %d\n", __func__, index);
  5898. prot_config.feedback_path_cfg.minor_version = 1;
  5899. ret = afe_spk_prot_prepare(src_port, dst_port,
  5900. AFE_PARAM_ID_FEEDBACK_PATH_CFG, &prot_config);
  5901. fail_cmd:
  5902. return ret;
  5903. }
  5904. static int get_cal_type_index(int32_t cal_type)
  5905. {
  5906. int ret = -EINVAL;
  5907. switch (cal_type) {
  5908. case AFE_COMMON_RX_CAL_TYPE:
  5909. ret = AFE_COMMON_RX_CAL;
  5910. break;
  5911. case AFE_COMMON_TX_CAL_TYPE:
  5912. ret = AFE_COMMON_TX_CAL;
  5913. break;
  5914. case AFE_AANC_CAL_TYPE:
  5915. ret = AFE_AANC_CAL;
  5916. break;
  5917. case AFE_HW_DELAY_CAL_TYPE:
  5918. ret = AFE_HW_DELAY_CAL;
  5919. break;
  5920. case AFE_FB_SPKR_PROT_CAL_TYPE:
  5921. ret = AFE_FB_SPKR_PROT_CAL;
  5922. break;
  5923. case AFE_SIDETONE_CAL_TYPE:
  5924. ret = AFE_SIDETONE_CAL;
  5925. break;
  5926. case AFE_SIDETONE_IIR_CAL_TYPE:
  5927. ret = AFE_SIDETONE_IIR_CAL;
  5928. break;
  5929. case AFE_TOPOLOGY_CAL_TYPE:
  5930. ret = AFE_TOPOLOGY_CAL;
  5931. break;
  5932. case AFE_CUST_TOPOLOGY_CAL_TYPE:
  5933. ret = AFE_CUST_TOPOLOGY_CAL;
  5934. break;
  5935. default:
  5936. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  5937. }
  5938. return ret;
  5939. }
  5940. int afe_alloc_cal(int32_t cal_type, size_t data_size,
  5941. void *data)
  5942. {
  5943. int ret = 0;
  5944. int cal_index;
  5945. cal_index = get_cal_type_index(cal_type);
  5946. pr_debug("%s: cal_type = %d cal_index = %d\n",
  5947. __func__, cal_type, cal_index);
  5948. if (cal_index < 0) {
  5949. pr_err("%s: could not get cal index %d!\n",
  5950. __func__, cal_index);
  5951. ret = -EINVAL;
  5952. goto done;
  5953. }
  5954. ret = cal_utils_alloc_cal(data_size, data,
  5955. this_afe.cal_data[cal_index], 0, NULL);
  5956. if (ret < 0) {
  5957. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  5958. __func__, ret, cal_type);
  5959. ret = -EINVAL;
  5960. goto done;
  5961. }
  5962. done:
  5963. return ret;
  5964. }
  5965. static int afe_dealloc_cal(int32_t cal_type, size_t data_size,
  5966. void *data)
  5967. {
  5968. int ret = 0;
  5969. int cal_index;
  5970. pr_debug("%s:\n", __func__);
  5971. cal_index = get_cal_type_index(cal_type);
  5972. if (cal_index < 0) {
  5973. pr_err("%s: could not get cal index %d!\n",
  5974. __func__, cal_index);
  5975. ret = -EINVAL;
  5976. goto done;
  5977. }
  5978. ret = cal_utils_dealloc_cal(data_size, data,
  5979. this_afe.cal_data[cal_index]);
  5980. if (ret < 0) {
  5981. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  5982. __func__, ret, cal_type);
  5983. ret = -EINVAL;
  5984. goto done;
  5985. }
  5986. done:
  5987. return ret;
  5988. }
  5989. static int afe_set_cal(int32_t cal_type, size_t data_size,
  5990. void *data)
  5991. {
  5992. int ret = 0;
  5993. int cal_index;
  5994. pr_debug("%s:\n", __func__);
  5995. cal_index = get_cal_type_index(cal_type);
  5996. if (cal_index < 0) {
  5997. pr_err("%s: could not get cal index %d!\n",
  5998. __func__, cal_index);
  5999. ret = -EINVAL;
  6000. goto done;
  6001. }
  6002. ret = cal_utils_set_cal(data_size, data,
  6003. this_afe.cal_data[cal_index], 0, NULL);
  6004. if (ret < 0) {
  6005. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  6006. __func__, ret, cal_type);
  6007. ret = -EINVAL;
  6008. goto done;
  6009. }
  6010. if (cal_index == AFE_CUST_TOPOLOGY_CAL) {
  6011. mutex_lock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  6012. this_afe.set_custom_topology = 1;
  6013. pr_debug("%s:[AFE_CUSTOM_TOPOLOGY] ret = %d, cal type = %d!\n",
  6014. __func__, ret, cal_type);
  6015. mutex_unlock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  6016. }
  6017. done:
  6018. return ret;
  6019. }
  6020. static struct cal_block_data *afe_find_hw_delay_by_path(
  6021. struct cal_type_data *cal_type, int path)
  6022. {
  6023. struct list_head *ptr, *next;
  6024. struct cal_block_data *cal_block = NULL;
  6025. pr_debug("%s:\n", __func__);
  6026. list_for_each_safe(ptr, next,
  6027. &cal_type->cal_blocks) {
  6028. cal_block = list_entry(ptr,
  6029. struct cal_block_data, list);
  6030. if (((struct audio_cal_info_hw_delay *)cal_block->cal_info)
  6031. ->path == path) {
  6032. return cal_block;
  6033. }
  6034. }
  6035. return NULL;
  6036. }
  6037. static int afe_get_cal_hw_delay(int32_t path,
  6038. struct audio_cal_hw_delay_entry *entry)
  6039. {
  6040. int ret = 0;
  6041. int i;
  6042. struct cal_block_data *cal_block = NULL;
  6043. struct audio_cal_hw_delay_data *hw_delay_info = NULL;
  6044. pr_debug("%s:\n", __func__);
  6045. if (this_afe.cal_data[AFE_HW_DELAY_CAL] == NULL) {
  6046. pr_err("%s: AFE_HW_DELAY_CAL not initialized\n", __func__);
  6047. ret = -EINVAL;
  6048. goto done;
  6049. }
  6050. if (entry == NULL) {
  6051. pr_err("%s: entry is NULL\n", __func__);
  6052. ret = -EINVAL;
  6053. goto done;
  6054. }
  6055. if ((path >= MAX_PATH_TYPE) || (path < 0)) {
  6056. pr_err("%s: bad path: %d\n",
  6057. __func__, path);
  6058. ret = -EINVAL;
  6059. goto done;
  6060. }
  6061. mutex_lock(&this_afe.cal_data[AFE_HW_DELAY_CAL]->lock);
  6062. cal_block = afe_find_hw_delay_by_path(
  6063. this_afe.cal_data[AFE_HW_DELAY_CAL], path);
  6064. if (cal_block == NULL)
  6065. goto unlock;
  6066. hw_delay_info = &((struct audio_cal_info_hw_delay *)
  6067. cal_block->cal_info)->data;
  6068. if (hw_delay_info->num_entries > MAX_HW_DELAY_ENTRIES) {
  6069. pr_err("%s: invalid num entries: %d\n",
  6070. __func__, hw_delay_info->num_entries);
  6071. ret = -EINVAL;
  6072. goto unlock;
  6073. }
  6074. for (i = 0; i < hw_delay_info->num_entries; i++) {
  6075. if (hw_delay_info->entry[i].sample_rate ==
  6076. entry->sample_rate) {
  6077. entry->delay_usec = hw_delay_info->entry[i].delay_usec;
  6078. break;
  6079. }
  6080. }
  6081. if (i == hw_delay_info->num_entries) {
  6082. pr_err("%s: Unable to find delay for sample rate %d\n",
  6083. __func__, entry->sample_rate);
  6084. ret = -EFAULT;
  6085. goto unlock;
  6086. }
  6087. pr_debug("%s: Path = %d samplerate = %u usec = %u status %d\n",
  6088. __func__, path, entry->sample_rate, entry->delay_usec, ret);
  6089. unlock:
  6090. mutex_unlock(&this_afe.cal_data[AFE_HW_DELAY_CAL]->lock);
  6091. done:
  6092. return ret;
  6093. }
  6094. static int afe_set_cal_sp_th_vi_ftm_cfg(int32_t cal_type, size_t data_size,
  6095. void *data)
  6096. {
  6097. int ret = 0;
  6098. struct audio_cal_type_sp_th_vi_ftm_cfg *cal_data = data;
  6099. if (this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL ||
  6100. cal_data == NULL ||
  6101. data_size != sizeof(*cal_data))
  6102. goto done;
  6103. pr_debug("%s: cal_type = %d\n", __func__, cal_type);
  6104. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  6105. memcpy(&this_afe.th_ftm_cfg, &cal_data->cal_info,
  6106. sizeof(this_afe.th_ftm_cfg));
  6107. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  6108. done:
  6109. return ret;
  6110. }
  6111. static int afe_set_cal_sp_ex_vi_ftm_cfg(int32_t cal_type, size_t data_size,
  6112. void *data)
  6113. {
  6114. int ret = 0;
  6115. struct audio_cal_type_sp_ex_vi_ftm_cfg *cal_data = data;
  6116. if (this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL] == NULL ||
  6117. cal_data == NULL ||
  6118. data_size != sizeof(*cal_data))
  6119. goto done;
  6120. pr_debug("%s: cal_type = %d\n", __func__, cal_type);
  6121. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  6122. memcpy(&this_afe.ex_ftm_cfg, &cal_data->cal_info,
  6123. sizeof(this_afe.ex_ftm_cfg));
  6124. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  6125. done:
  6126. return ret;
  6127. }
  6128. static int afe_set_cal_fb_spkr_prot(int32_t cal_type, size_t data_size,
  6129. void *data)
  6130. {
  6131. int ret = 0;
  6132. struct audio_cal_type_fb_spk_prot_cfg *cal_data = data;
  6133. pr_debug("%s:\n", __func__);
  6134. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL)
  6135. goto done;
  6136. if (cal_data == NULL)
  6137. goto done;
  6138. if (data_size != sizeof(*cal_data))
  6139. goto done;
  6140. if (cal_data->cal_info.mode == MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS)
  6141. __pm_wakeup_event(&wl.ws, jiffies_to_msecs(WAKELOCK_TIMEOUT));
  6142. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  6143. memcpy(&this_afe.prot_cfg, &cal_data->cal_info,
  6144. sizeof(this_afe.prot_cfg));
  6145. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  6146. done:
  6147. return ret;
  6148. }
  6149. static int afe_get_cal_sp_th_vi_ftm_param(int32_t cal_type, size_t data_size,
  6150. void *data)
  6151. {
  6152. int i, ret = 0;
  6153. struct audio_cal_type_sp_th_vi_param *cal_data = data;
  6154. struct afe_sp_th_vi_get_param th_vi;
  6155. pr_debug("%s: cal_type = %d\n", __func__, cal_type);
  6156. if (this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL ||
  6157. cal_data == NULL ||
  6158. data_size != sizeof(*cal_data))
  6159. goto done;
  6160. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  6161. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  6162. cal_data->cal_info.status[i] = -EINVAL;
  6163. cal_data->cal_info.r_dc_q24[i] = -1;
  6164. cal_data->cal_info.temp_q22[i] = -1;
  6165. }
  6166. if (!afe_get_sp_th_vi_ftm_data(&th_vi)) {
  6167. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  6168. pr_debug("%s: ftm param status = %d\n",
  6169. __func__, th_vi.param.status[i]);
  6170. if (th_vi.param.status[i] == FBSP_IN_PROGRESS) {
  6171. cal_data->cal_info.status[i] = -EAGAIN;
  6172. } else if (th_vi.param.status[i] == FBSP_SUCCESS) {
  6173. cal_data->cal_info.status[i] = 0;
  6174. cal_data->cal_info.r_dc_q24[i] =
  6175. th_vi.param.dc_res_q24[i];
  6176. cal_data->cal_info.temp_q22[i] =
  6177. th_vi.param.temp_q22[i];
  6178. }
  6179. }
  6180. }
  6181. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  6182. done:
  6183. return ret;
  6184. }
  6185. static int afe_get_cal_sp_ex_vi_ftm_param(int32_t cal_type, size_t data_size,
  6186. void *data)
  6187. {
  6188. int i, ret = 0;
  6189. struct audio_cal_type_sp_ex_vi_param *cal_data = data;
  6190. struct afe_sp_ex_vi_get_param ex_vi;
  6191. pr_debug("%s: cal_type = %d\n", __func__, cal_type);
  6192. if (this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL] == NULL ||
  6193. cal_data == NULL ||
  6194. data_size != sizeof(*cal_data))
  6195. goto done;
  6196. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  6197. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  6198. cal_data->cal_info.status[i] = -EINVAL;
  6199. cal_data->cal_info.freq_q20[i] = -1;
  6200. cal_data->cal_info.resis_q24[i] = -1;
  6201. cal_data->cal_info.qmct_q24[i] = -1;
  6202. }
  6203. if (!afe_get_sp_ex_vi_ftm_data(&ex_vi)) {
  6204. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  6205. pr_debug("%s: ftm param status = %d\n",
  6206. __func__, ex_vi.param.status[i]);
  6207. if (ex_vi.param.status[i] == FBSP_IN_PROGRESS) {
  6208. cal_data->cal_info.status[i] = -EAGAIN;
  6209. } else if (ex_vi.param.status[i] == FBSP_SUCCESS) {
  6210. cal_data->cal_info.status[i] = 0;
  6211. cal_data->cal_info.freq_q20[i] =
  6212. ex_vi.param.freq_q20[i];
  6213. cal_data->cal_info.resis_q24[i] =
  6214. ex_vi.param.resis_q24[i];
  6215. cal_data->cal_info.qmct_q24[i] =
  6216. ex_vi.param.qmct_q24[i];
  6217. }
  6218. }
  6219. }
  6220. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  6221. done:
  6222. return ret;
  6223. }
  6224. static int afe_get_cal_fb_spkr_prot(int32_t cal_type, size_t data_size,
  6225. void *data)
  6226. {
  6227. int ret = 0;
  6228. struct audio_cal_type_fb_spk_prot_status *cal_data = data;
  6229. struct afe_spkr_prot_get_vi_calib calib_resp;
  6230. pr_debug("%s:\n", __func__);
  6231. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL)
  6232. goto done;
  6233. if (cal_data == NULL)
  6234. goto done;
  6235. if (data_size != sizeof(*cal_data))
  6236. goto done;
  6237. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  6238. if (this_afe.prot_cfg.mode == MSM_SPKR_PROT_CALIBRATED) {
  6239. cal_data->cal_info.r0[SP_V2_SPKR_1] =
  6240. this_afe.prot_cfg.r0[SP_V2_SPKR_1];
  6241. cal_data->cal_info.r0[SP_V2_SPKR_2] =
  6242. this_afe.prot_cfg.r0[SP_V2_SPKR_2];
  6243. cal_data->cal_info.status = 0;
  6244. } else if (this_afe.prot_cfg.mode ==
  6245. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS) {
  6246. /*Call AFE to query the status*/
  6247. cal_data->cal_info.status = -EINVAL;
  6248. cal_data->cal_info.r0[SP_V2_SPKR_1] = -1;
  6249. cal_data->cal_info.r0[SP_V2_SPKR_2] = -1;
  6250. if (!afe_spk_prot_get_calib_data(&calib_resp)) {
  6251. if (calib_resp.res_cfg.th_vi_ca_state ==
  6252. FBSP_IN_PROGRESS)
  6253. cal_data->cal_info.status = -EAGAIN;
  6254. else if (calib_resp.res_cfg.th_vi_ca_state ==
  6255. FBSP_SUCCESS) {
  6256. cal_data->cal_info.status = 0;
  6257. cal_data->cal_info.r0[SP_V2_SPKR_1] =
  6258. calib_resp.res_cfg.r0_cali_q24[SP_V2_SPKR_1];
  6259. cal_data->cal_info.r0[SP_V2_SPKR_2] =
  6260. calib_resp.res_cfg.r0_cali_q24[SP_V2_SPKR_2];
  6261. }
  6262. }
  6263. if (!cal_data->cal_info.status) {
  6264. this_afe.prot_cfg.mode =
  6265. MSM_SPKR_PROT_CALIBRATED;
  6266. this_afe.prot_cfg.r0[SP_V2_SPKR_1] =
  6267. cal_data->cal_info.r0[SP_V2_SPKR_1];
  6268. this_afe.prot_cfg.r0[SP_V2_SPKR_2] =
  6269. cal_data->cal_info.r0[SP_V2_SPKR_2];
  6270. }
  6271. } else {
  6272. /*Indicates calibration data is invalid*/
  6273. cal_data->cal_info.status = -EINVAL;
  6274. cal_data->cal_info.r0[SP_V2_SPKR_1] = -1;
  6275. cal_data->cal_info.r0[SP_V2_SPKR_2] = -1;
  6276. }
  6277. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  6278. __pm_relax(&wl.ws);
  6279. done:
  6280. return ret;
  6281. }
  6282. static int afe_map_cal_data(int32_t cal_type,
  6283. struct cal_block_data *cal_block)
  6284. {
  6285. int ret = 0;
  6286. int cal_index;
  6287. pr_debug("%s:\n", __func__);
  6288. cal_index = get_cal_type_index(cal_type);
  6289. if (cal_index < 0) {
  6290. pr_err("%s: could not get cal index %d!\n",
  6291. __func__, cal_index);
  6292. ret = -EINVAL;
  6293. goto done;
  6294. }
  6295. mutex_lock(&this_afe.afe_cmd_lock);
  6296. atomic_set(&this_afe.mem_map_cal_index, cal_index);
  6297. ret = afe_cmd_memory_map(cal_block->cal_data.paddr,
  6298. cal_block->map_data.map_size);
  6299. atomic_set(&this_afe.mem_map_cal_index, -1);
  6300. if (ret < 0) {
  6301. pr_err("%s: mmap did not work! size = %zd ret %d\n",
  6302. __func__,
  6303. cal_block->map_data.map_size, ret);
  6304. pr_debug("%s: mmap did not work! addr = 0x%pK, size = %zd\n",
  6305. __func__,
  6306. &cal_block->cal_data.paddr,
  6307. cal_block->map_data.map_size);
  6308. mutex_unlock(&this_afe.afe_cmd_lock);
  6309. goto done;
  6310. }
  6311. cal_block->map_data.q6map_handle = atomic_read(&this_afe.
  6312. mem_map_cal_handles[cal_index]);
  6313. mutex_unlock(&this_afe.afe_cmd_lock);
  6314. done:
  6315. return ret;
  6316. }
  6317. static int afe_unmap_cal_data(int32_t cal_type,
  6318. struct cal_block_data *cal_block)
  6319. {
  6320. int ret = 0;
  6321. int cal_index;
  6322. pr_debug("%s:\n", __func__);
  6323. cal_index = get_cal_type_index(cal_type);
  6324. if (cal_index < 0) {
  6325. pr_err("%s: could not get cal index %d!\n",
  6326. __func__, cal_index);
  6327. ret = -EINVAL;
  6328. goto done;
  6329. }
  6330. if (cal_block == NULL) {
  6331. pr_err("%s: Cal block is NULL!\n",
  6332. __func__);
  6333. goto done;
  6334. }
  6335. if (cal_block->map_data.q6map_handle == 0) {
  6336. pr_err("%s: Map handle is NULL, nothing to unmap\n",
  6337. __func__);
  6338. goto done;
  6339. }
  6340. atomic_set(&this_afe.mem_map_cal_handles[cal_index],
  6341. cal_block->map_data.q6map_handle);
  6342. atomic_set(&this_afe.mem_map_cal_index, cal_index);
  6343. ret = afe_cmd_memory_unmap_nowait(
  6344. cal_block->map_data.q6map_handle);
  6345. atomic_set(&this_afe.mem_map_cal_index, -1);
  6346. if (ret < 0) {
  6347. pr_err("%s: unmap did not work! cal_type %i ret %d\n",
  6348. __func__, cal_index, ret);
  6349. }
  6350. cal_block->map_data.q6map_handle = 0;
  6351. done:
  6352. return ret;
  6353. }
  6354. static void afe_delete_cal_data(void)
  6355. {
  6356. pr_debug("%s:\n", __func__);
  6357. cal_utils_destroy_cal_types(MAX_AFE_CAL_TYPES, this_afe.cal_data);
  6358. }
  6359. static int afe_init_cal_data(void)
  6360. {
  6361. int ret = 0;
  6362. struct cal_type_info cal_type_info[] = {
  6363. {{AFE_COMMON_RX_CAL_TYPE,
  6364. {afe_alloc_cal, afe_dealloc_cal, NULL,
  6365. afe_set_cal, NULL, NULL} },
  6366. {afe_map_cal_data, afe_unmap_cal_data,
  6367. cal_utils_match_buf_num} },
  6368. {{AFE_COMMON_TX_CAL_TYPE,
  6369. {afe_alloc_cal, afe_dealloc_cal, NULL,
  6370. afe_set_cal, NULL, NULL} },
  6371. {afe_map_cal_data, afe_unmap_cal_data,
  6372. cal_utils_match_buf_num} },
  6373. {{AFE_AANC_CAL_TYPE,
  6374. {afe_alloc_cal, afe_dealloc_cal, NULL,
  6375. afe_set_cal, NULL, NULL} },
  6376. {afe_map_cal_data, afe_unmap_cal_data,
  6377. cal_utils_match_buf_num} },
  6378. {{AFE_FB_SPKR_PROT_CAL_TYPE,
  6379. {NULL, NULL, NULL, afe_set_cal_fb_spkr_prot,
  6380. afe_get_cal_fb_spkr_prot, NULL} },
  6381. {NULL, NULL, cal_utils_match_buf_num} },
  6382. {{AFE_HW_DELAY_CAL_TYPE,
  6383. {NULL, NULL, NULL,
  6384. afe_set_cal, NULL, NULL} },
  6385. {NULL, NULL, cal_utils_match_buf_num} },
  6386. {{AFE_SIDETONE_CAL_TYPE,
  6387. {NULL, NULL, NULL,
  6388. afe_set_cal, NULL, NULL} },
  6389. {NULL, NULL, cal_utils_match_buf_num} },
  6390. {{AFE_SIDETONE_IIR_CAL_TYPE,
  6391. {NULL, NULL, NULL,
  6392. afe_set_cal, NULL, NULL} },
  6393. {NULL, NULL, cal_utils_match_buf_num} },
  6394. {{AFE_TOPOLOGY_CAL_TYPE,
  6395. {NULL, NULL, NULL,
  6396. afe_set_cal, NULL, NULL} },
  6397. {NULL, NULL,
  6398. cal_utils_match_buf_num} },
  6399. {{AFE_CUST_TOPOLOGY_CAL_TYPE,
  6400. {afe_alloc_cal, afe_dealloc_cal, NULL,
  6401. afe_set_cal, NULL, NULL} },
  6402. {afe_map_cal_data, afe_unmap_cal_data,
  6403. cal_utils_match_buf_num} },
  6404. {{AFE_FB_SPKR_PROT_TH_VI_CAL_TYPE,
  6405. {NULL, NULL, NULL, afe_set_cal_sp_th_vi_ftm_cfg,
  6406. afe_get_cal_sp_th_vi_ftm_param, NULL} },
  6407. {NULL, NULL, cal_utils_match_buf_num} },
  6408. {{AFE_FB_SPKR_PROT_EX_VI_CAL_TYPE,
  6409. {NULL, NULL, NULL, afe_set_cal_sp_ex_vi_ftm_cfg,
  6410. afe_get_cal_sp_ex_vi_ftm_param, NULL} },
  6411. {NULL, NULL, cal_utils_match_buf_num} },
  6412. };
  6413. pr_debug("%s:\n", __func__);
  6414. ret = cal_utils_create_cal_types(MAX_AFE_CAL_TYPES, this_afe.cal_data,
  6415. cal_type_info);
  6416. if (ret < 0) {
  6417. pr_err("%s: could not create cal type! %d\n",
  6418. __func__, ret);
  6419. ret = -EINVAL;
  6420. goto err;
  6421. }
  6422. return ret;
  6423. err:
  6424. afe_delete_cal_data();
  6425. return ret;
  6426. }
  6427. int afe_map_rtac_block(struct rtac_cal_block_data *cal_block)
  6428. {
  6429. int result = 0;
  6430. pr_debug("%s:\n", __func__);
  6431. if (cal_block == NULL) {
  6432. pr_err("%s: cal_block is NULL!\n",
  6433. __func__);
  6434. result = -EINVAL;
  6435. goto done;
  6436. }
  6437. if (cal_block->cal_data.paddr == 0) {
  6438. pr_debug("%s: No address to map!\n",
  6439. __func__);
  6440. result = -EINVAL;
  6441. goto done;
  6442. }
  6443. if (cal_block->map_data.map_size == 0) {
  6444. pr_debug("%s: map size is 0!\n",
  6445. __func__);
  6446. result = -EINVAL;
  6447. goto done;
  6448. }
  6449. result = afe_cmd_memory_map(cal_block->cal_data.paddr,
  6450. cal_block->map_data.map_size);
  6451. if (result < 0) {
  6452. pr_err("%s: afe_cmd_memory_map failed for addr = 0x%pK, size = %d, err %d\n",
  6453. __func__, &cal_block->cal_data.paddr,
  6454. cal_block->map_data.map_size, result);
  6455. return result;
  6456. }
  6457. cal_block->map_data.map_handle = this_afe.mmap_handle;
  6458. done:
  6459. return result;
  6460. }
  6461. int afe_unmap_rtac_block(uint32_t *mem_map_handle)
  6462. {
  6463. int result = 0;
  6464. pr_debug("%s:\n", __func__);
  6465. if (mem_map_handle == NULL) {
  6466. pr_err("%s: Map handle is NULL, nothing to unmap\n",
  6467. __func__);
  6468. goto done;
  6469. }
  6470. if (*mem_map_handle == 0) {
  6471. pr_debug("%s: Map handle is 0, nothing to unmap\n",
  6472. __func__);
  6473. goto done;
  6474. }
  6475. result = afe_cmd_memory_unmap(*mem_map_handle);
  6476. if (result) {
  6477. pr_err("%s: AFE memory unmap failed %d, handle 0x%x\n",
  6478. __func__, result, *mem_map_handle);
  6479. goto done;
  6480. } else {
  6481. *mem_map_handle = 0;
  6482. }
  6483. done:
  6484. return result;
  6485. }
  6486. static int __init afe_init(void)
  6487. {
  6488. int i = 0, ret;
  6489. atomic_set(&this_afe.state, 0);
  6490. atomic_set(&this_afe.status, 0);
  6491. atomic_set(&this_afe.mem_map_cal_index, -1);
  6492. this_afe.apr = NULL;
  6493. this_afe.dtmf_gen_rx_portid = -1;
  6494. this_afe.mmap_handle = 0;
  6495. this_afe.vi_tx_port = -1;
  6496. this_afe.vi_rx_port = -1;
  6497. this_afe.prot_cfg.mode = MSM_SPKR_PROT_DISABLED;
  6498. this_afe.th_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  6499. this_afe.ex_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  6500. mutex_init(&this_afe.afe_cmd_lock);
  6501. for (i = 0; i < AFE_MAX_PORTS; i++) {
  6502. this_afe.afe_cal_mode[i] = AFE_CAL_MODE_DEFAULT;
  6503. this_afe.afe_sample_rates[i] = 0;
  6504. this_afe.dev_acdb_id[i] = 0;
  6505. init_waitqueue_head(&this_afe.wait[i]);
  6506. }
  6507. wakeup_source_init(&wl.ws, "spkr-prot");
  6508. ret = afe_init_cal_data();
  6509. if (ret)
  6510. pr_err("%s: could not init cal data! %d\n", __func__, ret);
  6511. config_debug_fs_init();
  6512. return 0;
  6513. }
  6514. static void __exit afe_exit(void)
  6515. {
  6516. afe_delete_cal_data();
  6517. config_debug_fs_exit();
  6518. mutex_destroy(&this_afe.afe_cmd_lock);
  6519. wakeup_source_trash(&wl.ws);
  6520. }
  6521. device_initcall(afe_init);
  6522. __exitcall(afe_exit);