q6afe.c 230 KB

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