q6afe.c 265 KB

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