q6afe.c 214 KB

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