q6afe.c 214 KB

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