msm-compress-q6-v2.c 131 KB

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