q6afe.c 225 KB

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