q6afe.c 299 KB

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