msm-compress-q6-v2.c 133 KB

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