q6afe.c 261 KB

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