q6afe.c 261 KB

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