q6afe.c 203 KB

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