q6afe.c 225 KB

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