q6afe.c 264 KB

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