q6afe.c 234 KB

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