q6afe.c 260 KB

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