q6afe.c 230 KB

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