q6afe.c 310 KB

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