msm-compress-q6-v2.c 130 KB

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