msm-compress-q6-v2.c 160 KB

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