q6afe.c 255 KB

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