q6afe.c 260 KB

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