msm-compress-q6-v2.c 132 KB

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