msm-compress-q6-v2.c 133 KB

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