q6afe.c 314 KB

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