q6afe.c 264 KB

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