msm-compress-q6-v2.c 132 KB

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