q6afe.c 206 KB

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