msm-compress-q6-v2.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/init.h>
  5. #include <linux/err.h>
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/time.h>
  9. #include <linux/math64.h>
  10. #include <linux/wait.h>
  11. #include <linux/mutex.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <sound/core.h>
  15. #include <sound/soc.h>
  16. #include <sound/soc-dapm.h>
  17. #include <sound/pcm.h>
  18. #include <sound/initval.h>
  19. #include <sound/control.h>
  20. #include <sound/pcm_params.h>
  21. #include <audio/sound/audio_effects.h>
  22. #include <audio/sound/audio_compressed_formats.h>
  23. #include <asm/dma.h>
  24. #include <linux/dma-mapping.h>
  25. #include <audio/linux/msm_audio.h>
  26. #include <sound/timer.h>
  27. #include <sound/tlv.h>
  28. #include <sound/compress_params.h>
  29. #include <sound/compress_offload.h>
  30. #include <sound/compress_driver.h>
  31. #include <dsp/msm_audio_ion.h>
  32. #include <dsp/apr_audio-v2.h>
  33. #include <dsp/q6asm-v2.h>
  34. #include <dsp/q6core.h>
  35. #include <dsp/msm-audio-effects-q6-v2.h>
  36. #include "msm-pcm-routing-v2.h"
  37. #include "msm-qti-pp-config.h"
  38. #define DRV_NAME "msm-compress-q6-v2"
  39. #define TIMEOUT_MS 1000
  40. #define DSP_PP_BUFFERING_IN_MSEC 25
  41. #define PARTIAL_DRAIN_ACK_EARLY_BY_MSEC 150
  42. #define MP3_OUTPUT_FRAME_SZ 1152
  43. #define AAC_OUTPUT_FRAME_SZ 1024
  44. #define AC3_OUTPUT_FRAME_SZ 1536
  45. #define EAC3_OUTPUT_FRAME_SZ 1536
  46. #define DSP_NUM_OUTPUT_FRAME_BUFFERED 2
  47. #define FLAC_BLK_SIZE_LIMIT 65535
  48. /* Timestamp mode payload offsets */
  49. #define CAPTURE_META_DATA_TS_OFFSET_LSW 6
  50. #define CAPTURE_META_DATA_TS_OFFSET_MSW 7
  51. /* decoder parameter length */
  52. #define DDP_DEC_MAX_NUM_PARAM 18
  53. /* Default values used if user space does not set */
  54. #define COMPR_PLAYBACK_MIN_FRAGMENT_SIZE (8 * 1024)
  55. #define COMPR_PLAYBACK_MAX_FRAGMENT_SIZE (128 * 1024)
  56. #define COMPR_PLAYBACK_MIN_NUM_FRAGMENTS (4)
  57. #define COMPR_PLAYBACK_MAX_NUM_FRAGMENTS (16 * 4)
  58. #define COMPRESSED_LR_VOL_MAX_STEPS 0x2000
  59. const DECLARE_TLV_DB_LINEAR(msm_compr_vol_gain, 0,
  60. COMPRESSED_LR_VOL_MAX_STEPS);
  61. /* Stream id switches between 1 and 2 */
  62. #define NEXT_STREAM_ID(stream_id) ((stream_id & 1) + 1)
  63. #define STREAM_ARRAY_INDEX(stream_id) (stream_id - 1)
  64. #define MAX_NUMBER_OF_STREAMS 2
  65. #define SND_DEC_DDP_MAX_PARAMS 18
  66. #ifndef COMPRESSED_PERF_MODE_FLAG
  67. #define COMPRESSED_PERF_MODE_FLAG 0
  68. #endif
  69. #define DSD_BLOCK_SIZE_4 4
  70. struct msm_compr_gapless_state {
  71. bool set_next_stream_id;
  72. int32_t stream_opened[MAX_NUMBER_OF_STREAMS];
  73. uint32_t initial_samples_drop;
  74. uint32_t trailing_samples_drop;
  75. uint32_t gapless_transition;
  76. bool use_dsp_gapless_mode;
  77. union snd_codec_options codec_options;
  78. };
  79. static unsigned int supported_sample_rates[] = {
  80. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000,
  81. 88200, 96000, 128000, 144000, 176400, 192000, 352800, 384000, 2822400,
  82. 5644800
  83. };
  84. struct msm_compr_pdata {
  85. struct snd_compr_stream *cstream[MSM_FRONTEND_DAI_MAX];
  86. uint32_t volume[MSM_FRONTEND_DAI_MAX][2]; /* For both L & R */
  87. struct msm_compr_audio_effects *audio_effects[MSM_FRONTEND_DAI_MAX];
  88. bool use_dsp_gapless_mode;
  89. bool use_legacy_api; /* indicates use older asm apis*/
  90. struct msm_compr_dec_params *dec_params[MSM_FRONTEND_DAI_MAX];
  91. struct msm_compr_ch_map *ch_map[MSM_FRONTEND_DAI_MAX];
  92. bool is_in_use[MSM_FRONTEND_DAI_MAX];
  93. struct msm_pcm_channel_mixer *chmixer_pspd[MSM_FRONTEND_DAI_MM_SIZE];
  94. struct mutex lock;
  95. };
  96. struct msm_compr_audio {
  97. struct snd_compr_stream *cstream;
  98. struct snd_compr_caps compr_cap;
  99. struct snd_compr_codec_caps codec_caps;
  100. struct snd_compr_params codec_param;
  101. struct audio_client *audio_client;
  102. uint32_t codec;
  103. uint32_t compr_passthr;
  104. void *buffer; /* virtual address */
  105. phys_addr_t buffer_paddr; /* physical address */
  106. uint32_t app_pointer;
  107. uint32_t buffer_size;
  108. uint32_t byte_offset;
  109. uint64_t copied_total; /* bytes consumed by DSP */
  110. uint64_t bytes_received; /* from userspace */
  111. uint64_t bytes_sent; /* to DSP */
  112. uint64_t received_total; /* bytes received from DSP */
  113. uint64_t bytes_copied; /* to userspace */
  114. uint64_t bytes_read; /* from DSP */
  115. uint32_t bytes_read_offset; /* bytes read offset */
  116. uint32_t ts_header_offset; /* holds the timestamp header offset */
  117. int32_t first_buffer;
  118. int32_t last_buffer;
  119. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  120. int32_t zero_buffer;
  121. #endif
  122. int32_t partial_drain_delay;
  123. uint16_t session_id;
  124. uint32_t sample_rate;
  125. uint32_t num_channels;
  126. /*
  127. * convention - commands coming from the same thread
  128. * can use the common cmd_ack var. Others (e.g drain/EOS)
  129. * must use separate vars to track command status.
  130. */
  131. uint32_t cmd_ack;
  132. uint32_t cmd_interrupt;
  133. uint32_t drain_ready;
  134. uint32_t eos_ack;
  135. uint32_t stream_available;
  136. uint32_t next_stream;
  137. uint32_t run_mode;
  138. uint32_t start_delay_lsw;
  139. uint32_t start_delay_msw;
  140. uint64_t marker_timestamp;
  141. struct msm_compr_gapless_state gapless_state;
  142. atomic_t start;
  143. atomic_t eos;
  144. atomic_t drain;
  145. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  146. atomic_t partial_drain;
  147. #endif
  148. atomic_t xrun;
  149. atomic_t close;
  150. atomic_t wait_on_close;
  151. atomic_t error;
  152. wait_queue_head_t eos_wait;
  153. wait_queue_head_t drain_wait;
  154. wait_queue_head_t close_wait;
  155. wait_queue_head_t wait_for_stream_avail;
  156. spinlock_t lock;
  157. };
  158. const u32 compr_codecs[] = {
  159. SND_AUDIOCODEC_AC3, SND_AUDIOCODEC_EAC3, SND_AUDIOCODEC_DTS,
  160. SND_AUDIOCODEC_TRUEHD, SND_AUDIOCODEC_IEC61937};
  161. struct query_audio_effect {
  162. uint32_t mod_id;
  163. uint32_t parm_id;
  164. uint32_t size;
  165. uint32_t offset;
  166. uint32_t device;
  167. };
  168. struct msm_compr_audio_effects {
  169. struct bass_boost_params bass_boost;
  170. struct pbe_params pbe;
  171. struct virtualizer_params virtualizer;
  172. struct reverb_params reverb;
  173. struct eq_params equalizer;
  174. struct soft_volume_params volume;
  175. struct query_audio_effect query;
  176. };
  177. struct snd_dec_ddp {
  178. __u32 params_length;
  179. __u32 params_id[SND_DEC_DDP_MAX_PARAMS];
  180. __u32 params_value[SND_DEC_DDP_MAX_PARAMS];
  181. } __attribute__((packed, aligned(4)));
  182. struct msm_compr_dec_params {
  183. struct snd_dec_ddp ddp_params;
  184. };
  185. struct msm_compr_ch_map {
  186. bool set_ch_map;
  187. char channel_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8];
  188. };
  189. static int msm_compr_send_dec_params(struct snd_compr_stream *cstream,
  190. struct msm_compr_dec_params *dec_params,
  191. int stream_id);
  192. static int msm_compr_set_render_mode(struct msm_compr_audio *prtd,
  193. uint32_t render_mode) {
  194. int ret = -EINVAL;
  195. struct audio_client *ac = prtd->audio_client;
  196. pr_debug("%s, got render mode %u\n", __func__, render_mode);
  197. if (render_mode == SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER) {
  198. render_mode = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_DEFAULT;
  199. } else if (render_mode == SNDRV_COMPRESS_RENDER_MODE_STC_MASTER) {
  200. render_mode = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_LOCAL_STC;
  201. prtd->run_mode = ASM_SESSION_CMD_RUN_STARTIME_RUN_WITH_DELAY;
  202. } else {
  203. pr_err("%s, Invalid render mode %u\n", __func__,
  204. render_mode);
  205. ret = -EINVAL;
  206. goto exit;
  207. }
  208. ret = q6asm_send_mtmx_strtr_render_mode(ac, render_mode);
  209. if (ret) {
  210. pr_err("%s, Render mode can't be set error %d\n", __func__,
  211. ret);
  212. }
  213. exit:
  214. return ret;
  215. }
  216. static int msm_compr_set_clk_rec_mode(struct audio_client *ac,
  217. uint32_t clk_rec_mode) {
  218. int ret = -EINVAL;
  219. pr_debug("%s, got clk rec mode %u\n", __func__, clk_rec_mode);
  220. if (clk_rec_mode == SNDRV_COMPRESS_CLK_REC_MODE_NONE) {
  221. clk_rec_mode = ASM_SESSION_MTMX_STRTR_PARAM_CLK_REC_NONE;
  222. } else if (clk_rec_mode == SNDRV_COMPRESS_CLK_REC_MODE_AUTO) {
  223. clk_rec_mode = ASM_SESSION_MTMX_STRTR_PARAM_CLK_REC_AUTO;
  224. } else {
  225. pr_err("%s, Invalid clk rec_mode mode %u\n", __func__,
  226. clk_rec_mode);
  227. ret = -EINVAL;
  228. goto exit;
  229. }
  230. ret = q6asm_send_mtmx_strtr_clk_rec_mode(ac, clk_rec_mode);
  231. if (ret) {
  232. pr_err("%s, clk rec mode can't be set, error %d\n", __func__,
  233. ret);
  234. }
  235. exit:
  236. return ret;
  237. }
  238. static int msm_compr_set_render_window(struct audio_client *ac,
  239. uint32_t ws_lsw, uint32_t ws_msw,
  240. uint32_t we_lsw, uint32_t we_msw)
  241. {
  242. int ret = -EINVAL;
  243. struct asm_session_mtmx_strtr_param_window_v2_t asm_mtmx_strtr_window;
  244. uint32_t param_id;
  245. pr_debug("%s, ws_lsw 0x%x ws_msw 0x%x we_lsw 0x%x we_ms 0x%x\n",
  246. __func__, ws_lsw, ws_msw, we_lsw, we_msw);
  247. memset(&asm_mtmx_strtr_window, 0,
  248. sizeof(struct asm_session_mtmx_strtr_param_window_v2_t));
  249. asm_mtmx_strtr_window.window_lsw = ws_lsw;
  250. asm_mtmx_strtr_window.window_msw = ws_msw;
  251. param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_START_V2;
  252. ret = q6asm_send_mtmx_strtr_window(ac, &asm_mtmx_strtr_window,
  253. param_id);
  254. if (ret) {
  255. pr_err("%s, start window can't be set error %d\n", __func__,
  256. ret);
  257. goto exit;
  258. }
  259. asm_mtmx_strtr_window.window_lsw = we_lsw;
  260. asm_mtmx_strtr_window.window_msw = we_msw;
  261. param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_END_V2;
  262. ret = q6asm_send_mtmx_strtr_window(ac, &asm_mtmx_strtr_window,
  263. param_id);
  264. if (ret) {
  265. pr_err("%s, end window can't be set error %d\n", __func__,
  266. ret);
  267. }
  268. exit:
  269. return ret;
  270. }
  271. static int msm_compr_enable_adjust_session_clock(struct audio_client *ac,
  272. bool enable)
  273. {
  274. int ret;
  275. pr_debug("%s, enable adjust_session %d\n", __func__, enable);
  276. ret = q6asm_send_mtmx_strtr_enable_adjust_session_clock(ac, enable);
  277. if (ret)
  278. pr_err("%s, adjust session clock can't be set error %d\n",
  279. __func__, ret);
  280. return ret;
  281. }
  282. static int msm_compr_adjust_session_clock(struct audio_client *ac,
  283. uint32_t adjust_session_lsw, uint32_t adjust_session_msw)
  284. {
  285. int ret;
  286. pr_debug("%s, adjust_session_time_msw 0x%x adjust_session_time_lsw 0x%x\n",
  287. __func__, adjust_session_msw, adjust_session_lsw);
  288. ret = q6asm_adjust_session_clock(ac,
  289. adjust_session_lsw,
  290. adjust_session_msw);
  291. if (ret)
  292. pr_err("%s, adjust session clock can't be set error %d\n",
  293. __func__, ret);
  294. return ret;
  295. }
  296. static int msm_compr_set_volume(struct snd_compr_stream *cstream,
  297. uint32_t volume_l, uint32_t volume_r)
  298. {
  299. struct msm_compr_audio *prtd;
  300. int rc = 0;
  301. uint32_t avg_vol, gain_list[VOLUME_CONTROL_MAX_CHANNELS];
  302. uint32_t num_channels;
  303. struct snd_soc_pcm_runtime *rtd;
  304. struct snd_soc_component *component = NULL;
  305. struct msm_compr_pdata *pdata;
  306. bool use_default = true;
  307. u8 *chmap = NULL;
  308. pr_debug("%s: volume_l %d volume_r %d\n",
  309. __func__, volume_l, volume_r);
  310. if (!cstream || !cstream->runtime) {
  311. pr_err("%s: session not active\n", __func__);
  312. return -EPERM;
  313. }
  314. rtd = cstream->private_data;
  315. prtd = cstream->runtime->private_data;
  316. if (!rtd || !prtd || !prtd->audio_client) {
  317. pr_err("%s: invalid rtd, prtd or audio client", __func__);
  318. return rc;
  319. }
  320. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  321. if (!component) {
  322. pr_err("%s: invalid component\n", __func__);
  323. return rc;
  324. }
  325. pdata = snd_soc_component_get_drvdata(component);
  326. if (!pdata)
  327. return -EINVAL;
  328. if (prtd->compr_passthr != LEGACY_PCM) {
  329. pr_debug("%s: No volume config for passthrough %d\n",
  330. __func__, prtd->compr_passthr);
  331. return rc;
  332. }
  333. if (!rtd->dai_link || !pdata->ch_map[rtd->dai_link->id])
  334. return -EINVAL;
  335. use_default = !(pdata->ch_map[rtd->dai_link->id]->set_ch_map);
  336. chmap = pdata->ch_map[rtd->dai_link->id]->channel_map;
  337. num_channels = prtd->num_channels;
  338. if (prtd->num_channels > 2) {
  339. /*
  340. * Currently the left and right gains are averaged an applied
  341. * to all channels. This might not be desirable. But currently,
  342. * there exists no API in userspace to send a list of gains for
  343. * each channel either. If such an API does become available,
  344. * the mixer control must be updated to accept more than 2
  345. * channel gains.
  346. *
  347. */
  348. avg_vol = (volume_l + volume_r) / 2;
  349. rc = q6asm_set_volume(prtd->audio_client, avg_vol);
  350. } else {
  351. gain_list[0] = volume_l;
  352. gain_list[1] = volume_r;
  353. gain_list[2] = volume_l;
  354. if (use_default)
  355. num_channels = 3;
  356. rc = q6asm_set_multich_gain(prtd->audio_client, num_channels,
  357. gain_list, chmap, use_default);
  358. }
  359. if (rc < 0)
  360. pr_err("%s: Send vol gain command failed rc=%d\n",
  361. __func__, rc);
  362. return rc;
  363. }
  364. static int msm_compr_send_ddp_cfg(struct audio_client *ac,
  365. struct snd_dec_ddp *ddp,
  366. int stream_id)
  367. {
  368. int i, rc;
  369. pr_debug("%s\n", __func__);
  370. for (i = 0; i < ddp->params_length; i++) {
  371. rc = q6asm_ds1_set_stream_endp_params(ac, ddp->params_id[i],
  372. ddp->params_value[i],
  373. stream_id);
  374. if (rc) {
  375. pr_err("sending params_id: %d failed\n",
  376. ddp->params_id[i]);
  377. return rc;
  378. }
  379. }
  380. return 0;
  381. }
  382. static int msm_compr_send_buffer(struct msm_compr_audio *prtd)
  383. {
  384. int buffer_length;
  385. uint64_t bytes_available;
  386. struct audio_aio_write_param param;
  387. struct snd_codec_metadata *buff_addr;
  388. if (!atomic_read(&prtd->start)) {
  389. pr_err("%s: stream is not in started state\n", __func__);
  390. return -EINVAL;
  391. }
  392. if (atomic_read(&prtd->xrun)) {
  393. WARN(1, "%s called while xrun is true", __func__);
  394. return -EPERM;
  395. }
  396. pr_debug("%s: bytes_received = %llu copied_total = %llu\n",
  397. __func__, prtd->bytes_received, prtd->copied_total);
  398. if (prtd->first_buffer && prtd->gapless_state.use_dsp_gapless_mode &&
  399. prtd->compr_passthr == LEGACY_PCM)
  400. q6asm_stream_send_meta_data(prtd->audio_client,
  401. prtd->audio_client->stream_id,
  402. prtd->gapless_state.initial_samples_drop,
  403. prtd->gapless_state.trailing_samples_drop);
  404. buffer_length = prtd->codec_param.buffer.fragment_size;
  405. bytes_available = prtd->bytes_received - prtd->copied_total;
  406. if (bytes_available < prtd->codec_param.buffer.fragment_size)
  407. buffer_length = bytes_available;
  408. if (prtd->byte_offset + buffer_length > prtd->buffer_size) {
  409. buffer_length = (prtd->buffer_size - prtd->byte_offset);
  410. pr_debug("%s: wrap around situation, send partial data %d now",
  411. __func__, buffer_length);
  412. }
  413. if (buffer_length) {
  414. param.paddr = prtd->buffer_paddr + prtd->byte_offset;
  415. WARN(prtd->byte_offset % 32 != 0, "offset %x not multiple of 32\n",
  416. prtd->byte_offset);
  417. } else {
  418. param.paddr = prtd->buffer_paddr;
  419. }
  420. param.len = buffer_length;
  421. if (prtd->ts_header_offset) {
  422. buff_addr = (struct snd_codec_metadata *)
  423. (prtd->buffer + prtd->byte_offset);
  424. param.len = buff_addr->length;
  425. param.msw_ts = (uint32_t)
  426. ((buff_addr->timestamp & 0xFFFFFFFF00000000LL) >> 32);
  427. param.lsw_ts = (uint32_t) (buff_addr->timestamp & 0xFFFFFFFFLL);
  428. param.paddr += prtd->ts_header_offset;
  429. param.flags = SET_TIMESTAMP;
  430. param.metadata_len = prtd->ts_header_offset;
  431. } else {
  432. param.msw_ts = 0;
  433. param.lsw_ts = 0;
  434. param.flags = NO_TIMESTAMP;
  435. param.metadata_len = 0;
  436. }
  437. param.uid = buffer_length;
  438. param.last_buffer = prtd->last_buffer;
  439. pr_debug("%s: sending %d bytes to DSP byte_offset = %d\n",
  440. __func__, param.len, prtd->byte_offset);
  441. if (q6asm_async_write(prtd->audio_client, &param) < 0) {
  442. pr_err("%s:q6asm_async_write failed\n", __func__);
  443. } else {
  444. prtd->bytes_sent += buffer_length;
  445. if (prtd->first_buffer)
  446. prtd->first_buffer = 0;
  447. }
  448. return 0;
  449. }
  450. static int msm_compr_read_buffer(struct msm_compr_audio *prtd)
  451. {
  452. int buffer_length;
  453. uint64_t bytes_available;
  454. uint64_t buffer_sent;
  455. struct audio_aio_read_param param;
  456. int ret;
  457. if (!atomic_read(&prtd->start)) {
  458. pr_err("%s: stream is not in started state\n", __func__);
  459. return -EINVAL;
  460. }
  461. buffer_length = prtd->codec_param.buffer.fragment_size -
  462. prtd->ts_header_offset;
  463. bytes_available = prtd->received_total - prtd->bytes_copied;
  464. buffer_sent = prtd->bytes_read - prtd->bytes_copied;
  465. if (buffer_sent + buffer_length + prtd->ts_header_offset
  466. > prtd->buffer_size) {
  467. pr_debug(" %s : Buffer is Full bytes_available: %llu\n",
  468. __func__, bytes_available);
  469. return 0;
  470. }
  471. memset(&param, 0x0, sizeof(struct audio_aio_read_param));
  472. param.paddr = prtd->buffer_paddr + prtd->bytes_read_offset +
  473. prtd->ts_header_offset;
  474. param.len = buffer_length;
  475. param.uid = buffer_length;
  476. /* reserved[1] is for flags */
  477. param.flags = prtd->codec_param.codec.reserved[1];
  478. pr_debug("%s: reading %d bytes from DSP byte_offset = %llu\n",
  479. __func__, buffer_length, prtd->bytes_read);
  480. ret = q6asm_async_read(prtd->audio_client, &param);
  481. if (ret < 0) {
  482. pr_err("%s: q6asm_async_read failed - %d\n",
  483. __func__, ret);
  484. return ret;
  485. }
  486. prtd->bytes_read += buffer_length + prtd->ts_header_offset;
  487. prtd->bytes_read_offset += buffer_length + prtd->ts_header_offset;
  488. if (prtd->bytes_read_offset >= prtd->buffer_size)
  489. prtd->bytes_read_offset -= prtd->buffer_size;
  490. return 0;
  491. }
  492. static void compr_event_handler(uint32_t opcode,
  493. uint32_t token, uint32_t *payload, void *priv)
  494. {
  495. struct msm_compr_audio *prtd = priv;
  496. struct snd_compr_stream *cstream;
  497. struct audio_client *ac;
  498. uint32_t chan_mode = 0;
  499. uint32_t sample_rate = 0;
  500. uint64_t bytes_available;
  501. int stream_id;
  502. uint32_t stream_index;
  503. unsigned long flags;
  504. uint64_t read_size;
  505. uint32_t *buff_addr;
  506. struct snd_soc_pcm_runtime *rtd;
  507. int ret = 0;
  508. if (!prtd) {
  509. pr_err("%s: prtd is NULL\n", __func__);
  510. return;
  511. }
  512. cstream = prtd->cstream;
  513. if (!cstream) {
  514. pr_err("%s: cstream is NULL\n", __func__);
  515. return;
  516. }
  517. ac = prtd->audio_client;
  518. /*
  519. * Token for rest of the compressed commands use to set
  520. * session id, stream id, dir etc.
  521. */
  522. stream_id = q6asm_get_stream_id_from_token(token);
  523. pr_debug("%s opcode =%08x\n", __func__, opcode);
  524. switch (opcode) {
  525. case ASM_DATA_EVENT_WRITE_DONE_V2:
  526. spin_lock_irqsave(&prtd->lock, flags);
  527. if (payload[3]) {
  528. pr_err("%s: WRITE FAILED w/ err 0x%x !, paddr 0x%x, byte_offset=%d,copied_total=%llu,token=%d\n",
  529. __func__,
  530. payload[3],
  531. payload[0],
  532. prtd->byte_offset,
  533. prtd->copied_total, token);
  534. if (atomic_cmpxchg(&prtd->drain, 1, 0) &&
  535. prtd->last_buffer) {
  536. pr_debug("%s: wake up on drain\n", __func__);
  537. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  538. prtd->drain_ready = 1;
  539. wake_up(&prtd->drain_wait);
  540. #endif
  541. prtd->last_buffer = 0;
  542. } else {
  543. atomic_set(&prtd->start, 0);
  544. }
  545. } else {
  546. pr_debug("ASM_DATA_EVENT_WRITE_DONE_V2 offset %d, length %d\n",
  547. prtd->byte_offset, token);
  548. }
  549. /*
  550. * Token for WRITE command represents the amount of data
  551. * written to ADSP in the last write, update offset and
  552. * total copied data accordingly.
  553. */
  554. if (prtd->ts_header_offset) {
  555. /* Always assume that the data will be sent to DSP on
  556. * frame boundary.
  557. * i.e, one frame of userspace write will result in
  558. * one kernel write to DSP. This is needed as
  559. * timestamp will be sent per frame.
  560. */
  561. prtd->byte_offset +=
  562. prtd->codec_param.buffer.fragment_size;
  563. prtd->copied_total +=
  564. prtd->codec_param.buffer.fragment_size;
  565. } else {
  566. prtd->byte_offset += token;
  567. prtd->copied_total += token;
  568. }
  569. if (prtd->byte_offset >= prtd->buffer_size)
  570. prtd->byte_offset -= prtd->buffer_size;
  571. snd_compr_fragment_elapsed(cstream);
  572. if (!atomic_read(&prtd->start)) {
  573. /* Writes must be restarted from _copy() */
  574. pr_debug("write_done received while not started, treat as xrun");
  575. atomic_set(&prtd->xrun, 1);
  576. spin_unlock_irqrestore(&prtd->lock, flags);
  577. break;
  578. }
  579. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  580. if (prtd->zero_buffer) {
  581. pr_debug("write_done for zero buffer\n");
  582. prtd->zero_buffer = 0;
  583. /* move to next stream and reset vars */
  584. pr_debug("%s: Moving to next stream in gapless\n",
  585. __func__);
  586. ac->stream_id = NEXT_STREAM_ID(ac->stream_id);
  587. prtd->byte_offset = 0;
  588. prtd->app_pointer = 0;
  589. prtd->first_buffer = 1;
  590. prtd->last_buffer = 0;
  591. /*
  592. * Set gapless transition flag only if EOS hasn't been
  593. * acknowledged already.
  594. */
  595. if (atomic_read(&prtd->eos))
  596. prtd->gapless_state.gapless_transition = 1;
  597. prtd->marker_timestamp = 0;
  598. /*
  599. * Don't reset these as these vars map to
  600. * total_bytes_transferred and total_bytes_available
  601. * directly, only total_bytes_transferred will be
  602. * updated in the next avail() ioctl
  603. * prtd->copied_total = 0;
  604. * prtd->bytes_received = 0;
  605. */
  606. atomic_set(&prtd->drain, 0);
  607. atomic_set(&prtd->xrun, 1);
  608. pr_debug("%s: issue CMD_RUN", __func__);
  609. q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  610. snd_compr_drain_notify(cstream);
  611. spin_unlock_irqrestore(&prtd->lock, flags);
  612. break;
  613. }
  614. bytes_available = prtd->bytes_received - prtd->copied_total;
  615. if (bytes_available == 0) {
  616. pr_debug("%s:bytes_available is 0\n", __func__);
  617. if (prtd->last_buffer)
  618. prtd->last_buffer = 0;
  619. if (atomic_read(&prtd->partial_drain) &&
  620. prtd->gapless_state.set_next_stream_id &&
  621. !prtd->zero_buffer) {
  622. pr_debug("%s:Partial Drain Case\n", __func__);
  623. pr_debug("%s:Send EOS command\n", __func__);
  624. /* send EOS */
  625. prtd->eos_ack = 0;
  626. atomic_set(&prtd->eos, 1);
  627. atomic_set(&prtd->drain, 0);
  628. q6asm_stream_cmd_nowait(ac, CMD_EOS, ac->stream_id);
  629. /* send a zero length buffer in case of partial drain*/
  630. atomic_set(&prtd->xrun, 0);
  631. pr_debug("%s:Send zero size buffer\n", __func__);
  632. msm_compr_send_buffer(prtd);
  633. prtd->zero_buffer = 1;
  634. } else {
  635. /*
  636. * moving to next stream failed, so reset the gapless state
  637. * set next stream id for the same session so that the same
  638. * stream can be used for gapless playback
  639. */
  640. pr_debug("%s:Drain Case\n", __func__);
  641. pr_debug("%s:Reset Gapless params \n", __func__);
  642. prtd->gapless_state.set_next_stream_id = false;
  643. prtd->gapless_state.gapless_transition = 0;
  644. pr_debug("%s:Send EOS command\n", __func__);
  645. prtd->eos_ack = 0;
  646. atomic_set(&prtd->eos, 1);
  647. atomic_set(&prtd->drain, 0);
  648. q6asm_stream_cmd_nowait(ac, CMD_EOS, ac->stream_id);
  649. prtd->cmd_interrupt = 0;
  650. }
  651. } else if (bytes_available < cstream->runtime->fragment_size) {
  652. pr_debug("%s:Partial Buffer Case \n", __func__);
  653. atomic_set(&prtd->xrun, 1);
  654. if (prtd->last_buffer)
  655. prtd->last_buffer = 0;
  656. if (atomic_read(&prtd->drain)) {
  657. if (bytes_available > 0) {
  658. pr_debug("%s: send %d partial bytes at the end",
  659. __func__, bytes_available);
  660. atomic_set(&prtd->xrun, 0);
  661. prtd->last_buffer = 1;
  662. msm_compr_send_buffer(prtd);
  663. }
  664. }
  665. #else
  666. bytes_available = prtd->bytes_received - prtd->copied_total;
  667. if (bytes_available < cstream->runtime->fragment_size) {
  668. pr_debug("WRITE_DONE Insufficient data to send. break out\n");
  669. atomic_set(&prtd->xrun, 1);
  670. if (prtd->last_buffer)
  671. prtd->last_buffer = 0;
  672. if (atomic_read(&prtd->drain)) {
  673. pr_debug("wake up on drain\n");
  674. prtd->drain_ready = 1;
  675. wake_up(&prtd->drain_wait);
  676. atomic_set(&prtd->drain, 0);
  677. }
  678. #endif
  679. } else if ((bytes_available == cstream->runtime->fragment_size)
  680. && atomic_read(&prtd->drain)) {
  681. prtd->last_buffer = 1;
  682. msm_compr_send_buffer(prtd);
  683. prtd->last_buffer = 0;
  684. } else
  685. msm_compr_send_buffer(prtd);
  686. spin_unlock_irqrestore(&prtd->lock, flags);
  687. break;
  688. case ASM_DATA_EVENT_READ_DONE_V2:
  689. spin_lock_irqsave(&prtd->lock, flags);
  690. pr_debug("ASM_DATA_EVENT_READ_DONE_V2 offset %d, length %d\n",
  691. prtd->byte_offset, payload[4]);
  692. if (prtd->ts_header_offset) {
  693. /* Update the header for received buffer */
  694. buff_addr = prtd->buffer + prtd->byte_offset;
  695. /* Write the actual length of the received buffer */
  696. *buff_addr = payload[4];
  697. buff_addr++;
  698. /* Write the offset */
  699. *buff_addr = prtd->ts_header_offset;
  700. buff_addr++;
  701. /* Write the TS LSW */
  702. *buff_addr = payload[CAPTURE_META_DATA_TS_OFFSET_LSW];
  703. buff_addr++;
  704. /* Write the TS MSW */
  705. *buff_addr = payload[CAPTURE_META_DATA_TS_OFFSET_MSW];
  706. }
  707. /* Always assume read_size is same as fragment_size */
  708. read_size = prtd->codec_param.buffer.fragment_size;
  709. prtd->byte_offset += read_size;
  710. prtd->received_total += read_size;
  711. if (prtd->byte_offset >= prtd->buffer_size)
  712. prtd->byte_offset -= prtd->buffer_size;
  713. snd_compr_fragment_elapsed(cstream);
  714. if (!atomic_read(&prtd->start)) {
  715. pr_debug("read_done received while not started, treat as xrun");
  716. atomic_set(&prtd->xrun, 1);
  717. spin_unlock_irqrestore(&prtd->lock, flags);
  718. break;
  719. }
  720. msm_compr_read_buffer(prtd);
  721. spin_unlock_irqrestore(&prtd->lock, flags);
  722. break;
  723. case ASM_DATA_EVENT_RENDERED_EOS:
  724. case ASM_DATA_EVENT_RENDERED_EOS_V2:
  725. spin_lock_irqsave(&prtd->lock, flags);
  726. pr_debug("%s: ASM_DATA_CMDRSP_EOS token 0x%x,stream id %d\n",
  727. __func__, token, stream_id);
  728. if (atomic_read(&prtd->eos) &&
  729. !prtd->gapless_state.set_next_stream_id) {
  730. pr_debug("ASM_DATA_CMDRSP_EOS wake up\n");
  731. prtd->eos_ack = 1;
  732. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  733. wake_up(&prtd->eos_wait);
  734. #else
  735. pr_debug("%s:issue CMD_PAUSE stream_id %d",
  736. __func__, ac->stream_id);
  737. q6asm_stream_cmd_nowait(ac, CMD_PAUSE, ac->stream_id);
  738. prtd->cmd_ack = 0;
  739. pr_debug("%s:DRAIN,don't wait for EOS ack\n", __func__);
  740. /*
  741. * Don't reset these as these vars map to
  742. * total_bytes_transferred and total_bytes_available.
  743. * Just total_bytes_transferred will be updated
  744. * in the next avail() ioctl.
  745. * prtd->copied_total = 0;
  746. * prtd->bytes_received = 0;
  747. * do not reset prtd->bytes_sent as well as the same
  748. * session is used for gapless playback
  749. */
  750. prtd->byte_offset = 0;
  751. prtd->app_pointer = 0;
  752. prtd->first_buffer = 1;
  753. prtd->last_buffer = 0;
  754. atomic_set(&prtd->drain, 0);
  755. atomic_set(&prtd->xrun, 1);
  756. pr_debug("%s:issue CMD_FLUSH ac->stream_id %d",
  757. __func__, ac->stream_id);
  758. q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  759. snd_compr_drain_notify(cstream);
  760. #endif
  761. }
  762. atomic_set(&prtd->eos, 0);
  763. stream_index = STREAM_ARRAY_INDEX(stream_id);
  764. if (stream_index >= MAX_NUMBER_OF_STREAMS ||
  765. stream_index < 0) {
  766. pr_err("%s: Invalid stream index %d", __func__,
  767. stream_index);
  768. spin_unlock_irqrestore(&prtd->lock, flags);
  769. break;
  770. }
  771. if (prtd->gapless_state.set_next_stream_id &&
  772. prtd->gapless_state.stream_opened[stream_index]) {
  773. pr_debug("%s: CMD_CLOSE stream_id %d\n",
  774. __func__, stream_id);
  775. q6asm_stream_cmd_nowait(ac, CMD_CLOSE, stream_id);
  776. atomic_set(&prtd->close, 1);
  777. prtd->gapless_state.stream_opened[stream_index] = 0;
  778. prtd->gapless_state.set_next_stream_id = false;
  779. }
  780. if (prtd->gapless_state.gapless_transition)
  781. prtd->gapless_state.gapless_transition = 0;
  782. spin_unlock_irqrestore(&prtd->lock, flags);
  783. break;
  784. case ASM_STREAM_PP_EVENT:
  785. case ASM_STREAM_CMD_ENCDEC_EVENTS:
  786. pr_debug("%s: ASM_STREAM_EVENT(0x%x)\n", __func__, opcode);
  787. rtd = cstream->private_data;
  788. if (!rtd) {
  789. pr_err("%s: rtd is NULL\n", __func__);
  790. return;
  791. }
  792. ret = msm_adsp_inform_mixer_ctl(rtd, payload);
  793. if (ret) {
  794. pr_err("%s: failed to inform mixer ctrl. err = %d\n",
  795. __func__, ret);
  796. return;
  797. }
  798. break;
  799. case ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY:
  800. case ASM_DATA_EVENT_ENC_SR_CM_CHANGE_NOTIFY: {
  801. pr_debug("ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY\n");
  802. chan_mode = payload[1] >> 16;
  803. sample_rate = payload[2] >> 16;
  804. if (prtd && (chan_mode != prtd->num_channels ||
  805. sample_rate != prtd->sample_rate)) {
  806. prtd->num_channels = chan_mode;
  807. prtd->sample_rate = sample_rate;
  808. }
  809. }
  810. /* Fallthrough here */
  811. case APR_BASIC_RSP_RESULT: {
  812. switch (payload[0]) {
  813. case ASM_SESSION_CMD_RUN_V2:
  814. /* check if the first buffer need to be sent to DSP */
  815. pr_debug("ASM_SESSION_CMD_RUN_V2\n");
  816. /* FIXME: A state is a better way, dealing with this */
  817. spin_lock_irqsave(&prtd->lock, flags);
  818. if (cstream->direction == SND_COMPRESS_CAPTURE) {
  819. atomic_set(&prtd->start, 1);
  820. msm_compr_read_buffer(prtd);
  821. spin_unlock_irqrestore(&prtd->lock, flags);
  822. break;
  823. }
  824. if (!prtd->bytes_sent) {
  825. bytes_available = prtd->bytes_received -
  826. prtd->copied_total;
  827. if (bytes_available <
  828. cstream->runtime->fragment_size) {
  829. pr_debug("CMD_RUN_V2 Insufficient data to send. break out\n");
  830. atomic_set(&prtd->xrun, 1);
  831. } else {
  832. msm_compr_send_buffer(prtd);
  833. }
  834. }
  835. /*
  836. * The condition below ensures playback finishes in the
  837. * follow cornercase
  838. * WRITE(last buffer)
  839. * WAIT_FOR_DRAIN
  840. * PAUSE
  841. * WRITE_DONE(X)
  842. * RESUME
  843. */
  844. if ((prtd->copied_total == prtd->bytes_sent) &&
  845. atomic_read(&prtd->drain)) {
  846. bytes_available = prtd->bytes_received - prtd->copied_total;
  847. if (bytes_available < cstream->runtime->fragment_size) {
  848. pr_debug("%s: RUN ack, wake up & continue pending drain\n",
  849. __func__);
  850. if (prtd->last_buffer)
  851. prtd->last_buffer = 0;
  852. prtd->drain_ready = 1;
  853. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  854. wake_up(&prtd->drain_wait);
  855. #endif
  856. atomic_set(&prtd->drain, 0);
  857. } else if (atomic_read(&prtd->xrun)) {
  858. pr_debug("%s: RUN ack, continue write cycle\n", __func__);
  859. atomic_set(&prtd->xrun, 0);
  860. msm_compr_send_buffer(prtd);
  861. }
  862. }
  863. spin_unlock_irqrestore(&prtd->lock, flags);
  864. break;
  865. case ASM_STREAM_CMD_FLUSH:
  866. pr_debug("%s: ASM_STREAM_CMD_FLUSH:", __func__);
  867. pr_debug("token 0x%x, stream id %d\n", token,
  868. stream_id);
  869. prtd->cmd_ack = 1;
  870. break;
  871. case ASM_DATA_CMD_REMOVE_INITIAL_SILENCE:
  872. pr_debug("%s: ASM_DATA_CMD_REMOVE_INITIAL_SILENCE:",
  873. __func__);
  874. pr_debug("token 0x%x, stream id = %d\n", token,
  875. stream_id);
  876. break;
  877. case ASM_DATA_CMD_REMOVE_TRAILING_SILENCE:
  878. pr_debug("%s: ASM_DATA_CMD_REMOVE_TRAILING_SILENCE:",
  879. __func__);
  880. pr_debug("token = 0x%x, stream id = %d\n", token,
  881. stream_id);
  882. break;
  883. case ASM_STREAM_CMD_CLOSE:
  884. pr_debug("%s: ASM_DATA_CMD_CLOSE:", __func__);
  885. pr_debug("token 0x%x, stream id %d\n", token,
  886. stream_id);
  887. /*
  888. * wakeup wait for stream avail on stream 3
  889. * after stream 1 ends.
  890. */
  891. if (prtd->next_stream) {
  892. pr_debug("%s:CLOSE:wakeup wait for stream\n",
  893. __func__);
  894. prtd->stream_available = 1;
  895. wake_up(&prtd->wait_for_stream_avail);
  896. prtd->next_stream = 0;
  897. }
  898. if (atomic_read(&prtd->close) &&
  899. atomic_read(&prtd->wait_on_close)) {
  900. prtd->cmd_ack = 1;
  901. wake_up(&prtd->close_wait);
  902. }
  903. atomic_set(&prtd->close, 0);
  904. break;
  905. case ASM_STREAM_CMD_REGISTER_PP_EVENTS:
  906. pr_debug("%s: ASM_STREAM_CMD_REGISTER_PP_EVENTS:",
  907. __func__);
  908. break;
  909. default:
  910. break;
  911. }
  912. break;
  913. }
  914. case ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3:
  915. pr_debug("%s: ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3\n",
  916. __func__);
  917. break;
  918. case RESET_EVENTS:
  919. pr_err("%s: Received reset events CB, move to error state",
  920. __func__);
  921. spin_lock_irqsave(&prtd->lock, flags);
  922. /*
  923. * Since ADSP is down, let this driver pretend that it copied
  924. * all the bytes received, so that next write will be triggered
  925. */
  926. prtd->copied_total = prtd->bytes_received;
  927. snd_compr_fragment_elapsed(cstream);
  928. atomic_set(&prtd->error, 1);
  929. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  930. wake_up(&prtd->drain_wait);
  931. if (atomic_cmpxchg(&prtd->eos, 1, 0)) {
  932. pr_debug("%s:unblock eos wait queues", __func__);
  933. wake_up(&prtd->eos_wait);
  934. }
  935. #endif
  936. spin_unlock_irqrestore(&prtd->lock, flags);
  937. break;
  938. default:
  939. pr_debug("%s: Not Supported Event opcode[0x%x]\n",
  940. __func__, opcode);
  941. break;
  942. }
  943. }
  944. static int msm_compr_get_partial_drain_delay(int frame_sz, int sample_rate)
  945. {
  946. int delay_time_ms = 0;
  947. delay_time_ms = ((DSP_NUM_OUTPUT_FRAME_BUFFERED * frame_sz * 1000) /
  948. sample_rate) + DSP_PP_BUFFERING_IN_MSEC;
  949. delay_time_ms = delay_time_ms > PARTIAL_DRAIN_ACK_EARLY_BY_MSEC ?
  950. delay_time_ms - PARTIAL_DRAIN_ACK_EARLY_BY_MSEC : 0;
  951. pr_debug("%s: frame_sz %d, sample_rate %d, partial drain delay %d\n",
  952. __func__, frame_sz, sample_rate, delay_time_ms);
  953. return delay_time_ms;
  954. }
  955. static void populate_codec_list(struct msm_compr_audio *prtd)
  956. {
  957. pr_debug("%s\n", __func__);
  958. prtd->compr_cap.direction = SND_COMPRESS_PLAYBACK;
  959. prtd->compr_cap.min_fragment_size =
  960. COMPR_PLAYBACK_MIN_FRAGMENT_SIZE;
  961. prtd->compr_cap.max_fragment_size =
  962. COMPR_PLAYBACK_MAX_FRAGMENT_SIZE;
  963. prtd->compr_cap.min_fragments =
  964. COMPR_PLAYBACK_MIN_NUM_FRAGMENTS;
  965. prtd->compr_cap.max_fragments =
  966. COMPR_PLAYBACK_MAX_NUM_FRAGMENTS;
  967. prtd->compr_cap.num_codecs = 10;
  968. prtd->compr_cap.codecs[0] = SND_AUDIOCODEC_MP3;
  969. prtd->compr_cap.codecs[1] = SND_AUDIOCODEC_AAC;
  970. prtd->compr_cap.codecs[2] = SND_AUDIOCODEC_AC3;
  971. prtd->compr_cap.codecs[3] = SND_AUDIOCODEC_EAC3;
  972. prtd->compr_cap.codecs[4] = SND_AUDIOCODEC_MP2;
  973. prtd->compr_cap.codecs[5] = SND_AUDIOCODEC_PCM;
  974. prtd->compr_cap.codecs[6] = SND_AUDIOCODEC_DTS;
  975. prtd->compr_cap.codecs[7] = SND_AUDIOCODEC_TRUEHD;
  976. prtd->compr_cap.codecs[8] = SND_AUDIOCODEC_IEC61937;
  977. prtd->compr_cap.codecs[9] = SND_AUDIOCODEC_BESPOKE;
  978. }
  979. static int msm_compr_get_decoder_format(struct msm_compr_audio *prtd,
  980. int *frame_sz, bool *is_format_gapless)
  981. {
  982. int ret = 0;
  983. __s32 *gdec;
  984. pr_debug("%s: generic format = %x", __func__,
  985. prtd->codec_param.codec.options.generic.reserved[0]);
  986. switch(prtd->codec_param.codec.options.generic.reserved[0]) {
  987. case AUDIO_COMP_FORMAT_ALAC:
  988. ret = FORMAT_ALAC;
  989. break;
  990. case AUDIO_COMP_FORMAT_APE:
  991. ret = FORMAT_APE;
  992. break;
  993. case AUDIO_COMP_FORMAT_APTX:
  994. ret = FORMAT_APTX;
  995. break;
  996. case AUDIO_COMP_FORMAT_DSD:
  997. ret = FORMAT_DSD;
  998. break;
  999. case AUDIO_COMP_FORMAT_FLAC:
  1000. ret = FORMAT_FLAC;
  1001. *is_format_gapless = true;
  1002. gdec = &(prtd->codec_param.codec.options.generic.reserved[1]);
  1003. *frame_sz = ((struct snd_generic_dec_flac *)gdec)->min_blk_size;
  1004. break;
  1005. case AUDIO_COMP_FORMAT_VORBIS:
  1006. ret = FORMAT_VORBIS;
  1007. break;
  1008. case AUDIO_COMP_FORMAT_WMA:
  1009. ret = FORMAT_WMA_V9;
  1010. break;
  1011. case AUDIO_COMP_FORMAT_WMA_PRO:
  1012. ret = FORMAT_WMA_V10PRO;
  1013. break;
  1014. default:
  1015. ret = -EINVAL;
  1016. break;
  1017. }
  1018. return ret;
  1019. }
  1020. static int msm_compr_send_media_format_block(struct snd_compr_stream *cstream,
  1021. int stream_id,
  1022. bool use_gapless_codec_options)
  1023. {
  1024. struct snd_compr_runtime *runtime = cstream->runtime;
  1025. struct msm_compr_audio *prtd = runtime->private_data;
  1026. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  1027. struct snd_soc_component *component =NULL;
  1028. struct msm_compr_pdata *pdata = NULL;
  1029. struct asm_aac_cfg aac_cfg;
  1030. struct asm_wma_cfg wma_cfg;
  1031. struct asm_wmapro_cfg wma_pro_cfg;
  1032. struct asm_flac_cfg flac_cfg;
  1033. struct asm_vorbis_cfg vorbis_cfg;
  1034. struct asm_alac_cfg alac_cfg;
  1035. struct asm_ape_cfg ape_cfg;
  1036. struct asm_dsd_cfg dsd_cfg;
  1037. struct aptx_dec_bt_addr_cfg aptx_cfg;
  1038. struct asm_amrwbplus_cfg amrwbplus_cfg;
  1039. union snd_codec_options *codec_options;
  1040. int ret = 0;
  1041. uint16_t bit_width;
  1042. bool use_default_chmap = true;
  1043. char *chmap = NULL;
  1044. uint16_t sample_word_size;
  1045. __s32 *gdec;
  1046. pr_debug("%s: use_gapless_codec_options %d\n",
  1047. __func__, use_gapless_codec_options);
  1048. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1049. if (!component) {
  1050. pr_err("%s: component is NULL\n", __func__);
  1051. return -EINVAL;
  1052. }
  1053. pdata = snd_soc_component_get_drvdata(component);
  1054. if (use_gapless_codec_options)
  1055. codec_options = &(prtd->gapless_state.codec_options);
  1056. else
  1057. codec_options = &(prtd->codec_param.codec.options);
  1058. if (!codec_options) {
  1059. pr_err("%s: codec_options is NULL\n", __func__);
  1060. return -EINVAL;
  1061. }
  1062. switch (prtd->codec) {
  1063. case FORMAT_LINEAR_PCM:
  1064. pr_debug("SND_AUDIOCODEC_PCM\n");
  1065. if (pdata->ch_map[rtd->dai_link->id]) {
  1066. use_default_chmap =
  1067. !(pdata->ch_map[rtd->dai_link->id]->set_ch_map);
  1068. chmap =
  1069. pdata->ch_map[rtd->dai_link->id]->channel_map;
  1070. }
  1071. switch (prtd->codec_param.codec.format) {
  1072. case SNDRV_PCM_FORMAT_S32_LE:
  1073. bit_width = 32;
  1074. sample_word_size = 32;
  1075. break;
  1076. case SNDRV_PCM_FORMAT_S24_LE:
  1077. bit_width = 24;
  1078. sample_word_size = 32;
  1079. break;
  1080. case SNDRV_PCM_FORMAT_S24_3LE:
  1081. bit_width = 24;
  1082. sample_word_size = 24;
  1083. break;
  1084. case SNDRV_PCM_FORMAT_S16_LE:
  1085. default:
  1086. bit_width = 16;
  1087. sample_word_size = 16;
  1088. break;
  1089. }
  1090. if (q6core_get_avcs_api_version_per_service(
  1091. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  1092. ADSP_ASM_API_VERSION_V2) {
  1093. ret = q6asm_media_format_block_pcm_format_support_v5(
  1094. prtd->audio_client,
  1095. prtd->sample_rate,
  1096. prtd->num_channels,
  1097. bit_width, stream_id,
  1098. use_default_chmap,
  1099. chmap,
  1100. sample_word_size,
  1101. ASM_LITTLE_ENDIAN,
  1102. DEFAULT_QF);
  1103. } else {
  1104. ret = q6asm_media_format_block_pcm_format_support_v4(
  1105. prtd->audio_client,
  1106. prtd->sample_rate,
  1107. prtd->num_channels,
  1108. bit_width, stream_id,
  1109. use_default_chmap,
  1110. chmap,
  1111. sample_word_size,
  1112. ASM_LITTLE_ENDIAN,
  1113. DEFAULT_QF);
  1114. }
  1115. if (ret < 0)
  1116. pr_err("%s: CMD Format block failed\n", __func__);
  1117. break;
  1118. case FORMAT_MP3:
  1119. pr_debug("SND_AUDIOCODEC_MP3\n");
  1120. /* no media format block needed */
  1121. break;
  1122. case FORMAT_MPEG4_AAC:
  1123. pr_debug("SND_AUDIOCODEC_AAC\n");
  1124. memset(&aac_cfg, 0x0, sizeof(struct asm_aac_cfg));
  1125. aac_cfg.aot = AAC_ENC_MODE_EAAC_P;
  1126. if (prtd->codec_param.codec.format ==
  1127. SND_AUDIOSTREAMFORMAT_MP4ADTS)
  1128. aac_cfg.format = 0x0;
  1129. else if (prtd->codec_param.codec.format ==
  1130. SND_AUDIOSTREAMFORMAT_MP4LATM)
  1131. aac_cfg.format = 0x04;
  1132. else
  1133. aac_cfg.format = 0x03;
  1134. aac_cfg.ch_cfg = prtd->num_channels;
  1135. aac_cfg.sample_rate = prtd->sample_rate;
  1136. ret = q6asm_stream_media_format_block_aac(prtd->audio_client,
  1137. &aac_cfg, stream_id);
  1138. if (ret < 0)
  1139. pr_err("%s: CMD Format block failed\n", __func__);
  1140. break;
  1141. case FORMAT_AC3:
  1142. pr_debug("SND_AUDIOCODEC_AC3\n");
  1143. break;
  1144. case FORMAT_EAC3:
  1145. pr_debug("SND_AUDIOCODEC_EAC3\n");
  1146. break;
  1147. case FORMAT_WMA_V9:
  1148. pr_debug("SND_AUDIOCODEC_WMA\n");
  1149. memset(&wma_cfg, 0x0, sizeof(struct asm_wma_cfg));
  1150. wma_cfg.format_tag = prtd->codec_param.codec.format;
  1151. wma_cfg.ch_cfg = prtd->codec_param.codec.ch_in;
  1152. wma_cfg.sample_rate = prtd->sample_rate;
  1153. gdec = &(codec_options->generic.reserved[1]);
  1154. wma_cfg.avg_bytes_per_sec =
  1155. ((struct snd_generic_dec_wma *)gdec)->avg_bit_rate/8;
  1156. wma_cfg.block_align =
  1157. ((struct snd_generic_dec_wma *)gdec)->super_block_align;
  1158. wma_cfg.valid_bits_per_sample =
  1159. ((struct snd_generic_dec_wma *)gdec)->bits_per_sample;
  1160. wma_cfg.ch_mask =
  1161. ((struct snd_generic_dec_wma *)gdec)->channelmask;
  1162. wma_cfg.encode_opt =
  1163. ((struct snd_generic_dec_wma *)gdec)->encodeopt;
  1164. ret = q6asm_media_format_block_wma(prtd->audio_client,
  1165. &wma_cfg, stream_id);
  1166. if (ret < 0)
  1167. pr_err("%s: CMD Format block failed\n", __func__);
  1168. break;
  1169. case FORMAT_WMA_V10PRO:
  1170. pr_debug("SND_AUDIOCODEC_WMA_PRO\n");
  1171. memset(&wma_pro_cfg, 0x0, sizeof(struct asm_wmapro_cfg));
  1172. wma_pro_cfg.format_tag = prtd->codec_param.codec.format;
  1173. wma_pro_cfg.ch_cfg = prtd->codec_param.codec.ch_in;
  1174. wma_pro_cfg.sample_rate = prtd->sample_rate;
  1175. gdec = &(codec_options->generic.reserved[1]);
  1176. wma_cfg.avg_bytes_per_sec =
  1177. ((struct snd_generic_dec_wma *)gdec)->avg_bit_rate/8;
  1178. wma_pro_cfg.block_align =
  1179. ((struct snd_generic_dec_wma *)gdec)->super_block_align;
  1180. wma_pro_cfg.valid_bits_per_sample =
  1181. ((struct snd_generic_dec_wma *)gdec)->bits_per_sample;
  1182. wma_pro_cfg.ch_mask =
  1183. ((struct snd_generic_dec_wma *)gdec)->channelmask;
  1184. wma_pro_cfg.encode_opt =
  1185. ((struct snd_generic_dec_wma *)gdec)->encodeopt;
  1186. wma_pro_cfg.adv_encode_opt =
  1187. ((struct snd_generic_dec_wma *)gdec)->encodeopt1;
  1188. wma_pro_cfg.adv_encode_opt2 =
  1189. ((struct snd_generic_dec_wma *)gdec)->encodeopt2;
  1190. ret = q6asm_media_format_block_wmapro(prtd->audio_client,
  1191. &wma_pro_cfg, stream_id);
  1192. if (ret < 0)
  1193. pr_err("%s: CMD Format block failed\n", __func__);
  1194. break;
  1195. case FORMAT_MP2:
  1196. pr_debug("%s: SND_AUDIOCODEC_MP2\n", __func__);
  1197. break;
  1198. case FORMAT_FLAC:
  1199. pr_debug("%s: SND_AUDIOCODEC_FLAC\n", __func__);
  1200. memset(&flac_cfg, 0x0, sizeof(struct asm_flac_cfg));
  1201. flac_cfg.ch_cfg = prtd->num_channels;
  1202. flac_cfg.sample_rate = prtd->sample_rate;
  1203. flac_cfg.stream_info_present = 1;
  1204. gdec = &(codec_options->generic.reserved[1]);
  1205. flac_cfg.sample_size =
  1206. ((struct snd_generic_dec_flac *)gdec)->sample_size;
  1207. flac_cfg.min_blk_size =
  1208. ((struct snd_generic_dec_flac *)gdec)->min_blk_size;
  1209. flac_cfg.max_blk_size =
  1210. ((struct snd_generic_dec_flac *)gdec)->max_blk_size;
  1211. flac_cfg.max_frame_size =
  1212. ((struct snd_generic_dec_flac *)gdec)->max_frame_size;
  1213. flac_cfg.min_frame_size =
  1214. ((struct snd_generic_dec_flac *)gdec)->min_frame_size;
  1215. ret = q6asm_stream_media_format_block_flac(prtd->audio_client,
  1216. &flac_cfg, stream_id);
  1217. if (ret < 0)
  1218. pr_err("%s: CMD Format block failed ret %d\n",
  1219. __func__, ret);
  1220. break;
  1221. case FORMAT_VORBIS:
  1222. pr_debug("%s: SND_AUDIOCODEC_VORBIS\n", __func__);
  1223. memset(&vorbis_cfg, 0x0, sizeof(struct asm_vorbis_cfg));
  1224. gdec = &(codec_options->generic.reserved[1]);
  1225. vorbis_cfg.bit_stream_fmt =
  1226. ((struct snd_generic_dec_vorbis *)gdec)->bit_stream_fmt;
  1227. ret = q6asm_stream_media_format_block_vorbis(
  1228. prtd->audio_client, &vorbis_cfg,
  1229. stream_id);
  1230. if (ret < 0)
  1231. pr_err("%s: CMD Format block failed ret %d\n",
  1232. __func__, ret);
  1233. break;
  1234. case FORMAT_ALAC:
  1235. pr_debug("%s: SND_AUDIOCODEC_ALAC\n", __func__);
  1236. memset(&alac_cfg, 0x0, sizeof(struct asm_alac_cfg));
  1237. alac_cfg.num_channels = prtd->num_channels;
  1238. alac_cfg.sample_rate = prtd->sample_rate;
  1239. gdec = &(codec_options->generic.reserved[1]);
  1240. alac_cfg.frame_length =
  1241. ((struct snd_generic_dec_alac *)gdec)->frame_length;
  1242. alac_cfg.compatible_version =
  1243. ((struct snd_generic_dec_alac *)gdec)->compatible_version;
  1244. alac_cfg.bit_depth =
  1245. ((struct snd_generic_dec_alac *)gdec)->bit_depth;
  1246. alac_cfg.pb = ((struct snd_generic_dec_alac *)gdec)->pb;
  1247. alac_cfg.mb = ((struct snd_generic_dec_alac *)gdec)->mb;
  1248. alac_cfg.kb = ((struct snd_generic_dec_alac *)gdec)->kb;
  1249. alac_cfg.max_run =
  1250. ((struct snd_generic_dec_alac *)gdec)->max_run;
  1251. alac_cfg.max_frame_bytes =
  1252. ((struct snd_generic_dec_alac *)gdec)->max_frame_bytes;
  1253. alac_cfg.avg_bit_rate =
  1254. ((struct snd_generic_dec_alac *)gdec)->avg_bit_rate;
  1255. alac_cfg.channel_layout_tag =
  1256. ((struct snd_generic_dec_alac *)gdec)->channel_layout_tag;
  1257. ret = q6asm_media_format_block_alac(prtd->audio_client,
  1258. &alac_cfg, stream_id);
  1259. if (ret < 0)
  1260. pr_err("%s: CMD Format block failed ret %d\n",
  1261. __func__, ret);
  1262. break;
  1263. case FORMAT_APE:
  1264. pr_debug("%s: SND_AUDIOCODEC_APE\n", __func__);
  1265. memset(&ape_cfg, 0x0, sizeof(struct asm_ape_cfg));
  1266. ape_cfg.num_channels = prtd->num_channels;
  1267. ape_cfg.sample_rate = prtd->sample_rate;
  1268. gdec = &(codec_options->generic.reserved[1]);
  1269. ape_cfg.compatible_version =
  1270. ((struct snd_generic_dec_ape *)gdec)->compatible_version;
  1271. ape_cfg.compression_level =
  1272. ((struct snd_generic_dec_ape *)gdec)->compression_level;
  1273. ape_cfg.format_flags =
  1274. ((struct snd_generic_dec_ape *)gdec)->format_flags;
  1275. ape_cfg.blocks_per_frame =
  1276. ((struct snd_generic_dec_ape *)gdec)->blocks_per_frame;
  1277. ape_cfg.final_frame_blocks =
  1278. ((struct snd_generic_dec_ape *)gdec)->final_frame_blocks;
  1279. ape_cfg.total_frames =
  1280. ((struct snd_generic_dec_ape *)gdec)->total_frames;
  1281. ape_cfg.bits_per_sample =
  1282. ((struct snd_generic_dec_ape *)gdec)->bits_per_sample;
  1283. ape_cfg.seek_table_present =
  1284. ((struct snd_generic_dec_ape *)gdec)->seek_table_present;
  1285. ret = q6asm_media_format_block_ape(prtd->audio_client,
  1286. &ape_cfg, stream_id);
  1287. if (ret < 0)
  1288. pr_err("%s: CMD Format block failed ret %d\n",
  1289. __func__, ret);
  1290. break;
  1291. case FORMAT_DTS:
  1292. pr_debug("SND_AUDIOCODEC_DTS\n");
  1293. /* no media format block needed */
  1294. break;
  1295. case FORMAT_DSD:
  1296. pr_debug("%s: SND_AUDIOCODEC_DSD\n", __func__);
  1297. memset(&dsd_cfg, 0x0, sizeof(struct asm_dsd_cfg));
  1298. dsd_cfg.num_channels = prtd->num_channels;
  1299. dsd_cfg.dsd_data_rate = prtd->sample_rate;
  1300. dsd_cfg.num_version = 0;
  1301. dsd_cfg.is_bitwise_big_endian = 1;
  1302. dsd_cfg.dsd_channel_block_size = 1;
  1303. gdec = &(codec_options->generic.reserved[1]);
  1304. if (((struct snd_generic_dec_dsd *)gdec)->blk_size ==
  1305. DSD_BLOCK_SIZE_4)
  1306. dsd_cfg.dsd_channel_block_size =
  1307. ((struct snd_generic_dec_dsd *)gdec)->blk_size;
  1308. ret = q6asm_media_format_block_dsd(prtd->audio_client,
  1309. &dsd_cfg, stream_id);
  1310. if (ret < 0)
  1311. pr_err("%s: CMD DSD Format block failed ret %d\n",
  1312. __func__, ret);
  1313. break;
  1314. case FORMAT_TRUEHD:
  1315. pr_debug("SND_AUDIOCODEC_TRUEHD\n");
  1316. /* no media format block needed */
  1317. break;
  1318. case FORMAT_IEC61937:
  1319. pr_debug("SND_AUDIOCODEC_IEC61937\n");
  1320. ret = q6asm_media_format_block_iec(prtd->audio_client,
  1321. prtd->sample_rate,
  1322. prtd->num_channels);
  1323. if (ret < 0)
  1324. pr_err("%s: CMD IEC61937 Format block failed ret %d\n",
  1325. __func__, ret);
  1326. break;
  1327. case FORMAT_APTX:
  1328. pr_debug("SND_AUDIOCODEC_APTX\n");
  1329. memset(&aptx_cfg, 0x0, sizeof(struct aptx_dec_bt_addr_cfg));
  1330. ret = q6asm_stream_media_format_block_aptx_dec(
  1331. prtd->audio_client,
  1332. prtd->sample_rate,
  1333. stream_id);
  1334. if (ret >= 0) {
  1335. gdec = &(codec_options->generic.reserved[1]);
  1336. aptx_cfg.nap =
  1337. ((struct snd_generic_dec_aptx *)gdec)->nap;
  1338. aptx_cfg.uap =
  1339. ((struct snd_generic_dec_aptx *)gdec)->uap;
  1340. aptx_cfg.lap =
  1341. ((struct snd_generic_dec_aptx *)gdec)->lap;
  1342. q6asm_set_aptx_dec_bt_addr(prtd->audio_client,
  1343. &aptx_cfg);
  1344. } else {
  1345. pr_err("%s: CMD Format block failed ret %d\n",
  1346. __func__, ret);
  1347. }
  1348. break;
  1349. case FORMAT_AMRNB:
  1350. pr_debug("SND_AUDIOCODEC_AMR\n");
  1351. /* no media format block needed */
  1352. break;
  1353. case FORMAT_AMRWB:
  1354. pr_debug("SND_AUDIOCODEC_AMRWB\n");
  1355. /* no media format block needed */
  1356. break;
  1357. case FORMAT_AMR_WB_PLUS:
  1358. pr_debug("SND_AUDIOCODEC_AMRWBPLUS\n");
  1359. memset(&amrwbplus_cfg, 0x0, sizeof(struct asm_amrwbplus_cfg));
  1360. gdec = &(codec_options->generic.reserved[1]);
  1361. amrwbplus_cfg.amr_frame_fmt =
  1362. ((struct snd_generic_dec_amrwb_plus *)gdec)->bit_stream_fmt;
  1363. ret = q6asm_media_format_block_amrwbplus(
  1364. prtd->audio_client,
  1365. &amrwbplus_cfg);
  1366. if (ret < 0)
  1367. pr_err("%s: CMD AMRWBPLUS Format block failed ret %d\n",
  1368. __func__, ret);
  1369. break;
  1370. default:
  1371. pr_debug("%s, unsupported format, skip", __func__);
  1372. break;
  1373. }
  1374. return ret;
  1375. }
  1376. static int msm_compr_init_pp_params(struct snd_compr_stream *cstream,
  1377. struct audio_client *ac)
  1378. {
  1379. int ret = 0;
  1380. struct asm_softvolume_params softvol = {
  1381. .period = SOFT_VOLUME_PERIOD,
  1382. .step = SOFT_VOLUME_STEP,
  1383. .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
  1384. };
  1385. switch (ac->topology) {
  1386. default:
  1387. ret = q6asm_set_softvolume_v2(ac, &softvol,
  1388. SOFT_VOLUME_INSTANCE_1);
  1389. if (ret < 0)
  1390. pr_err("%s: Send SoftVolume Param failed ret=%d\n",
  1391. __func__, ret);
  1392. break;
  1393. }
  1394. return ret;
  1395. }
  1396. static int msm_compr_configure_dsp_for_playback
  1397. (struct snd_compr_stream *cstream)
  1398. {
  1399. struct snd_compr_runtime *runtime = cstream->runtime;
  1400. struct msm_compr_audio *prtd = runtime->private_data;
  1401. struct snd_soc_pcm_runtime *soc_prtd = cstream->private_data;
  1402. uint16_t bits_per_sample = 16;
  1403. int dir = IN, ret = 0;
  1404. struct audio_client *ac = prtd->audio_client;
  1405. uint32_t stream_index;
  1406. struct asm_softpause_params softpause = {
  1407. .enable = SOFT_PAUSE_ENABLE,
  1408. .period = SOFT_PAUSE_PERIOD,
  1409. .step = SOFT_PAUSE_STEP,
  1410. .rampingcurve = SOFT_PAUSE_CURVE_LINEAR,
  1411. };
  1412. struct asm_softvolume_params softvol = {
  1413. .period = SOFT_VOLUME_PERIOD,
  1414. .step = SOFT_VOLUME_STEP,
  1415. .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
  1416. };
  1417. struct snd_kcontrol *kctl;
  1418. struct snd_ctl_elem_value kctl_elem_value;
  1419. uint16_t target_asm_bit_width = 0;
  1420. pr_debug("%s: stream_id %d\n", __func__, ac->stream_id);
  1421. stream_index = STREAM_ARRAY_INDEX(ac->stream_id);
  1422. if (stream_index >= MAX_NUMBER_OF_STREAMS || stream_index < 0) {
  1423. pr_err("%s: Invalid stream index:%d", __func__, stream_index);
  1424. return -EINVAL;
  1425. }
  1426. kctl = snd_soc_card_get_kcontrol(soc_prtd->card,
  1427. DSP_BIT_WIDTH_MIXER_CTL);
  1428. if (kctl) {
  1429. kctl->get(kctl, &kctl_elem_value);
  1430. target_asm_bit_width = kctl_elem_value.value.integer.value[0];
  1431. if (target_asm_bit_width > 0) {
  1432. pr_debug("%s enforce ASM bitwidth to %d from %d\n",
  1433. __func__,
  1434. target_asm_bit_width,
  1435. bits_per_sample);
  1436. bits_per_sample = target_asm_bit_width;
  1437. }
  1438. } else {
  1439. pr_info("%s: failed to get mixer ctl for %s.\n",
  1440. __func__, DSP_BIT_WIDTH_MIXER_CTL);
  1441. }
  1442. if ((prtd->codec_param.codec.format == SNDRV_PCM_FORMAT_S24_LE) ||
  1443. (prtd->codec_param.codec.format == SNDRV_PCM_FORMAT_S24_3LE))
  1444. bits_per_sample = 24;
  1445. else if (prtd->codec_param.codec.format == SNDRV_PCM_FORMAT_S32_LE)
  1446. bits_per_sample = 32;
  1447. if (prtd->compr_passthr != LEGACY_PCM) {
  1448. ret = q6asm_open_write_compressed(ac, prtd->codec,
  1449. prtd->compr_passthr);
  1450. if (ret < 0) {
  1451. pr_err("%s:ASM open write err[%d] for compr_type[%d]\n",
  1452. __func__, ret, prtd->compr_passthr);
  1453. return ret;
  1454. }
  1455. prtd->gapless_state.stream_opened[stream_index] = 1;
  1456. ret = msm_pcm_routing_reg_phy_compr_stream(
  1457. soc_prtd->dai_link->id,
  1458. ac->perf_mode,
  1459. prtd->session_id,
  1460. SNDRV_PCM_STREAM_PLAYBACK,
  1461. prtd->compr_passthr);
  1462. if (ret) {
  1463. pr_err("%s: compr stream reg failed:%d\n", __func__,
  1464. ret);
  1465. return ret;
  1466. }
  1467. } else {
  1468. pr_debug("%s: stream_id %d bits_per_sample %d\n",
  1469. __func__, ac->stream_id, bits_per_sample);
  1470. if (q6core_get_avcs_api_version_per_service(
  1471. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  1472. ADSP_ASM_API_VERSION_V2)
  1473. ret = q6asm_stream_open_write_v5(ac,
  1474. prtd->codec, bits_per_sample,
  1475. ac->stream_id,
  1476. prtd->gapless_state.use_dsp_gapless_mode);
  1477. else
  1478. ret = q6asm_stream_open_write_v4(ac,
  1479. prtd->codec, bits_per_sample,
  1480. ac->stream_id,
  1481. prtd->gapless_state.use_dsp_gapless_mode);
  1482. if (ret < 0) {
  1483. pr_err("%s:ASM open write err[%d] for compr type[%d]\n",
  1484. __func__, ret, prtd->compr_passthr);
  1485. return -ENOMEM;
  1486. }
  1487. prtd->gapless_state.stream_opened[stream_index] = 1;
  1488. pr_debug("%s: BE id %d\n", __func__, soc_prtd->dai_link->id);
  1489. ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
  1490. ac->perf_mode,
  1491. prtd->session_id,
  1492. SNDRV_PCM_STREAM_PLAYBACK);
  1493. if (ret) {
  1494. pr_err("%s: stream reg failed:%d\n", __func__, ret);
  1495. return ret;
  1496. }
  1497. }
  1498. ret = msm_compr_set_volume(cstream, 0, 0);
  1499. if (ret < 0)
  1500. pr_err("%s : Set Volume failed : %d", __func__, ret);
  1501. if (prtd->compr_passthr != LEGACY_PCM) {
  1502. pr_debug("%s : Don't send cal and PP params for compress path",
  1503. __func__);
  1504. } else {
  1505. ret = q6asm_send_cal(ac);
  1506. if (ret < 0)
  1507. pr_debug("%s : Send cal failed : %d", __func__, ret);
  1508. ret = q6asm_set_softpause(ac, &softpause);
  1509. if (ret < 0)
  1510. pr_err("%s: Send SoftPause Param failed ret=%d\n",
  1511. __func__, ret);
  1512. ret = q6asm_set_softvolume(ac, &softvol);
  1513. if (ret < 0)
  1514. pr_err("%s: Send SoftVolume Param failed ret=%d\n",
  1515. __func__, ret);
  1516. }
  1517. ret = q6asm_set_io_mode(ac, (COMPRESSED_STREAM_IO | ASYNC_IO_MODE));
  1518. if (ret < 0) {
  1519. pr_err("%s: Set IO mode failed\n", __func__);
  1520. return -EINVAL;
  1521. }
  1522. runtime->fragments = prtd->codec_param.buffer.fragments;
  1523. runtime->fragment_size = prtd->codec_param.buffer.fragment_size;
  1524. pr_debug("allocate %d buffers each of size %d\n",
  1525. runtime->fragments,
  1526. runtime->fragment_size);
  1527. ret = q6asm_audio_client_buf_alloc_contiguous(dir, ac,
  1528. runtime->fragment_size,
  1529. runtime->fragments);
  1530. if (ret < 0) {
  1531. pr_err("Audio Start: Buffer Allocation failed rc = %d\n", ret);
  1532. return -ENOMEM;
  1533. }
  1534. prtd->byte_offset = 0;
  1535. prtd->copied_total = 0;
  1536. prtd->app_pointer = 0;
  1537. prtd->bytes_received = 0;
  1538. prtd->bytes_sent = 0;
  1539. prtd->buffer = ac->port[dir].buf[0].data;
  1540. prtd->buffer_paddr = ac->port[dir].buf[0].phys;
  1541. prtd->buffer_size = runtime->fragments * runtime->fragment_size;
  1542. /* Bit-0 of flags represent timestamp mode */
  1543. /* reserved[1] is for flags */
  1544. if (prtd->codec_param.codec.reserved[1] & COMPRESSED_TIMESTAMP_FLAG)
  1545. prtd->ts_header_offset = sizeof(struct snd_codec_metadata);
  1546. else
  1547. prtd->ts_header_offset = 0;
  1548. ret = msm_compr_send_media_format_block(cstream, ac->stream_id, false);
  1549. if (ret < 0)
  1550. pr_err("%s, failed to send media format block\n", __func__);
  1551. return ret;
  1552. }
  1553. static int msm_compr_configure_dsp_for_capture(struct snd_compr_stream *cstream)
  1554. {
  1555. struct snd_compr_runtime *runtime = cstream->runtime;
  1556. struct msm_compr_audio *prtd = runtime->private_data;
  1557. struct snd_soc_pcm_runtime *soc_prtd = cstream->private_data;
  1558. uint16_t bits_per_sample;
  1559. uint16_t sample_word_size;
  1560. int dir = OUT, ret = 0;
  1561. struct audio_client *ac = prtd->audio_client;
  1562. uint32_t stream_index;
  1563. uint32_t enc_cfg_id = ENC_CFG_ID_NONE;
  1564. bool compress_ts = false;
  1565. switch (prtd->codec_param.codec.format) {
  1566. case SNDRV_PCM_FORMAT_S24_LE:
  1567. bits_per_sample = 24;
  1568. sample_word_size = 32;
  1569. break;
  1570. case SNDRV_PCM_FORMAT_S24_3LE:
  1571. bits_per_sample = 24;
  1572. sample_word_size = 24;
  1573. break;
  1574. case SNDRV_PCM_FORMAT_S32_LE:
  1575. bits_per_sample = 32;
  1576. sample_word_size = 32;
  1577. break;
  1578. case SNDRV_PCM_FORMAT_S16_LE:
  1579. default:
  1580. bits_per_sample = 16;
  1581. sample_word_size = 16;
  1582. if (prtd->codec == FORMAT_BESPOKE)
  1583. enc_cfg_id =
  1584. prtd->codec_param.codec.options.generic.reserved[0];
  1585. break;
  1586. }
  1587. pr_debug("%s: stream_id %d bits_per_sample %d compr_passthr %d\n",
  1588. __func__, ac->stream_id, bits_per_sample,
  1589. prtd->compr_passthr);
  1590. if (prtd->compr_passthr != LEGACY_PCM) {
  1591. ret = q6asm_open_read_compressed(prtd->audio_client,
  1592. prtd->codec, prtd->compr_passthr);
  1593. if (ret < 0) {
  1594. pr_err("%s:ASM open read err[%d] for compr_type[%d]\n",
  1595. __func__, ret, prtd->compr_passthr);
  1596. return ret;
  1597. }
  1598. ret = msm_pcm_routing_reg_phy_compr_stream(
  1599. soc_prtd->dai_link->id,
  1600. ac->perf_mode,
  1601. prtd->session_id,
  1602. SNDRV_PCM_STREAM_CAPTURE,
  1603. prtd->compr_passthr);
  1604. if (ret) {
  1605. pr_err("%s: compr stream reg failed:%d\n",
  1606. __func__, ret);
  1607. return ret;
  1608. }
  1609. } else {
  1610. /* reserved[1] is for flags */
  1611. if (prtd->codec_param.codec.reserved[1]
  1612. & COMPRESSED_TIMESTAMP_FLAG)
  1613. compress_ts = true;
  1614. if (q6core_get_avcs_api_version_per_service(
  1615. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  1616. ADSP_ASM_API_VERSION_V2)
  1617. ret = q6asm_open_read_v5(prtd->audio_client,
  1618. prtd->codec, bits_per_sample,
  1619. compress_ts, enc_cfg_id);
  1620. else
  1621. ret = q6asm_open_read_v4(prtd->audio_client,
  1622. prtd->codec, bits_per_sample,
  1623. compress_ts, enc_cfg_id);
  1624. if (ret < 0) {
  1625. pr_err("%s: q6asm_open_read failed:%d\n",
  1626. __func__, ret);
  1627. return ret;
  1628. }
  1629. ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
  1630. ac->perf_mode,
  1631. prtd->session_id,
  1632. SNDRV_PCM_STREAM_CAPTURE);
  1633. if (ret) {
  1634. pr_err("%s: stream reg failed:%d\n", __func__, ret);
  1635. return ret;
  1636. }
  1637. }
  1638. ret = q6asm_set_io_mode(ac, (COMPRESSED_STREAM_IO | ASYNC_IO_MODE));
  1639. if (ret < 0) {
  1640. pr_err("%s: Set IO mode failed\n", __func__);
  1641. return -EINVAL;
  1642. }
  1643. stream_index = STREAM_ARRAY_INDEX(ac->stream_id);
  1644. if (stream_index >= MAX_NUMBER_OF_STREAMS || stream_index < 0) {
  1645. pr_err("%s: Invalid stream index:%d", __func__, stream_index);
  1646. return -EINVAL;
  1647. }
  1648. runtime->fragments = prtd->codec_param.buffer.fragments;
  1649. runtime->fragment_size = prtd->codec_param.buffer.fragment_size;
  1650. pr_debug("%s: allocate %d buffers each of size %d\n",
  1651. __func__, runtime->fragments,
  1652. runtime->fragment_size);
  1653. ret = q6asm_audio_client_buf_alloc_contiguous(dir, ac,
  1654. runtime->fragment_size,
  1655. runtime->fragments);
  1656. if (ret < 0) {
  1657. pr_err("Audio Start: Buffer Allocation failed rc = %d\n", ret);
  1658. return -ENOMEM;
  1659. }
  1660. prtd->byte_offset = 0;
  1661. prtd->received_total = 0;
  1662. prtd->app_pointer = 0;
  1663. prtd->bytes_copied = 0;
  1664. prtd->bytes_read = 0;
  1665. prtd->bytes_read_offset = 0;
  1666. prtd->buffer = ac->port[dir].buf[0].data;
  1667. prtd->buffer_paddr = ac->port[dir].buf[0].phys;
  1668. prtd->buffer_size = runtime->fragments * runtime->fragment_size;
  1669. /* Bit-0 of flags represent timestamp mode */
  1670. /* reserved[1] is for flags */
  1671. if (prtd->codec_param.codec.reserved[1] & COMPRESSED_TIMESTAMP_FLAG)
  1672. prtd->ts_header_offset = sizeof(struct snd_codec_metadata);
  1673. else
  1674. prtd->ts_header_offset = 0;
  1675. pr_debug("%s: sample_rate = %d channels = %d bps = %d sample_word_size = %d\n",
  1676. __func__, prtd->sample_rate, prtd->num_channels,
  1677. bits_per_sample, sample_word_size);
  1678. if (prtd->codec == FORMAT_BESPOKE) {
  1679. /*
  1680. * For BESPOKE codec, encoder specific config params are
  1681. * included as part of generic.
  1682. */
  1683. ret = q6asm_enc_cfg_blk_custom(prtd->audio_client, prtd->sample_rate,
  1684. prtd->num_channels, prtd->codec,
  1685. (void *)&prtd->codec_param.codec.options.generic);
  1686. } else if (prtd->compr_passthr == LEGACY_PCM) {
  1687. if (q6core_get_avcs_api_version_per_service(
  1688. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  1689. ADSP_ASM_API_VERSION_V2)
  1690. ret = q6asm_enc_cfg_blk_pcm_format_support_v5(
  1691. prtd->audio_client,
  1692. prtd->sample_rate, prtd->num_channels,
  1693. bits_per_sample, sample_word_size,
  1694. ASM_LITTLE_ENDIAN, DEFAULT_QF);
  1695. else
  1696. ret = q6asm_enc_cfg_blk_pcm_format_support_v4(
  1697. prtd->audio_client,
  1698. prtd->sample_rate, prtd->num_channels,
  1699. bits_per_sample, sample_word_size,
  1700. ASM_LITTLE_ENDIAN, DEFAULT_QF);
  1701. }
  1702. return ret;
  1703. }
  1704. static int msm_compr_playback_open(struct snd_compr_stream *cstream)
  1705. {
  1706. struct snd_compr_runtime *runtime = cstream->runtime;
  1707. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  1708. struct snd_soc_component *component = NULL;
  1709. struct msm_compr_audio *prtd = NULL;
  1710. struct msm_compr_pdata *pdata = NULL;
  1711. enum apr_subsys_state subsys_state;
  1712. pr_debug("%s\n", __func__);
  1713. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1714. if (!component) {
  1715. pr_err("%s: component is NULL\n", __func__);
  1716. return -EINVAL;
  1717. }
  1718. pdata = snd_soc_component_get_drvdata(component);
  1719. if (pdata->is_in_use[rtd->dai_link->id] == true) {
  1720. pr_err("%s: %s is already in use, err: %d\n",
  1721. __func__, rtd->dai_link->cpus->dai_name, -EBUSY);
  1722. return -EBUSY;
  1723. }
  1724. subsys_state = apr_get_subsys_state();
  1725. if (subsys_state == APR_SUBSYS_DOWN) {
  1726. pr_debug("%s: adsp is down\n", __func__);
  1727. return -ENETRESET;
  1728. }
  1729. prtd = kzalloc(sizeof(struct msm_compr_audio), GFP_KERNEL);
  1730. if (prtd == NULL) {
  1731. pr_err("Failed to allocate memory for msm_compr_audio\n");
  1732. return -ENOMEM;
  1733. }
  1734. runtime->private_data = NULL;
  1735. prtd->cstream = cstream;
  1736. pdata->cstream[rtd->dai_link->id] = cstream;
  1737. pdata->audio_effects[rtd->dai_link->id] =
  1738. kzalloc(sizeof(struct msm_compr_audio_effects), GFP_KERNEL);
  1739. if (pdata->audio_effects[rtd->dai_link->id] == NULL) {
  1740. pr_err("%s: Could not allocate memory for effects\n", __func__);
  1741. pdata->cstream[rtd->dai_link->id] = NULL;
  1742. kfree(prtd);
  1743. return -ENOMEM;
  1744. }
  1745. pdata->dec_params[rtd->dai_link->id] =
  1746. kzalloc(sizeof(struct msm_compr_dec_params), GFP_KERNEL);
  1747. if (pdata->dec_params[rtd->dai_link->id] == NULL) {
  1748. pr_err("%s: Could not allocate memory for dec params\n",
  1749. __func__);
  1750. kfree(pdata->audio_effects[rtd->dai_link->id]);
  1751. pdata->audio_effects[rtd->dai_link->id] = NULL;
  1752. pdata->cstream[rtd->dai_link->id] = NULL;
  1753. kfree(prtd);
  1754. return -ENOMEM;
  1755. }
  1756. prtd->codec = FORMAT_MP3;
  1757. prtd->bytes_received = 0;
  1758. prtd->bytes_sent = 0;
  1759. prtd->copied_total = 0;
  1760. prtd->byte_offset = 0;
  1761. prtd->sample_rate = 44100;
  1762. prtd->num_channels = 2;
  1763. prtd->drain_ready = 0;
  1764. prtd->last_buffer = 0;
  1765. prtd->first_buffer = 1;
  1766. prtd->partial_drain_delay = 0;
  1767. prtd->next_stream = 0;
  1768. memset(&prtd->gapless_state, 0, sizeof(struct msm_compr_gapless_state));
  1769. /*
  1770. * Update the use_dsp_gapless_mode from gapless struture with the value
  1771. * part of platform data.
  1772. */
  1773. prtd->gapless_state.use_dsp_gapless_mode = pdata->use_dsp_gapless_mode;
  1774. pr_debug("%s: gapless mode %d", __func__, pdata->use_dsp_gapless_mode);
  1775. spin_lock_init(&prtd->lock);
  1776. atomic_set(&prtd->eos, 0);
  1777. atomic_set(&prtd->start, 0);
  1778. atomic_set(&prtd->drain, 0);
  1779. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  1780. atomic_set(&prtd->partial_drain, 0);
  1781. #endif
  1782. atomic_set(&prtd->xrun, 0);
  1783. atomic_set(&prtd->close, 0);
  1784. atomic_set(&prtd->wait_on_close, 0);
  1785. atomic_set(&prtd->error, 0);
  1786. init_waitqueue_head(&prtd->eos_wait);
  1787. init_waitqueue_head(&prtd->drain_wait);
  1788. init_waitqueue_head(&prtd->close_wait);
  1789. init_waitqueue_head(&prtd->wait_for_stream_avail);
  1790. runtime->private_data = prtd;
  1791. populate_codec_list(prtd);
  1792. prtd->audio_client = q6asm_audio_client_alloc(
  1793. (app_cb)compr_event_handler, prtd);
  1794. if (prtd->audio_client == NULL) {
  1795. pr_err("%s: Could not allocate memory for client\n", __func__);
  1796. kfree(pdata->audio_effects[rtd->dai_link->id]);
  1797. pdata->audio_effects[rtd->dai_link->id] = NULL;
  1798. kfree(pdata->dec_params[rtd->dai_link->id]);
  1799. pdata->dec_params[rtd->dai_link->id] = NULL;
  1800. pdata->cstream[rtd->dai_link->id] = NULL;
  1801. kfree(prtd);
  1802. runtime->private_data = NULL;
  1803. return -ENOMEM;
  1804. }
  1805. pr_debug("%s: session ID %d\n", __func__, prtd->audio_client->session);
  1806. prtd->audio_client->perf_mode = false;
  1807. prtd->session_id = prtd->audio_client->session;
  1808. msm_adsp_init_mixer_ctl_pp_event_queue(rtd);
  1809. pdata->is_in_use[rtd->dai_link->id] = true;
  1810. return 0;
  1811. }
  1812. static int msm_compr_capture_open(struct snd_compr_stream *cstream)
  1813. {
  1814. struct snd_compr_runtime *runtime = cstream->runtime;
  1815. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  1816. struct snd_soc_component *component = NULL;
  1817. struct msm_compr_audio *prtd;
  1818. struct msm_compr_pdata *pdata = NULL;
  1819. enum apr_subsys_state subsys_state;
  1820. pr_debug("%s\n", __func__);
  1821. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1822. if (!component) {
  1823. pr_err("%s: component is NULL\n", __func__);
  1824. return -EINVAL;
  1825. }
  1826. pdata = snd_soc_component_get_drvdata(component);
  1827. if (!pdata) {
  1828. pr_err("%s: pdata is NULL\n", __func__);
  1829. return -EINVAL;
  1830. }
  1831. subsys_state = apr_get_subsys_state();
  1832. if (subsys_state == APR_SUBSYS_DOWN) {
  1833. pr_debug("%s: adsp is down\n", __func__);
  1834. return -ENETRESET;
  1835. }
  1836. prtd = kzalloc(sizeof(struct msm_compr_audio), GFP_KERNEL);
  1837. if (!prtd) {
  1838. pr_err("Failed to allocate memory for msm_compr_audio\n");
  1839. return -ENOMEM;
  1840. }
  1841. runtime->private_data = NULL;
  1842. prtd->cstream = cstream;
  1843. pdata->cstream[rtd->dai_link->id] = cstream;
  1844. prtd->audio_client = q6asm_audio_client_alloc(
  1845. (app_cb)compr_event_handler, prtd);
  1846. if (!prtd->audio_client) {
  1847. pr_err("%s: Could not allocate memory for client\n", __func__);
  1848. pdata->cstream[rtd->dai_link->id] = NULL;
  1849. kfree(prtd);
  1850. return -ENOMEM;
  1851. }
  1852. pr_debug("%s: session ID %d\n", __func__, prtd->audio_client->session);
  1853. prtd->audio_client->perf_mode = false;
  1854. prtd->session_id = prtd->audio_client->session;
  1855. prtd->codec = FORMAT_LINEAR_PCM;
  1856. prtd->bytes_copied = 0;
  1857. prtd->bytes_read = 0;
  1858. prtd->bytes_read_offset = 0;
  1859. prtd->received_total = 0;
  1860. prtd->byte_offset = 0;
  1861. prtd->sample_rate = 48000;
  1862. prtd->num_channels = 2;
  1863. prtd->first_buffer = 0;
  1864. spin_lock_init(&prtd->lock);
  1865. atomic_set(&prtd->eos, 0);
  1866. atomic_set(&prtd->start, 0);
  1867. atomic_set(&prtd->drain, 0);
  1868. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  1869. atomic_set(&prtd->partial_drain, 0);
  1870. #endif
  1871. atomic_set(&prtd->xrun, 0);
  1872. atomic_set(&prtd->close, 0);
  1873. atomic_set(&prtd->wait_on_close, 0);
  1874. atomic_set(&prtd->error, 0);
  1875. init_waitqueue_head(&prtd->eos_wait);
  1876. init_waitqueue_head(&prtd->drain_wait);
  1877. init_waitqueue_head(&prtd->close_wait);
  1878. init_waitqueue_head(&prtd->wait_for_stream_avail);
  1879. runtime->private_data = prtd;
  1880. return 0;
  1881. }
  1882. static int msm_compr_open(struct snd_compr_stream *cstream)
  1883. {
  1884. int ret = 0;
  1885. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  1886. ret = msm_compr_playback_open(cstream);
  1887. else if (cstream->direction == SND_COMPRESS_CAPTURE)
  1888. ret = msm_compr_capture_open(cstream);
  1889. return ret;
  1890. }
  1891. static int msm_compr_playback_free(struct snd_compr_stream *cstream)
  1892. {
  1893. struct snd_compr_runtime *runtime;
  1894. struct msm_compr_audio *prtd;
  1895. struct snd_soc_pcm_runtime *soc_prtd;
  1896. struct snd_soc_component *component = NULL;
  1897. struct msm_compr_pdata *pdata;
  1898. struct audio_client *ac;
  1899. int dir = IN, ret = 0, stream_id;
  1900. unsigned long flags;
  1901. uint32_t stream_index;
  1902. pr_debug("%s\n", __func__);
  1903. if (!cstream) {
  1904. pr_err("%s cstream is null\n", __func__);
  1905. return 0;
  1906. }
  1907. runtime = cstream->runtime;
  1908. soc_prtd = cstream->private_data;
  1909. if (!runtime || !soc_prtd) {
  1910. pr_err("%s runtime or soc_prtd is null\n",
  1911. __func__);
  1912. return 0;
  1913. }
  1914. component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  1915. if (!component) {
  1916. pr_err("%s component is null\n", __func__);
  1917. return 0;
  1918. }
  1919. prtd = runtime->private_data;
  1920. if (!prtd) {
  1921. pr_err("%s prtd is null\n", __func__);
  1922. return 0;
  1923. }
  1924. prtd->cmd_interrupt = 1;
  1925. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1926. wake_up(&prtd->drain_wait);
  1927. #endif
  1928. pdata = snd_soc_component_get_drvdata(component);
  1929. ac = prtd->audio_client;
  1930. if (!pdata || !ac) {
  1931. pr_err("%s pdata or ac is null\n", __func__);
  1932. return 0;
  1933. }
  1934. if (atomic_read(&prtd->eos)) {
  1935. ret = wait_event_timeout(prtd->eos_wait,
  1936. prtd->eos_ack,
  1937. msecs_to_jiffies(TIMEOUT_MS));
  1938. if (!ret)
  1939. pr_err("%s: CMD_EOS failed\n", __func__);
  1940. }
  1941. if (atomic_read(&prtd->close)) {
  1942. prtd->cmd_ack = 0;
  1943. atomic_set(&prtd->wait_on_close, 1);
  1944. ret = wait_event_timeout(prtd->close_wait,
  1945. prtd->cmd_ack,
  1946. msecs_to_jiffies(TIMEOUT_MS));
  1947. if (!ret)
  1948. pr_err("%s: CMD_CLOSE failed\n", __func__);
  1949. }
  1950. spin_lock_irqsave(&prtd->lock, flags);
  1951. stream_id = ac->stream_id;
  1952. stream_index = STREAM_ARRAY_INDEX(NEXT_STREAM_ID(stream_id));
  1953. if ((stream_index < MAX_NUMBER_OF_STREAMS && stream_index >= 0) &&
  1954. (prtd->gapless_state.stream_opened[stream_index])) {
  1955. prtd->gapless_state.stream_opened[stream_index] = 0;
  1956. spin_unlock_irqrestore(&prtd->lock, flags);
  1957. pr_debug(" close stream %d", NEXT_STREAM_ID(stream_id));
  1958. q6asm_stream_cmd(ac, CMD_CLOSE, NEXT_STREAM_ID(stream_id));
  1959. spin_lock_irqsave(&prtd->lock, flags);
  1960. }
  1961. stream_index = STREAM_ARRAY_INDEX(stream_id);
  1962. if ((stream_index < MAX_NUMBER_OF_STREAMS && stream_index >= 0) &&
  1963. (prtd->gapless_state.stream_opened[stream_index])) {
  1964. prtd->gapless_state.stream_opened[stream_index] = 0;
  1965. spin_unlock_irqrestore(&prtd->lock, flags);
  1966. pr_debug("close stream %d", stream_id);
  1967. q6asm_stream_cmd(ac, CMD_CLOSE, stream_id);
  1968. spin_lock_irqsave(&prtd->lock, flags);
  1969. }
  1970. spin_unlock_irqrestore(&prtd->lock, flags);
  1971. mutex_lock(&pdata->lock);
  1972. pdata->cstream[soc_prtd->dai_link->id] = NULL;
  1973. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  1974. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  1975. SNDRV_PCM_STREAM_PLAYBACK);
  1976. }
  1977. q6asm_audio_client_buf_free_contiguous(dir, ac);
  1978. q6asm_audio_client_free(ac);
  1979. msm_adsp_clean_mixer_ctl_pp_event_queue(soc_prtd);
  1980. if (pdata->audio_effects[soc_prtd->dai_link->id] != NULL) {
  1981. kfree(pdata->audio_effects[soc_prtd->dai_link->id]);
  1982. pdata->audio_effects[soc_prtd->dai_link->id] = NULL;
  1983. }
  1984. if (pdata->dec_params[soc_prtd->dai_link->id] != NULL) {
  1985. kfree(pdata->dec_params[soc_prtd->dai_link->id]);
  1986. pdata->dec_params[soc_prtd->dai_link->id] = NULL;
  1987. }
  1988. if (pdata->ch_map[soc_prtd->dai_link->id]) {
  1989. pdata->ch_map[soc_prtd->dai_link->id]->set_ch_map = false;
  1990. }
  1991. pdata->is_in_use[soc_prtd->dai_link->id] = false;
  1992. kfree(prtd);
  1993. runtime->private_data = NULL;
  1994. mutex_unlock(&pdata->lock);
  1995. return 0;
  1996. }
  1997. static int msm_compr_capture_free(struct snd_compr_stream *cstream)
  1998. {
  1999. struct snd_compr_runtime *runtime;
  2000. struct msm_compr_audio *prtd;
  2001. struct snd_soc_pcm_runtime *soc_prtd;
  2002. struct snd_soc_component *component = NULL;
  2003. struct msm_compr_pdata *pdata;
  2004. struct audio_client *ac;
  2005. int dir = OUT, stream_id;
  2006. unsigned long flags;
  2007. uint32_t stream_index;
  2008. if (!cstream) {
  2009. pr_err("%s cstream is null\n", __func__);
  2010. return 0;
  2011. }
  2012. runtime = cstream->runtime;
  2013. soc_prtd = cstream->private_data;
  2014. if (!runtime || !soc_prtd) {
  2015. pr_err("%s runtime or soc_prtd is null\n", __func__);
  2016. return 0;
  2017. }
  2018. component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  2019. if (!component) {
  2020. pr_err("%s component is null\n", __func__);
  2021. return 0;
  2022. }
  2023. prtd = runtime->private_data;
  2024. if (!prtd) {
  2025. pr_err("%s prtd is null\n", __func__);
  2026. return 0;
  2027. }
  2028. pdata = snd_soc_component_get_drvdata(component);
  2029. ac = prtd->audio_client;
  2030. if (!pdata || !ac) {
  2031. pr_err("%s pdata or ac is null\n", __func__);
  2032. return 0;
  2033. }
  2034. spin_lock_irqsave(&prtd->lock, flags);
  2035. stream_id = ac->stream_id;
  2036. stream_index = STREAM_ARRAY_INDEX(stream_id);
  2037. if ((stream_index < MAX_NUMBER_OF_STREAMS && stream_index >= 0)) {
  2038. spin_unlock_irqrestore(&prtd->lock, flags);
  2039. pr_debug("close stream %d", stream_id);
  2040. q6asm_stream_cmd(ac, CMD_CLOSE, stream_id);
  2041. spin_lock_irqsave(&prtd->lock, flags);
  2042. }
  2043. spin_unlock_irqrestore(&prtd->lock, flags);
  2044. mutex_lock(&pdata->lock);
  2045. pdata->cstream[soc_prtd->dai_link->id] = NULL;
  2046. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  2047. SNDRV_PCM_STREAM_CAPTURE);
  2048. q6asm_audio_client_buf_free_contiguous(dir, ac);
  2049. q6asm_audio_client_free(ac);
  2050. kfree(prtd);
  2051. runtime->private_data = NULL;
  2052. mutex_unlock(&pdata->lock);
  2053. return 0;
  2054. }
  2055. static int msm_compr_free(struct snd_compr_stream *cstream)
  2056. {
  2057. int ret = 0;
  2058. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  2059. ret = msm_compr_playback_free(cstream);
  2060. else if (cstream->direction == SND_COMPRESS_CAPTURE)
  2061. ret = msm_compr_capture_free(cstream);
  2062. return ret;
  2063. }
  2064. static bool msm_compr_validate_codec_compr(__u32 codec_id)
  2065. {
  2066. int32_t i;
  2067. for (i = 0; i < ARRAY_SIZE(compr_codecs); i++) {
  2068. if (compr_codecs[i] == codec_id)
  2069. return true;
  2070. }
  2071. return false;
  2072. }
  2073. /* compress stream operations */
  2074. static int msm_compr_set_params(struct snd_compr_stream *cstream,
  2075. struct snd_compr_params *params)
  2076. {
  2077. struct snd_compr_runtime *runtime = cstream->runtime;
  2078. struct msm_compr_audio *prtd = runtime->private_data;
  2079. int ret = 0, frame_sz = 0;
  2080. int i, num_rates;
  2081. bool is_format_gapless = false;
  2082. pr_debug("%s\n", __func__);
  2083. num_rates = sizeof(supported_sample_rates)/sizeof(unsigned int);
  2084. for (i = 0; i < num_rates; i++)
  2085. if (params->codec.sample_rate == supported_sample_rates[i])
  2086. break;
  2087. if (i == num_rates)
  2088. return -EINVAL;
  2089. memcpy(&prtd->codec_param, params, sizeof(struct snd_compr_params));
  2090. /* ToDo: remove duplicates */
  2091. prtd->num_channels = prtd->codec_param.codec.ch_in;
  2092. prtd->sample_rate = prtd->codec_param.codec.sample_rate;
  2093. pr_debug("%s: sample_rate %d\n", __func__, prtd->sample_rate);
  2094. /* prtd->codec_param.codec.reserved[0] is for compr_passthr */
  2095. if ((prtd->codec_param.codec.reserved[0] >= LEGACY_PCM &&
  2096. prtd->codec_param.
  2097. codec.reserved[0] <= COMPRESSED_PASSTHROUGH_DSD) ||
  2098. (prtd->codec_param.
  2099. codec.reserved[0] == COMPRESSED_PASSTHROUGH_IEC61937))
  2100. prtd->compr_passthr = prtd->codec_param.codec.reserved[0];
  2101. else
  2102. prtd->compr_passthr = LEGACY_PCM;
  2103. pr_debug("%s: compr_passthr = %d", __func__, prtd->compr_passthr);
  2104. if (prtd->compr_passthr != LEGACY_PCM) {
  2105. pr_debug("%s: Reset gapless mode playback for compr_type[%d]\n",
  2106. __func__, prtd->compr_passthr);
  2107. prtd->gapless_state.use_dsp_gapless_mode = 0;
  2108. if (!msm_compr_validate_codec_compr(params->codec.id)) {
  2109. pr_err("%s codec not supported in passthrough,id =%d\n",
  2110. __func__, params->codec.id);
  2111. return -EINVAL;
  2112. }
  2113. }
  2114. /* reserved[1] is for flags */
  2115. if (params->codec.reserved[1] & COMPRESSED_PERF_MODE_FLAG) {
  2116. pr_debug("%s: setting perf mode = %d", __func__, LOW_LATENCY_PCM_MODE);
  2117. prtd->audio_client->perf_mode = LOW_LATENCY_PCM_MODE;
  2118. }
  2119. switch (params->codec.id) {
  2120. case SND_AUDIOCODEC_PCM: {
  2121. pr_debug("SND_AUDIOCODEC_PCM\n");
  2122. prtd->codec = FORMAT_LINEAR_PCM;
  2123. is_format_gapless = true;
  2124. break;
  2125. }
  2126. case SND_AUDIOCODEC_MP3: {
  2127. pr_debug("SND_AUDIOCODEC_MP3\n");
  2128. prtd->codec = FORMAT_MP3;
  2129. frame_sz = MP3_OUTPUT_FRAME_SZ;
  2130. is_format_gapless = true;
  2131. break;
  2132. }
  2133. case SND_AUDIOCODEC_AAC: {
  2134. pr_debug("SND_AUDIOCODEC_AAC\n");
  2135. prtd->codec = FORMAT_MPEG4_AAC;
  2136. frame_sz = AAC_OUTPUT_FRAME_SZ;
  2137. is_format_gapless = true;
  2138. break;
  2139. }
  2140. case SND_AUDIOCODEC_AC3: {
  2141. pr_debug("SND_AUDIOCODEC_AC3\n");
  2142. prtd->codec = FORMAT_AC3;
  2143. frame_sz = AC3_OUTPUT_FRAME_SZ;
  2144. is_format_gapless = true;
  2145. break;
  2146. }
  2147. case SND_AUDIOCODEC_EAC3: {
  2148. pr_debug("SND_AUDIOCODEC_EAC3\n");
  2149. prtd->codec = FORMAT_EAC3;
  2150. frame_sz = EAC3_OUTPUT_FRAME_SZ;
  2151. is_format_gapless = true;
  2152. break;
  2153. }
  2154. case SND_AUDIOCODEC_MP2: {
  2155. pr_debug("SND_AUDIOCODEC_MP2\n");
  2156. prtd->codec = FORMAT_MP2;
  2157. break;
  2158. }
  2159. case SND_AUDIOCODEC_DTS: {
  2160. pr_debug("%s: SND_AUDIOCODEC_DTS\n", __func__);
  2161. prtd->codec = FORMAT_DTS;
  2162. break;
  2163. }
  2164. case SND_AUDIOCODEC_TRUEHD: {
  2165. pr_debug("%s: SND_AUDIOCODEC_TRUEHD\n", __func__);
  2166. prtd->codec = FORMAT_TRUEHD;
  2167. break;
  2168. }
  2169. case SND_AUDIOCODEC_IEC61937: {
  2170. pr_debug("%s: SND_AUDIOCODEC_IEC61937\n", __func__);
  2171. prtd->codec = FORMAT_IEC61937;
  2172. break;
  2173. }
  2174. case SND_AUDIOCODEC_BESPOKE: {
  2175. pr_debug("%s: SND_AUDIOCODEC_BESPOKE\n", __func__);
  2176. ret = msm_compr_get_decoder_format(prtd, &frame_sz,
  2177. &is_format_gapless);
  2178. if (ret < 0)
  2179. return ret;
  2180. prtd->codec = ret;
  2181. break;
  2182. }
  2183. case SND_AUDIOCODEC_AMR: {
  2184. pr_debug("%s:SND_AUDIOCODEC_AMR\n", __func__);
  2185. prtd->codec = FORMAT_AMRNB;
  2186. break;
  2187. }
  2188. case SND_AUDIOCODEC_AMRWB: {
  2189. pr_debug("%s:SND_AUDIOCODEC_AMRWB\n", __func__);
  2190. prtd->codec = FORMAT_AMRWB;
  2191. break;
  2192. }
  2193. case SND_AUDIOCODEC_AMRWBPLUS: {
  2194. pr_debug("%s:SND_AUDIOCODEC_AMRWBPLUS\n", __func__);
  2195. prtd->codec = FORMAT_AMR_WB_PLUS;
  2196. break;
  2197. }
  2198. default:
  2199. pr_err("codec not supported, id =%d\n", params->codec.id);
  2200. return -EINVAL;
  2201. }
  2202. if (!is_format_gapless)
  2203. prtd->gapless_state.use_dsp_gapless_mode = false;
  2204. prtd->partial_drain_delay =
  2205. msm_compr_get_partial_drain_delay(frame_sz, prtd->sample_rate);
  2206. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  2207. ret = msm_compr_configure_dsp_for_playback(cstream);
  2208. else if (cstream->direction == SND_COMPRESS_CAPTURE)
  2209. ret = msm_compr_configure_dsp_for_capture(cstream);
  2210. return ret;
  2211. }
  2212. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2213. static int msm_compr_drain_buffer(struct msm_compr_audio *prtd,
  2214. unsigned long *flags)
  2215. {
  2216. int rc = 0;
  2217. atomic_set(&prtd->drain, 1);
  2218. prtd->drain_ready = 0;
  2219. spin_unlock_irqrestore(&prtd->lock, *flags);
  2220. pr_debug("%s: wait for buffer to be drained\n", __func__);
  2221. rc = wait_event_interruptible(prtd->drain_wait,
  2222. prtd->drain_ready ||
  2223. prtd->cmd_interrupt ||
  2224. atomic_read(&prtd->xrun) ||
  2225. atomic_read(&prtd->error));
  2226. pr_debug("%s: out of buffer drain wait with ret %d\n", __func__, rc);
  2227. spin_lock_irqsave(&prtd->lock, *flags);
  2228. if (prtd->cmd_interrupt) {
  2229. pr_debug("%s: buffer drain interrupted by flush)\n", __func__);
  2230. rc = -EINTR;
  2231. prtd->cmd_interrupt = 0;
  2232. }
  2233. if (atomic_read(&prtd->error)) {
  2234. pr_err("%s: Got RESET EVENTS notification, return\n",
  2235. __func__);
  2236. rc = -ENETRESET;
  2237. }
  2238. return rc;
  2239. }
  2240. #endif
  2241. static int msm_compr_wait_for_stream_avail(struct msm_compr_audio *prtd,
  2242. unsigned long *flags)
  2243. {
  2244. int rc = 0;
  2245. pr_debug("next session is already in opened state\n");
  2246. prtd->next_stream = 1;
  2247. prtd->cmd_interrupt = 0;
  2248. spin_unlock_irqrestore(&prtd->lock, *flags);
  2249. /*
  2250. * Wait for stream to be available, or the wait to be interrupted by
  2251. * commands like flush or till a timeout of one second.
  2252. */
  2253. rc = wait_event_timeout(prtd->wait_for_stream_avail,
  2254. prtd->stream_available || prtd->cmd_interrupt, 1 * HZ);
  2255. pr_err("%s:prtd->stream_available %d, prtd->cmd_interrupt %d rc %d\n",
  2256. __func__, prtd->stream_available, prtd->cmd_interrupt, rc);
  2257. spin_lock_irqsave(&prtd->lock, *flags);
  2258. if (rc == 0) {
  2259. pr_err("%s: wait_for_stream_avail timed out\n",
  2260. __func__);
  2261. rc = -ETIMEDOUT;
  2262. } else if (prtd->cmd_interrupt == 1) {
  2263. /*
  2264. * This scenario might not happen as we do not allow
  2265. * flush in transition state.
  2266. */
  2267. pr_debug("%s: wait_for_stream_avail interrupted\n", __func__);
  2268. prtd->cmd_interrupt = 0;
  2269. prtd->stream_available = 0;
  2270. rc = -EINTR;
  2271. } else {
  2272. prtd->stream_available = 0;
  2273. rc = 0;
  2274. }
  2275. pr_debug("%s : rc = %d", __func__, rc);
  2276. return rc;
  2277. }
  2278. static int msm_compr_trigger(struct snd_compr_stream *cstream, int cmd)
  2279. {
  2280. struct snd_compr_runtime *runtime = cstream->runtime;
  2281. struct msm_compr_audio *prtd = runtime->private_data;
  2282. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  2283. struct snd_soc_component *component = NULL;
  2284. struct msm_compr_pdata *pdata = NULL;
  2285. uint32_t *volume = NULL;
  2286. struct audio_client *ac = prtd->audio_client;
  2287. unsigned long fe_id = rtd->dai_link->id;
  2288. int rc = 0;
  2289. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2290. int bytes_to_write;
  2291. #endif
  2292. unsigned long flags;
  2293. int stream_id;
  2294. uint32_t stream_index;
  2295. uint16_t bits_per_sample = 16;
  2296. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2297. if (!component) {
  2298. pr_err("%s: component is NULL\n", __func__);
  2299. return -EINVAL;
  2300. }
  2301. pdata = snd_soc_component_get_drvdata(component);
  2302. if (!pdata) {
  2303. pr_err("%s: pdata is NULL\n", __func__);
  2304. return -EINVAL;
  2305. }
  2306. volume = pdata->volume[rtd->dai_link->id];
  2307. spin_lock_irqsave(&prtd->lock, flags);
  2308. if (atomic_read(&prtd->error)) {
  2309. pr_err("%s Got RESET EVENTS notification, return immediately",
  2310. __func__);
  2311. spin_unlock_irqrestore(&prtd->lock, flags);
  2312. return 0;
  2313. }
  2314. spin_unlock_irqrestore(&prtd->lock, flags);
  2315. switch (cmd) {
  2316. case SNDRV_PCM_TRIGGER_START:
  2317. pr_debug("%s: SNDRV_PCM_TRIGGER_START\n", __func__);
  2318. atomic_set(&prtd->start, 1);
  2319. /*
  2320. * compr_set_volume and compr_init_pp_params
  2321. * are used to configure ASM volume hence not
  2322. * needed for compress passthrough playback.
  2323. *
  2324. * compress passthrough volume is controlled in
  2325. * ADM by adm_send_compressed_device_mute()
  2326. */
  2327. if (prtd->compr_passthr == LEGACY_PCM &&
  2328. cstream->direction == SND_COMPRESS_PLAYBACK) {
  2329. /* set volume for the stream before RUN */
  2330. rc = msm_compr_set_volume(cstream,
  2331. volume[0], volume[1]);
  2332. if (rc)
  2333. pr_err("%s : Set Volume failed : %d\n",
  2334. __func__, rc);
  2335. rc = msm_compr_init_pp_params(cstream, ac);
  2336. if (rc)
  2337. pr_err("%s : init PP params failed : %d\n",
  2338. __func__, rc);
  2339. } else {
  2340. msm_compr_read_buffer(prtd);
  2341. }
  2342. /* issue RUN command for the stream */
  2343. q6asm_run_nowait(prtd->audio_client, prtd->run_mode,
  2344. prtd->start_delay_msw, prtd->start_delay_lsw);
  2345. break;
  2346. case SNDRV_PCM_TRIGGER_STOP:
  2347. spin_lock_irqsave(&prtd->lock, flags);
  2348. pr_debug("%s: SNDRV_PCM_TRIGGER_STOP transition %d\n", __func__,
  2349. prtd->gapless_state.gapless_transition);
  2350. stream_id = ac->stream_id;
  2351. atomic_set(&prtd->start, 0);
  2352. if (cstream->direction == SND_COMPRESS_CAPTURE) {
  2353. q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  2354. atomic_set(&prtd->xrun, 0);
  2355. prtd->received_total = 0;
  2356. prtd->bytes_copied = 0;
  2357. prtd->bytes_read = 0;
  2358. prtd->bytes_read_offset = 0;
  2359. prtd->byte_offset = 0;
  2360. prtd->app_pointer = 0;
  2361. spin_unlock_irqrestore(&prtd->lock, flags);
  2362. break;
  2363. }
  2364. if (prtd->next_stream) {
  2365. pr_debug("%s: interrupt next track wait queues\n",
  2366. __func__);
  2367. prtd->cmd_interrupt = 1;
  2368. wake_up(&prtd->wait_for_stream_avail);
  2369. prtd->next_stream = 0;
  2370. }
  2371. if (atomic_read(&prtd->eos)) {
  2372. pr_debug("%s: interrupt eos wait queues", __func__);
  2373. /*
  2374. * Gapless playback does not wait for eos, do not set
  2375. * cmd_int and do not wake up eos_wait during gapless
  2376. * transition
  2377. */
  2378. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2379. if (!prtd->gapless_state.gapless_transition) {
  2380. prtd->cmd_interrupt = 1;
  2381. wake_up(&prtd->eos_wait);
  2382. }
  2383. #endif
  2384. atomic_set(&prtd->eos, 0);
  2385. }
  2386. if (atomic_read(&prtd->drain)) {
  2387. pr_debug("%s: interrupt drain wait queues", __func__);
  2388. prtd->cmd_interrupt = 1;
  2389. prtd->drain_ready = 1;
  2390. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2391. wake_up(&prtd->drain_wait);
  2392. #endif
  2393. atomic_set(&prtd->drain, 0);
  2394. }
  2395. prtd->last_buffer = 0;
  2396. prtd->cmd_ack = 0;
  2397. if (!prtd->gapless_state.gapless_transition) {
  2398. pr_debug("issue CMD_FLUSH stream_id %d\n", stream_id);
  2399. spin_unlock_irqrestore(&prtd->lock, flags);
  2400. q6asm_stream_cmd(
  2401. prtd->audio_client, CMD_FLUSH, stream_id);
  2402. spin_lock_irqsave(&prtd->lock, flags);
  2403. } else {
  2404. prtd->first_buffer = 0;
  2405. }
  2406. /* FIXME. only reset if flush was successful */
  2407. prtd->byte_offset = 0;
  2408. prtd->copied_total = 0;
  2409. prtd->app_pointer = 0;
  2410. prtd->bytes_received = 0;
  2411. prtd->bytes_sent = 0;
  2412. prtd->marker_timestamp = 0;
  2413. atomic_set(&prtd->xrun, 0);
  2414. spin_unlock_irqrestore(&prtd->lock, flags);
  2415. break;
  2416. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  2417. pr_debug("SNDRV_PCM_TRIGGER_PAUSE_PUSH transition %d\n",
  2418. prtd->gapless_state.gapless_transition);
  2419. if (!prtd->gapless_state.gapless_transition) {
  2420. pr_debug("issue CMD_PAUSE stream_id %d\n",
  2421. ac->stream_id);
  2422. q6asm_stream_cmd_nowait(ac, CMD_PAUSE, ac->stream_id);
  2423. atomic_set(&prtd->start, 0);
  2424. }
  2425. break;
  2426. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  2427. pr_debug("SNDRV_PCM_TRIGGER_PAUSE_RELEASE transition %d\n",
  2428. prtd->gapless_state.gapless_transition);
  2429. if (!prtd->gapless_state.gapless_transition) {
  2430. atomic_set(&prtd->start, 1);
  2431. q6asm_run_nowait(prtd->audio_client, prtd->run_mode,
  2432. 0, 0);
  2433. }
  2434. break;
  2435. case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
  2436. pr_debug("%s: SND_COMPR_TRIGGER_PARTIAL_DRAIN\n", __func__);
  2437. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  2438. spin_lock_irqsave(&prtd->lock, flags);
  2439. atomic_set(&prtd->partial_drain, 1);
  2440. #endif
  2441. if (!prtd->gapless_state.use_dsp_gapless_mode) {
  2442. pr_debug("%s: set partial drain as drain\n", __func__);
  2443. cmd = SND_COMPR_TRIGGER_DRAIN;
  2444. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  2445. atomic_set(&prtd->partial_drain, 0);
  2446. #endif
  2447. }
  2448. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  2449. spin_unlock_irqrestore(&prtd->lock, flags);
  2450. #endif
  2451. case SND_COMPR_TRIGGER_DRAIN:
  2452. pr_debug("%s: SNDRV_COMPRESS_DRAIN\n", __func__);
  2453. /* Make sure all the data is sent to DSP before sending EOS */
  2454. spin_lock_irqsave(&prtd->lock, flags);
  2455. if (!atomic_read(&prtd->start)) {
  2456. pr_err("%s: stream is not in started state\n",
  2457. __func__);
  2458. rc = -EPERM;
  2459. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  2460. atomic_set(&prtd->partial_drain, 0);
  2461. #endif
  2462. spin_unlock_irqrestore(&prtd->lock, flags);
  2463. break;
  2464. }
  2465. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2466. if (prtd->bytes_received > prtd->copied_total) {
  2467. pr_debug("%s: wait till all the data is sent to dsp\n",
  2468. __func__);
  2469. rc = msm_compr_drain_buffer(prtd, &flags);
  2470. if (rc || !atomic_read(&prtd->start)) {
  2471. if (rc != -ENETRESET)
  2472. rc = -EINTR;
  2473. spin_unlock_irqrestore(&prtd->lock, flags);
  2474. break;
  2475. }
  2476. /*
  2477. * FIXME: Bug.
  2478. * Write(32767)
  2479. * Start
  2480. * Drain <- Indefinite wait
  2481. * sol1 : if (prtd->copied_total) then wait?
  2482. * sol2 : (prtd->cmd_interrupt || prtd->drain_ready ||
  2483. * atomic_read(xrun)
  2484. */
  2485. bytes_to_write = prtd->bytes_received
  2486. - prtd->copied_total;
  2487. WARN(bytes_to_write > runtime->fragment_size,
  2488. "last write %d cannot be > than fragment_size",
  2489. bytes_to_write);
  2490. if (bytes_to_write > 0) {
  2491. pr_debug("%s: send %d partial bytes at the end",
  2492. __func__, bytes_to_write);
  2493. atomic_set(&prtd->xrun, 0);
  2494. prtd->last_buffer = 1;
  2495. msm_compr_send_buffer(prtd);
  2496. }
  2497. }
  2498. if ((cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN) &&
  2499. (prtd->gapless_state.set_next_stream_id)) {
  2500. /* wait for the last buffer to be returned */
  2501. if (prtd->last_buffer) {
  2502. pr_debug("%s: last buffer drain\n", __func__);
  2503. rc = msm_compr_drain_buffer(prtd, &flags);
  2504. if (rc || !atomic_read(&prtd->start)) {
  2505. spin_unlock_irqrestore(&prtd->lock,
  2506. flags);
  2507. break;
  2508. }
  2509. }
  2510. /* send EOS */
  2511. prtd->eos_ack = 0;
  2512. atomic_set(&prtd->eos, 1);
  2513. pr_debug("issue CMD_EOS stream_id %d\n", ac->stream_id);
  2514. q6asm_stream_cmd_nowait(ac, CMD_EOS, ac->stream_id);
  2515. pr_info("PARTIAL DRAIN, do not wait for EOS ack\n");
  2516. /* send a zero length buffer */
  2517. atomic_set(&prtd->xrun, 0);
  2518. msm_compr_send_buffer(prtd);
  2519. /* wait for the zero length buffer to be returned */
  2520. pr_debug("%s: zero length buffer drain\n", __func__);
  2521. rc = msm_compr_drain_buffer(prtd, &flags);
  2522. if (rc || !atomic_read(&prtd->start)) {
  2523. spin_unlock_irqrestore(&prtd->lock, flags);
  2524. break;
  2525. }
  2526. /* sleep for additional duration partial drain */
  2527. atomic_set(&prtd->drain, 1);
  2528. prtd->drain_ready = 0;
  2529. pr_debug("%s, additional sleep: %d\n", __func__,
  2530. prtd->partial_drain_delay);
  2531. spin_unlock_irqrestore(&prtd->lock, flags);
  2532. rc = wait_event_timeout(prtd->drain_wait,
  2533. prtd->drain_ready || prtd->cmd_interrupt,
  2534. msecs_to_jiffies(prtd->partial_drain_delay));
  2535. pr_debug("%s: out of additional wait for low sample rate\n",
  2536. __func__);
  2537. spin_lock_irqsave(&prtd->lock, flags);
  2538. if (prtd->cmd_interrupt) {
  2539. pr_debug("%s: additional wait interrupted by flush)\n",
  2540. __func__);
  2541. rc = -EINTR;
  2542. prtd->cmd_interrupt = 0;
  2543. spin_unlock_irqrestore(&prtd->lock, flags);
  2544. break;
  2545. }
  2546. /* move to next stream and reset vars */
  2547. pr_debug("%s: Moving to next stream in gapless\n",
  2548. __func__);
  2549. ac->stream_id = NEXT_STREAM_ID(ac->stream_id);
  2550. prtd->byte_offset = 0;
  2551. prtd->app_pointer = 0;
  2552. prtd->first_buffer = 1;
  2553. prtd->last_buffer = 0;
  2554. /*
  2555. * Set gapless transition flag only if EOS hasn't been
  2556. * acknowledged already.
  2557. */
  2558. if (atomic_read(&prtd->eos))
  2559. prtd->gapless_state.gapless_transition = 1;
  2560. prtd->marker_timestamp = 0;
  2561. /*
  2562. * Don't reset these as these vars map to
  2563. * total_bytes_transferred and total_bytes_available
  2564. * directly, only total_bytes_transferred will be
  2565. * updated in the next avail() ioctl
  2566. * prtd->copied_total = 0;
  2567. * prtd->bytes_received = 0;
  2568. */
  2569. atomic_set(&prtd->drain, 0);
  2570. atomic_set(&prtd->xrun, 1);
  2571. pr_debug("%s: issue CMD_RUN", __func__);
  2572. q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  2573. spin_unlock_irqrestore(&prtd->lock, flags);
  2574. break;
  2575. }
  2576. /*
  2577. * moving to next stream failed, so reset the gapless state
  2578. * set next stream id for the same session so that the same
  2579. * stream can be used for gapless playback
  2580. */
  2581. prtd->gapless_state.set_next_stream_id = false;
  2582. prtd->gapless_state.gapless_transition = 0;
  2583. pr_debug("%s:CMD_EOS stream_id %d\n", __func__, ac->stream_id);
  2584. prtd->eos_ack = 0;
  2585. atomic_set(&prtd->eos, 1);
  2586. q6asm_stream_cmd_nowait(ac, CMD_EOS, ac->stream_id);
  2587. spin_unlock_irqrestore(&prtd->lock, flags);
  2588. /* Wait indefinitely for DRAIN. Flush can also signal this*/
  2589. rc = wait_event_interruptible(prtd->eos_wait,
  2590. (prtd->eos_ack ||
  2591. prtd->cmd_interrupt ||
  2592. atomic_read(&prtd->error)));
  2593. if (rc < 0)
  2594. pr_err("%s: EOS wait failed\n", __func__);
  2595. pr_debug("%s: SNDRV_COMPRESS_DRAIN out of wait for EOS\n",
  2596. __func__);
  2597. if (prtd->cmd_interrupt)
  2598. rc = -EINTR;
  2599. if (atomic_read(&prtd->error)) {
  2600. pr_err("%s: Got RESET EVENTS notification, return\n",
  2601. __func__);
  2602. rc = -ENETRESET;
  2603. }
  2604. /*FIXME : what if a flush comes while PC is here */
  2605. if (rc == 0) {
  2606. /*
  2607. * Failed to open second stream in DSP for gapless
  2608. * so prepare the current stream in session
  2609. * for gapless playback
  2610. */
  2611. spin_lock_irqsave(&prtd->lock, flags);
  2612. pr_debug("%s:issue CMD_PAUSE stream_id %d",
  2613. __func__, ac->stream_id);
  2614. q6asm_stream_cmd_nowait(ac, CMD_PAUSE, ac->stream_id);
  2615. prtd->cmd_ack = 0;
  2616. spin_unlock_irqrestore(&prtd->lock, flags);
  2617. /*
  2618. * Cache this time as last known time
  2619. */
  2620. if (pdata->use_legacy_api)
  2621. q6asm_get_session_time_legacy(
  2622. prtd->audio_client,
  2623. &prtd->marker_timestamp);
  2624. else
  2625. q6asm_get_session_time(prtd->audio_client,
  2626. &prtd->marker_timestamp);
  2627. spin_lock_irqsave(&prtd->lock, flags);
  2628. /*
  2629. * Don't reset these as these vars map to
  2630. * total_bytes_transferred and total_bytes_available.
  2631. * Just total_bytes_transferred will be updated
  2632. * in the next avail() ioctl.
  2633. * prtd->copied_total = 0;
  2634. * prtd->bytes_received = 0;
  2635. * do not reset prtd->bytes_sent as well as the same
  2636. * session is used for gapless playback
  2637. */
  2638. prtd->byte_offset = 0;
  2639. prtd->app_pointer = 0;
  2640. prtd->first_buffer = 1;
  2641. prtd->last_buffer = 0;
  2642. atomic_set(&prtd->drain, 0);
  2643. atomic_set(&prtd->xrun, 1);
  2644. spin_unlock_irqrestore(&prtd->lock, flags);
  2645. pr_debug("%s:issue CMD_FLUSH ac->stream_id %d",
  2646. __func__, ac->stream_id);
  2647. q6asm_stream_cmd(ac, CMD_FLUSH, ac->stream_id);
  2648. q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  2649. }
  2650. #else
  2651. atomic_set(&prtd->drain, 1);
  2652. spin_unlock_irqrestore(&prtd->lock, flags);
  2653. #endif
  2654. prtd->cmd_interrupt = 0;
  2655. break;
  2656. case SND_COMPR_TRIGGER_NEXT_TRACK:
  2657. if (!prtd->gapless_state.use_dsp_gapless_mode) {
  2658. pr_debug("%s: ignore trigger next track\n", __func__);
  2659. rc = 0;
  2660. break;
  2661. }
  2662. pr_debug("%s: SND_COMPR_TRIGGER_NEXT_TRACK\n", __func__);
  2663. spin_lock_irqsave(&prtd->lock, flags);
  2664. rc = 0;
  2665. /* next stream in gapless */
  2666. stream_id = NEXT_STREAM_ID(ac->stream_id);
  2667. /*
  2668. * Wait if stream 1 has not completed before honoring next
  2669. * track for stream 3. Scenario happens if second clip is
  2670. * small and fills in one buffer so next track will be
  2671. * called immediately.
  2672. */
  2673. stream_index = STREAM_ARRAY_INDEX(stream_id);
  2674. if (stream_index >= MAX_NUMBER_OF_STREAMS ||
  2675. stream_index < 0) {
  2676. pr_err("%s: Invalid stream index: %d", __func__,
  2677. stream_index);
  2678. spin_unlock_irqrestore(&prtd->lock, flags);
  2679. rc = -EINVAL;
  2680. break;
  2681. }
  2682. if (prtd->gapless_state.stream_opened[stream_index]) {
  2683. if (prtd->gapless_state.gapless_transition) {
  2684. rc = msm_compr_wait_for_stream_avail(prtd,
  2685. &flags);
  2686. } else {
  2687. /*
  2688. * If session is already opened break out if
  2689. * the state is not gapless transition. This
  2690. * is when seek happens after the last buffer
  2691. * is sent to the driver. Next track would be
  2692. * called again after last buffer is sent.
  2693. */
  2694. pr_debug("next session is in opened state\n");
  2695. spin_unlock_irqrestore(&prtd->lock, flags);
  2696. break;
  2697. }
  2698. }
  2699. spin_unlock_irqrestore(&prtd->lock, flags);
  2700. if (rc < 0) {
  2701. /*
  2702. * if return type EINTR then reset to zero. Tiny
  2703. * compress treats EINTR as error and prevents PARTIAL
  2704. * DRAIN. EINTR is not an error. wait for stream avail
  2705. * is interrupted by some other command like FLUSH.
  2706. */
  2707. if (rc == -EINTR) {
  2708. pr_debug("%s: EINTR reset rc to 0\n", __func__);
  2709. rc = 0;
  2710. }
  2711. break;
  2712. }
  2713. if (prtd->codec_param.codec.format == SNDRV_PCM_FORMAT_S24_LE)
  2714. bits_per_sample = 24;
  2715. else if (prtd->codec_param.codec.format ==
  2716. SNDRV_PCM_FORMAT_S32_LE)
  2717. bits_per_sample = 32;
  2718. pr_debug("%s: open_write stream_id %d bits_per_sample %d",
  2719. __func__, stream_id, bits_per_sample);
  2720. if (q6core_get_avcs_api_version_per_service(
  2721. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  2722. ADSP_ASM_API_VERSION_V2)
  2723. rc = q6asm_stream_open_write_v5(prtd->audio_client,
  2724. prtd->codec, bits_per_sample,
  2725. stream_id,
  2726. prtd->gapless_state.use_dsp_gapless_mode);
  2727. else
  2728. rc = q6asm_stream_open_write_v4(prtd->audio_client,
  2729. prtd->codec, bits_per_sample,
  2730. stream_id,
  2731. prtd->gapless_state.use_dsp_gapless_mode);
  2732. if (rc < 0) {
  2733. pr_err("%s: Session out open failed for gapless [%d]\n",
  2734. __func__, rc);
  2735. break;
  2736. }
  2737. spin_lock_irqsave(&prtd->lock, flags);
  2738. prtd->gapless_state.stream_opened[stream_index] = 1;
  2739. prtd->gapless_state.set_next_stream_id = true;
  2740. spin_unlock_irqrestore(&prtd->lock, flags);
  2741. rc = msm_compr_send_media_format_block(cstream,
  2742. stream_id, false);
  2743. if (rc < 0) {
  2744. pr_err("%s, failed to send media format block\n",
  2745. __func__);
  2746. break;
  2747. }
  2748. msm_compr_send_dec_params(cstream, pdata->dec_params[fe_id],
  2749. stream_id);
  2750. break;
  2751. }
  2752. return rc;
  2753. }
  2754. static int msm_compr_pointer(struct snd_compr_stream *cstream,
  2755. struct snd_compr_tstamp *arg)
  2756. {
  2757. struct snd_compr_runtime *runtime = cstream->runtime;
  2758. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  2759. struct msm_compr_audio *prtd = runtime->private_data;
  2760. struct snd_soc_component *component = NULL;
  2761. struct msm_compr_pdata *pdata = NULL;
  2762. struct snd_compr_tstamp tstamp;
  2763. uint64_t timestamp = 0;
  2764. int rc = 0, first_buffer;
  2765. unsigned long flags;
  2766. uint32_t gapless_transition;
  2767. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2768. if (!component) {
  2769. pr_err("%s: component is NULL\n", __func__);
  2770. return -EINVAL;
  2771. }
  2772. pdata = snd_soc_component_get_drvdata(component);
  2773. if (!pdata) {
  2774. pr_err("%s: pdata is NULL\n", __func__);
  2775. return -EINVAL;
  2776. }
  2777. pr_debug("%s\n", __func__);
  2778. memset(&tstamp, 0x0, sizeof(struct snd_compr_tstamp));
  2779. spin_lock_irqsave(&prtd->lock, flags);
  2780. tstamp.sampling_rate = prtd->sample_rate;
  2781. tstamp.byte_offset = prtd->byte_offset;
  2782. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  2783. tstamp.copied_total = prtd->copied_total;
  2784. else if (cstream->direction == SND_COMPRESS_CAPTURE)
  2785. tstamp.copied_total = prtd->received_total;
  2786. first_buffer = prtd->first_buffer;
  2787. if (atomic_read(&prtd->error)) {
  2788. pr_err_ratelimited("%s Got RESET EVENTS notification, return error\n",
  2789. __func__);
  2790. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  2791. runtime->total_bytes_transferred = tstamp.copied_total;
  2792. else
  2793. runtime->total_bytes_available = tstamp.copied_total;
  2794. tstamp.pcm_io_frames = 0;
  2795. memcpy(arg, &tstamp, sizeof(struct snd_compr_tstamp));
  2796. spin_unlock_irqrestore(&prtd->lock, flags);
  2797. return -ENETRESET;
  2798. }
  2799. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  2800. gapless_transition = prtd->gapless_state.gapless_transition;
  2801. spin_unlock_irqrestore(&prtd->lock, flags);
  2802. if (gapless_transition)
  2803. pr_debug("%s session time in gapless transition",
  2804. __func__);
  2805. /*
  2806. *- Do not query if no buffer has been given.
  2807. *- Do not query on a gapless transition.
  2808. * Playback for the 2nd stream can start (thus returning time
  2809. * starting from 0) before the driver knows about EOS of first
  2810. * stream.
  2811. */
  2812. if (!first_buffer || gapless_transition) {
  2813. if (pdata->use_legacy_api)
  2814. rc = q6asm_get_session_time_legacy(
  2815. prtd->audio_client, &prtd->marker_timestamp);
  2816. else
  2817. rc = q6asm_get_session_time(
  2818. prtd->audio_client, &prtd->marker_timestamp);
  2819. if (rc < 0) {
  2820. if (atomic_read(&prtd->error))
  2821. return -ENETRESET;
  2822. else
  2823. return rc;
  2824. }
  2825. }
  2826. } else {
  2827. spin_unlock_irqrestore(&prtd->lock, flags);
  2828. }
  2829. timestamp = prtd->marker_timestamp;
  2830. /* DSP returns timestamp in usec */
  2831. pr_debug("%s: timestamp = %lld usec\n", __func__, timestamp);
  2832. timestamp *= prtd->sample_rate;
  2833. tstamp.pcm_io_frames = (snd_pcm_uframes_t)div64_u64(timestamp, 1000000);
  2834. memcpy(arg, &tstamp, sizeof(struct snd_compr_tstamp));
  2835. return 0;
  2836. }
  2837. static int msm_compr_ack(struct snd_compr_stream *cstream,
  2838. size_t count)
  2839. {
  2840. struct snd_compr_runtime *runtime = cstream->runtime;
  2841. struct msm_compr_audio *prtd = runtime->private_data;
  2842. void *src, *dstn;
  2843. size_t copy;
  2844. unsigned long flags;
  2845. WARN(1, "This path is untested");
  2846. return -EINVAL;
  2847. pr_debug("%s: count = %zd\n", __func__, count);
  2848. if (!prtd->buffer) {
  2849. pr_err("%s: Buffer is not allocated yet ??\n", __func__);
  2850. return -EINVAL;
  2851. }
  2852. src = runtime->buffer + prtd->app_pointer;
  2853. dstn = prtd->buffer + prtd->app_pointer;
  2854. if (count < prtd->buffer_size - prtd->app_pointer) {
  2855. memcpy(dstn, src, count);
  2856. prtd->app_pointer += count;
  2857. } else {
  2858. copy = prtd->buffer_size - prtd->app_pointer;
  2859. memcpy(dstn, src, copy);
  2860. memcpy(prtd->buffer, runtime->buffer, count - copy);
  2861. prtd->app_pointer = count - copy;
  2862. }
  2863. /*
  2864. * If the stream is started and all the bytes received were
  2865. * copied to DSP, the newly received bytes should be
  2866. * sent right away
  2867. */
  2868. spin_lock_irqsave(&prtd->lock, flags);
  2869. if (atomic_read(&prtd->start) &&
  2870. prtd->bytes_received == prtd->copied_total) {
  2871. prtd->bytes_received += count;
  2872. msm_compr_send_buffer(prtd);
  2873. } else
  2874. prtd->bytes_received += count;
  2875. spin_unlock_irqrestore(&prtd->lock, flags);
  2876. return 0;
  2877. }
  2878. static int msm_compr_playback_copy(struct snd_compr_stream *cstream,
  2879. char __user *buf, size_t count)
  2880. {
  2881. struct snd_compr_runtime *runtime = cstream->runtime;
  2882. struct msm_compr_audio *prtd = runtime->private_data;
  2883. void *dstn;
  2884. size_t copy;
  2885. uint64_t bytes_available = 0;
  2886. unsigned long flags;
  2887. pr_debug("%s: count = %zd\n", __func__, count);
  2888. if (!prtd->buffer) {
  2889. pr_err("%s: Buffer is not allocated yet ??", __func__);
  2890. return 0;
  2891. }
  2892. spin_lock_irqsave(&prtd->lock, flags);
  2893. if (atomic_read(&prtd->error)) {
  2894. pr_err("%s Got RESET EVENTS notification", __func__);
  2895. spin_unlock_irqrestore(&prtd->lock, flags);
  2896. return -ENETRESET;
  2897. }
  2898. spin_unlock_irqrestore(&prtd->lock, flags);
  2899. dstn = prtd->buffer + prtd->app_pointer;
  2900. if (count < prtd->buffer_size - prtd->app_pointer) {
  2901. if (copy_from_user(dstn, buf, count))
  2902. return -EFAULT;
  2903. prtd->app_pointer += count;
  2904. } else {
  2905. copy = prtd->buffer_size - prtd->app_pointer;
  2906. if (copy_from_user(dstn, buf, copy))
  2907. return -EFAULT;
  2908. if (copy_from_user(prtd->buffer, buf + copy, count - copy))
  2909. return -EFAULT;
  2910. prtd->app_pointer = count - copy;
  2911. }
  2912. /*
  2913. * If stream is started and there has been an xrun,
  2914. * since the available bytes fits fragment_size, copy the data
  2915. * right away.
  2916. */
  2917. spin_lock_irqsave(&prtd->lock, flags);
  2918. prtd->bytes_received += count;
  2919. if (atomic_read(&prtd->start)) {
  2920. if (atomic_read(&prtd->xrun)) {
  2921. pr_debug("%s: in xrun, count = %zd\n", __func__, count);
  2922. bytes_available = prtd->bytes_received -
  2923. prtd->copied_total;
  2924. if (bytes_available >= runtime->fragment_size) {
  2925. pr_debug("%s: handle xrun, bytes_to_write = %llu\n",
  2926. __func__, bytes_available);
  2927. atomic_set(&prtd->xrun, 0);
  2928. msm_compr_send_buffer(prtd);
  2929. } /* else not sufficient data */
  2930. } /* writes will continue on the next write_done */
  2931. }
  2932. spin_unlock_irqrestore(&prtd->lock, flags);
  2933. return count;
  2934. }
  2935. static int msm_compr_capture_copy(struct snd_compr_stream *cstream,
  2936. char __user *buf, size_t count)
  2937. {
  2938. struct snd_compr_runtime *runtime = cstream->runtime;
  2939. struct msm_compr_audio *prtd = runtime->private_data;
  2940. void *source;
  2941. unsigned long flags;
  2942. pr_debug("%s: count = %zd\n", __func__, count);
  2943. if (!prtd->buffer) {
  2944. pr_err("%s: Buffer is not allocated yet ??", __func__);
  2945. return 0;
  2946. }
  2947. spin_lock_irqsave(&prtd->lock, flags);
  2948. if (atomic_read(&prtd->error)) {
  2949. pr_err("%s Got RESET EVENTS notification", __func__);
  2950. spin_unlock_irqrestore(&prtd->lock, flags);
  2951. return -ENETRESET;
  2952. }
  2953. source = prtd->buffer + prtd->app_pointer;
  2954. /* check if we have requested amount of data to copy to user*/
  2955. if (count <= prtd->received_total - prtd->bytes_copied) {
  2956. spin_unlock_irqrestore(&prtd->lock, flags);
  2957. if (copy_to_user(buf, source, count)) {
  2958. pr_err("copy_to_user failed");
  2959. return -EFAULT;
  2960. }
  2961. spin_lock_irqsave(&prtd->lock, flags);
  2962. prtd->app_pointer += count;
  2963. if (prtd->app_pointer >= prtd->buffer_size)
  2964. prtd->app_pointer -= prtd->buffer_size;
  2965. prtd->bytes_copied += count;
  2966. }
  2967. msm_compr_read_buffer(prtd);
  2968. spin_unlock_irqrestore(&prtd->lock, flags);
  2969. return count;
  2970. }
  2971. static int msm_compr_copy(struct snd_compr_stream *cstream,
  2972. char __user *buf, size_t count)
  2973. {
  2974. int ret = 0;
  2975. pr_debug(" In %s\n", __func__);
  2976. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  2977. ret = msm_compr_playback_copy(cstream, buf, count);
  2978. else if (cstream->direction == SND_COMPRESS_CAPTURE)
  2979. ret = msm_compr_capture_copy(cstream, buf, count);
  2980. return ret;
  2981. }
  2982. static int msm_compr_get_caps(struct snd_compr_stream *cstream,
  2983. struct snd_compr_caps *arg)
  2984. {
  2985. struct snd_compr_runtime *runtime = cstream->runtime;
  2986. struct msm_compr_audio *prtd = runtime->private_data;
  2987. int ret = 0;
  2988. pr_debug("%s\n", __func__);
  2989. if ((arg != NULL) && (prtd != NULL)) {
  2990. memcpy(arg, &prtd->compr_cap, sizeof(struct snd_compr_caps));
  2991. } else {
  2992. ret = -EINVAL;
  2993. pr_err("%s: arg (0x%pK), prtd (0x%pK)\n", __func__, arg, prtd);
  2994. }
  2995. return ret;
  2996. }
  2997. static int msm_compr_get_codec_caps(struct snd_compr_stream *cstream,
  2998. struct snd_compr_codec_caps *codec)
  2999. {
  3000. pr_debug("%s\n", __func__);
  3001. switch (codec->codec) {
  3002. case SND_AUDIOCODEC_MP3:
  3003. codec->num_descriptors = 2;
  3004. codec->descriptor[0].max_ch = 2;
  3005. memcpy(codec->descriptor[0].sample_rates,
  3006. supported_sample_rates,
  3007. sizeof(supported_sample_rates));
  3008. codec->descriptor[0].num_sample_rates =
  3009. sizeof(supported_sample_rates)/sizeof(unsigned int);
  3010. codec->descriptor[0].bit_rate[0] = 320; /* 320kbps */
  3011. codec->descriptor[0].bit_rate[1] = 128;
  3012. codec->descriptor[0].num_bitrates = 2;
  3013. codec->descriptor[0].profiles = 0;
  3014. codec->descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO;
  3015. codec->descriptor[0].formats = 0;
  3016. break;
  3017. case SND_AUDIOCODEC_AAC:
  3018. codec->num_descriptors = 2;
  3019. codec->descriptor[1].max_ch = 2;
  3020. memcpy(codec->descriptor[1].sample_rates,
  3021. supported_sample_rates,
  3022. sizeof(supported_sample_rates));
  3023. codec->descriptor[1].num_sample_rates =
  3024. sizeof(supported_sample_rates)/sizeof(unsigned int);
  3025. codec->descriptor[1].bit_rate[0] = 320; /* 320kbps */
  3026. codec->descriptor[1].bit_rate[1] = 128;
  3027. codec->descriptor[1].num_bitrates = 2;
  3028. codec->descriptor[1].profiles = 0;
  3029. codec->descriptor[1].modes = 0;
  3030. codec->descriptor[1].formats =
  3031. (SND_AUDIOSTREAMFORMAT_MP4ADTS |
  3032. SND_AUDIOSTREAMFORMAT_RAW);
  3033. break;
  3034. case SND_AUDIOCODEC_AC3:
  3035. case SND_AUDIOCODEC_EAC3:
  3036. case SND_AUDIOCODEC_DTS:
  3037. case SND_AUDIOCODEC_TRUEHD:
  3038. case SND_AUDIOCODEC_IEC61937:
  3039. case SND_AUDIOCODEC_BESPOKE:
  3040. break;
  3041. default:
  3042. pr_err("%s: Unsupported audio codec %d\n",
  3043. __func__, codec->codec);
  3044. return -EINVAL;
  3045. }
  3046. return 0;
  3047. }
  3048. static int msm_compr_set_metadata(struct snd_compr_stream *cstream,
  3049. struct snd_compr_metadata *metadata)
  3050. {
  3051. struct msm_compr_audio *prtd;
  3052. struct audio_client *ac;
  3053. pr_debug("%s\n", __func__);
  3054. if (!metadata || !cstream)
  3055. return -EINVAL;
  3056. prtd = cstream->runtime->private_data;
  3057. if (!prtd || !prtd->audio_client) {
  3058. pr_err("%s: prtd or audio client is NULL\n", __func__);
  3059. return -EINVAL;
  3060. }
  3061. if (((metadata->key == SNDRV_COMPRESS_ENCODER_PADDING) ||
  3062. (metadata->key == SNDRV_COMPRESS_ENCODER_DELAY)) &&
  3063. (prtd->compr_passthr != LEGACY_PCM)) {
  3064. pr_debug("%s: No trailing silence for compress_type[%d]\n",
  3065. __func__, prtd->compr_passthr);
  3066. return 0;
  3067. }
  3068. ac = prtd->audio_client;
  3069. if (metadata->key == SNDRV_COMPRESS_ENCODER_PADDING) {
  3070. pr_debug("%s, got encoder padding %u",
  3071. __func__, metadata->value[0]);
  3072. prtd->gapless_state.trailing_samples_drop = metadata->value[0];
  3073. } else if (metadata->key == SNDRV_COMPRESS_ENCODER_DELAY) {
  3074. pr_debug("%s, got encoder delay %u",
  3075. __func__, metadata->value[0]);
  3076. prtd->gapless_state.initial_samples_drop = metadata->value[0];
  3077. } else if (metadata->key == SNDRV_COMPRESS_RENDER_MODE) {
  3078. return msm_compr_set_render_mode(prtd, metadata->value[0]);
  3079. } else if (metadata->key == SNDRV_COMPRESS_CLK_REC_MODE) {
  3080. return msm_compr_set_clk_rec_mode(ac, metadata->value[0]);
  3081. } else if (metadata->key == SNDRV_COMPRESS_RENDER_WINDOW) {
  3082. return msm_compr_set_render_window(
  3083. ac,
  3084. metadata->value[0],
  3085. metadata->value[1],
  3086. metadata->value[2],
  3087. metadata->value[3]);
  3088. } else if (metadata->key == SNDRV_COMPRESS_START_DELAY) {
  3089. prtd->start_delay_lsw = metadata->value[0];
  3090. prtd->start_delay_msw = metadata->value[1];
  3091. } else if (metadata->key ==
  3092. SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK) {
  3093. return msm_compr_enable_adjust_session_clock(ac,
  3094. metadata->value[0]);
  3095. } else if (metadata->key == SNDRV_COMPRESS_ADJUST_SESSION_CLOCK) {
  3096. return msm_compr_adjust_session_clock(ac,
  3097. metadata->value[0],
  3098. metadata->value[1]);
  3099. }
  3100. return 0;
  3101. }
  3102. static int msm_compr_get_metadata(struct snd_compr_stream *cstream,
  3103. struct snd_compr_metadata *metadata)
  3104. {
  3105. struct msm_compr_audio *prtd;
  3106. struct audio_client *ac;
  3107. int ret = -EINVAL;
  3108. uint64_t ses_time = 0, frames = 0, abs_time = 0;
  3109. uint64_t *val = NULL;
  3110. int64_t av_offset = 0;
  3111. int32_t clock_id = -EINVAL;
  3112. pr_debug("%s\n", __func__);
  3113. if (!metadata || !cstream || !cstream->runtime)
  3114. return ret;
  3115. if (metadata->key != SNDRV_COMPRESS_PATH_DELAY &&
  3116. metadata->key != SNDRV_COMPRESS_DSP_POSITION) {
  3117. pr_err("%s, unsupported key %d\n", __func__, metadata->key);
  3118. return ret;
  3119. }
  3120. prtd = cstream->runtime->private_data;
  3121. if (!prtd || !prtd->audio_client) {
  3122. pr_err("%s: prtd or audio client is NULL\n", __func__);
  3123. return ret;
  3124. }
  3125. switch (metadata->key) {
  3126. case SNDRV_COMPRESS_PATH_DELAY:
  3127. ac = prtd->audio_client;
  3128. ret = q6asm_get_path_delay(prtd->audio_client);
  3129. if (ret) {
  3130. pr_err("%s: get_path_delay failed, ret=%d\n",
  3131. __func__, ret);
  3132. return ret;
  3133. }
  3134. pr_debug("%s, path delay(in us) %u\n", __func__,
  3135. ac->path_delay);
  3136. metadata->value[0] = ac->path_delay;
  3137. break;
  3138. case SNDRV_COMPRESS_DSP_POSITION:
  3139. clock_id = metadata->value[0];
  3140. pr_debug("%s, clock_id %d\n", __func__, clock_id);
  3141. ret = q6asm_get_session_time_v2(prtd->audio_client,
  3142. &ses_time, &abs_time);
  3143. if (ret) {
  3144. pr_err("%s: q6asm_get_session_time_v2 failed, ret=%d\n",
  3145. __func__, ret);
  3146. return ret;
  3147. }
  3148. frames = div64_u64((ses_time * prtd->sample_rate), 1000000);
  3149. ret = avcs_core_query_timer_offset(&av_offset, clock_id);
  3150. if (ret) {
  3151. pr_err("%s: avcs query failed, ret=%d\n",
  3152. __func__, ret);
  3153. return ret;
  3154. }
  3155. val = (uint64_t *) &metadata->value[1];
  3156. val[0] = frames;
  3157. val[1] = abs_time + av_offset;
  3158. pr_debug("%s, vals frames %lld, time %lld, avoff %lld, abst %lld, sess_time %llu sr %d\n",
  3159. __func__, val[0], val[1], av_offset, abs_time,
  3160. ses_time, prtd->sample_rate);
  3161. break;
  3162. default:
  3163. pr_err("%s, unsupported key %d\n", __func__, metadata->key);
  3164. break;
  3165. }
  3166. return ret;
  3167. }
  3168. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  3169. static int msm_compr_set_next_track_param(struct snd_compr_stream *cstream,
  3170. union snd_codec_options *codec_options)
  3171. {
  3172. struct msm_compr_audio *prtd;
  3173. struct audio_client *ac;
  3174. int ret = 0;
  3175. if (!codec_options || !cstream)
  3176. return -EINVAL;
  3177. prtd = cstream->runtime->private_data;
  3178. if (!prtd || !prtd->audio_client) {
  3179. pr_err("%s: prtd or audio client is NULL\n", __func__);
  3180. return -EINVAL;
  3181. }
  3182. ac = prtd->audio_client;
  3183. pr_debug("%s: got codec options for codec type %u",
  3184. __func__, prtd->codec);
  3185. switch (prtd->codec) {
  3186. case FORMAT_WMA_V9:
  3187. case FORMAT_WMA_V10PRO:
  3188. case FORMAT_FLAC:
  3189. case FORMAT_VORBIS:
  3190. case FORMAT_ALAC:
  3191. case FORMAT_APE:
  3192. case FORMAT_AMRNB:
  3193. case FORMAT_AMRWB:
  3194. case FORMAT_AMR_WB_PLUS:
  3195. memcpy(&(prtd->gapless_state.codec_options),
  3196. codec_options,
  3197. sizeof(union snd_codec_options));
  3198. ret = msm_compr_send_media_format_block(cstream,
  3199. ac->stream_id, true);
  3200. if (ret < 0) {
  3201. pr_err("%s: failed to send media format block\n",
  3202. __func__);
  3203. }
  3204. break;
  3205. default:
  3206. pr_debug("%s: Ignore sending CMD Format block\n",
  3207. __func__);
  3208. break;
  3209. }
  3210. return ret;
  3211. }
  3212. #endif /* CONFIG_AUDIO_QGKI */
  3213. static int msm_compr_volume_put(struct snd_kcontrol *kcontrol,
  3214. struct snd_ctl_elem_value *ucontrol)
  3215. {
  3216. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3217. unsigned long fe_id = kcontrol->private_value;
  3218. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3219. snd_soc_component_get_drvdata(comp);
  3220. struct snd_compr_stream *cstream = NULL;
  3221. uint32_t *volume = NULL;
  3222. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3223. pr_err("%s Received out of bounds fe_id %lu\n",
  3224. __func__, fe_id);
  3225. return -EINVAL;
  3226. }
  3227. cstream = pdata->cstream[fe_id];
  3228. volume = pdata->volume[fe_id];
  3229. volume[0] = ucontrol->value.integer.value[0];
  3230. volume[1] = ucontrol->value.integer.value[1];
  3231. pr_debug("%s: fe_id %lu left_vol %d right_vol %d\n",
  3232. __func__, fe_id, volume[0], volume[1]);
  3233. if (cstream)
  3234. msm_compr_set_volume(cstream, volume[0], volume[1]);
  3235. return 0;
  3236. }
  3237. static int msm_compr_volume_get(struct snd_kcontrol *kcontrol,
  3238. struct snd_ctl_elem_value *ucontrol)
  3239. {
  3240. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3241. unsigned long fe_id = kcontrol->private_value;
  3242. struct msm_compr_pdata *pdata =
  3243. snd_soc_component_get_drvdata(comp);
  3244. uint32_t *volume = NULL;
  3245. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3246. pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
  3247. return -EINVAL;
  3248. }
  3249. volume = pdata->volume[fe_id];
  3250. pr_debug("%s: fe_id %lu\n", __func__, fe_id);
  3251. ucontrol->value.integer.value[0] = volume[0];
  3252. ucontrol->value.integer.value[1] = volume[1];
  3253. return 0;
  3254. }
  3255. static int msm_compr_audio_effects_config_put(struct snd_kcontrol *kcontrol,
  3256. struct snd_ctl_elem_value *ucontrol)
  3257. {
  3258. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3259. unsigned long fe_id = kcontrol->private_value;
  3260. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3261. snd_soc_component_get_drvdata(comp);
  3262. struct msm_compr_audio_effects *audio_effects = NULL;
  3263. struct snd_compr_stream *cstream = NULL;
  3264. struct msm_compr_audio *prtd = NULL;
  3265. long *values = &(ucontrol->value.integer.value[0]);
  3266. int ret = 0;
  3267. int effects_module;
  3268. pr_debug("%s\n", __func__);
  3269. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3270. pr_err("%s Received out of bounds fe_id %lu\n",
  3271. __func__, fe_id);
  3272. return -EINVAL;
  3273. }
  3274. mutex_lock(&pdata->lock);
  3275. cstream = pdata->cstream[fe_id];
  3276. audio_effects = pdata->audio_effects[fe_id];
  3277. if (!cstream || !audio_effects) {
  3278. pr_err("%s: stream or effects inactive\n", __func__);
  3279. ret = -EINVAL;
  3280. goto done;
  3281. }
  3282. prtd = cstream->runtime->private_data;
  3283. if (!prtd) {
  3284. pr_err("%s: cannot set audio effects\n", __func__);
  3285. ret = -EINVAL;
  3286. goto done;
  3287. }
  3288. if (prtd->compr_passthr != LEGACY_PCM) {
  3289. pr_debug("%s: No effects for compr_type[%d]\n",
  3290. __func__, prtd->compr_passthr);
  3291. goto done;
  3292. }
  3293. pr_debug("%s: Effects supported for compr_type[%d]\n",
  3294. __func__, prtd->compr_passthr);
  3295. effects_module = *values++;
  3296. switch (effects_module) {
  3297. case VIRTUALIZER_MODULE:
  3298. pr_debug("%s: VIRTUALIZER_MODULE\n", __func__);
  3299. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3300. prtd->audio_client->topology))
  3301. msm_audio_effects_virtualizer_handler(
  3302. prtd->audio_client,
  3303. &(audio_effects->virtualizer),
  3304. values);
  3305. break;
  3306. case REVERB_MODULE:
  3307. pr_debug("%s: REVERB_MODULE\n", __func__);
  3308. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3309. prtd->audio_client->topology))
  3310. msm_audio_effects_reverb_handler(prtd->audio_client,
  3311. &(audio_effects->reverb),
  3312. values);
  3313. break;
  3314. case BASS_BOOST_MODULE:
  3315. pr_debug("%s: BASS_BOOST_MODULE\n", __func__);
  3316. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3317. prtd->audio_client->topology))
  3318. msm_audio_effects_bass_boost_handler(prtd->audio_client,
  3319. &(audio_effects->bass_boost),
  3320. values);
  3321. break;
  3322. case PBE_MODULE:
  3323. pr_debug("%s: PBE_MODULE\n", __func__);
  3324. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3325. prtd->audio_client->topology))
  3326. msm_audio_effects_pbe_handler(prtd->audio_client,
  3327. &(audio_effects->pbe),
  3328. values);
  3329. break;
  3330. case EQ_MODULE:
  3331. pr_debug("%s: EQ_MODULE\n", __func__);
  3332. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3333. prtd->audio_client->topology))
  3334. msm_audio_effects_popless_eq_handler(prtd->audio_client,
  3335. &(audio_effects->equalizer),
  3336. values);
  3337. break;
  3338. case SOFT_VOLUME_MODULE:
  3339. pr_debug("%s: SOFT_VOLUME_MODULE\n", __func__);
  3340. break;
  3341. case SOFT_VOLUME2_MODULE:
  3342. pr_debug("%s: SOFT_VOLUME2_MODULE\n", __func__);
  3343. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3344. prtd->audio_client->topology))
  3345. msm_audio_effects_volume_handler_v2(prtd->audio_client,
  3346. &(audio_effects->volume),
  3347. values, SOFT_VOLUME_INSTANCE_2);
  3348. break;
  3349. default:
  3350. pr_err("%s Invalid effects config module\n", __func__);
  3351. ret = -EINVAL;
  3352. }
  3353. done:
  3354. mutex_unlock(&pdata->lock);
  3355. return ret;
  3356. }
  3357. static int msm_compr_audio_effects_config_get(struct snd_kcontrol *kcontrol,
  3358. struct snd_ctl_elem_value *ucontrol)
  3359. {
  3360. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3361. unsigned long fe_id = kcontrol->private_value;
  3362. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3363. snd_soc_component_get_drvdata(comp);
  3364. struct msm_compr_audio_effects *audio_effects = NULL;
  3365. struct snd_compr_stream *cstream = NULL;
  3366. int ret = 0;
  3367. struct msm_compr_audio *prtd = NULL;
  3368. pr_debug("%s\n", __func__);
  3369. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3370. pr_err("%s Received out of bounds fe_id %lu\n",
  3371. __func__, fe_id);
  3372. return -EINVAL;
  3373. }
  3374. mutex_lock(&pdata->lock);
  3375. cstream = pdata->cstream[fe_id];
  3376. audio_effects = pdata->audio_effects[fe_id];
  3377. if (!cstream || !audio_effects) {
  3378. pr_debug("%s: stream or effects inactive\n", __func__);
  3379. ret = -EINVAL;
  3380. goto done;
  3381. }
  3382. prtd = cstream->runtime->private_data;
  3383. if (!prtd) {
  3384. pr_err("%s: cannot set audio effects\n", __func__);
  3385. ret = -EINVAL;
  3386. }
  3387. done:
  3388. mutex_unlock(&pdata->lock);
  3389. return ret;
  3390. }
  3391. static int msm_compr_query_audio_effect_put(struct snd_kcontrol *kcontrol,
  3392. struct snd_ctl_elem_value *ucontrol)
  3393. {
  3394. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3395. unsigned long fe_id = kcontrol->private_value;
  3396. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3397. snd_soc_component_get_drvdata(comp);
  3398. struct msm_compr_audio_effects *audio_effects = NULL;
  3399. struct snd_compr_stream *cstream = NULL;
  3400. struct msm_compr_audio *prtd = NULL;
  3401. int ret = 0;
  3402. long *values = &(ucontrol->value.integer.value[0]);
  3403. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3404. pr_err("%s Received out of bounds fe_id %lu\n",
  3405. __func__, fe_id);
  3406. return -EINVAL;
  3407. }
  3408. mutex_lock(&pdata->lock);
  3409. cstream = pdata->cstream[fe_id];
  3410. audio_effects = pdata->audio_effects[fe_id];
  3411. if (!cstream || !audio_effects) {
  3412. pr_err("%s: stream or effects inactive\n", __func__);
  3413. ret = -EINVAL;
  3414. goto done;
  3415. }
  3416. prtd = cstream->runtime->private_data;
  3417. if (!prtd) {
  3418. pr_err("%s: cannot set audio effects\n", __func__);
  3419. ret = -EINVAL;
  3420. goto done;
  3421. }
  3422. if (prtd->compr_passthr != LEGACY_PCM) {
  3423. pr_err("%s: No effects for compr_type[%d]\n",
  3424. __func__, prtd->compr_passthr);
  3425. ret = -EPERM;
  3426. goto done;
  3427. }
  3428. audio_effects->query.mod_id = (u32)*values++;
  3429. audio_effects->query.parm_id = (u32)*values++;
  3430. audio_effects->query.size = (u32)*values++;
  3431. audio_effects->query.offset = (u32)*values++;
  3432. audio_effects->query.device = (u32)*values++;
  3433. done:
  3434. mutex_unlock(&pdata->lock);
  3435. return ret;
  3436. }
  3437. static int msm_compr_query_audio_effect_get(struct snd_kcontrol *kcontrol,
  3438. struct snd_ctl_elem_value *ucontrol)
  3439. {
  3440. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3441. unsigned long fe_id = kcontrol->private_value;
  3442. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3443. snd_soc_component_get_drvdata(comp);
  3444. struct msm_compr_audio_effects *audio_effects = NULL;
  3445. struct snd_compr_stream *cstream = NULL;
  3446. struct msm_compr_audio *prtd = NULL;
  3447. int ret = 0;
  3448. long *values = &(ucontrol->value.integer.value[0]);
  3449. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3450. pr_err("%s Received out of bounds fe_id %lu\n",
  3451. __func__, fe_id);
  3452. return -EINVAL;
  3453. }
  3454. mutex_lock(&pdata->lock);
  3455. cstream = pdata->cstream[fe_id];
  3456. audio_effects = pdata->audio_effects[fe_id];
  3457. if (!cstream || !audio_effects) {
  3458. pr_debug("%s: stream or effects inactive\n", __func__);
  3459. ret = -EINVAL;
  3460. goto done;
  3461. }
  3462. prtd = cstream->runtime->private_data;
  3463. if (!prtd) {
  3464. pr_err("%s: cannot set audio effects\n", __func__);
  3465. ret = -EINVAL;
  3466. goto done;
  3467. }
  3468. values[0] = (long)audio_effects->query.mod_id;
  3469. values[1] = (long)audio_effects->query.parm_id;
  3470. values[2] = (long)audio_effects->query.size;
  3471. values[3] = (long)audio_effects->query.offset;
  3472. values[4] = (long)audio_effects->query.device;
  3473. done:
  3474. mutex_unlock(&pdata->lock);
  3475. return ret;
  3476. }
  3477. static int msm_compr_send_dec_params(struct snd_compr_stream *cstream,
  3478. struct msm_compr_dec_params *dec_params,
  3479. int stream_id)
  3480. {
  3481. int rc = 0;
  3482. struct msm_compr_audio *prtd = NULL;
  3483. struct snd_dec_ddp *ddp = &dec_params->ddp_params;
  3484. if (!cstream || !dec_params) {
  3485. pr_err("%s: stream or dec_params inactive\n", __func__);
  3486. rc = -EINVAL;
  3487. goto end;
  3488. }
  3489. prtd = cstream->runtime->private_data;
  3490. if (!prtd) {
  3491. pr_err("%s: cannot set dec_params\n", __func__);
  3492. rc = -EINVAL;
  3493. goto end;
  3494. }
  3495. switch (prtd->codec) {
  3496. case FORMAT_MP3:
  3497. case FORMAT_MPEG4_AAC:
  3498. case FORMAT_TRUEHD:
  3499. case FORMAT_IEC61937:
  3500. case FORMAT_APTX:
  3501. pr_debug("%s: no runtime parameters for codec: %d\n", __func__,
  3502. prtd->codec);
  3503. break;
  3504. case FORMAT_AC3:
  3505. case FORMAT_EAC3:
  3506. if (prtd->compr_passthr != LEGACY_PCM) {
  3507. pr_debug("%s: No DDP param for compr_type[%d]\n",
  3508. __func__, prtd->compr_passthr);
  3509. break;
  3510. }
  3511. rc = msm_compr_send_ddp_cfg(prtd->audio_client, ddp, stream_id);
  3512. if (rc < 0)
  3513. pr_err("%s: DDP CMD CFG failed %d\n", __func__, rc);
  3514. break;
  3515. default:
  3516. break;
  3517. }
  3518. end:
  3519. return rc;
  3520. }
  3521. static int msm_compr_dec_params_put(struct snd_kcontrol *kcontrol,
  3522. struct snd_ctl_elem_value *ucontrol)
  3523. {
  3524. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3525. unsigned long fe_id = kcontrol->private_value;
  3526. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3527. snd_soc_component_get_drvdata(comp);
  3528. struct msm_compr_dec_params *dec_params = NULL;
  3529. struct snd_compr_stream *cstream = NULL;
  3530. struct msm_compr_audio *prtd = NULL;
  3531. long *values = &(ucontrol->value.integer.value[0]);
  3532. int rc = 0;
  3533. pr_debug("%s\n", __func__);
  3534. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3535. pr_err("%s Received out of bounds fe_id %lu\n",
  3536. __func__, fe_id);
  3537. return -EINVAL;
  3538. }
  3539. cstream = pdata->cstream[fe_id];
  3540. dec_params = pdata->dec_params[fe_id];
  3541. if (!cstream || !dec_params) {
  3542. pr_err("%s: stream or dec_params inactive\n", __func__);
  3543. return -EINVAL;
  3544. }
  3545. prtd = cstream->runtime->private_data;
  3546. if (!prtd) {
  3547. pr_err("%s: cannot set dec_params\n", __func__);
  3548. return -EINVAL;
  3549. }
  3550. mutex_lock(&pdata->lock);
  3551. switch (prtd->codec) {
  3552. case FORMAT_MP3:
  3553. case FORMAT_MPEG4_AAC:
  3554. case FORMAT_FLAC:
  3555. case FORMAT_VORBIS:
  3556. case FORMAT_ALAC:
  3557. case FORMAT_APE:
  3558. case FORMAT_DTS:
  3559. case FORMAT_DSD:
  3560. case FORMAT_TRUEHD:
  3561. case FORMAT_IEC61937:
  3562. case FORMAT_APTX:
  3563. case FORMAT_AMRNB:
  3564. case FORMAT_AMRWB:
  3565. case FORMAT_AMR_WB_PLUS:
  3566. pr_debug("%s: no runtime parameters for codec: %d\n", __func__,
  3567. prtd->codec);
  3568. break;
  3569. case FORMAT_AC3:
  3570. case FORMAT_EAC3: {
  3571. struct snd_dec_ddp *ddp = &dec_params->ddp_params;
  3572. int cnt;
  3573. if (prtd->compr_passthr != LEGACY_PCM) {
  3574. pr_debug("%s: No DDP param for compr_type[%d]\n",
  3575. __func__, prtd->compr_passthr);
  3576. break;
  3577. }
  3578. ddp->params_length = (*values++);
  3579. if (ddp->params_length > DDP_DEC_MAX_NUM_PARAM) {
  3580. pr_err("%s: invalid num of params:: %d\n", __func__,
  3581. ddp->params_length);
  3582. rc = -EINVAL;
  3583. goto end;
  3584. }
  3585. for (cnt = 0; cnt < ddp->params_length; cnt++) {
  3586. ddp->params_id[cnt] = *values++;
  3587. ddp->params_value[cnt] = *values++;
  3588. }
  3589. prtd = cstream->runtime->private_data;
  3590. if (prtd && prtd->audio_client)
  3591. rc = msm_compr_send_dec_params(cstream, dec_params,
  3592. prtd->audio_client->stream_id);
  3593. break;
  3594. }
  3595. default:
  3596. break;
  3597. }
  3598. end:
  3599. pr_debug("%s: ret %d\n", __func__, rc);
  3600. mutex_unlock(&pdata->lock);
  3601. return rc;
  3602. }
  3603. static int msm_compr_dec_params_get(struct snd_kcontrol *kcontrol,
  3604. struct snd_ctl_elem_value *ucontrol)
  3605. {
  3606. /* dummy function */
  3607. return 0;
  3608. }
  3609. static int msm_compr_playback_app_type_cfg_put(struct snd_kcontrol *kcontrol,
  3610. struct snd_ctl_elem_value *ucontrol)
  3611. {
  3612. u64 fe_id = kcontrol->private_value;
  3613. int session_type = SESSION_TYPE_RX;
  3614. int be_id = ucontrol->value.integer.value[3];
  3615. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  3616. int ret = 0;
  3617. cfg_data.app_type = ucontrol->value.integer.value[0];
  3618. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  3619. if (ucontrol->value.integer.value[2] != 0)
  3620. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  3621. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  3622. __func__, fe_id, session_type, be_id,
  3623. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  3624. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  3625. be_id, &cfg_data);
  3626. if (ret < 0)
  3627. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  3628. __func__, ret);
  3629. return ret;
  3630. }
  3631. static int msm_compr_playback_app_type_cfg_get(struct snd_kcontrol *kcontrol,
  3632. struct snd_ctl_elem_value *ucontrol)
  3633. {
  3634. u64 fe_id = kcontrol->private_value;
  3635. int session_type = SESSION_TYPE_RX;
  3636. int be_id = 0;
  3637. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  3638. int ret = 0;
  3639. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  3640. &be_id, &cfg_data);
  3641. if (ret < 0) {
  3642. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  3643. __func__, ret);
  3644. goto done;
  3645. }
  3646. ucontrol->value.integer.value[0] = cfg_data.app_type;
  3647. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  3648. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  3649. ucontrol->value.integer.value[3] = be_id;
  3650. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  3651. __func__, fe_id, session_type, be_id,
  3652. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  3653. done:
  3654. return ret;
  3655. }
  3656. static int msm_compr_capture_app_type_cfg_put(struct snd_kcontrol *kcontrol,
  3657. struct snd_ctl_elem_value *ucontrol)
  3658. {
  3659. u64 fe_id = kcontrol->private_value;
  3660. int session_type = SESSION_TYPE_TX;
  3661. int be_id = ucontrol->value.integer.value[3];
  3662. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  3663. int ret = 0;
  3664. cfg_data.app_type = ucontrol->value.integer.value[0];
  3665. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  3666. if (ucontrol->value.integer.value[2] != 0)
  3667. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  3668. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  3669. __func__, fe_id, session_type, be_id,
  3670. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  3671. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  3672. be_id, &cfg_data);
  3673. if (ret < 0)
  3674. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  3675. __func__, ret);
  3676. return ret;
  3677. }
  3678. static int msm_compr_capture_app_type_cfg_get(struct snd_kcontrol *kcontrol,
  3679. struct snd_ctl_elem_value *ucontrol)
  3680. {
  3681. u64 fe_id = kcontrol->private_value;
  3682. int session_type = SESSION_TYPE_TX;
  3683. int be_id = 0;
  3684. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  3685. int ret = 0;
  3686. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  3687. &be_id, &cfg_data);
  3688. if (ret < 0) {
  3689. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  3690. __func__, ret);
  3691. goto done;
  3692. }
  3693. ucontrol->value.integer.value[0] = cfg_data.app_type;
  3694. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  3695. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  3696. ucontrol->value.integer.value[3] = be_id;
  3697. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  3698. __func__, fe_id, session_type, be_id,
  3699. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  3700. done:
  3701. return ret;
  3702. }
  3703. static int msm_compr_channel_map_put(struct snd_kcontrol *kcontrol,
  3704. struct snd_ctl_elem_value *ucontrol)
  3705. {
  3706. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3707. u64 fe_id = kcontrol->private_value;
  3708. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3709. snd_soc_component_get_drvdata(comp);
  3710. int rc = 0, i;
  3711. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  3712. pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
  3713. if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
  3714. pr_err("%s Received out of bounds fe_id %llu\n",
  3715. __func__, fe_id);
  3716. rc = -EINVAL;
  3717. goto end;
  3718. }
  3719. if (pdata->ch_map[fe_id]) {
  3720. pdata->ch_map[fe_id]->set_ch_map = true;
  3721. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  3722. pdata->ch_map[fe_id]->channel_map[i] =
  3723. (char)(ucontrol->value.integer.value[i]);
  3724. /* update chmixer_pspd chmap cached with routing driver as well */
  3725. chmixer_pspd = pdata->chmixer_pspd[fe_id];
  3726. if (chmixer_pspd && chmixer_pspd->enable) {
  3727. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  3728. chmixer_pspd->in_ch_map[i] =
  3729. pdata->ch_map[fe_id]->channel_map[i];
  3730. chmixer_pspd->override_in_ch_map = true;
  3731. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  3732. SESSION_TYPE_RX, chmixer_pspd);
  3733. }
  3734. } else {
  3735. pr_debug("%s: no memory for ch_map, default will be set\n",
  3736. __func__);
  3737. }
  3738. end:
  3739. pr_debug("%s: ret %d\n", __func__, rc);
  3740. return rc;
  3741. }
  3742. static int msm_compr_channel_map_get(struct snd_kcontrol *kcontrol,
  3743. struct snd_ctl_elem_value *ucontrol)
  3744. {
  3745. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3746. u64 fe_id = kcontrol->private_value;
  3747. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3748. snd_soc_component_get_drvdata(comp);
  3749. int rc = 0, i;
  3750. pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
  3751. if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
  3752. pr_err("%s: Received out of bounds fe_id %llu\n",
  3753. __func__, fe_id);
  3754. rc = -EINVAL;
  3755. goto end;
  3756. }
  3757. if (pdata->ch_map[fe_id]) {
  3758. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  3759. ucontrol->value.integer.value[i] =
  3760. pdata->ch_map[fe_id]->channel_map[i];
  3761. }
  3762. end:
  3763. pr_debug("%s: ret %d\n", __func__, rc);
  3764. return rc;
  3765. }
  3766. static int msm_compr_adsp_stream_cmd_put(struct snd_kcontrol *kcontrol,
  3767. struct snd_ctl_elem_value *ucontrol)
  3768. {
  3769. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3770. unsigned long fe_id = kcontrol->private_value;
  3771. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3772. snd_soc_component_get_drvdata(comp);
  3773. struct snd_compr_stream *cstream = NULL;
  3774. struct msm_compr_audio *prtd;
  3775. int ret = 0;
  3776. struct msm_adsp_event_data *event_data = NULL;
  3777. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3778. pr_err("%s Received invalid fe_id %lu\n",
  3779. __func__, fe_id);
  3780. return -EINVAL;
  3781. }
  3782. cstream = pdata->cstream[fe_id];
  3783. if (cstream == NULL) {
  3784. pr_err("%s cstream is null\n", __func__);
  3785. return -EINVAL;
  3786. }
  3787. prtd = cstream->runtime->private_data;
  3788. if (!prtd) {
  3789. pr_err("%s: prtd is null\n", __func__);
  3790. return -EINVAL;
  3791. }
  3792. mutex_lock(&pdata->lock);
  3793. if (prtd->audio_client == NULL) {
  3794. pr_err("%s: audio_client is null\n", __func__);
  3795. ret = -EINVAL;
  3796. goto done;
  3797. }
  3798. event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
  3799. if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
  3800. (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
  3801. pr_err("%s: invalid event_type=%d",
  3802. __func__, event_data->event_type);
  3803. ret = -EINVAL;
  3804. goto done;
  3805. }
  3806. if (event_data->payload_len > sizeof(ucontrol->value.bytes.data)
  3807. - sizeof(struct msm_adsp_event_data)) {
  3808. pr_err("%s param length=%d exceeds limit",
  3809. __func__, event_data->payload_len);
  3810. ret = -EINVAL;
  3811. goto done;
  3812. }
  3813. ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
  3814. if (ret < 0)
  3815. pr_err("%s: failed to send stream event cmd, err = %d\n",
  3816. __func__, ret);
  3817. done:
  3818. mutex_unlock(&pdata->lock);
  3819. return ret;
  3820. }
  3821. static int msm_compr_ion_fd_map_put(struct snd_kcontrol *kcontrol,
  3822. struct snd_ctl_elem_value *ucontrol)
  3823. {
  3824. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3825. unsigned long fe_id = kcontrol->private_value;
  3826. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3827. snd_soc_component_get_drvdata(comp);
  3828. struct snd_compr_stream *cstream = NULL;
  3829. struct msm_compr_audio *prtd;
  3830. int fd;
  3831. int ret = 0;
  3832. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3833. pr_err("%s Received out of bounds invalid fe_id %lu\n",
  3834. __func__, fe_id);
  3835. return -EINVAL;
  3836. }
  3837. cstream = pdata->cstream[fe_id];
  3838. if (cstream == NULL) {
  3839. pr_err("%s cstream is null\n", __func__);
  3840. return -EINVAL;
  3841. }
  3842. prtd = cstream->runtime->private_data;
  3843. if (!prtd) {
  3844. pr_err("%s: prtd is null\n", __func__);
  3845. return -EINVAL;
  3846. }
  3847. mutex_lock(&pdata->lock);
  3848. if (prtd->audio_client == NULL) {
  3849. pr_err("%s: audio_client is null\n", __func__);
  3850. ret = -EINVAL;
  3851. goto done;
  3852. }
  3853. memcpy(&fd, ucontrol->value.bytes.data, sizeof(fd));
  3854. ret = q6asm_send_ion_fd(prtd->audio_client, fd);
  3855. if (ret < 0)
  3856. pr_err("%s: failed to register ion fd\n", __func__);
  3857. done:
  3858. mutex_unlock(&pdata->lock);
  3859. return ret;
  3860. }
  3861. static int msm_compr_rtic_event_ack_put(struct snd_kcontrol *kcontrol,
  3862. struct snd_ctl_elem_value *ucontrol)
  3863. {
  3864. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3865. unsigned long fe_id = kcontrol->private_value;
  3866. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3867. snd_soc_component_get_drvdata(comp);
  3868. struct snd_compr_stream *cstream = NULL;
  3869. struct msm_compr_audio *prtd;
  3870. int ret = 0;
  3871. int param_length = 0;
  3872. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3873. pr_err("%s Received invalid fe_id %lu\n",
  3874. __func__, fe_id);
  3875. return -EINVAL;
  3876. }
  3877. mutex_lock(&pdata->lock);
  3878. cstream = pdata->cstream[fe_id];
  3879. if (cstream == NULL) {
  3880. pr_err("%s cstream is null\n", __func__);
  3881. ret = -EINVAL;
  3882. goto done;
  3883. }
  3884. prtd = cstream->runtime->private_data;
  3885. if (!prtd) {
  3886. pr_err("%s: prtd is null\n", __func__);
  3887. ret = -EINVAL;
  3888. goto done;
  3889. }
  3890. if (prtd->audio_client == NULL) {
  3891. pr_err("%s: audio_client is null\n", __func__);
  3892. ret = -EINVAL;
  3893. goto done;
  3894. }
  3895. memcpy(&param_length, ucontrol->value.bytes.data,
  3896. sizeof(param_length));
  3897. if ((param_length + sizeof(param_length))
  3898. >= sizeof(ucontrol->value.bytes.data)) {
  3899. pr_err("%s param length=%d exceeds limit",
  3900. __func__, param_length);
  3901. ret = -EINVAL;
  3902. goto done;
  3903. }
  3904. ret = q6asm_send_rtic_event_ack(prtd->audio_client,
  3905. ucontrol->value.bytes.data + sizeof(param_length),
  3906. param_length);
  3907. if (ret < 0)
  3908. pr_err("%s: failed to send rtic event ack, err = %d\n",
  3909. __func__, ret);
  3910. done:
  3911. mutex_unlock(&pdata->lock);
  3912. return ret;
  3913. }
  3914. static int msm_compr_gapless_put(struct snd_kcontrol *kcontrol,
  3915. struct snd_ctl_elem_value *ucontrol)
  3916. {
  3917. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3918. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3919. snd_soc_component_get_drvdata(comp);
  3920. pdata->use_dsp_gapless_mode = ucontrol->value.integer.value[0];
  3921. pr_debug("%s: value: %ld\n", __func__,
  3922. ucontrol->value.integer.value[0]);
  3923. return 0;
  3924. }
  3925. static int msm_compr_gapless_get(struct snd_kcontrol *kcontrol,
  3926. struct snd_ctl_elem_value *ucontrol)
  3927. {
  3928. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3929. struct msm_compr_pdata *pdata =
  3930. snd_soc_component_get_drvdata(comp);
  3931. pr_debug("%s:gapless mode %d\n", __func__, pdata->use_dsp_gapless_mode);
  3932. ucontrol->value.integer.value[0] = pdata->use_dsp_gapless_mode;
  3933. return 0;
  3934. }
  3935. static const struct snd_kcontrol_new msm_compr_gapless_controls[] = {
  3936. SOC_SINGLE_EXT("Compress Gapless Playback",
  3937. 0, 0, 1, 0,
  3938. msm_compr_gapless_get,
  3939. msm_compr_gapless_put),
  3940. };
  3941. static int msm_compr_probe(struct snd_soc_component *component)
  3942. {
  3943. struct msm_compr_pdata *pdata;
  3944. int i;
  3945. int rc;
  3946. const char *qdsp_version;
  3947. pr_debug("%s\n", __func__);
  3948. pdata = (struct msm_compr_pdata *) dev_get_drvdata(component->dev);
  3949. if (!pdata) {
  3950. pr_err("%s platform data not set\n", __func__);
  3951. return -EINVAL;
  3952. }
  3953. snd_soc_component_set_drvdata(component, pdata);
  3954. for (i = 0; i < MSM_FRONTEND_DAI_MAX; i++) {
  3955. pdata->volume[i][0] = COMPRESSED_LR_VOL_MAX_STEPS;
  3956. pdata->volume[i][1] = COMPRESSED_LR_VOL_MAX_STEPS;
  3957. pdata->audio_effects[i] = NULL;
  3958. pdata->dec_params[i] = NULL;
  3959. pdata->cstream[i] = NULL;
  3960. pdata->ch_map[i] = NULL;
  3961. pdata->is_in_use[i] = false;
  3962. }
  3963. snd_soc_add_component_controls(component, msm_compr_gapless_controls,
  3964. ARRAY_SIZE(msm_compr_gapless_controls));
  3965. rc = of_property_read_string(component->dev->of_node,
  3966. "qcom,adsp-version", &qdsp_version);
  3967. if (!rc) {
  3968. if (!strcmp(qdsp_version, "MDSP 1.2"))
  3969. pdata->use_legacy_api = true;
  3970. else
  3971. pdata->use_legacy_api = false;
  3972. } else
  3973. pdata->use_legacy_api = false;
  3974. pr_debug("%s: use legacy api %d\n", __func__, pdata->use_legacy_api);
  3975. /*
  3976. * use_dsp_gapless_mode part of platform data(pdata) is updated from HAL
  3977. * through a mixer control before compress driver is opened. The mixer
  3978. * control is used to decide if dsp gapless mode needs to be enabled.
  3979. * Gapless is disabled by default.
  3980. */
  3981. pdata->use_dsp_gapless_mode = false;
  3982. return 0;
  3983. }
  3984. static int msm_compr_volume_info(struct snd_kcontrol *kcontrol,
  3985. struct snd_ctl_elem_info *uinfo)
  3986. {
  3987. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3988. uinfo->count = 2;
  3989. uinfo->value.integer.min = 0;
  3990. uinfo->value.integer.max = COMPRESSED_LR_VOL_MAX_STEPS;
  3991. return 0;
  3992. }
  3993. static int msm_compr_audio_effects_config_info(struct snd_kcontrol *kcontrol,
  3994. struct snd_ctl_elem_info *uinfo)
  3995. {
  3996. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3997. uinfo->count = MAX_PP_PARAMS_SZ;
  3998. uinfo->value.integer.min = 0;
  3999. uinfo->value.integer.max = 0xFFFFFFFF;
  4000. return 0;
  4001. }
  4002. static int msm_compr_query_audio_effect_info(struct snd_kcontrol *kcontrol,
  4003. struct snd_ctl_elem_info *uinfo)
  4004. {
  4005. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4006. uinfo->count = 128;
  4007. uinfo->value.integer.min = 0;
  4008. uinfo->value.integer.max = 0xFFFFFFFF;
  4009. return 0;
  4010. }
  4011. static int msm_compr_dec_params_info(struct snd_kcontrol *kcontrol,
  4012. struct snd_ctl_elem_info *uinfo)
  4013. {
  4014. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4015. uinfo->count = 128;
  4016. uinfo->value.integer.min = 0;
  4017. uinfo->value.integer.max = 0xFFFFFFFF;
  4018. return 0;
  4019. }
  4020. static int msm_compr_app_type_cfg_info(struct snd_kcontrol *kcontrol,
  4021. struct snd_ctl_elem_info *uinfo)
  4022. {
  4023. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4024. uinfo->count = 5;
  4025. uinfo->value.integer.min = 0;
  4026. uinfo->value.integer.max = 0xFFFFFFFF;
  4027. return 0;
  4028. }
  4029. static int msm_compr_channel_map_info(struct snd_kcontrol *kcontrol,
  4030. struct snd_ctl_elem_info *uinfo)
  4031. {
  4032. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4033. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  4034. uinfo->value.integer.min = 0;
  4035. /* See PCM_MAX_CHANNEL_MAP in apr_audio-v2.h */
  4036. uinfo->value.integer.max = PCM_MAX_CHANNEL_MAP;
  4037. return 0;
  4038. }
  4039. static int msm_compr_add_volume_control(struct snd_soc_pcm_runtime *rtd)
  4040. {
  4041. struct snd_soc_component *component = NULL;
  4042. const char *mixer_ctl_name = "Compress Playback";
  4043. const char *deviceNo = "NN";
  4044. const char *suffix = "Volume";
  4045. char *mixer_str = NULL;
  4046. int ctl_len;
  4047. struct snd_kcontrol_new fe_volume_control[1] = {
  4048. {
  4049. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4050. .name = "?",
  4051. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  4052. SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4053. .info = msm_compr_volume_info,
  4054. .tlv.p = msm_compr_vol_gain,
  4055. .get = msm_compr_volume_get,
  4056. .put = msm_compr_volume_put,
  4057. .private_value = 0,
  4058. }
  4059. };
  4060. if (!rtd) {
  4061. pr_err("%s NULL rtd\n", __func__);
  4062. return 0;
  4063. }
  4064. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4065. if (!component) {
  4066. pr_err("%s: component is NULL\n", __func__);
  4067. return 0;
  4068. }
  4069. pr_debug("%s: added new compr FE with name %s, id %d, cpu dai %s, device no %d\n",
  4070. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4071. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4072. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
  4073. strlen(suffix) + 1;
  4074. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4075. if (!mixer_str) {
  4076. pr_err("failed to allocate mixer ctrl str of len %d", ctl_len);
  4077. return 0;
  4078. }
  4079. snprintf(mixer_str, ctl_len, "%s %d %s", mixer_ctl_name,
  4080. rtd->pcm->device, suffix);
  4081. fe_volume_control[0].name = mixer_str;
  4082. fe_volume_control[0].private_value = rtd->dai_link->id;
  4083. pr_debug("Registering new mixer ctl %s", mixer_str);
  4084. snd_soc_add_component_controls(component, fe_volume_control,
  4085. ARRAY_SIZE(fe_volume_control));
  4086. kfree(mixer_str);
  4087. return 0;
  4088. }
  4089. static int msm_compr_add_audio_effects_control(struct snd_soc_pcm_runtime *rtd)
  4090. {
  4091. struct snd_soc_component *component = NULL;
  4092. const char *mixer_ctl_name = "Audio Effects Config";
  4093. const char *deviceNo = "NN";
  4094. char *mixer_str = NULL;
  4095. int ctl_len;
  4096. struct snd_kcontrol_new fe_audio_effects_config_control[1] = {
  4097. {
  4098. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4099. .name = "?",
  4100. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4101. .info = msm_compr_audio_effects_config_info,
  4102. .get = msm_compr_audio_effects_config_get,
  4103. .put = msm_compr_audio_effects_config_put,
  4104. .private_value = 0,
  4105. }
  4106. };
  4107. if (!rtd) {
  4108. pr_err("%s NULL rtd\n", __func__);
  4109. return 0;
  4110. }
  4111. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4112. if (!component) {
  4113. pr_err("%s: component is NULL\n", __func__);
  4114. return 0;
  4115. }
  4116. pr_debug("%s: added new compr FE with name %s, id %d, cpu dai %s, device no %d\n",
  4117. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4118. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4119. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4120. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4121. if (!mixer_str)
  4122. return 0;
  4123. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4124. fe_audio_effects_config_control[0].name = mixer_str;
  4125. fe_audio_effects_config_control[0].private_value = rtd->dai_link->id;
  4126. pr_debug("Registering new mixer ctl %s\n", mixer_str);
  4127. snd_soc_add_component_controls(component,
  4128. fe_audio_effects_config_control,
  4129. ARRAY_SIZE(fe_audio_effects_config_control));
  4130. kfree(mixer_str);
  4131. return 0;
  4132. }
  4133. static int msm_compr_add_query_audio_effect_control(
  4134. struct snd_soc_pcm_runtime *rtd)
  4135. {
  4136. struct snd_soc_component *component = NULL;
  4137. const char *mixer_ctl_name = "Query Audio Effect Param";
  4138. const char *deviceNo = "NN";
  4139. char *mixer_str = NULL;
  4140. int ctl_len;
  4141. struct snd_kcontrol_new fe_query_audio_effect_control[1] = {
  4142. {
  4143. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4144. .name = "?",
  4145. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4146. .info = msm_compr_query_audio_effect_info,
  4147. .get = msm_compr_query_audio_effect_get,
  4148. .put = msm_compr_query_audio_effect_put,
  4149. .private_value = 0,
  4150. }
  4151. };
  4152. if (!rtd) {
  4153. pr_err("%s NULL rtd\n", __func__);
  4154. return 0;
  4155. }
  4156. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4157. if (!component) {
  4158. pr_err("%s: component is NULL\n", __func__);
  4159. return 0;
  4160. }
  4161. pr_debug("%s: added new compr FE with name %s, id %d, cpu dai %s, device no %d\n",
  4162. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4163. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4164. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4165. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4166. if (!mixer_str) {
  4167. pr_err("failed to allocate mixer ctrl str of len %d", ctl_len);
  4168. return 0;
  4169. }
  4170. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4171. fe_query_audio_effect_control[0].name = mixer_str;
  4172. fe_query_audio_effect_control[0].private_value = rtd->dai_link->id;
  4173. pr_debug("%s: registering new mixer ctl %s\n", __func__, mixer_str);
  4174. snd_soc_add_component_controls(component,
  4175. fe_query_audio_effect_control,
  4176. ARRAY_SIZE(fe_query_audio_effect_control));
  4177. kfree(mixer_str);
  4178. return 0;
  4179. }
  4180. static int msm_compr_add_audio_adsp_stream_cmd_control(
  4181. struct snd_soc_pcm_runtime *rtd)
  4182. {
  4183. struct snd_soc_component *component = NULL;
  4184. const char *mixer_ctl_name = DSP_STREAM_CMD;
  4185. const char *deviceNo = "NN";
  4186. char *mixer_str = NULL;
  4187. int ctl_len = 0, ret = 0;
  4188. struct snd_kcontrol_new fe_audio_adsp_stream_cmd_config_control[1] = {
  4189. {
  4190. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4191. .name = "?",
  4192. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4193. .info = msm_adsp_stream_cmd_info,
  4194. .put = msm_compr_adsp_stream_cmd_put,
  4195. .private_value = 0,
  4196. }
  4197. };
  4198. if (!rtd) {
  4199. pr_err("%s NULL rtd\n", __func__);
  4200. return -EINVAL;
  4201. }
  4202. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4203. if (!component) {
  4204. pr_err("%s: component is NULL\n", __func__);
  4205. return -EINVAL;
  4206. }
  4207. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4208. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4209. if (!mixer_str)
  4210. return -ENOMEM;
  4211. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4212. fe_audio_adsp_stream_cmd_config_control[0].name = mixer_str;
  4213. fe_audio_adsp_stream_cmd_config_control[0].private_value =
  4214. rtd->dai_link->id;
  4215. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  4216. ret = snd_soc_add_component_controls(component,
  4217. fe_audio_adsp_stream_cmd_config_control,
  4218. ARRAY_SIZE(fe_audio_adsp_stream_cmd_config_control));
  4219. if (ret < 0)
  4220. pr_err("%s: failed to add ctl %s. err = %d\n",
  4221. __func__, mixer_str, ret);
  4222. kfree(mixer_str);
  4223. return ret;
  4224. }
  4225. static int msm_compr_add_audio_adsp_stream_callback_control(
  4226. struct snd_soc_pcm_runtime *rtd)
  4227. {
  4228. struct snd_soc_component *component = NULL;
  4229. const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
  4230. const char *deviceNo = "NN";
  4231. char *mixer_str = NULL;
  4232. int ctl_len = 0, ret = 0;
  4233. struct snd_kcontrol *kctl;
  4234. struct snd_kcontrol_new fe_audio_adsp_callback_config_control[1] = {
  4235. {
  4236. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4237. .name = "?",
  4238. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4239. .info = msm_adsp_stream_callback_info,
  4240. .get = msm_adsp_stream_callback_get,
  4241. .private_value = 0,
  4242. }
  4243. };
  4244. if (!rtd) {
  4245. pr_err("%s: rtd is NULL\n", __func__);
  4246. ret = -EINVAL;
  4247. goto done;
  4248. }
  4249. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4250. if (!component) {
  4251. pr_err("%s: component is NULL\n", __func__);
  4252. return -EINVAL;
  4253. }
  4254. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4255. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4256. if (!mixer_str) {
  4257. ret = -ENOMEM;
  4258. goto done;
  4259. }
  4260. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4261. fe_audio_adsp_callback_config_control[0].name = mixer_str;
  4262. fe_audio_adsp_callback_config_control[0].private_value =
  4263. rtd->dai_link->id;
  4264. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  4265. ret = snd_soc_add_component_controls(component,
  4266. fe_audio_adsp_callback_config_control,
  4267. ARRAY_SIZE(fe_audio_adsp_callback_config_control));
  4268. if (ret < 0) {
  4269. pr_err("%s: failed to add ctl %s. err = %d\n",
  4270. __func__, mixer_str, ret);
  4271. ret = -EINVAL;
  4272. goto free_mixer_str;
  4273. }
  4274. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  4275. if (!kctl) {
  4276. pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
  4277. ret = -EINVAL;
  4278. goto free_mixer_str;
  4279. }
  4280. kctl->private_data = NULL;
  4281. free_mixer_str:
  4282. kfree(mixer_str);
  4283. done:
  4284. return ret;
  4285. }
  4286. static int msm_compr_add_dec_runtime_params_control(
  4287. struct snd_soc_pcm_runtime *rtd)
  4288. {
  4289. struct snd_soc_component *component = NULL;
  4290. const char *mixer_ctl_name = "Audio Stream";
  4291. const char *deviceNo = "NN";
  4292. const char *suffix = "Dec Params";
  4293. char *mixer_str = NULL;
  4294. int ctl_len;
  4295. struct snd_kcontrol_new fe_dec_params_control[1] = {
  4296. {
  4297. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4298. .name = "?",
  4299. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4300. .info = msm_compr_dec_params_info,
  4301. .get = msm_compr_dec_params_get,
  4302. .put = msm_compr_dec_params_put,
  4303. .private_value = 0,
  4304. }
  4305. };
  4306. if (!rtd) {
  4307. pr_err("%s NULL rtd\n", __func__);
  4308. return 0;
  4309. }
  4310. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4311. if (!component) {
  4312. pr_err("%s: component is NULL\n", __func__);
  4313. return 0;
  4314. }
  4315. pr_debug("%s: added new compr FE with name %s, id %d, cpu dai %s, device no %d\n",
  4316. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4317. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4318. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
  4319. strlen(suffix) + 1;
  4320. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4321. if (!mixer_str)
  4322. return 0;
  4323. snprintf(mixer_str, ctl_len, "%s %d %s", mixer_ctl_name,
  4324. rtd->pcm->device, suffix);
  4325. fe_dec_params_control[0].name = mixer_str;
  4326. fe_dec_params_control[0].private_value = rtd->dai_link->id;
  4327. pr_debug("Registering new mixer ctl %s", mixer_str);
  4328. snd_soc_add_component_controls(component,
  4329. fe_dec_params_control,
  4330. ARRAY_SIZE(fe_dec_params_control));
  4331. kfree(mixer_str);
  4332. return 0;
  4333. }
  4334. static int msm_compr_add_app_type_cfg_control(struct snd_soc_pcm_runtime *rtd)
  4335. {
  4336. struct snd_soc_component *component = NULL;
  4337. const char *playback_mixer_ctl_name = "Audio Stream";
  4338. const char *capture_mixer_ctl_name = "Audio Stream Capture";
  4339. const char *deviceNo = "NN";
  4340. const char *suffix = "App Type Cfg";
  4341. char *mixer_str = NULL;
  4342. int ctl_len;
  4343. struct snd_kcontrol_new fe_app_type_cfg_control[1] = {
  4344. {
  4345. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4346. .name = "?",
  4347. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4348. .info = msm_compr_app_type_cfg_info,
  4349. .put = msm_compr_playback_app_type_cfg_put,
  4350. .get = msm_compr_playback_app_type_cfg_get,
  4351. .private_value = 0,
  4352. }
  4353. };
  4354. if (!rtd) {
  4355. pr_err("%s NULL rtd\n", __func__);
  4356. return 0;
  4357. }
  4358. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4359. if (!component) {
  4360. pr_err("%s: component is NULL\n", __func__);
  4361. return 0;
  4362. }
  4363. pr_debug("%s: added new compr FE ctl with name %s, id %d, cpu dai %s, device no %d\n",
  4364. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4365. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4366. if (rtd->compr->direction == SND_COMPRESS_PLAYBACK)
  4367. ctl_len = strlen(playback_mixer_ctl_name) + 1 + strlen(deviceNo)
  4368. + 1 + strlen(suffix) + 1;
  4369. else
  4370. ctl_len = strlen(capture_mixer_ctl_name) + 1 + strlen(deviceNo)
  4371. + 1 + strlen(suffix) + 1;
  4372. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4373. if (!mixer_str)
  4374. return 0;
  4375. if (rtd->compr->direction == SND_COMPRESS_PLAYBACK)
  4376. snprintf(mixer_str, ctl_len, "%s %d %s",
  4377. playback_mixer_ctl_name, rtd->pcm->device, suffix);
  4378. else
  4379. snprintf(mixer_str, ctl_len, "%s %d %s",
  4380. capture_mixer_ctl_name, rtd->pcm->device, suffix);
  4381. fe_app_type_cfg_control[0].name = mixer_str;
  4382. fe_app_type_cfg_control[0].private_value = rtd->dai_link->id;
  4383. if (rtd->compr->direction == SND_COMPRESS_PLAYBACK) {
  4384. fe_app_type_cfg_control[0].put =
  4385. msm_compr_playback_app_type_cfg_put;
  4386. fe_app_type_cfg_control[0].get =
  4387. msm_compr_playback_app_type_cfg_get;
  4388. } else {
  4389. fe_app_type_cfg_control[0].put =
  4390. msm_compr_capture_app_type_cfg_put;
  4391. fe_app_type_cfg_control[0].get =
  4392. msm_compr_capture_app_type_cfg_get;
  4393. }
  4394. pr_debug("Registering new mixer ctl %s", mixer_str);
  4395. snd_soc_add_component_controls(component,
  4396. fe_app_type_cfg_control,
  4397. ARRAY_SIZE(fe_app_type_cfg_control));
  4398. kfree(mixer_str);
  4399. return 0;
  4400. }
  4401. static int msm_compr_add_channel_map_control(struct snd_soc_pcm_runtime *rtd)
  4402. {
  4403. struct snd_soc_component *component = NULL;
  4404. const char *mixer_ctl_name = "Playback Channel Map";
  4405. const char *deviceNo = "NN";
  4406. char *mixer_str = NULL;
  4407. struct msm_compr_pdata *pdata = NULL;
  4408. int ctl_len;
  4409. struct snd_kcontrol_new fe_channel_map_control[1] = {
  4410. {
  4411. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4412. .name = "?",
  4413. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4414. .info = msm_compr_channel_map_info,
  4415. .get = msm_compr_channel_map_get,
  4416. .put = msm_compr_channel_map_put,
  4417. .private_value = 0,
  4418. }
  4419. };
  4420. if (!rtd) {
  4421. pr_err("%s: NULL rtd\n", __func__);
  4422. return -EINVAL;
  4423. }
  4424. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4425. if (!component) {
  4426. pr_err("%s: component is NULL\n", __func__);
  4427. return -EINVAL;
  4428. }
  4429. pr_debug("%s: added new compr FE with name %s, id %d, cpu dai %s, device no %d\n",
  4430. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4431. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4432. ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) + 1;
  4433. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4434. if (!mixer_str)
  4435. return -ENOMEM;
  4436. snprintf(mixer_str, ctl_len, "%s%d", mixer_ctl_name, rtd->pcm->device);
  4437. fe_channel_map_control[0].name = mixer_str;
  4438. fe_channel_map_control[0].private_value = rtd->dai_link->id;
  4439. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  4440. snd_soc_add_component_controls(component,
  4441. fe_channel_map_control,
  4442. ARRAY_SIZE(fe_channel_map_control));
  4443. pdata = snd_soc_component_get_drvdata(component);
  4444. pdata->ch_map[rtd->dai_link->id] =
  4445. kzalloc(sizeof(struct msm_compr_ch_map), GFP_KERNEL);
  4446. if (!pdata->ch_map[rtd->dai_link->id]) {
  4447. pr_err("%s: Could not allocate memory for channel map\n",
  4448. __func__);
  4449. kfree(mixer_str);
  4450. return -ENOMEM;
  4451. }
  4452. kfree(mixer_str);
  4453. return 0;
  4454. }
  4455. static int msm_compr_add_io_fd_cmd_control(struct snd_soc_pcm_runtime *rtd)
  4456. {
  4457. struct snd_soc_component *component = NULL;
  4458. const char *mixer_ctl_name = "Playback ION FD";
  4459. const char *deviceNo = "NN";
  4460. char *mixer_str = NULL;
  4461. int ctl_len = 0, ret = 0;
  4462. struct snd_kcontrol_new fe_ion_fd_config_control[1] = {
  4463. {
  4464. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4465. .name = "?",
  4466. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4467. .info = msm_adsp_stream_cmd_info,
  4468. .put = msm_compr_ion_fd_map_put,
  4469. .private_value = 0,
  4470. }
  4471. };
  4472. if (!rtd) {
  4473. pr_err("%s NULL rtd\n", __func__);
  4474. return -EINVAL;
  4475. }
  4476. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4477. if (!component) {
  4478. pr_err("%s: component is NULL\n", __func__);
  4479. return -EINVAL;
  4480. }
  4481. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4482. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4483. if (!mixer_str)
  4484. return -ENOMEM;
  4485. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4486. fe_ion_fd_config_control[0].name = mixer_str;
  4487. fe_ion_fd_config_control[0].private_value = rtd->dai_link->id;
  4488. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  4489. ret = snd_soc_add_component_controls(component,
  4490. fe_ion_fd_config_control,
  4491. ARRAY_SIZE(fe_ion_fd_config_control));
  4492. if (ret < 0)
  4493. pr_err("%s: failed to add ctl %s\n", __func__, mixer_str);
  4494. kfree(mixer_str);
  4495. return ret;
  4496. }
  4497. static int msm_compr_add_event_ack_cmd_control(struct snd_soc_pcm_runtime *rtd)
  4498. {
  4499. struct snd_soc_component *component = NULL;
  4500. const char *mixer_ctl_name = "Playback Event Ack";
  4501. const char *deviceNo = "NN";
  4502. char *mixer_str = NULL;
  4503. int ctl_len = 0, ret = 0;
  4504. struct snd_kcontrol_new fe_event_ack_config_control[1] = {
  4505. {
  4506. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4507. .name = "?",
  4508. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4509. .info = msm_adsp_stream_cmd_info,
  4510. .put = msm_compr_rtic_event_ack_put,
  4511. .private_value = 0,
  4512. }
  4513. };
  4514. if (!rtd) {
  4515. pr_err("%s NULL rtd\n", __func__);
  4516. return -EINVAL;
  4517. }
  4518. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4519. if (!component) {
  4520. pr_err("%s: component is NULL\n", __func__);
  4521. return -EINVAL;
  4522. }
  4523. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4524. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4525. if (!mixer_str)
  4526. return -ENOMEM;
  4527. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4528. fe_event_ack_config_control[0].name = mixer_str;
  4529. fe_event_ack_config_control[0].private_value = rtd->dai_link->id;
  4530. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  4531. ret = snd_soc_add_component_controls(component,
  4532. fe_event_ack_config_control,
  4533. ARRAY_SIZE(fe_event_ack_config_control));
  4534. if (ret < 0)
  4535. pr_err("%s: failed to add ctl %s\n", __func__, mixer_str);
  4536. kfree(mixer_str);
  4537. return ret;
  4538. }
  4539. static struct msm_pcm_channel_mixer *msm_compr_get_chmixer(
  4540. struct msm_compr_pdata *pdata, u64 fe_id)
  4541. {
  4542. if (!pdata) {
  4543. pr_err("%s: missing pdata\n", __func__);
  4544. return NULL;
  4545. }
  4546. if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
  4547. pr_err("%s: invalid FE %llu\n", __func__, fe_id);
  4548. return NULL;
  4549. }
  4550. return pdata->chmixer_pspd[fe_id];
  4551. }
  4552. static int msm_compr_channel_mixer_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  4553. struct snd_ctl_elem_value *ucontrol)
  4554. {
  4555. u64 fe_id = kcontrol->private_value & 0xFF;
  4556. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  4557. int ret = 0, i = 0, stream_id = 0, be_id = 0;
  4558. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4559. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4560. snd_soc_component_get_drvdata(comp);
  4561. struct snd_compr_stream *cstream = NULL;
  4562. struct msm_compr_audio *prtd = NULL;
  4563. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  4564. u8 asm_ch_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8] = {0};
  4565. bool reset_override_out_ch_map = false;
  4566. bool reset_override_in_ch_map = false;
  4567. if ((session_type != SESSION_TYPE_TX) &&
  4568. (session_type != SESSION_TYPE_RX)) {
  4569. pr_err("%s: invalid session type %d\n", __func__, session_type);
  4570. return -EINVAL;
  4571. }
  4572. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4573. if (!chmixer_pspd) {
  4574. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4575. return -EINVAL;
  4576. }
  4577. chmixer_pspd->enable = ucontrol->value.integer.value[0];
  4578. chmixer_pspd->rule = ucontrol->value.integer.value[1];
  4579. chmixer_pspd->input_channel = ucontrol->value.integer.value[2];
  4580. chmixer_pspd->output_channel = ucontrol->value.integer.value[3];
  4581. chmixer_pspd->port_idx = ucontrol->value.integer.value[4];
  4582. if (chmixer_pspd->input_channel < 0 ||
  4583. chmixer_pspd->input_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8 ||
  4584. chmixer_pspd->output_channel < 0 ||
  4585. chmixer_pspd->output_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  4586. pr_err("%s: Invalid channels, in %d, out %d\n",
  4587. __func__, chmixer_pspd->input_channel,
  4588. chmixer_pspd->output_channel);
  4589. return -EINVAL;
  4590. }
  4591. if (chmixer_pspd->enable) {
  4592. if (session_type == SESSION_TYPE_RX &&
  4593. !chmixer_pspd->override_in_ch_map) {
  4594. if (pdata->ch_map[fe_id]->set_ch_map) {
  4595. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4596. chmixer_pspd->in_ch_map[i] =
  4597. pdata->ch_map[fe_id]->channel_map[i];
  4598. } else {
  4599. q6asm_map_channels(asm_ch_map,
  4600. chmixer_pspd->input_channel, false);
  4601. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4602. chmixer_pspd->in_ch_map[i] = asm_ch_map[i];
  4603. }
  4604. chmixer_pspd->override_in_ch_map = true;
  4605. reset_override_in_ch_map = true;
  4606. } else if (session_type == SESSION_TYPE_TX &&
  4607. !chmixer_pspd->override_out_ch_map) {
  4608. if (pdata->ch_map[fe_id]->set_ch_map) {
  4609. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4610. chmixer_pspd->out_ch_map[i] =
  4611. pdata->ch_map[fe_id]->channel_map[i];
  4612. } else {
  4613. q6asm_map_channels(asm_ch_map,
  4614. chmixer_pspd->output_channel, false);
  4615. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4616. chmixer_pspd->out_ch_map[i] = asm_ch_map[i];
  4617. }
  4618. chmixer_pspd->override_out_ch_map = true;
  4619. reset_override_out_ch_map = true;
  4620. }
  4621. } else {
  4622. chmixer_pspd->override_out_ch_map = false;
  4623. chmixer_pspd->override_in_ch_map = false;
  4624. }
  4625. /* cache value and take effect during adm_open stage */
  4626. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  4627. session_type,
  4628. chmixer_pspd);
  4629. cstream = pdata->cstream[fe_id];
  4630. if (chmixer_pspd->enable && cstream && cstream->runtime) {
  4631. prtd = cstream->runtime->private_data;
  4632. if (prtd && prtd->audio_client) {
  4633. stream_id = prtd->audio_client->session;
  4634. be_id = chmixer_pspd->port_idx;
  4635. msm_pcm_routing_set_channel_mixer_runtime(be_id,
  4636. stream_id, session_type, chmixer_pspd);
  4637. }
  4638. }
  4639. if (reset_override_out_ch_map)
  4640. chmixer_pspd->override_out_ch_map = false;
  4641. if (reset_override_in_ch_map)
  4642. chmixer_pspd->override_in_ch_map = false;
  4643. return ret;
  4644. }
  4645. static int msm_compr_channel_mixer_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  4646. struct snd_ctl_elem_value *ucontrol)
  4647. {
  4648. u64 fe_id = kcontrol->private_value & 0xFF;
  4649. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4650. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4651. snd_soc_component_get_drvdata(comp);
  4652. struct msm_pcm_channel_mixer *chmixer_pspd;
  4653. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4654. if (!chmixer_pspd) {
  4655. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4656. return -EINVAL;
  4657. }
  4658. ucontrol->value.integer.value[0] = chmixer_pspd->enable;
  4659. ucontrol->value.integer.value[1] = chmixer_pspd->rule;
  4660. ucontrol->value.integer.value[2] = chmixer_pspd->input_channel;
  4661. ucontrol->value.integer.value[3] = chmixer_pspd->output_channel;
  4662. ucontrol->value.integer.value[4] = chmixer_pspd->port_idx;
  4663. return 0;
  4664. }
  4665. static int msm_compr_channel_mixer_output_map_ctl_put(
  4666. struct snd_kcontrol *kcontrol,
  4667. struct snd_ctl_elem_value *ucontrol)
  4668. {
  4669. u64 fe_id = kcontrol->private_value & 0xFF;
  4670. int i = 0;
  4671. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4672. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4673. snd_soc_component_get_drvdata(comp);
  4674. struct msm_pcm_channel_mixer *chmixer_pspd;
  4675. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4676. if (!chmixer_pspd) {
  4677. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4678. return -EINVAL;
  4679. }
  4680. chmixer_pspd->override_out_ch_map = true;
  4681. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4682. chmixer_pspd->out_ch_map[i] =
  4683. ucontrol->value.integer.value[i];
  4684. return 0;
  4685. }
  4686. static int msm_compr_channel_mixer_output_map_ctl_get(
  4687. struct snd_kcontrol *kcontrol,
  4688. struct snd_ctl_elem_value *ucontrol)
  4689. {
  4690. u64 fe_id = kcontrol->private_value & 0xFF;
  4691. int i = 0;
  4692. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4693. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4694. snd_soc_component_get_drvdata(comp);
  4695. struct msm_pcm_channel_mixer *chmixer_pspd;
  4696. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4697. if (!chmixer_pspd) {
  4698. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4699. return -EINVAL;
  4700. }
  4701. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4702. ucontrol->value.integer.value[i] =
  4703. chmixer_pspd->out_ch_map[i];
  4704. return 0;
  4705. }
  4706. static int msm_compr_channel_mixer_input_map_ctl_put(
  4707. struct snd_kcontrol *kcontrol,
  4708. struct snd_ctl_elem_value *ucontrol)
  4709. {
  4710. u64 fe_id = kcontrol->private_value & 0xFF;
  4711. int i = 0;
  4712. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4713. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4714. snd_soc_component_get_drvdata(comp);
  4715. struct msm_pcm_channel_mixer *chmixer_pspd;
  4716. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4717. if (!chmixer_pspd) {
  4718. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4719. return -EINVAL;
  4720. }
  4721. chmixer_pspd->override_in_ch_map = true;
  4722. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4723. chmixer_pspd->in_ch_map[i] = ucontrol->value.integer.value[i];
  4724. return 0;
  4725. }
  4726. static int msm_compr_channel_mixer_input_map_ctl_get(
  4727. struct snd_kcontrol *kcontrol,
  4728. struct snd_ctl_elem_value *ucontrol)
  4729. {
  4730. u64 fe_id = kcontrol->private_value & 0xFF;
  4731. int i = 0;
  4732. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4733. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4734. snd_soc_component_get_drvdata(comp);
  4735. struct msm_pcm_channel_mixer *chmixer_pspd;
  4736. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4737. if (!chmixer_pspd) {
  4738. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4739. return -EINVAL;
  4740. }
  4741. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4742. ucontrol->value.integer.value[i] =
  4743. chmixer_pspd->in_ch_map[i];
  4744. return 0;
  4745. }
  4746. static int msm_compr_channel_mixer_weight_ctl_put(
  4747. struct snd_kcontrol *kcontrol,
  4748. struct snd_ctl_elem_value *ucontrol)
  4749. {
  4750. u64 fe_id = kcontrol->private_value & 0xFF;
  4751. int channel = (kcontrol->private_value >> 16) & 0xFF;
  4752. int i = 0;
  4753. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4754. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4755. snd_soc_component_get_drvdata(comp);
  4756. struct msm_pcm_channel_mixer *chmixer_pspd;
  4757. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4758. if (!chmixer_pspd) {
  4759. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4760. return -EINVAL;
  4761. }
  4762. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  4763. pr_err("%s: invalid channel number %d\n", __func__, channel);
  4764. return -EINVAL;
  4765. }
  4766. channel--;
  4767. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4768. chmixer_pspd->channel_weight[channel][i] =
  4769. ucontrol->value.integer.value[i];
  4770. return 0;
  4771. }
  4772. static int msm_compr_channel_mixer_weight_ctl_get(
  4773. struct snd_kcontrol *kcontrol,
  4774. struct snd_ctl_elem_value *ucontrol)
  4775. {
  4776. u64 fe_id = kcontrol->private_value & 0xFF;
  4777. int channel = (kcontrol->private_value >> 16) & 0xFF;
  4778. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4779. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4780. snd_soc_component_get_drvdata(comp);
  4781. int i = 0;
  4782. struct msm_pcm_channel_mixer *chmixer_pspd;
  4783. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  4784. pr_err("%s: invalid channel number %d\n", __func__, channel);
  4785. return -EINVAL;
  4786. }
  4787. channel--;
  4788. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4789. if (!chmixer_pspd) {
  4790. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4791. return -EINVAL;
  4792. }
  4793. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4794. ucontrol->value.integer.value[i] =
  4795. chmixer_pspd->channel_weight[channel][i];
  4796. return 0;
  4797. }
  4798. static int msm_compr_add_platform_controls(struct snd_kcontrol_new *kctl,
  4799. struct snd_soc_pcm_runtime *rtd, const char *name_prefix,
  4800. const char *name_suffix, int session_type, int channels)
  4801. {
  4802. int ret = -EINVAL;
  4803. char *mixer_name = NULL;
  4804. const char *deviceNo = "NN";
  4805. const char *channelNo = "NN";
  4806. int ctl_len = 0;
  4807. struct snd_soc_component *component = NULL;
  4808. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4809. if (!component) {
  4810. pr_err("%s: component is NULL\n", __func__);
  4811. return -EINVAL;
  4812. }
  4813. ctl_len = strlen(name_prefix) + 1 + strlen(deviceNo) + 1 +
  4814. strlen(channelNo) + 1 + strlen(name_suffix) + 1;
  4815. mixer_name = kzalloc(ctl_len, GFP_KERNEL);
  4816. if (mixer_name == NULL)
  4817. return -ENOMEM;
  4818. if (channels >= 0) {
  4819. snprintf(mixer_name, ctl_len, "%s %d %s %d",
  4820. name_prefix, rtd->pcm->device, name_suffix, channels);
  4821. kctl->private_value = (rtd->dai_link->id) | (channels << 16);
  4822. } else {
  4823. snprintf(mixer_name, ctl_len, "%s %d %s",
  4824. name_prefix, rtd->pcm->device, name_suffix);
  4825. kctl->private_value = (rtd->dai_link->id);
  4826. }
  4827. if (session_type != INVALID_SESSION)
  4828. kctl->private_value |= (session_type << 8);
  4829. kctl->name = mixer_name;
  4830. ret = snd_soc_add_component_controls(component, kctl, 1);
  4831. kfree(mixer_name);
  4832. return ret;
  4833. }
  4834. static int msm_compr_channel_mixer_output_map_info(struct snd_kcontrol *kcontrol,
  4835. struct snd_ctl_elem_info *uinfo)
  4836. {
  4837. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4838. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  4839. /* Valid channel map value ranges from 1 to 64 */
  4840. uinfo->value.integer.min = 1;
  4841. uinfo->value.integer.max = 64;
  4842. return 0;
  4843. }
  4844. static int msm_compr_add_channel_mixer_output_map_controls(
  4845. struct snd_soc_pcm_runtime *rtd)
  4846. {
  4847. const char *playback_mixer_ctl_name = "AudStr";
  4848. const char *capture_mixer_ctl_name = "AudStr Capture";
  4849. const char *suffix = "ChMixer Output Map";
  4850. const char *mixer_ctl_name = NULL;
  4851. int ret = 0, session_type = INVALID_SESSION, channel = -1;
  4852. struct snd_kcontrol_new channel_mixer_output_map_control = {
  4853. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4854. .name = "?",
  4855. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4856. .info = msm_compr_channel_mixer_output_map_info,
  4857. .put = msm_compr_channel_mixer_output_map_ctl_put,
  4858. .get = msm_compr_channel_mixer_output_map_ctl_get,
  4859. .private_value = 0,
  4860. };
  4861. mixer_ctl_name = rtd->compr->direction == SND_COMPRESS_PLAYBACK ?
  4862. playback_mixer_ctl_name : capture_mixer_ctl_name ;
  4863. ret = msm_compr_add_platform_controls(&channel_mixer_output_map_control,
  4864. rtd, mixer_ctl_name, suffix, session_type, channel);
  4865. if (ret < 0) {
  4866. pr_err("%s: failed add platform ctl, err = %d\n",
  4867. __func__, ret);
  4868. }
  4869. return ret;
  4870. }
  4871. static int msm_compr_channel_mixer_input_map_info(struct snd_kcontrol *kcontrol,
  4872. struct snd_ctl_elem_info *uinfo)
  4873. {
  4874. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4875. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  4876. /* Valid channel map value ranges from 1 to 64 */
  4877. uinfo->value.integer.min = 1;
  4878. uinfo->value.integer.max = 64;
  4879. return 0;
  4880. }
  4881. static int msm_compr_add_channel_mixer_input_map_controls(
  4882. struct snd_soc_pcm_runtime *rtd)
  4883. {
  4884. const char *playback_mixer_ctl_name = "AudStr";
  4885. const char *capture_mixer_ctl_name = "AudStr Capture";
  4886. const char *suffix = "ChMixer Input Map";
  4887. const char *mixer_ctl_name = NULL;
  4888. int ret = 0, session_type = INVALID_SESSION, channel = -1;
  4889. struct snd_kcontrol_new channel_mixer_input_map_control = {
  4890. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4891. .name = "?",
  4892. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4893. .info = msm_compr_channel_mixer_input_map_info,
  4894. .put = msm_compr_channel_mixer_input_map_ctl_put,
  4895. .get = msm_compr_channel_mixer_input_map_ctl_get,
  4896. .private_value = 0,
  4897. };
  4898. mixer_ctl_name = rtd->compr->direction == SND_COMPRESS_PLAYBACK ?
  4899. playback_mixer_ctl_name : capture_mixer_ctl_name ;
  4900. ret = msm_compr_add_platform_controls(&channel_mixer_input_map_control,
  4901. rtd, mixer_ctl_name, suffix, session_type, channel);
  4902. if (ret < 0) {
  4903. pr_err("%s: failed add platform ctl, err = %d\n",
  4904. __func__, ret);
  4905. }
  4906. return ret;
  4907. }
  4908. static int msm_compr_channel_mixer_cfg_info(struct snd_kcontrol *kcontrol,
  4909. struct snd_ctl_elem_info *uinfo)
  4910. {
  4911. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4912. /* five int values: enable, rule, in_channels, out_channels and port_id */
  4913. uinfo->count = 5;
  4914. /* Valid range is all positive values to support above controls */
  4915. uinfo->value.integer.min = 0;
  4916. uinfo->value.integer.max = INT_MAX;
  4917. return 0;
  4918. }
  4919. static int msm_compr_add_channel_mixer_cfg_controls(
  4920. struct snd_soc_pcm_runtime *rtd)
  4921. {
  4922. const char *playback_mixer_ctl_name = "AudStr";
  4923. const char *capture_mixer_ctl_name = "AudStr Capture";
  4924. const char *suffix = "ChMixer Cfg";
  4925. const char *mixer_ctl_name = NULL;
  4926. int ret = 0, session_type = INVALID_SESSION, channel = -1;
  4927. struct snd_kcontrol_new channel_mixer_cfg_control = {
  4928. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4929. .name = "?",
  4930. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4931. .info = msm_compr_channel_mixer_cfg_info,
  4932. .put = msm_compr_channel_mixer_cfg_ctl_put,
  4933. .get = msm_compr_channel_mixer_cfg_ctl_get,
  4934. .private_value = 0,
  4935. };
  4936. if (rtd->compr->direction == SND_COMPRESS_PLAYBACK) {
  4937. session_type = SESSION_TYPE_RX;
  4938. mixer_ctl_name = playback_mixer_ctl_name;
  4939. } else {
  4940. session_type = SESSION_TYPE_TX;
  4941. mixer_ctl_name = capture_mixer_ctl_name;
  4942. }
  4943. ret = msm_compr_add_platform_controls(&channel_mixer_cfg_control,
  4944. rtd, mixer_ctl_name, suffix, session_type, channel);
  4945. if (ret < 0) {
  4946. pr_err("%s: failed add platform ctl, err = %d\n",
  4947. __func__, ret);
  4948. }
  4949. return ret;
  4950. }
  4951. static int msm_compr_channel_mixer_weight_info(struct snd_kcontrol *kcontrol,
  4952. struct snd_ctl_elem_info *uinfo)
  4953. {
  4954. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4955. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  4956. /* Valid range: 0 to 0x4000(Unity) gain weightage */
  4957. uinfo->value.integer.min = 0;
  4958. uinfo->value.integer.max = 0x4000;
  4959. return 0;
  4960. }
  4961. static int msm_compr_add_channel_mixer_weight_controls(
  4962. struct snd_soc_pcm_runtime *rtd,
  4963. int channel)
  4964. {
  4965. const char *playback_mixer_ctl_name = "AudStr";
  4966. const char *capture_mixer_ctl_name = "AudStr Capture";
  4967. const char *suffix = "ChMixer Weight Ch";
  4968. const char *mixer_ctl_name = NULL;
  4969. int ret = 0, session_type = INVALID_SESSION;
  4970. struct snd_kcontrol_new channel_mixer_weight_control = {
  4971. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4972. .name = "?",
  4973. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4974. .info = msm_compr_channel_mixer_weight_info,
  4975. .put = msm_compr_channel_mixer_weight_ctl_put,
  4976. .get = msm_compr_channel_mixer_weight_ctl_get,
  4977. .private_value = 0,
  4978. };
  4979. mixer_ctl_name = rtd->compr->direction == SND_COMPRESS_PLAYBACK ?
  4980. playback_mixer_ctl_name : capture_mixer_ctl_name ;
  4981. ret = msm_compr_add_platform_controls(&channel_mixer_weight_control,
  4982. rtd, mixer_ctl_name, suffix, session_type, channel);
  4983. if (ret < 0) {
  4984. pr_err("%s: failed add platform ctl, err = %d\n",
  4985. __func__, ret);
  4986. }
  4987. return ret;
  4988. }
  4989. static int msm_compr_add_channel_mixer_controls(struct snd_soc_pcm_runtime *rtd)
  4990. {
  4991. int i, ret = 0;
  4992. struct msm_compr_pdata *pdata = NULL;
  4993. struct snd_soc_component *component = NULL;
  4994. if (!rtd) {
  4995. pr_err("%s NULL rtd\n", __func__);
  4996. return -EINVAL;
  4997. }
  4998. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4999. if (!component) {
  5000. pr_err("%s: component is NULL\n", __func__);
  5001. return -EINVAL;
  5002. }
  5003. pdata = (struct msm_compr_pdata *)
  5004. snd_soc_component_get_drvdata(component);
  5005. if (!pdata) {
  5006. pr_err("%s: platform data not populated\n", __func__);
  5007. return -EINVAL;
  5008. }
  5009. if (!pdata->chmixer_pspd[rtd->dai_link->id]) {
  5010. pdata->chmixer_pspd[rtd->dai_link->id] =
  5011. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  5012. if (!pdata->chmixer_pspd[rtd->dai_link->id])
  5013. return -ENOMEM;
  5014. }
  5015. ret = msm_compr_add_channel_mixer_cfg_controls(rtd);
  5016. if (ret) {
  5017. pr_err("%s: pcm add channel mixer cfg controls failed:%d\n",
  5018. __func__, ret);
  5019. goto fail;
  5020. }
  5021. ret = msm_compr_add_channel_mixer_input_map_controls(rtd);
  5022. if (ret) {
  5023. pr_err("%s: pcm add channel mixer input map controls failed:%d\n",
  5024. __func__, ret);
  5025. goto fail;
  5026. }
  5027. ret = msm_compr_add_channel_mixer_output_map_controls(rtd);
  5028. if (ret) {
  5029. pr_err("%s: pcm add channel mixer output map controls failed:%d\n",
  5030. __func__, ret);
  5031. goto fail;
  5032. }
  5033. for (i = 1; i <= PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++) {
  5034. ret = msm_compr_add_channel_mixer_weight_controls(rtd, i);
  5035. if (ret) {
  5036. pr_err("%s: pcm add channel mixer weight controls failed:%d\n",
  5037. __func__, ret);
  5038. goto fail;
  5039. }
  5040. }
  5041. return 0;
  5042. fail:
  5043. kfree(pdata->chmixer_pspd[rtd->dai_link->id]);
  5044. pdata->chmixer_pspd[rtd->dai_link->id] = NULL;
  5045. return ret;
  5046. }
  5047. static int msm_compr_new(struct snd_soc_pcm_runtime *rtd)
  5048. {
  5049. int rc;
  5050. rc = msm_compr_add_volume_control(rtd);
  5051. if (rc)
  5052. pr_err("%s: Could not add Compr Volume Control\n", __func__);
  5053. rc = msm_compr_add_audio_effects_control(rtd);
  5054. if (rc)
  5055. pr_err("%s: Could not add Compr Audio Effects Control\n",
  5056. __func__);
  5057. rc = msm_compr_add_audio_adsp_stream_cmd_control(rtd);
  5058. if (rc)
  5059. pr_err("%s: Could not add Compr ADSP Stream Cmd Control\n",
  5060. __func__);
  5061. rc = msm_compr_add_audio_adsp_stream_callback_control(rtd);
  5062. if (rc)
  5063. pr_err("%s: Could not add Compr ADSP Stream Callback Control\n",
  5064. __func__);
  5065. rc = msm_compr_add_io_fd_cmd_control(rtd);
  5066. if (rc)
  5067. pr_err("%s: Could not add Compr ion fd Control\n",
  5068. __func__);
  5069. rc = msm_compr_add_event_ack_cmd_control(rtd);
  5070. if (rc)
  5071. pr_err("%s: Could not add Compr event ack Control\n",
  5072. __func__);
  5073. rc = msm_compr_add_query_audio_effect_control(rtd);
  5074. if (rc)
  5075. pr_err("%s: Could not add Compr Query Audio Effect Control\n",
  5076. __func__);
  5077. rc = msm_compr_add_dec_runtime_params_control(rtd);
  5078. if (rc)
  5079. pr_err("%s: Could not add Compr Dec runtime params Control\n",
  5080. __func__);
  5081. rc = msm_compr_add_app_type_cfg_control(rtd);
  5082. if (rc)
  5083. pr_err("%s: Could not add Compr App Type Cfg Control\n",
  5084. __func__);
  5085. rc = msm_compr_add_channel_map_control(rtd);
  5086. if (rc)
  5087. pr_err("%s: Could not add Compr Channel Map Control\n",
  5088. __func__);
  5089. rc = msm_compr_add_channel_mixer_controls(rtd);
  5090. if (rc)
  5091. pr_err("%s: Could not add Compr Channel Mixer Controls\n",
  5092. __func__);
  5093. return 0;
  5094. }
  5095. static struct snd_compr_ops msm_compr_ops = {
  5096. .open = msm_compr_open,
  5097. .free = msm_compr_free,
  5098. .trigger = msm_compr_trigger,
  5099. .pointer = msm_compr_pointer,
  5100. .set_params = msm_compr_set_params,
  5101. .set_metadata = msm_compr_set_metadata,
  5102. .get_metadata = msm_compr_get_metadata,
  5103. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  5104. .set_next_track_param = msm_compr_set_next_track_param,
  5105. #endif /* CONFIG_AUDIO_QGKI */
  5106. .ack = msm_compr_ack,
  5107. .copy = msm_compr_copy,
  5108. .get_caps = msm_compr_get_caps,
  5109. .get_codec_caps = msm_compr_get_codec_caps,
  5110. };
  5111. static struct snd_soc_component_driver msm_soc_component = {
  5112. .name = DRV_NAME,
  5113. .probe = msm_compr_probe,
  5114. .compr_ops = &msm_compr_ops,
  5115. .pcm_new = msm_compr_new,
  5116. };
  5117. static int msm_compr_dev_probe(struct platform_device *pdev)
  5118. {
  5119. struct msm_compr_pdata *pdata = NULL;
  5120. pr_debug("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  5121. pdata = (struct msm_compr_pdata *)
  5122. kzalloc(sizeof(*pdata), GFP_KERNEL);
  5123. if (!pdata)
  5124. return -ENOMEM;
  5125. mutex_init(&pdata->lock);
  5126. dev_set_drvdata(&pdev->dev, pdata);
  5127. return snd_soc_register_component(&pdev->dev,
  5128. &msm_soc_component, NULL, 0);
  5129. }
  5130. static int msm_compr_remove(struct platform_device *pdev)
  5131. {
  5132. int i = 0;
  5133. struct msm_compr_pdata *pdata = NULL;
  5134. pdata = dev_get_drvdata(&pdev->dev);
  5135. if (pdata) {
  5136. for (i = 0; i < MSM_FRONTEND_DAI_MM_SIZE; i++)
  5137. kfree(pdata->chmixer_pspd[i]);
  5138. }
  5139. mutex_destroy(&pdata->lock);
  5140. kfree(pdata);
  5141. snd_soc_unregister_component(&pdev->dev);
  5142. return 0;
  5143. }
  5144. static const struct of_device_id msm_compr_dt_match[] = {
  5145. {.compatible = "qcom,msm-compress-dsp"},
  5146. {}
  5147. };
  5148. MODULE_DEVICE_TABLE(of, msm_compr_dt_match);
  5149. static struct platform_driver msm_compr_driver = {
  5150. .driver = {
  5151. .name = "msm-compress-dsp",
  5152. .owner = THIS_MODULE,
  5153. .of_match_table = msm_compr_dt_match,
  5154. .suppress_bind_attrs = true,
  5155. },
  5156. .probe = msm_compr_dev_probe,
  5157. .remove = msm_compr_remove,
  5158. };
  5159. int __init msm_compress_dsp_init(void)
  5160. {
  5161. return platform_driver_register(&msm_compr_driver);
  5162. }
  5163. void msm_compress_dsp_exit(void)
  5164. {
  5165. platform_driver_unregister(&msm_compr_driver);
  5166. }
  5167. MODULE_DESCRIPTION("Compress Offload platform driver");
  5168. MODULE_LICENSE("GPL v2");