q6afe.c 235 KB

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