msm-compress-q6-v2.c 160 KB

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