msm-compress-q6-v2.c 131 KB

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