q6afe.c 303 KB

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