msm-compress-q6-v2.c 131 KB

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