q6afe.c 271 KB

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