q6afe.c 231 KB

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