q6afe.c 216 KB

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