msm-compress-q6-v2.c 133 KB

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