q6afe.c 326 KB

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