msm-pcm-q6-v2.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/init.h>
  5. #include <linux/err.h>
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/time.h>
  9. #include <linux/mutex.h>
  10. #include <linux/wait.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <sound/core.h>
  14. #include <sound/soc.h>
  15. #include <sound/soc-dapm.h>
  16. #include <sound/pcm.h>
  17. #include <sound/initval.h>
  18. #include <sound/control.h>
  19. #include <sound/timer.h>
  20. #include <asm/dma.h>
  21. #include <linux/dma-mapping.h>
  22. #include <audio/linux/msm_audio.h>
  23. #include <linux/of_device.h>
  24. #include <sound/tlv.h>
  25. #include <sound/pcm_params.h>
  26. #include <audio/sound/devdep_params.h>
  27. #include <dsp/msm_audio_ion.h>
  28. #include <dsp/q6audio-v2.h>
  29. #include <dsp/q6core.h>
  30. #include <dsp/q6asm-v2.h>
  31. #include "msm-pcm-q6-v2.h"
  32. #include "msm-pcm-routing-v2.h"
  33. #include "msm-qti-pp-config.h"
  34. #define DRV_NAME "msm-pcm-q6-v2"
  35. #define TIMEOUT_MS 1000
  36. enum stream_state {
  37. IDLE = 0,
  38. STOPPED,
  39. RUNNING,
  40. };
  41. static struct audio_locks the_locks;
  42. #define PCM_MASTER_VOL_MAX_STEPS 0x2000
  43. static const DECLARE_TLV_DB_LINEAR(msm_pcm_vol_gain, 0,
  44. PCM_MASTER_VOL_MAX_STEPS);
  45. struct snd_msm {
  46. struct snd_card *card;
  47. struct snd_pcm *pcm;
  48. };
  49. #define CMD_EOS_MIN_TIMEOUT_LENGTH 50
  50. #define CMD_EOS_TIMEOUT_MULTIPLIER (HZ * 50)
  51. #define MAX_PB_COPY_RETRIES 3
  52. static struct snd_pcm_hardware msm_pcm_hardware_capture = {
  53. .info = (SNDRV_PCM_INFO_MMAP |
  54. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  55. SNDRV_PCM_INFO_MMAP_VALID |
  56. SNDRV_PCM_INFO_INTERLEAVED |
  57. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  58. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  59. SNDRV_PCM_FMTBIT_S24_LE |
  60. SNDRV_PCM_FMTBIT_S24_3LE |
  61. SNDRV_PCM_FMTBIT_S32_LE),
  62. .rates = SNDRV_PCM_RATE_8000_384000,
  63. .rate_min = 8000,
  64. .rate_max = 384000,
  65. .channels_min = 1,
  66. .channels_max = 4,
  67. .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
  68. CAPTURE_MAX_PERIOD_SIZE,
  69. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  70. .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
  71. .periods_min = CAPTURE_MIN_NUM_PERIODS,
  72. .periods_max = CAPTURE_MAX_NUM_PERIODS,
  73. .fifo_size = 0,
  74. };
  75. static struct snd_pcm_hardware msm_pcm_hardware_playback = {
  76. .info = (SNDRV_PCM_INFO_MMAP |
  77. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  78. SNDRV_PCM_INFO_MMAP_VALID |
  79. SNDRV_PCM_INFO_INTERLEAVED |
  80. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  81. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  82. SNDRV_PCM_FMTBIT_S24_LE |
  83. SNDRV_PCM_FMTBIT_S24_3LE |
  84. SNDRV_PCM_FMTBIT_S32_LE),
  85. .rates = SNDRV_PCM_RATE_8000_384000,
  86. .rate_min = 8000,
  87. .rate_max = 384000,
  88. .channels_min = 1,
  89. .channels_max = 8,
  90. .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS *
  91. PLAYBACK_MAX_PERIOD_SIZE,
  92. .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
  93. .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
  94. .periods_min = PLAYBACK_MIN_NUM_PERIODS,
  95. .periods_max = PLAYBACK_MAX_NUM_PERIODS,
  96. .fifo_size = 0,
  97. };
  98. /* Conventional and unconventional sample rate supported */
  99. static unsigned int supported_sample_rates[] = {
  100. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
  101. 88200, 96000, 176400, 192000, 352800, 384000
  102. };
  103. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  104. .count = ARRAY_SIZE(supported_sample_rates),
  105. .list = supported_sample_rates,
  106. .mask = 0,
  107. };
  108. static void msm_pcm_route_event_handler(enum msm_pcm_routing_event event,
  109. void *priv_data)
  110. {
  111. struct msm_audio *prtd = priv_data;
  112. WARN_ON(!prtd);
  113. pr_debug("%s: event %x\n", __func__, event);
  114. switch (event) {
  115. case MSM_PCM_RT_EVT_BUF_RECFG:
  116. q6asm_cmd(prtd->audio_client, CMD_PAUSE);
  117. q6asm_cmd(prtd->audio_client, CMD_FLUSH);
  118. q6asm_run(prtd->audio_client, 0, 0, 0);
  119. /* fallthrough */
  120. default:
  121. break;
  122. }
  123. }
  124. static void event_handler(uint32_t opcode,
  125. uint32_t token, uint32_t *payload, void *priv)
  126. {
  127. struct msm_audio *prtd = priv;
  128. struct snd_pcm_substream *substream = prtd->substream;
  129. uint32_t *ptrmem = (uint32_t *)payload;
  130. uint32_t idx = 0;
  131. uint32_t size = 0;
  132. uint8_t buf_index;
  133. struct snd_soc_pcm_runtime *rtd;
  134. int ret = 0;
  135. switch (opcode) {
  136. case ASM_DATA_EVENT_WRITE_DONE_V2: {
  137. pr_debug("ASM_DATA_EVENT_WRITE_DONE_V2\n");
  138. pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
  139. prtd->pcm_irq_pos += prtd->pcm_count;
  140. if (atomic_read(&prtd->start))
  141. snd_pcm_period_elapsed(substream);
  142. atomic_inc(&prtd->out_count);
  143. wake_up(&the_locks.write_wait);
  144. if (!atomic_read(&prtd->start))
  145. break;
  146. if (!prtd->mmap_flag || prtd->reset_event)
  147. break;
  148. if (q6asm_is_cpu_buf_avail_nolock(IN,
  149. prtd->audio_client,
  150. &size, &idx)) {
  151. pr_debug("%s:writing %d bytes of buffer to dsp 2\n",
  152. __func__, prtd->pcm_count);
  153. q6asm_write_nolock(prtd->audio_client,
  154. prtd->pcm_count, 0, 0, NO_TIMESTAMP);
  155. }
  156. break;
  157. }
  158. case ASM_DATA_EVENT_RENDERED_EOS:
  159. case ASM_DATA_EVENT_RENDERED_EOS_V2:
  160. pr_debug("ASM_DATA_EVENT_RENDERED_EOS\n");
  161. clear_bit(CMD_EOS, &prtd->cmd_pending);
  162. wake_up(&the_locks.eos_wait);
  163. break;
  164. case ASM_DATA_EVENT_READ_DONE_V2: {
  165. pr_debug("ASM_DATA_EVENT_READ_DONE_V2\n");
  166. buf_index = q6asm_get_buf_index_from_token(token);
  167. if (buf_index >= CAPTURE_MAX_NUM_PERIODS) {
  168. pr_err("%s: buffer index %u is out of range.\n",
  169. __func__, buf_index);
  170. return;
  171. }
  172. pr_debug("%s: token=0x%08x buf_index=0x%08x\n",
  173. __func__, token, buf_index);
  174. prtd->in_frame_info[buf_index].size = payload[4];
  175. prtd->in_frame_info[buf_index].offset = payload[5];
  176. /* assume data size = 0 during flushing */
  177. if (prtd->in_frame_info[buf_index].size) {
  178. prtd->pcm_irq_pos +=
  179. prtd->in_frame_info[buf_index].size;
  180. pr_debug("pcm_irq_pos=%d\n", prtd->pcm_irq_pos);
  181. if (atomic_read(&prtd->start))
  182. snd_pcm_period_elapsed(substream);
  183. if (atomic_read(&prtd->in_count) <= prtd->periods)
  184. atomic_inc(&prtd->in_count);
  185. wake_up(&the_locks.read_wait);
  186. if (prtd->mmap_flag &&
  187. q6asm_is_cpu_buf_avail_nolock(OUT,
  188. prtd->audio_client,
  189. &size, &idx) &&
  190. (substream->runtime->status->state ==
  191. SNDRV_PCM_STATE_RUNNING))
  192. q6asm_read_nolock(prtd->audio_client);
  193. } else {
  194. pr_debug("%s: reclaim flushed buf in_count %x\n",
  195. __func__, atomic_read(&prtd->in_count));
  196. prtd->pcm_irq_pos += prtd->pcm_count;
  197. if (prtd->mmap_flag) {
  198. if (q6asm_is_cpu_buf_avail_nolock(OUT,
  199. prtd->audio_client,
  200. &size, &idx) &&
  201. (substream->runtime->status->state ==
  202. SNDRV_PCM_STATE_RUNNING))
  203. q6asm_read_nolock(prtd->audio_client);
  204. } else {
  205. atomic_inc(&prtd->in_count);
  206. }
  207. if (atomic_read(&prtd->in_count) == prtd->periods) {
  208. pr_info("%s: reclaimed all bufs\n", __func__);
  209. if (atomic_read(&prtd->start))
  210. snd_pcm_period_elapsed(substream);
  211. wake_up(&the_locks.read_wait);
  212. }
  213. }
  214. break;
  215. }
  216. case ASM_STREAM_PP_EVENT:
  217. case ASM_STREAM_CMD_ENCDEC_EVENTS: {
  218. pr_debug("%s: ASM_STREAM_EVENT (0x%x)\n", __func__, opcode);
  219. if (!substream) {
  220. pr_err("%s: substream is NULL.\n", __func__);
  221. return;
  222. }
  223. rtd = substream->private_data;
  224. if (!rtd) {
  225. pr_err("%s: rtd is NULL\n", __func__);
  226. return;
  227. }
  228. ret = msm_adsp_inform_mixer_ctl(rtd, payload);
  229. if (ret) {
  230. pr_err("%s: failed to inform mixer ctl. err = %d\n",
  231. __func__, ret);
  232. return;
  233. }
  234. break;
  235. }
  236. case APR_BASIC_RSP_RESULT: {
  237. switch (payload[0]) {
  238. case ASM_SESSION_CMD_RUN_V2:
  239. if (substream->stream
  240. != SNDRV_PCM_STREAM_PLAYBACK) {
  241. atomic_set(&prtd->start, 1);
  242. break;
  243. }
  244. if (prtd->mmap_flag) {
  245. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  246. __func__,
  247. prtd->pcm_count);
  248. q6asm_write_nolock(prtd->audio_client,
  249. prtd->pcm_count,
  250. 0, 0, NO_TIMESTAMP);
  251. } else {
  252. while (atomic_read(&prtd->out_needed)) {
  253. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  254. __func__,
  255. prtd->pcm_count);
  256. q6asm_write_nolock(prtd->audio_client,
  257. prtd->pcm_count,
  258. 0, 0, NO_TIMESTAMP);
  259. atomic_dec(&prtd->out_needed);
  260. wake_up(&the_locks.write_wait);
  261. };
  262. }
  263. atomic_set(&prtd->start, 1);
  264. break;
  265. case ASM_STREAM_CMD_REGISTER_PP_EVENTS:
  266. pr_debug("%s: ASM_STREAM_CMD_REGISTER_PP_EVENTS:",
  267. __func__);
  268. break;
  269. default:
  270. pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
  271. __func__, payload[0], payload[1]);
  272. break;
  273. }
  274. }
  275. break;
  276. case RESET_EVENTS:
  277. pr_debug("%s RESET_EVENTS\n", __func__);
  278. prtd->pcm_irq_pos += prtd->pcm_count;
  279. atomic_inc(&prtd->out_count);
  280. atomic_inc(&prtd->in_count);
  281. prtd->reset_event = true;
  282. if (atomic_read(&prtd->start))
  283. snd_pcm_period_elapsed(substream);
  284. wake_up(&the_locks.eos_wait);
  285. wake_up(&the_locks.write_wait);
  286. wake_up(&the_locks.read_wait);
  287. break;
  288. default:
  289. pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
  290. break;
  291. }
  292. }
  293. static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
  294. {
  295. struct snd_pcm_runtime *runtime = substream->runtime;
  296. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  297. struct snd_soc_component *component =
  298. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  299. struct msm_audio *prtd = runtime->private_data;
  300. struct msm_plat_data *pdata;
  301. struct snd_pcm_hw_params *params;
  302. int ret;
  303. uint32_t fmt_type = FORMAT_LINEAR_PCM;
  304. uint16_t bits_per_sample;
  305. uint16_t sample_word_size;
  306. if (!component) {
  307. pr_err("%s: component is NULL\n", __func__);
  308. return -EINVAL;
  309. }
  310. pdata = (struct msm_plat_data *)
  311. dev_get_drvdata(component->dev);
  312. if (!pdata) {
  313. pr_err("%s: platform data not populated\n", __func__);
  314. return -EINVAL;
  315. }
  316. if (!prtd || !prtd->audio_client) {
  317. pr_err("%s: private data null or audio client freed\n",
  318. __func__);
  319. return -EINVAL;
  320. }
  321. params = &soc_prtd->dpcm[substream->stream].hw_params;
  322. pr_debug("%s\n", __func__);
  323. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  324. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  325. prtd->pcm_irq_pos = 0;
  326. /* rate and channels are sent to audio driver */
  327. prtd->samp_rate = runtime->rate;
  328. prtd->channel_mode = runtime->channels;
  329. if (prtd->enabled)
  330. return 0;
  331. prtd->audio_client->perf_mode = pdata->perf_mode;
  332. pr_debug("%s: perf: %x\n", __func__, pdata->perf_mode);
  333. switch (params_format(params)) {
  334. case SNDRV_PCM_FORMAT_S32_LE:
  335. bits_per_sample = 32;
  336. sample_word_size = 32;
  337. break;
  338. case SNDRV_PCM_FORMAT_S24_LE:
  339. bits_per_sample = 24;
  340. sample_word_size = 32;
  341. break;
  342. case SNDRV_PCM_FORMAT_S24_3LE:
  343. bits_per_sample = 24;
  344. sample_word_size = 24;
  345. break;
  346. case SNDRV_PCM_FORMAT_S16_LE:
  347. default:
  348. bits_per_sample = 16;
  349. sample_word_size = 16;
  350. break;
  351. }
  352. if (prtd->compress_enable) {
  353. fmt_type = FORMAT_GEN_COMPR;
  354. pr_debug("%s: Compressed enabled!\n", __func__);
  355. ret = q6asm_open_write_compressed(prtd->audio_client, fmt_type,
  356. COMPRESSED_PASSTHROUGH_GEN);
  357. if (ret < 0) {
  358. pr_err("%s: q6asm_open_write_compressed failed (%d)\n",
  359. __func__, ret);
  360. q6asm_audio_client_free(prtd->audio_client);
  361. prtd->audio_client = NULL;
  362. return -ENOMEM;
  363. }
  364. } else {
  365. if ((q6core_get_avcs_api_version_per_service(
  366. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  367. ADSP_ASM_API_VERSION_V2))
  368. ret = q6asm_open_write_v5(prtd->audio_client,
  369. fmt_type, bits_per_sample);
  370. else
  371. ret = q6asm_open_write_v4(prtd->audio_client,
  372. fmt_type, bits_per_sample);
  373. if (ret < 0) {
  374. pr_err("%s: q6asm_open_write failed (%d)\n",
  375. __func__, ret);
  376. q6asm_audio_client_free(prtd->audio_client);
  377. prtd->audio_client = NULL;
  378. return -ENOMEM;
  379. }
  380. ret = q6asm_send_cal(prtd->audio_client);
  381. if (ret < 0)
  382. pr_debug("%s : Send cal failed : %d", __func__, ret);
  383. }
  384. pr_debug("%s: session ID %d\n", __func__,
  385. prtd->audio_client->session);
  386. prtd->session_id = prtd->audio_client->session;
  387. if (prtd->compress_enable) {
  388. ret = msm_pcm_routing_reg_phy_compr_stream(
  389. soc_prtd->dai_link->id,
  390. prtd->audio_client->perf_mode,
  391. prtd->session_id,
  392. SNDRV_PCM_STREAM_PLAYBACK,
  393. COMPRESSED_PASSTHROUGH_GEN);
  394. } else {
  395. ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
  396. prtd->audio_client->perf_mode,
  397. prtd->session_id, substream->stream);
  398. }
  399. if (ret) {
  400. pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
  401. return ret;
  402. }
  403. if (prtd->compress_enable) {
  404. ret = q6asm_media_format_block_gen_compr(
  405. prtd->audio_client, runtime->rate,
  406. runtime->channels, !prtd->set_channel_map,
  407. prtd->channel_map, bits_per_sample);
  408. } else {
  409. if ((q6core_get_avcs_api_version_per_service(
  410. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  411. ADSP_ASM_API_VERSION_V2)) {
  412. ret = q6asm_media_format_block_multi_ch_pcm_v5(
  413. prtd->audio_client, runtime->rate,
  414. runtime->channels, !prtd->set_channel_map,
  415. prtd->channel_map, bits_per_sample,
  416. sample_word_size, ASM_LITTLE_ENDIAN,
  417. DEFAULT_QF);
  418. } else {
  419. ret = q6asm_media_format_block_multi_ch_pcm_v4(
  420. prtd->audio_client, runtime->rate,
  421. runtime->channels, !prtd->set_channel_map,
  422. prtd->channel_map, bits_per_sample,
  423. sample_word_size, ASM_LITTLE_ENDIAN,
  424. DEFAULT_QF);
  425. }
  426. }
  427. if (ret < 0)
  428. pr_info("%s: CMD Format block failed\n", __func__);
  429. atomic_set(&prtd->out_count, runtime->periods);
  430. prtd->enabled = 1;
  431. prtd->cmd_pending = 0;
  432. prtd->cmd_interrupt = 0;
  433. return 0;
  434. }
  435. static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
  436. {
  437. struct snd_pcm_runtime *runtime = substream->runtime;
  438. struct msm_audio *prtd = runtime->private_data;
  439. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  440. struct snd_soc_component *component =
  441. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  442. struct msm_plat_data *pdata;
  443. struct snd_pcm_hw_params *params;
  444. struct msm_pcm_routing_evt event;
  445. int ret = 0;
  446. int i = 0;
  447. uint16_t bits_per_sample = 16;
  448. uint16_t sample_word_size;
  449. if (!component) {
  450. pr_err("%s: component is NULL\n", __func__);
  451. return -EINVAL;
  452. }
  453. pdata = (struct msm_plat_data *)
  454. dev_get_drvdata(component->dev);
  455. if (!pdata) {
  456. pr_err("%s: platform data not populated\n", __func__);
  457. return -EINVAL;
  458. }
  459. if (!prtd || !prtd->audio_client) {
  460. pr_err("%s: private data null or audio client freed\n",
  461. __func__);
  462. return -EINVAL;
  463. }
  464. if (prtd->enabled == IDLE) {
  465. pr_debug("%s:perf_mode=%d periods=%d\n", __func__,
  466. pdata->perf_mode, runtime->periods);
  467. params = &soc_prtd->dpcm[substream->stream].hw_params;
  468. if ((params_format(params) == SNDRV_PCM_FORMAT_S24_LE) ||
  469. (params_format(params) == SNDRV_PCM_FORMAT_S24_3LE))
  470. bits_per_sample = 24;
  471. else if (params_format(params) == SNDRV_PCM_FORMAT_S32_LE)
  472. bits_per_sample = 32;
  473. /* ULL mode is not supported in capture path */
  474. if (pdata->perf_mode == LEGACY_PCM_MODE)
  475. prtd->audio_client->perf_mode = LEGACY_PCM_MODE;
  476. else
  477. prtd->audio_client->perf_mode = LOW_LATENCY_PCM_MODE;
  478. pr_debug("%s Opening %d-ch PCM read stream, perf_mode %d\n",
  479. __func__, params_channels(params),
  480. prtd->audio_client->perf_mode);
  481. if ((q6core_get_avcs_api_version_per_service(
  482. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  483. ADSP_ASM_API_VERSION_V2))
  484. ret = q6asm_open_read_v5(prtd->audio_client,
  485. FORMAT_LINEAR_PCM,
  486. bits_per_sample, false, ENC_CFG_ID_NONE);
  487. else
  488. ret = q6asm_open_read_v4(prtd->audio_client,
  489. FORMAT_LINEAR_PCM,
  490. bits_per_sample, false, ENC_CFG_ID_NONE);
  491. if (ret < 0) {
  492. pr_err("%s: q6asm_open_read failed\n", __func__);
  493. q6asm_audio_client_free(prtd->audio_client);
  494. prtd->audio_client = NULL;
  495. return -ENOMEM;
  496. }
  497. ret = q6asm_send_cal(prtd->audio_client);
  498. if (ret < 0)
  499. pr_debug("%s : Send cal failed : %d", __func__, ret);
  500. pr_debug("%s: session ID %d\n",
  501. __func__, prtd->audio_client->session);
  502. prtd->session_id = prtd->audio_client->session;
  503. event.event_func = msm_pcm_route_event_handler;
  504. event.priv_data = (void *) prtd;
  505. ret = msm_pcm_routing_reg_phy_stream_v2(
  506. soc_prtd->dai_link->id,
  507. prtd->audio_client->perf_mode,
  508. prtd->session_id, substream->stream,
  509. event);
  510. if (ret) {
  511. pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
  512. return ret;
  513. }
  514. }
  515. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  516. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  517. prtd->pcm_irq_pos = 0;
  518. /* rate and channels are sent to audio driver */
  519. prtd->samp_rate = runtime->rate;
  520. prtd->channel_mode = runtime->channels;
  521. if (prtd->enabled == IDLE || prtd->enabled == STOPPED) {
  522. for (i = 0; i < runtime->periods; i++)
  523. q6asm_read(prtd->audio_client);
  524. prtd->periods = runtime->periods;
  525. }
  526. if (prtd->enabled != IDLE)
  527. return 0;
  528. switch (runtime->format) {
  529. case SNDRV_PCM_FORMAT_S32_LE:
  530. bits_per_sample = 32;
  531. sample_word_size = 32;
  532. break;
  533. case SNDRV_PCM_FORMAT_S24_LE:
  534. bits_per_sample = 24;
  535. sample_word_size = 32;
  536. break;
  537. case SNDRV_PCM_FORMAT_S24_3LE:
  538. bits_per_sample = 24;
  539. sample_word_size = 24;
  540. break;
  541. case SNDRV_PCM_FORMAT_S16_LE:
  542. default:
  543. bits_per_sample = 16;
  544. sample_word_size = 16;
  545. break;
  546. }
  547. pr_debug("%s: Samp_rate = %d Channel = %d bit width = %d, word size = %d\n",
  548. __func__, prtd->samp_rate, prtd->channel_mode,
  549. bits_per_sample, sample_word_size);
  550. if ((q6core_get_avcs_api_version_per_service(
  551. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  552. ADSP_ASM_API_VERSION_V2))
  553. ret = q6asm_enc_cfg_blk_pcm_format_support_v5(
  554. prtd->audio_client,
  555. prtd->samp_rate,
  556. prtd->channel_mode,
  557. bits_per_sample,
  558. sample_word_size,
  559. ASM_LITTLE_ENDIAN,
  560. DEFAULT_QF);
  561. else
  562. ret = q6asm_enc_cfg_blk_pcm_format_support_v4(
  563. prtd->audio_client,
  564. prtd->samp_rate,
  565. prtd->channel_mode,
  566. bits_per_sample,
  567. sample_word_size,
  568. ASM_LITTLE_ENDIAN,
  569. DEFAULT_QF);
  570. if (ret < 0)
  571. pr_debug("%s: cmd cfg pcm was block failed", __func__);
  572. prtd->enabled = RUNNING;
  573. return ret;
  574. }
  575. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  576. {
  577. int ret = 0;
  578. struct snd_pcm_runtime *runtime = substream->runtime;
  579. struct msm_audio *prtd = runtime->private_data;
  580. switch (cmd) {
  581. case SNDRV_PCM_TRIGGER_START:
  582. case SNDRV_PCM_TRIGGER_RESUME:
  583. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  584. pr_debug("%s: Trigger start\n", __func__);
  585. ret = q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  586. break;
  587. case SNDRV_PCM_TRIGGER_STOP:
  588. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  589. atomic_set(&prtd->start, 0);
  590. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) {
  591. prtd->enabled = STOPPED;
  592. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  593. break;
  594. }
  595. /* pending CMD_EOS isn't expected */
  596. WARN_ON_ONCE(test_bit(CMD_EOS, &prtd->cmd_pending));
  597. set_bit(CMD_EOS, &prtd->cmd_pending);
  598. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
  599. if (ret)
  600. clear_bit(CMD_EOS, &prtd->cmd_pending);
  601. break;
  602. case SNDRV_PCM_TRIGGER_SUSPEND:
  603. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  604. pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
  605. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  606. atomic_set(&prtd->start, 0);
  607. break;
  608. default:
  609. ret = -EINVAL;
  610. break;
  611. }
  612. return ret;
  613. }
  614. static int msm_pcm_open(struct snd_pcm_substream *substream)
  615. {
  616. struct snd_pcm_runtime *runtime = substream->runtime;
  617. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  618. struct snd_soc_component *component =
  619. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  620. struct msm_audio *prtd;
  621. struct msm_plat_data *pdata;
  622. enum apr_subsys_state subsys_state;
  623. int ret = 0;
  624. if (!component) {
  625. pr_err("%s: component is NULL\n", __func__);
  626. return -EINVAL;
  627. }
  628. pdata = (struct msm_plat_data *)
  629. dev_get_drvdata(component->dev);
  630. if (!pdata) {
  631. pr_err("%s: platform data not populated\n", __func__);
  632. return -EINVAL;
  633. }
  634. subsys_state = apr_get_subsys_state();
  635. if (subsys_state == APR_SUBSYS_DOWN) {
  636. pr_debug("%s: adsp is down\n", __func__);
  637. return -ENETRESET;
  638. }
  639. prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
  640. if (prtd == NULL)
  641. return -ENOMEM;
  642. prtd->substream = substream;
  643. prtd->audio_client = q6asm_audio_client_alloc(
  644. (app_cb)event_handler, prtd);
  645. if (!prtd->audio_client) {
  646. pr_info("%s: Could not allocate memory\n", __func__);
  647. kfree(prtd);
  648. prtd = NULL;
  649. return -ENOMEM;
  650. }
  651. prtd->audio_client->dev = component->dev;
  652. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  653. runtime->hw = msm_pcm_hardware_playback;
  654. /* Capture path */
  655. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  656. runtime->hw = msm_pcm_hardware_capture;
  657. else {
  658. pr_err("Invalid Stream type %d\n", substream->stream);
  659. return -EINVAL;
  660. }
  661. ret = snd_pcm_hw_constraint_list(runtime, 0,
  662. SNDRV_PCM_HW_PARAM_RATE,
  663. &constraints_sample_rates);
  664. if (ret < 0)
  665. pr_info("snd_pcm_hw_constraint_list failed\n");
  666. /* Ensure that buffer size is a multiple of period size */
  667. ret = snd_pcm_hw_constraint_integer(runtime,
  668. SNDRV_PCM_HW_PARAM_PERIODS);
  669. if (ret < 0)
  670. pr_info("snd_pcm_hw_constraint_integer failed\n");
  671. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  672. ret = snd_pcm_hw_constraint_minmax(runtime,
  673. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  674. PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
  675. PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
  676. if (ret < 0) {
  677. pr_err("constraint for buffer bytes min max ret = %d\n",
  678. ret);
  679. }
  680. }
  681. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  682. ret = snd_pcm_hw_constraint_minmax(runtime,
  683. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  684. CAPTURE_MIN_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
  685. CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
  686. if (ret < 0) {
  687. pr_err("constraint for buffer bytes min max ret = %d\n",
  688. ret);
  689. }
  690. }
  691. ret = snd_pcm_hw_constraint_step(runtime, 0,
  692. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  693. if (ret < 0) {
  694. pr_err("constraint for period bytes step ret = %d\n",
  695. ret);
  696. }
  697. ret = snd_pcm_hw_constraint_step(runtime, 0,
  698. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  699. if (ret < 0) {
  700. pr_err("constraint for buffer bytes step ret = %d\n",
  701. ret);
  702. }
  703. prtd->enabled = IDLE;
  704. prtd->dsp_cnt = 0;
  705. prtd->set_channel_map = false;
  706. prtd->reset_event = false;
  707. runtime->private_data = prtd;
  708. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  709. msm_adsp_init_mixer_ctl_pp_event_queue(soc_prtd);
  710. /* Vote to update the Rx thread priority to RT Thread for playback */
  711. if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
  712. (pdata->perf_mode == LOW_LATENCY_PCM_MODE))
  713. apr_start_rx_rt(prtd->audio_client->apr);
  714. return 0;
  715. }
  716. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  717. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  718. {
  719. int ret = 0;
  720. int xfer = 0;
  721. char *bufptr = NULL;
  722. void *data = NULL;
  723. uint32_t idx = 0;
  724. uint32_t size = 0;
  725. uint32_t retries = 0;
  726. struct snd_pcm_runtime *runtime = substream->runtime;
  727. struct msm_audio *prtd = runtime->private_data;
  728. pr_debug("%s: prtd->out_count = %d\n",
  729. __func__, atomic_read(&prtd->out_count));
  730. while ((fbytes > 0) && (retries < MAX_PB_COPY_RETRIES)) {
  731. if (prtd->reset_event) {
  732. pr_err("%s: In SSR return ENETRESET before wait\n",
  733. __func__);
  734. return -ENETRESET;
  735. }
  736. ret = wait_event_timeout(the_locks.write_wait,
  737. (atomic_read(&prtd->out_count)),
  738. msecs_to_jiffies(TIMEOUT_MS));
  739. if (!ret) {
  740. pr_err("%s: wait_event_timeout failed\n", __func__);
  741. ret = -ETIMEDOUT;
  742. goto fail;
  743. }
  744. ret = 0;
  745. if (prtd->reset_event) {
  746. pr_err("%s: In SSR return ENETRESET after wait\n",
  747. __func__);
  748. return -ENETRESET;
  749. }
  750. if (!atomic_read(&prtd->out_count)) {
  751. pr_err("%s: pcm stopped out_count 0\n", __func__);
  752. return 0;
  753. }
  754. data = q6asm_is_cpu_buf_avail(IN, prtd->audio_client, &size,
  755. &idx);
  756. if (data == NULL) {
  757. retries++;
  758. continue;
  759. } else {
  760. retries = 0;
  761. }
  762. if (fbytes > size)
  763. xfer = size;
  764. else
  765. xfer = fbytes;
  766. bufptr = data;
  767. if (bufptr) {
  768. pr_debug("%s:fbytes =%lu: xfer=%d size=%d\n",
  769. __func__, fbytes, xfer, size);
  770. if (copy_from_user(bufptr, buf, xfer)) {
  771. ret = -EFAULT;
  772. pr_err("%s: copy_from_user failed\n",
  773. __func__);
  774. q6asm_cpu_buf_release(IN, prtd->audio_client);
  775. goto fail;
  776. }
  777. buf += xfer;
  778. fbytes -= xfer;
  779. pr_debug("%s:fbytes = %lu: xfer=%d\n", __func__,
  780. fbytes, xfer);
  781. if (atomic_read(&prtd->start)) {
  782. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  783. __func__, xfer);
  784. ret = q6asm_write(prtd->audio_client, xfer,
  785. 0, 0, NO_TIMESTAMP);
  786. if (ret < 0) {
  787. ret = -EFAULT;
  788. q6asm_cpu_buf_release(IN,
  789. prtd->audio_client);
  790. goto fail;
  791. }
  792. } else
  793. atomic_inc(&prtd->out_needed);
  794. atomic_dec(&prtd->out_count);
  795. }
  796. }
  797. fail:
  798. if (retries >= MAX_PB_COPY_RETRIES)
  799. ret = -ENOMEM;
  800. return ret;
  801. }
  802. static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
  803. {
  804. struct snd_pcm_runtime *runtime = substream->runtime;
  805. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  806. struct msm_audio *prtd = runtime->private_data;
  807. struct snd_soc_component *component =
  808. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  809. struct msm_plat_data *pdata;
  810. uint32_t timeout;
  811. int dir = 0;
  812. int ret = 0;
  813. pr_debug("%s: cmd_pending 0x%lx\n", __func__, prtd->cmd_pending);
  814. if (!component) {
  815. pr_err("%s: component is NULL\n", __func__);
  816. return -EINVAL;
  817. }
  818. pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
  819. if (!pdata) {
  820. pr_err("%s: platform data is NULL\n", __func__);
  821. return -EINVAL;
  822. }
  823. mutex_lock(&pdata->lock);
  824. if (prtd->audio_client) {
  825. dir = IN;
  826. /*
  827. * Unvote to downgrade the Rx thread priority from
  828. * RT Thread for Low-Latency use case.
  829. */
  830. if (pdata) {
  831. if (pdata->perf_mode == LOW_LATENCY_PCM_MODE)
  832. apr_end_rx_rt(prtd->audio_client->apr);
  833. }
  834. /* determine timeout length */
  835. if (runtime->frame_bits == 0 || runtime->rate == 0) {
  836. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  837. } else {
  838. timeout = (runtime->period_size *
  839. CMD_EOS_TIMEOUT_MULTIPLIER) /
  840. ((runtime->frame_bits / 8) *
  841. runtime->rate);
  842. if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
  843. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  844. }
  845. pr_debug("%s: CMD_EOS timeout is %d\n", __func__, timeout);
  846. ret = wait_event_timeout(the_locks.eos_wait,
  847. !test_bit(CMD_EOS, &prtd->cmd_pending),
  848. timeout);
  849. if (!ret)
  850. pr_err("%s: CMD_EOS failed, cmd_pending 0x%lx\n",
  851. __func__, prtd->cmd_pending);
  852. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  853. q6asm_audio_client_buf_free_contiguous(dir,
  854. prtd->audio_client);
  855. q6asm_audio_client_free(prtd->audio_client);
  856. }
  857. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  858. SNDRV_PCM_STREAM_PLAYBACK);
  859. msm_adsp_clean_mixer_ctl_pp_event_queue(soc_prtd);
  860. kfree(prtd);
  861. runtime->private_data = NULL;
  862. mutex_unlock(&pdata->lock);
  863. return 0;
  864. }
  865. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  866. int channel, unsigned long hwoff, void __user *buf,
  867. unsigned long fbytes)
  868. {
  869. int ret = 0;
  870. int xfer;
  871. char *bufptr;
  872. void *data = NULL;
  873. uint32_t idx = 0;
  874. uint32_t size = 0;
  875. uint32_t offset = 0;
  876. struct snd_pcm_runtime *runtime = substream->runtime;
  877. struct msm_audio *prtd = substream->runtime->private_data;
  878. pr_debug("%s\n", __func__);
  879. pr_debug("appl_ptr %d\n", (int)runtime->control->appl_ptr);
  880. pr_debug("hw_ptr %d\n", (int)runtime->status->hw_ptr);
  881. pr_debug("avail_min %d\n", (int)runtime->control->avail_min);
  882. if (prtd->reset_event) {
  883. pr_err("%s: In SSR return ENETRESET before wait\n", __func__);
  884. return -ENETRESET;
  885. }
  886. ret = wait_event_timeout(the_locks.read_wait,
  887. (atomic_read(&prtd->in_count)),
  888. msecs_to_jiffies(TIMEOUT_MS));
  889. if (!ret) {
  890. pr_debug("%s: wait_event_timeout failed\n", __func__);
  891. goto fail;
  892. }
  893. if (prtd->reset_event) {
  894. pr_err("%s: In SSR return ENETRESET after wait\n", __func__);
  895. return -ENETRESET;
  896. }
  897. if (!atomic_read(&prtd->in_count)) {
  898. pr_debug("%s: pcm stopped in_count 0\n", __func__);
  899. return 0;
  900. }
  901. pr_debug("Checking if valid buffer is available...%pK\n",
  902. data);
  903. data = q6asm_is_cpu_buf_avail(OUT, prtd->audio_client, &size, &idx);
  904. bufptr = data;
  905. pr_debug("Size = %d\n", size);
  906. pr_debug("fbytes = %lu\n", fbytes);
  907. pr_debug("idx = %d\n", idx);
  908. if (bufptr) {
  909. xfer = fbytes;
  910. if (xfer > size)
  911. xfer = size;
  912. offset = prtd->in_frame_info[idx].offset;
  913. pr_debug("Offset value = %d\n", offset);
  914. if (copy_to_user(buf, bufptr+offset, xfer)) {
  915. pr_err("Failed to copy buf to user\n");
  916. ret = -EFAULT;
  917. q6asm_cpu_buf_release(OUT, prtd->audio_client);
  918. goto fail;
  919. }
  920. fbytes -= xfer;
  921. size -= xfer;
  922. prtd->in_frame_info[idx].offset += xfer;
  923. pr_debug("%s:fbytes = %lu: size=%d: xfer=%d\n",
  924. __func__, fbytes, size, xfer);
  925. pr_debug(" Sending next buffer to dsp\n");
  926. memset(&prtd->in_frame_info[idx], 0,
  927. sizeof(struct msm_audio_in_frame_info));
  928. atomic_dec(&prtd->in_count);
  929. ret = q6asm_read(prtd->audio_client);
  930. if (ret < 0) {
  931. pr_err("q6asm read failed\n");
  932. ret = -EFAULT;
  933. q6asm_cpu_buf_release(OUT, prtd->audio_client);
  934. goto fail;
  935. }
  936. } else
  937. pr_err("No valid buffer\n");
  938. pr_debug("Returning from capture_copy... %d\n", ret);
  939. fail:
  940. return ret;
  941. }
  942. static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
  943. {
  944. struct snd_pcm_runtime *runtime = substream->runtime;
  945. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  946. struct msm_audio *prtd = runtime->private_data;
  947. struct snd_soc_component *component =
  948. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  949. struct msm_plat_data *pdata;
  950. int dir = OUT;
  951. pr_debug("%s\n", __func__);
  952. if (!component) {
  953. pr_err("%s: component is NULL\n", __func__);
  954. return -EINVAL;
  955. }
  956. pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
  957. if (!pdata) {
  958. pr_err("%s: platform data is NULL\n", __func__);
  959. return -EINVAL;
  960. }
  961. mutex_lock(&pdata->lock);
  962. if (prtd->audio_client) {
  963. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  964. q6asm_audio_client_buf_free_contiguous(dir,
  965. prtd->audio_client);
  966. q6asm_audio_client_free(prtd->audio_client);
  967. }
  968. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  969. SNDRV_PCM_STREAM_CAPTURE);
  970. kfree(prtd);
  971. runtime->private_data = NULL;
  972. mutex_unlock(&pdata->lock);
  973. return 0;
  974. }
  975. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  976. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  977. {
  978. int ret = 0;
  979. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  980. ret = msm_pcm_playback_copy(substream, a, hwoff, buf, fbytes);
  981. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  982. ret = msm_pcm_capture_copy(substream, a, hwoff, buf, fbytes);
  983. return ret;
  984. }
  985. static int msm_pcm_close(struct snd_pcm_substream *substream)
  986. {
  987. int ret = 0;
  988. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  989. ret = msm_pcm_playback_close(substream);
  990. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  991. ret = msm_pcm_capture_close(substream);
  992. return ret;
  993. }
  994. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  995. {
  996. int ret = 0;
  997. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  998. ret = msm_pcm_playback_prepare(substream);
  999. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  1000. ret = msm_pcm_capture_prepare(substream);
  1001. return ret;
  1002. }
  1003. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  1004. {
  1005. struct snd_pcm_runtime *runtime = substream->runtime;
  1006. struct msm_audio *prtd = runtime->private_data;
  1007. if (prtd->pcm_irq_pos >= prtd->pcm_size)
  1008. prtd->pcm_irq_pos = 0;
  1009. pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
  1010. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  1011. }
  1012. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  1013. struct vm_area_struct *vma)
  1014. {
  1015. struct snd_pcm_runtime *runtime = substream->runtime;
  1016. struct msm_audio *prtd = runtime->private_data;
  1017. struct audio_client *ac = prtd->audio_client;
  1018. struct audio_port_data *apd = ac->port;
  1019. struct audio_buffer *ab;
  1020. int dir = -1;
  1021. prtd->mmap_flag = 1;
  1022. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1023. dir = IN;
  1024. else
  1025. dir = OUT;
  1026. ab = &(apd[dir].buf[0]);
  1027. return msm_audio_ion_mmap(ab, vma);
  1028. }
  1029. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  1030. struct snd_pcm_hw_params *params)
  1031. {
  1032. struct snd_pcm_runtime *runtime = substream->runtime;
  1033. struct msm_audio *prtd = runtime->private_data;
  1034. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  1035. struct audio_buffer *buf;
  1036. int dir, ret;
  1037. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1038. dir = IN;
  1039. else
  1040. dir = OUT;
  1041. ret = q6asm_audio_client_buf_alloc_contiguous(dir,
  1042. prtd->audio_client,
  1043. (params_buffer_bytes(params) / params_periods(params)),
  1044. params_periods(params));
  1045. if (ret < 0) {
  1046. pr_err("Audio Start: Buffer Allocation failed rc = %d\n",
  1047. ret);
  1048. return -ENOMEM;
  1049. }
  1050. buf = prtd->audio_client->port[dir].buf;
  1051. if (buf == NULL || buf[0].data == NULL)
  1052. return -ENOMEM;
  1053. pr_debug("%s:buf = %pK\n", __func__, buf);
  1054. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  1055. dma_buf->dev.dev = substream->pcm->card->dev;
  1056. dma_buf->private_data = NULL;
  1057. dma_buf->area = buf[0].data;
  1058. dma_buf->addr = buf[0].phys;
  1059. dma_buf->bytes = params_buffer_bytes(params);
  1060. if (!dma_buf->area)
  1061. return -ENOMEM;
  1062. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  1063. return 0;
  1064. }
  1065. static int msm_pcm_ioctl(struct snd_pcm_substream *substream,
  1066. unsigned int cmd, void __user *arg)
  1067. {
  1068. struct msm_audio *prtd = NULL;
  1069. struct snd_soc_pcm_runtime *rtd = NULL;
  1070. uint64_t ses_time = 0, abs_time = 0;
  1071. int64_t av_offset = 0;
  1072. int32_t clock_id = -EINVAL;
  1073. int rc = 0;
  1074. struct snd_pcm_prsnt_position userarg;
  1075. if (!substream || !substream->private_data) {
  1076. pr_err("%s: Invalid %s\n", __func__,
  1077. (!substream) ? "substream" : "private_data");
  1078. return -EINVAL;
  1079. }
  1080. if (!substream->runtime) {
  1081. pr_err("%s substream runtime not found\n", __func__);
  1082. return -EINVAL;
  1083. }
  1084. prtd = substream->runtime->private_data;
  1085. if (!prtd) {
  1086. pr_err("%s prtd is null.\n", __func__);
  1087. return -EINVAL;
  1088. }
  1089. rtd = substream->private_data;
  1090. switch (cmd) {
  1091. case SNDRV_PCM_IOCTL_DSP_POSITION:
  1092. dev_dbg(rtd->dev, "%s: SNDRV_PCM_DSP_POSITION", __func__);
  1093. if (!arg) {
  1094. dev_err(rtd->dev, "%s: Invalid params DSP_POSITION\n",
  1095. __func__);
  1096. rc = -EINVAL;
  1097. goto done;
  1098. }
  1099. memset(&userarg, 0, sizeof(userarg));
  1100. if (copy_from_user(&userarg, arg, sizeof(userarg))) {
  1101. dev_err(rtd->dev, "%s: err copyuser DSP_POSITION\n",
  1102. __func__);
  1103. rc = -EFAULT;
  1104. goto done;
  1105. }
  1106. clock_id = userarg.clock_id;
  1107. rc = q6asm_get_session_time_v2(prtd->audio_client, &ses_time,
  1108. &abs_time);
  1109. if (rc) {
  1110. pr_err("%s: q6asm_get_session_time_v2 failed, rc=%d\n",
  1111. __func__, rc);
  1112. goto done;
  1113. }
  1114. userarg.frames = div64_u64((ses_time * prtd->samp_rate),
  1115. 1000000);
  1116. rc = avcs_core_query_timer_offset(&av_offset, clock_id);
  1117. if (rc) {
  1118. pr_err("%s: avcs offset query failed, rc=%d\n",
  1119. __func__, rc);
  1120. goto done;
  1121. }
  1122. userarg.timestamp = abs_time + av_offset;
  1123. if (copy_to_user(arg, &userarg, sizeof(userarg))) {
  1124. dev_err(rtd->dev, "%s: err copy to user DSP_POSITION\n",
  1125. __func__);
  1126. rc = -EFAULT;
  1127. goto done;
  1128. }
  1129. pr_debug("%s, vals f %lld, t %lld, avoff %lld, abst %lld, sess_time %llu sr %d\n",
  1130. __func__, userarg.frames, userarg.timestamp,
  1131. av_offset, abs_time, ses_time, prtd->samp_rate);
  1132. break;
  1133. default:
  1134. rc = snd_pcm_lib_ioctl(substream, cmd, arg);
  1135. break;
  1136. }
  1137. done:
  1138. return rc;
  1139. }
  1140. #ifdef CONFIG_COMPAT
  1141. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1142. static int msm_pcm_compat_ioctl(struct snd_pcm_substream *substream,
  1143. unsigned int cmd, void __user *arg)
  1144. {
  1145. return msm_pcm_ioctl(substream, cmd, arg);
  1146. }
  1147. #endif /* CONFIG_AUDIO_QGKI */
  1148. #else
  1149. #define msm_pcm_compat_ioctl NULL
  1150. #endif
  1151. static const struct snd_pcm_ops msm_pcm_ops = {
  1152. .open = msm_pcm_open,
  1153. .copy_user = msm_pcm_copy,
  1154. .hw_params = msm_pcm_hw_params,
  1155. .close = msm_pcm_close,
  1156. .ioctl = msm_pcm_ioctl,
  1157. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1158. .compat_ioctl = msm_pcm_compat_ioctl,
  1159. #endif /* CONFIG_AUDIO_QGKI */
  1160. .prepare = msm_pcm_prepare,
  1161. .trigger = msm_pcm_trigger,
  1162. .pointer = msm_pcm_pointer,
  1163. .mmap = msm_pcm_mmap,
  1164. };
  1165. static int msm_pcm_adsp_stream_cmd_put(struct snd_kcontrol *kcontrol,
  1166. struct snd_ctl_elem_value *ucontrol)
  1167. {
  1168. struct snd_soc_component *component =
  1169. snd_soc_kcontrol_component(kcontrol);
  1170. struct msm_plat_data *pdata = NULL;
  1171. struct snd_pcm_substream *substream;
  1172. struct msm_audio *prtd;
  1173. int ret = 0;
  1174. struct msm_adsp_event_data *event_data = NULL;
  1175. if (!component) {
  1176. pr_err("%s: component is NULL\n", __func__);
  1177. return -EINVAL;
  1178. }
  1179. pdata = dev_get_drvdata(component->dev);
  1180. if (!pdata) {
  1181. pr_err("%s pdata is NULL\n", __func__);
  1182. return -ENODEV;
  1183. }
  1184. mutex_lock(&pdata->lock);
  1185. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1186. if (!substream) {
  1187. pr_err("%s substream not found\n", __func__);
  1188. ret = -EINVAL;
  1189. goto done;
  1190. }
  1191. if (!substream->runtime) {
  1192. pr_err("%s substream runtime not found\n", __func__);
  1193. ret = -EINVAL;
  1194. goto done;
  1195. }
  1196. prtd = substream->runtime->private_data;
  1197. if (prtd == NULL) {
  1198. pr_err("%s prtd is null.\n", __func__);
  1199. ret = -EINVAL;
  1200. goto done;
  1201. }
  1202. if (prtd->audio_client == NULL) {
  1203. pr_err("%s prtd is null.\n", __func__);
  1204. ret = -EINVAL;
  1205. goto done;
  1206. }
  1207. event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
  1208. if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
  1209. (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
  1210. pr_err("%s: invalid event_type=%d",
  1211. __func__, event_data->event_type);
  1212. ret = -EINVAL;
  1213. goto done;
  1214. }
  1215. if (event_data->payload_len > sizeof(ucontrol->value.bytes.data)
  1216. - sizeof(struct msm_adsp_event_data)) {
  1217. pr_err("%s param length=%d exceeds limit",
  1218. __func__, event_data->payload_len);
  1219. ret = -EINVAL;
  1220. goto done;
  1221. }
  1222. ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
  1223. if (ret < 0)
  1224. pr_err("%s: failed to send stream event cmd, err = %d\n",
  1225. __func__, ret);
  1226. done:
  1227. mutex_unlock(&pdata->lock);
  1228. return ret;
  1229. }
  1230. static int msm_pcm_add_audio_adsp_stream_cmd_control(
  1231. struct snd_soc_pcm_runtime *rtd)
  1232. {
  1233. struct snd_soc_component *component = NULL;
  1234. const char *mixer_ctl_name = DSP_STREAM_CMD;
  1235. const char *deviceNo = "NN";
  1236. char *mixer_str = NULL;
  1237. int ctl_len = 0, ret = 0;
  1238. struct snd_kcontrol_new fe_audio_adsp_stream_cmd_config_control[1] = {
  1239. {
  1240. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1241. .name = "?",
  1242. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1243. .info = msm_adsp_stream_cmd_info,
  1244. .put = msm_pcm_adsp_stream_cmd_put,
  1245. .private_value = 0,
  1246. }
  1247. };
  1248. if (!rtd) {
  1249. pr_err("%s rtd is NULL\n", __func__);
  1250. ret = -EINVAL;
  1251. goto done;
  1252. }
  1253. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1254. if (!component) {
  1255. pr_err("%s: component is NULL\n", __func__);
  1256. return -EINVAL;
  1257. }
  1258. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1259. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1260. if (!mixer_str) {
  1261. ret = -ENOMEM;
  1262. goto done;
  1263. }
  1264. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1265. fe_audio_adsp_stream_cmd_config_control[0].name = mixer_str;
  1266. fe_audio_adsp_stream_cmd_config_control[0].private_value =
  1267. rtd->dai_link->id;
  1268. pr_debug("Registering new mixer ctl %s\n", mixer_str);
  1269. ret = snd_soc_add_component_controls(component,
  1270. fe_audio_adsp_stream_cmd_config_control,
  1271. ARRAY_SIZE(fe_audio_adsp_stream_cmd_config_control));
  1272. if (ret < 0)
  1273. pr_err("%s: failed add ctl %s. err = %d\n",
  1274. __func__, mixer_str, ret);
  1275. kfree(mixer_str);
  1276. done:
  1277. return ret;
  1278. }
  1279. static int msm_pcm_add_audio_adsp_stream_callback_control(
  1280. struct snd_soc_pcm_runtime *rtd)
  1281. {
  1282. struct snd_soc_component *component = NULL;
  1283. const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
  1284. const char *deviceNo = "NN";
  1285. char *mixer_str = NULL;
  1286. int ctl_len = 0, ret = 0;
  1287. struct snd_kcontrol *kctl;
  1288. struct snd_kcontrol_new fe_audio_adsp_callback_config_control[1] = {
  1289. {
  1290. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1291. .name = "?",
  1292. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1293. .info = msm_adsp_stream_callback_info,
  1294. .get = msm_adsp_stream_callback_get,
  1295. .private_value = 0,
  1296. }
  1297. };
  1298. if (!rtd) {
  1299. pr_err("%s NULL rtd\n", __func__);
  1300. ret = -EINVAL;
  1301. goto done;
  1302. }
  1303. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1304. if (!component) {
  1305. pr_err("%s: component is NULL\n", __func__);
  1306. return -EINVAL;
  1307. }
  1308. pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
  1309. __func__, rtd->dai_link->name, rtd->dai_link->id,
  1310. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  1311. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1312. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1313. if (!mixer_str) {
  1314. ret = -ENOMEM;
  1315. goto done;
  1316. }
  1317. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1318. fe_audio_adsp_callback_config_control[0].name = mixer_str;
  1319. fe_audio_adsp_callback_config_control[0].private_value =
  1320. rtd->dai_link->id;
  1321. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1322. ret = snd_soc_add_component_controls(component,
  1323. fe_audio_adsp_callback_config_control,
  1324. ARRAY_SIZE(fe_audio_adsp_callback_config_control));
  1325. if (ret < 0) {
  1326. pr_err("%s: failed to add ctl %s. err = %d\n",
  1327. __func__, mixer_str, ret);
  1328. ret = -EINVAL;
  1329. goto free_mixer_str;
  1330. }
  1331. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  1332. if (!kctl) {
  1333. pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
  1334. ret = -EINVAL;
  1335. goto free_mixer_str;
  1336. }
  1337. kctl->private_data = NULL;
  1338. free_mixer_str:
  1339. kfree(mixer_str);
  1340. done:
  1341. return ret;
  1342. }
  1343. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1344. static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
  1345. {
  1346. int rc = 0;
  1347. if (prtd && prtd->audio_client) {
  1348. pr_debug("%s: channels %d volume 0x%x\n", __func__,
  1349. prtd->channel_mode, volume);
  1350. rc = q6asm_set_volume(prtd->audio_client, volume);
  1351. if (rc < 0) {
  1352. pr_err("%s: Send Volume command failed rc=%d\n",
  1353. __func__, rc);
  1354. }
  1355. }
  1356. return rc;
  1357. }
  1358. static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
  1359. struct snd_ctl_elem_value *ucontrol)
  1360. {
  1361. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  1362. struct msm_plat_data *pdata = NULL;
  1363. struct snd_pcm_substream *substream = NULL;
  1364. struct snd_soc_pcm_runtime *soc_prtd = NULL;
  1365. struct snd_soc_component *component = NULL;
  1366. struct msm_audio *prtd;
  1367. pr_debug("%s\n", __func__);
  1368. if (!vol) {
  1369. pr_err("%s: vol is NULL\n", __func__);
  1370. return -ENODEV;
  1371. }
  1372. substream = vol->pcm->streams[vol->stream].substream;
  1373. if (!substream) {
  1374. pr_err("%s substream not found\n", __func__);
  1375. return -ENODEV;
  1376. }
  1377. soc_prtd = substream->private_data;
  1378. if (!substream->runtime || !soc_prtd) {
  1379. pr_debug("%s substream runtime or private_data not found\n",
  1380. __func__);
  1381. return 0;
  1382. }
  1383. component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  1384. if (!component) {
  1385. pr_err("%s: component is NULL\n", __func__);
  1386. return -EINVAL;
  1387. }
  1388. pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
  1389. if (!pdata) {
  1390. pr_err("%s: pdata not found\n", __func__);
  1391. return -ENODEV;
  1392. }
  1393. mutex_lock(&pdata->lock);
  1394. prtd = substream->runtime->private_data;
  1395. if (prtd)
  1396. ucontrol->value.integer.value[0] = prtd->volume;
  1397. mutex_unlock(&pdata->lock);
  1398. return 0;
  1399. }
  1400. static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
  1401. struct snd_ctl_elem_value *ucontrol)
  1402. {
  1403. int rc = 0;
  1404. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  1405. struct msm_plat_data *pdata = NULL;
  1406. struct snd_pcm_substream *substream =
  1407. vol->pcm->streams[vol->stream].substream;
  1408. struct snd_soc_pcm_runtime *soc_prtd = NULL;
  1409. struct snd_soc_component *component = NULL;
  1410. struct msm_audio *prtd;
  1411. int volume = ucontrol->value.integer.value[0];
  1412. pr_debug("%s: volume : 0x%x\n", __func__, volume);
  1413. if (!substream) {
  1414. pr_err("%s: substream not found\n", __func__);
  1415. return -ENODEV;
  1416. }
  1417. soc_prtd = substream->private_data;
  1418. if (!substream->runtime || !soc_prtd) {
  1419. pr_err("%s: substream runtime or private_data not found\n",
  1420. __func__);
  1421. return 0;
  1422. }
  1423. component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  1424. if (!component) {
  1425. pr_err("%s: component is NULL\n", __func__);
  1426. return -EINVAL;
  1427. }
  1428. pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
  1429. if (!pdata) {
  1430. pr_err("%s: pdata not found\n", __func__);
  1431. return -ENODEV;
  1432. }
  1433. mutex_lock(&pdata->lock);
  1434. prtd = substream->runtime->private_data;
  1435. if (prtd) {
  1436. rc = msm_pcm_set_volume(prtd, volume);
  1437. prtd->volume = volume;
  1438. }
  1439. mutex_unlock(&pdata->lock);
  1440. return rc;
  1441. }
  1442. static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd,
  1443. int stream)
  1444. {
  1445. int ret = 0;
  1446. struct snd_pcm *pcm = rtd->pcm;
  1447. struct snd_pcm_volume *volume_info;
  1448. struct snd_kcontrol *kctl;
  1449. dev_dbg(rtd->dev, "%s, volume control add\n", __func__);
  1450. ret = snd_pcm_add_volume_ctls(pcm, stream,
  1451. NULL, 1, rtd->dai_link->id,
  1452. &volume_info);
  1453. if (ret < 0) {
  1454. pr_err("%s volume control failed ret %d\n", __func__, ret);
  1455. return ret;
  1456. }
  1457. kctl = volume_info->kctl;
  1458. kctl->put = msm_pcm_volume_ctl_put;
  1459. kctl->get = msm_pcm_volume_ctl_get;
  1460. kctl->tlv.p = msm_pcm_vol_gain;
  1461. return 0;
  1462. }
  1463. #else
  1464. static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd,
  1465. int stream)
  1466. {
  1467. return 0;
  1468. }
  1469. #endif /* CONFIG_AUDIO_QGKI */
  1470. static int msm_pcm_compress_ctl_info(struct snd_kcontrol *kcontrol,
  1471. struct snd_ctl_elem_info *uinfo)
  1472. {
  1473. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1474. uinfo->count = 1;
  1475. uinfo->value.integer.min = 0;
  1476. uinfo->value.integer.max = 0x2000;
  1477. return 0;
  1478. }
  1479. static int msm_pcm_compress_ctl_get(struct snd_kcontrol *kcontrol,
  1480. struct snd_ctl_elem_value *ucontrol)
  1481. {
  1482. struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
  1483. struct msm_plat_data *pdata = dev_get_drvdata(comp->dev);
  1484. struct snd_pcm_substream *substream;
  1485. struct msm_audio *prtd;
  1486. if (!pdata) {
  1487. pr_err("%s pdata is NULL\n", __func__);
  1488. return -ENODEV;
  1489. }
  1490. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1491. if (!substream) {
  1492. pr_err("%s substream not found\n", __func__);
  1493. return -EINVAL;
  1494. }
  1495. if (!substream->runtime) {
  1496. pr_debug("%s substream runtime not found\n", __func__);
  1497. return 0;
  1498. }
  1499. mutex_lock(&pdata->lock);
  1500. prtd = substream->runtime->private_data;
  1501. if (prtd)
  1502. ucontrol->value.integer.value[0] = prtd->compress_enable;
  1503. mutex_unlock(&pdata->lock);
  1504. return 0;
  1505. }
  1506. static int msm_pcm_compress_ctl_put(struct snd_kcontrol *kcontrol,
  1507. struct snd_ctl_elem_value *ucontrol)
  1508. {
  1509. int rc = 0;
  1510. struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
  1511. struct msm_plat_data *pdata = dev_get_drvdata(comp->dev);
  1512. struct snd_pcm_substream *substream;
  1513. struct msm_audio *prtd;
  1514. int compress = ucontrol->value.integer.value[0];
  1515. if (!pdata) {
  1516. pr_err("%s pdata is NULL\n", __func__);
  1517. return -ENODEV;
  1518. }
  1519. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1520. pr_debug("%s: compress : 0x%x\n", __func__, compress);
  1521. if (!substream) {
  1522. pr_err("%s substream not found\n", __func__);
  1523. return -EINVAL;
  1524. }
  1525. if (!substream->runtime) {
  1526. pr_err("%s substream runtime not found\n", __func__);
  1527. return 0;
  1528. }
  1529. mutex_lock(&pdata->lock);
  1530. prtd = substream->runtime->private_data;
  1531. if (prtd) {
  1532. pr_debug("%s: setting compress flag to 0x%x\n",
  1533. __func__, compress);
  1534. prtd->compress_enable = compress;
  1535. }
  1536. mutex_unlock(&pdata->lock);
  1537. return rc;
  1538. }
  1539. static int msm_pcm_add_compress_control(struct snd_soc_pcm_runtime *rtd)
  1540. {
  1541. struct snd_soc_component *component = NULL;
  1542. const char *mixer_ctl_name = "Playback ";
  1543. const char *mixer_ctl_end_name = " Compress";
  1544. const char *deviceNo = "NN";
  1545. char *mixer_str = NULL;
  1546. int ctl_len;
  1547. int ret = 0;
  1548. struct msm_plat_data *pdata;
  1549. struct snd_kcontrol_new pcm_compress_control[1] = {
  1550. {
  1551. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1552. .name = "?",
  1553. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1554. .info = msm_pcm_compress_ctl_info,
  1555. .get = msm_pcm_compress_ctl_get,
  1556. .put = msm_pcm_compress_ctl_put,
  1557. .private_value = 0,
  1558. }
  1559. };
  1560. if (!rtd) {
  1561. pr_err("%s: NULL rtd\n", __func__);
  1562. return -EINVAL;
  1563. }
  1564. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1565. if (!component) {
  1566. pr_err("%s: component is NULL\n", __func__);
  1567. return -EINVAL;
  1568. }
  1569. ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) +
  1570. strlen(mixer_ctl_end_name) + 1;
  1571. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1572. if (!mixer_str)
  1573. return -ENOMEM;
  1574. snprintf(mixer_str, ctl_len, "%s%d%s", mixer_ctl_name,
  1575. rtd->pcm->device, mixer_ctl_end_name);
  1576. pcm_compress_control[0].name = mixer_str;
  1577. pcm_compress_control[0].private_value = rtd->dai_link->id;
  1578. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1579. pdata = dev_get_drvdata(component->dev);
  1580. if (pdata) {
  1581. if (!pdata->pcm) {
  1582. pdata->pcm = rtd->pcm;
  1583. snd_soc_add_component_controls(component,
  1584. pcm_compress_control,
  1585. ARRAY_SIZE
  1586. (pcm_compress_control));
  1587. pr_debug("%s: add control success plt = %pK\n",
  1588. __func__, component);
  1589. }
  1590. } else {
  1591. pr_err("%s: NULL pdata\n", __func__);
  1592. ret = -EINVAL;
  1593. }
  1594. kfree(mixer_str);
  1595. return ret;
  1596. }
  1597. static int msm_pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
  1598. struct snd_ctl_elem_value *ucontrol)
  1599. {
  1600. int i;
  1601. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1602. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1603. struct snd_pcm_substream *substream;
  1604. struct msm_audio *prtd;
  1605. struct snd_soc_pcm_runtime *rtd = NULL;
  1606. struct msm_plat_data *pdata = NULL;
  1607. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  1608. struct snd_soc_component *component = NULL;
  1609. u64 fe_id = 0;
  1610. pr_debug("%s", __func__);
  1611. substream = snd_pcm_chmap_substream(info, idx);
  1612. if (!substream)
  1613. return -ENODEV;
  1614. rtd = substream->private_data;
  1615. if (rtd) {
  1616. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1617. if (component) {
  1618. pdata = (struct msm_plat_data *)
  1619. dev_get_drvdata(component->dev);
  1620. if (!pdata) {
  1621. pr_err("%s: pdata not found\n", __func__);
  1622. return -ENODEV;
  1623. }
  1624. } else {
  1625. pr_err("%s: component is NULL\n", __func__);
  1626. return -EINVAL;
  1627. }
  1628. }
  1629. if (!rtd)
  1630. return 0;
  1631. mutex_lock(&pdata->lock);
  1632. prtd = substream->runtime ? substream->runtime->private_data : NULL;
  1633. if (prtd) {
  1634. prtd->set_channel_map = true;
  1635. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1636. prtd->channel_map[i] =
  1637. (char)(ucontrol->value.integer.value[i]);
  1638. /* update chmixer_pspd chmap cached with routing driver as well */
  1639. if (rtd) {
  1640. if (component) {
  1641. fe_id = rtd->dai_link->id;
  1642. chmixer_pspd = pdata ?
  1643. pdata->chmixer_pspd[fe_id][SESSION_TYPE_RX] : NULL;
  1644. if (chmixer_pspd && chmixer_pspd->enable) {
  1645. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1646. chmixer_pspd->in_ch_map[i] = prtd->channel_map[i];
  1647. chmixer_pspd->override_in_ch_map = true;
  1648. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  1649. SESSION_TYPE_RX, chmixer_pspd);
  1650. }
  1651. }
  1652. }
  1653. }
  1654. mutex_unlock(&pdata->lock);
  1655. return 0;
  1656. }
  1657. static int msm_pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  1658. struct snd_ctl_elem_value *ucontrol)
  1659. {
  1660. int i;
  1661. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1662. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1663. struct snd_pcm_substream *substream;
  1664. struct msm_audio *prtd;
  1665. struct snd_soc_pcm_runtime *rtd = NULL;
  1666. struct msm_plat_data *pdata = NULL;
  1667. struct snd_soc_component *component = NULL;
  1668. pr_debug("%s", __func__);
  1669. substream = snd_pcm_chmap_substream(info, idx);
  1670. if (!substream)
  1671. return -ENODEV;
  1672. rtd = substream->private_data;
  1673. if (rtd) {
  1674. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1675. if (component) {
  1676. pdata = (struct msm_plat_data *)
  1677. dev_get_drvdata(component->dev);
  1678. if (!pdata) {
  1679. pr_err("%s: pdata not found\n", __func__);
  1680. return -ENODEV;
  1681. }
  1682. } else {
  1683. pr_err("%s: component is NULL\n", __func__);
  1684. return -EINVAL;
  1685. }
  1686. }
  1687. memset(ucontrol->value.integer.value, 0,
  1688. sizeof(ucontrol->value.integer.value));
  1689. if (!rtd)
  1690. return 0; /* no channels set */
  1691. mutex_lock(&pdata->lock);
  1692. prtd = substream->runtime ? substream->runtime->private_data : NULL;
  1693. if (prtd && prtd->set_channel_map == true) {
  1694. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1695. ucontrol->value.integer.value[i] =
  1696. (int)prtd->channel_map[i];
  1697. } else {
  1698. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1699. ucontrol->value.integer.value[i] = 0;
  1700. }
  1701. mutex_unlock(&pdata->lock);
  1702. return 0;
  1703. }
  1704. static int msm_pcm_add_chmap_controls(struct snd_soc_pcm_runtime *rtd)
  1705. {
  1706. struct snd_pcm *pcm = rtd->pcm;
  1707. struct snd_pcm_chmap *chmap_info;
  1708. struct snd_kcontrol *kctl;
  1709. char device_num[12];
  1710. int i, ret = 0;
  1711. pr_debug("%s, Channel map cntrl add\n", __func__);
  1712. ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1713. snd_pcm_std_chmaps,
  1714. PCM_FORMAT_MAX_NUM_CHANNEL_V8, 0,
  1715. &chmap_info);
  1716. if (ret < 0) {
  1717. pr_err("%s, channel map cntrl add failed\n", __func__);
  1718. return ret;
  1719. }
  1720. kctl = chmap_info->kctl;
  1721. for (i = 0; i < kctl->count; i++)
  1722. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  1723. snprintf(device_num, sizeof(device_num), "%d", pcm->device);
  1724. strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
  1725. pr_debug("%s, Overwriting channel map control name to: %s\n",
  1726. __func__, kctl->id.name);
  1727. kctl->put = msm_pcm_chmap_ctl_put;
  1728. kctl->get = msm_pcm_chmap_ctl_get;
  1729. return 0;
  1730. }
  1731. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1732. static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1733. struct snd_ctl_elem_value *ucontrol)
  1734. {
  1735. u64 fe_id = kcontrol->private_value;
  1736. int session_type = SESSION_TYPE_RX;
  1737. int be_id = ucontrol->value.integer.value[3];
  1738. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1739. int ret = 0;
  1740. cfg_data.app_type = ucontrol->value.integer.value[0];
  1741. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1742. if (ucontrol->value.integer.value[2] != 0)
  1743. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1744. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1745. __func__, fe_id, session_type, be_id,
  1746. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1747. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1748. be_id, &cfg_data);
  1749. if (ret < 0)
  1750. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1751. __func__, ret);
  1752. return ret;
  1753. }
  1754. static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1755. struct snd_ctl_elem_value *ucontrol)
  1756. {
  1757. u64 fe_id = kcontrol->private_value;
  1758. int session_type = SESSION_TYPE_RX;
  1759. int be_id = 0;
  1760. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1761. int ret = 0;
  1762. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1763. &be_id, &cfg_data);
  1764. if (ret < 0) {
  1765. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1766. __func__, ret);
  1767. goto done;
  1768. }
  1769. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1770. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1771. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1772. ucontrol->value.integer.value[3] = be_id;
  1773. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1774. __func__, fe_id, session_type, be_id,
  1775. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1776. done:
  1777. return ret;
  1778. }
  1779. static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1780. struct snd_ctl_elem_value *ucontrol)
  1781. {
  1782. u64 fe_id = kcontrol->private_value;
  1783. int session_type = SESSION_TYPE_TX;
  1784. int be_id = ucontrol->value.integer.value[3];
  1785. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1786. int ret = 0;
  1787. cfg_data.app_type = ucontrol->value.integer.value[0];
  1788. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1789. if (ucontrol->value.integer.value[2] != 0)
  1790. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1791. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1792. __func__, fe_id, session_type, be_id,
  1793. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1794. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1795. be_id, &cfg_data);
  1796. if (ret < 0)
  1797. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1798. __func__, ret);
  1799. return ret;
  1800. }
  1801. static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1802. struct snd_ctl_elem_value *ucontrol)
  1803. {
  1804. u64 fe_id = kcontrol->private_value;
  1805. int session_type = SESSION_TYPE_TX;
  1806. int be_id = 0;
  1807. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1808. int ret = 0;
  1809. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1810. &be_id, &cfg_data);
  1811. if (ret < 0) {
  1812. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1813. __func__, ret);
  1814. goto done;
  1815. }
  1816. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1817. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1818. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1819. ucontrol->value.integer.value[3] = be_id;
  1820. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1821. __func__, fe_id, session_type, be_id,
  1822. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1823. done:
  1824. return ret;
  1825. }
  1826. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  1827. {
  1828. struct snd_pcm *pcm = rtd->pcm;
  1829. struct snd_pcm_usr *app_type_info;
  1830. struct snd_kcontrol *kctl;
  1831. const char *playback_mixer_ctl_name = "Audio Stream";
  1832. const char *capture_mixer_ctl_name = "Audio Stream Capture";
  1833. const char *deviceNo = "NN";
  1834. const char *suffix = "App Type Cfg";
  1835. int ctl_len, ret = 0;
  1836. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  1837. ctl_len = strlen(playback_mixer_ctl_name) + 1 +
  1838. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1839. pr_debug("%s: Playback app type cntrl add\n", __func__);
  1840. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1841. NULL, 1, ctl_len, rtd->dai_link->id,
  1842. &app_type_info);
  1843. if (ret < 0) {
  1844. pr_err("%s: playback app type cntrl add failed: %d\n",
  1845. __func__, ret);
  1846. return ret;
  1847. }
  1848. kctl = app_type_info->kctl;
  1849. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1850. playback_mixer_ctl_name, rtd->pcm->device, suffix);
  1851. kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
  1852. kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
  1853. }
  1854. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  1855. ctl_len = strlen(capture_mixer_ctl_name) + 1 +
  1856. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1857. pr_debug("%s: Capture app type cntrl add\n", __func__);
  1858. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1859. NULL, 1, ctl_len, rtd->dai_link->id,
  1860. &app_type_info);
  1861. if (ret < 0) {
  1862. pr_err("%s: capture app type cntrl add failed: %d\n",
  1863. __func__, ret);
  1864. return ret;
  1865. }
  1866. kctl = app_type_info->kctl;
  1867. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1868. capture_mixer_ctl_name, rtd->pcm->device, suffix);
  1869. kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
  1870. kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
  1871. }
  1872. return 0;
  1873. }
  1874. #else
  1875. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  1876. {
  1877. return 0;
  1878. }
  1879. #endif /* CONFIG_AUDIO_QGKI */
  1880. static struct msm_pcm_channel_mixer *msm_pcm_get_chmixer(
  1881. struct msm_plat_data *pdata,
  1882. u64 fe_id, int session_type)
  1883. {
  1884. if (!pdata) {
  1885. pr_err("%s: missing pdata\n", __func__);
  1886. return NULL;
  1887. }
  1888. if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
  1889. pr_err("%s: invalid FE %llu\n", __func__, fe_id);
  1890. return NULL;
  1891. }
  1892. if ((session_type != SESSION_TYPE_TX) &&
  1893. (session_type != SESSION_TYPE_RX)) {
  1894. pr_err("%s: invalid session type %d\n", __func__, session_type);
  1895. return NULL;
  1896. }
  1897. return pdata->chmixer_pspd[fe_id][session_type];
  1898. }
  1899. static int msm_pcm_channel_mixer_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1900. struct snd_ctl_elem_value *ucontrol)
  1901. {
  1902. u64 fe_id = kcontrol->private_value & 0xFF;
  1903. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1904. int ret = 0;
  1905. int stream_id = 0;
  1906. int be_id = 0, i = 0;
  1907. struct msm_audio *prtd = NULL;
  1908. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1909. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1910. struct snd_pcm *pcm = NULL;
  1911. struct snd_pcm_substream *substream = NULL;
  1912. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  1913. u8 asm_ch_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8] = {0};
  1914. bool reset_override_out_ch_map = false;
  1915. bool reset_override_in_ch_map = false;
  1916. pcm = pdata->pcm_device[fe_id];
  1917. if (!pcm) {
  1918. pr_err("%s invalid pcm handle for fe_id %llu\n",
  1919. __func__, fe_id);
  1920. return -EINVAL;
  1921. }
  1922. if (session_type == SESSION_TYPE_RX)
  1923. substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1924. else
  1925. substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1926. if (!substream) {
  1927. pr_err("%s substream not found\n", __func__);
  1928. return -EINVAL;
  1929. }
  1930. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1931. if (!chmixer_pspd) {
  1932. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1933. return -EINVAL;
  1934. }
  1935. chmixer_pspd->enable = ucontrol->value.integer.value[0];
  1936. chmixer_pspd->rule = ucontrol->value.integer.value[1];
  1937. chmixer_pspd->input_channel = ucontrol->value.integer.value[2];
  1938. chmixer_pspd->output_channel = ucontrol->value.integer.value[3];
  1939. chmixer_pspd->port_idx = ucontrol->value.integer.value[4];
  1940. if (chmixer_pspd->input_channel < 0 ||
  1941. chmixer_pspd->input_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8 ||
  1942. chmixer_pspd->output_channel < 0 ||
  1943. chmixer_pspd->output_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  1944. pr_err("%s: Invalid channels, in %d, out %d\n",
  1945. __func__, chmixer_pspd->input_channel,
  1946. chmixer_pspd->output_channel);
  1947. return -EINVAL;
  1948. }
  1949. mutex_lock(&pdata->lock);
  1950. prtd = substream->runtime ? substream->runtime->private_data : NULL;
  1951. if (chmixer_pspd->enable && prtd) {
  1952. if (session_type == SESSION_TYPE_RX &&
  1953. !chmixer_pspd->override_in_ch_map) {
  1954. if (prtd->set_channel_map) {
  1955. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1956. chmixer_pspd->in_ch_map[i] = prtd->channel_map[i];
  1957. } else {
  1958. q6asm_map_channels(asm_ch_map,
  1959. chmixer_pspd->input_channel, false);
  1960. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1961. chmixer_pspd->in_ch_map[i] = asm_ch_map[i];
  1962. }
  1963. chmixer_pspd->override_in_ch_map = true;
  1964. reset_override_in_ch_map = true;
  1965. } else if (session_type == SESSION_TYPE_TX &&
  1966. !chmixer_pspd->override_out_ch_map) {
  1967. /*
  1968. * Channel map set in prtd is for plyback only,
  1969. * hence always use default for capture path.
  1970. */
  1971. q6asm_map_channels(asm_ch_map,
  1972. chmixer_pspd->output_channel, false);
  1973. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1974. chmixer_pspd->out_ch_map[i] = asm_ch_map[i];
  1975. chmixer_pspd->override_out_ch_map = true;
  1976. reset_override_out_ch_map = true;
  1977. }
  1978. } else {
  1979. chmixer_pspd->override_out_ch_map = false;
  1980. chmixer_pspd->override_in_ch_map = false;
  1981. }
  1982. /* cache value and take effect during adm_open stage */
  1983. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  1984. session_type,
  1985. chmixer_pspd);
  1986. if (chmixer_pspd->enable && prtd && prtd->audio_client) {
  1987. stream_id = prtd->audio_client->session;
  1988. be_id = chmixer_pspd->port_idx;
  1989. msm_pcm_routing_set_channel_mixer_runtime(be_id,
  1990. stream_id,
  1991. session_type,
  1992. chmixer_pspd);
  1993. }
  1994. if (reset_override_out_ch_map)
  1995. chmixer_pspd->override_out_ch_map = false;
  1996. if (reset_override_in_ch_map)
  1997. chmixer_pspd->override_in_ch_map = false;
  1998. mutex_unlock(&pdata->lock);
  1999. return ret;
  2000. }
  2001. static int msm_pcm_channel_mixer_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  2002. struct snd_ctl_elem_value *ucontrol)
  2003. {
  2004. u64 fe_id = kcontrol->private_value & 0xFF;
  2005. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2006. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2007. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2008. struct msm_pcm_channel_mixer *chmixer_pspd;
  2009. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2010. if (!chmixer_pspd) {
  2011. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2012. return -EINVAL;
  2013. }
  2014. ucontrol->value.integer.value[0] = chmixer_pspd->enable;
  2015. ucontrol->value.integer.value[1] = chmixer_pspd->rule;
  2016. ucontrol->value.integer.value[2] = chmixer_pspd->input_channel;
  2017. ucontrol->value.integer.value[3] = chmixer_pspd->output_channel;
  2018. ucontrol->value.integer.value[4] = chmixer_pspd->port_idx;
  2019. return 0;
  2020. }
  2021. static int msm_pcm_channel_mixer_output_map_ctl_put(
  2022. struct snd_kcontrol *kcontrol,
  2023. struct snd_ctl_elem_value *ucontrol)
  2024. {
  2025. u64 fe_id = kcontrol->private_value & 0xFF;
  2026. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2027. int i = 0;
  2028. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2029. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2030. struct msm_pcm_channel_mixer *chmixer_pspd;
  2031. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2032. if (!chmixer_pspd) {
  2033. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2034. return -EINVAL;
  2035. }
  2036. chmixer_pspd->override_out_ch_map = true;
  2037. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2038. chmixer_pspd->out_ch_map[i] =
  2039. ucontrol->value.integer.value[i];
  2040. return 0;
  2041. }
  2042. static int msm_pcm_channel_mixer_output_map_ctl_get(
  2043. struct snd_kcontrol *kcontrol,
  2044. struct snd_ctl_elem_value *ucontrol)
  2045. {
  2046. u64 fe_id = kcontrol->private_value & 0xFF;
  2047. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2048. int i = 0;
  2049. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2050. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2051. struct msm_pcm_channel_mixer *chmixer_pspd;
  2052. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2053. if (!chmixer_pspd) {
  2054. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2055. return -EINVAL;
  2056. }
  2057. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2058. ucontrol->value.integer.value[i] =
  2059. chmixer_pspd->out_ch_map[i];
  2060. return 0;
  2061. }
  2062. static int msm_pcm_channel_mixer_input_map_ctl_put(
  2063. struct snd_kcontrol *kcontrol,
  2064. struct snd_ctl_elem_value *ucontrol)
  2065. {
  2066. u64 fe_id = kcontrol->private_value & 0xFF;
  2067. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2068. int i = 0;
  2069. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2070. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2071. struct msm_pcm_channel_mixer *chmixer_pspd;
  2072. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2073. if (!chmixer_pspd) {
  2074. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2075. return -EINVAL;
  2076. }
  2077. chmixer_pspd->override_in_ch_map = true;
  2078. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2079. chmixer_pspd->in_ch_map[i] = ucontrol->value.integer.value[i];
  2080. return 0;
  2081. }
  2082. static int msm_pcm_channel_mixer_input_map_ctl_get(
  2083. struct snd_kcontrol *kcontrol,
  2084. struct snd_ctl_elem_value *ucontrol)
  2085. {
  2086. u64 fe_id = kcontrol->private_value & 0xFF;
  2087. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2088. int i = 0;
  2089. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2090. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2091. struct msm_pcm_channel_mixer *chmixer_pspd;
  2092. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2093. if (!chmixer_pspd) {
  2094. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2095. return -EINVAL;
  2096. }
  2097. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2098. ucontrol->value.integer.value[i] =
  2099. chmixer_pspd->in_ch_map[i];
  2100. return 0;
  2101. }
  2102. static int msm_pcm_channel_mixer_weight_ctl_put(
  2103. struct snd_kcontrol *kcontrol,
  2104. struct snd_ctl_elem_value *ucontrol)
  2105. {
  2106. u64 fe_id = kcontrol->private_value & 0xFF;
  2107. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2108. int channel = (kcontrol->private_value >> 16) & 0xFF;
  2109. int i = 0;
  2110. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2111. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2112. struct msm_pcm_channel_mixer *chmixer_pspd;
  2113. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2114. if (!chmixer_pspd) {
  2115. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2116. return -EINVAL;
  2117. }
  2118. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  2119. pr_err("%s: invalid channel number %d\n", __func__, channel);
  2120. return -EINVAL;
  2121. }
  2122. channel--;
  2123. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2124. chmixer_pspd->channel_weight[channel][i] =
  2125. ucontrol->value.integer.value[i];
  2126. return 0;
  2127. }
  2128. static int msm_pcm_channel_mixer_weight_ctl_get(
  2129. struct snd_kcontrol *kcontrol,
  2130. struct snd_ctl_elem_value *ucontrol)
  2131. {
  2132. u64 fe_id = kcontrol->private_value & 0xFF;
  2133. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2134. int channel = (kcontrol->private_value >> 16) & 0xFF;
  2135. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2136. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2137. int i = 0;
  2138. struct msm_pcm_channel_mixer *chmixer_pspd;
  2139. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  2140. pr_err("%s: invalid channel number %d\n", __func__, channel);
  2141. return -EINVAL;
  2142. }
  2143. channel--;
  2144. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2145. if (!chmixer_pspd) {
  2146. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2147. return -EINVAL;
  2148. }
  2149. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2150. ucontrol->value.integer.value[i] =
  2151. chmixer_pspd->channel_weight[channel][i];
  2152. return 0;
  2153. }
  2154. static int msm_pcm_add_platform_controls(struct snd_kcontrol_new *kctl,
  2155. struct snd_soc_pcm_runtime *rtd, const char *name_prefix,
  2156. const char *name_suffix, int session_type, int channels)
  2157. {
  2158. int ret = -EINVAL;
  2159. char *mixer_name = NULL;
  2160. struct snd_pcm *pcm = rtd->pcm;
  2161. const char *deviceNo = "NN";
  2162. const char *channelNo = "NN";
  2163. int ctl_len = 0;
  2164. struct snd_soc_component *component = NULL;
  2165. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2166. if (!component) {
  2167. pr_err("%s: component is NULL\n", __func__);
  2168. return -EINVAL;
  2169. }
  2170. ctl_len = strlen(name_prefix) + 1 + strlen(deviceNo) + 1 +
  2171. strlen(channelNo) + 1 + strlen(name_suffix) + 1;
  2172. mixer_name = kzalloc(ctl_len, GFP_KERNEL);
  2173. if (mixer_name == NULL)
  2174. return -ENOMEM;
  2175. if (channels >= 0) {
  2176. snprintf(mixer_name, ctl_len, "%s %d %s %d",
  2177. name_prefix, pcm->device, name_suffix, channels);
  2178. kctl->private_value = (rtd->dai_link->id) | (session_type << 8) |
  2179. (channels << 16);
  2180. } else {
  2181. snprintf(mixer_name, ctl_len, "%s %d %s",
  2182. name_prefix, pcm->device, name_suffix);
  2183. kctl->private_value = (rtd->dai_link->id) | (session_type << 8);
  2184. }
  2185. kctl->name = mixer_name;
  2186. ret = snd_soc_add_component_controls(component, kctl, 1);
  2187. kfree(mixer_name);
  2188. return ret;
  2189. }
  2190. static int msm_pcm_channel_mixer_output_map_info(struct snd_kcontrol *kcontrol,
  2191. struct snd_ctl_elem_info *uinfo)
  2192. {
  2193. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2194. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  2195. /* Valid channel map value ranges from 1 to 64 */
  2196. uinfo->value.integer.min = 1;
  2197. uinfo->value.integer.max = 64;
  2198. return 0;
  2199. }
  2200. static int msm_pcm_add_channel_mixer_output_map_controls(
  2201. struct snd_soc_pcm_runtime *rtd)
  2202. {
  2203. struct snd_pcm *pcm = rtd->pcm;
  2204. const char *playback_mixer_ctl_name = "AudStr";
  2205. const char *capture_mixer_ctl_name = "AudStr Capture";
  2206. const char *suffix = "ChMixer Output Map";
  2207. int session_type = 0, ret = 0, channel = -1;
  2208. struct snd_kcontrol_new channel_mixer_output_map_control = {
  2209. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2210. .name = "?",
  2211. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2212. .info = msm_pcm_channel_mixer_output_map_info,
  2213. .put = msm_pcm_channel_mixer_output_map_ctl_put,
  2214. .get = msm_pcm_channel_mixer_output_map_ctl_get,
  2215. .private_value = 0,
  2216. };
  2217. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2218. session_type = SESSION_TYPE_RX;
  2219. ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
  2220. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2221. if (ret < 0)
  2222. goto fail;
  2223. }
  2224. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2225. session_type = SESSION_TYPE_TX;
  2226. ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
  2227. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2228. if (ret < 0)
  2229. goto fail;
  2230. }
  2231. return 0;
  2232. fail:
  2233. pr_err("%s: failed add platform ctl, err = %d\n",
  2234. __func__, ret);
  2235. return ret;
  2236. }
  2237. static int msm_pcm_channel_mixer_input_map_info(struct snd_kcontrol *kcontrol,
  2238. struct snd_ctl_elem_info *uinfo)
  2239. {
  2240. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2241. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  2242. /* Valid channel map value ranges from 1 to 64 */
  2243. uinfo->value.integer.min = 1;
  2244. uinfo->value.integer.max = 64;
  2245. return 0;
  2246. }
  2247. static int msm_pcm_add_channel_mixer_input_map_controls(
  2248. struct snd_soc_pcm_runtime *rtd)
  2249. {
  2250. struct snd_pcm *pcm = rtd->pcm;
  2251. const char *playback_mixer_ctl_name = "AudStr";
  2252. const char *capture_mixer_ctl_name = "AudStr Capture";
  2253. const char *suffix = "ChMixer Input Map";
  2254. int session_type = 0, ret = 0, channel = -1;
  2255. struct snd_kcontrol_new channel_mixer_input_map_control = {
  2256. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2257. .name = "?",
  2258. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2259. .info = msm_pcm_channel_mixer_input_map_info,
  2260. .put = msm_pcm_channel_mixer_input_map_ctl_put,
  2261. .get = msm_pcm_channel_mixer_input_map_ctl_get,
  2262. .private_value = 0,
  2263. };
  2264. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2265. session_type = SESSION_TYPE_RX;
  2266. ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
  2267. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2268. if (ret < 0)
  2269. goto fail;
  2270. }
  2271. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2272. session_type = SESSION_TYPE_TX;
  2273. ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
  2274. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2275. if (ret < 0)
  2276. goto fail;
  2277. }
  2278. return 0;
  2279. fail:
  2280. pr_err("%s: failed add platform ctl, err = %d\n",
  2281. __func__, ret);
  2282. return ret;
  2283. }
  2284. static int msm_pcm_channel_mixer_cfg_info(struct snd_kcontrol *kcontrol,
  2285. struct snd_ctl_elem_info *uinfo)
  2286. {
  2287. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2288. /* five int values: enable, rule, in_channels, out_channels and port_id */
  2289. uinfo->count = 5;
  2290. /* Valid range is all positive values to support above controls */
  2291. uinfo->value.integer.min = 0;
  2292. uinfo->value.integer.max = INT_MAX;
  2293. return 0;
  2294. }
  2295. static int msm_pcm_add_channel_mixer_cfg_controls(
  2296. struct snd_soc_pcm_runtime *rtd)
  2297. {
  2298. struct snd_pcm *pcm = rtd->pcm;
  2299. const char *playback_mixer_ctl_name = "AudStr";
  2300. const char *capture_mixer_ctl_name = "AudStr Capture";
  2301. const char *suffix = "ChMixer Cfg";
  2302. int session_type = 0, ret = 0, channel = -1;
  2303. struct msm_plat_data *pdata = NULL;
  2304. struct snd_soc_component *component = NULL;
  2305. struct snd_kcontrol_new channel_mixer_cfg_control = {
  2306. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2307. .name = "?",
  2308. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2309. .info = msm_pcm_channel_mixer_cfg_info,
  2310. .put = msm_pcm_channel_mixer_cfg_ctl_put,
  2311. .get = msm_pcm_channel_mixer_cfg_ctl_get,
  2312. .private_value = 0,
  2313. };
  2314. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2315. if (!component) {
  2316. pr_err("%s: component is NULL\n", __func__);
  2317. return -EINVAL;
  2318. }
  2319. pdata = (struct msm_plat_data *)
  2320. dev_get_drvdata(component->dev);
  2321. pdata->pcm_device[rtd->dai_link->id] = rtd->pcm;
  2322. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2323. session_type = SESSION_TYPE_RX;
  2324. ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
  2325. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2326. if (ret < 0)
  2327. goto fail;
  2328. }
  2329. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2330. session_type = SESSION_TYPE_TX;
  2331. ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
  2332. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2333. if (ret < 0)
  2334. goto fail;
  2335. }
  2336. return 0;
  2337. fail:
  2338. pr_err("%s: failed add platform ctl, err = %d\n",
  2339. __func__, ret);
  2340. return ret;
  2341. }
  2342. static int msm_pcm_channel_mixer_weight_info(struct snd_kcontrol *kcontrol,
  2343. struct snd_ctl_elem_info *uinfo)
  2344. {
  2345. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2346. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  2347. /* Valid range: 0 to 0x4000(Unity) gain weightage */
  2348. uinfo->value.integer.min = 0;
  2349. uinfo->value.integer.max = 0x4000;
  2350. return 0;
  2351. }
  2352. static int msm_pcm_add_channel_mixer_weight_controls(
  2353. struct snd_soc_pcm_runtime *rtd,
  2354. int channel)
  2355. {
  2356. struct snd_pcm *pcm = rtd->pcm;
  2357. const char *playback_mixer_ctl_name = "AudStr";
  2358. const char *capture_mixer_ctl_name = "AudStr Capture";
  2359. const char *suffix = "ChMixer Weight Ch";
  2360. int session_type = 0, ret = 0;
  2361. struct snd_kcontrol_new channel_mixer_weight_control = {
  2362. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2363. .name = "?",
  2364. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2365. .info = msm_pcm_channel_mixer_weight_info,
  2366. .put = msm_pcm_channel_mixer_weight_ctl_put,
  2367. .get = msm_pcm_channel_mixer_weight_ctl_get,
  2368. .private_value = 0,
  2369. };
  2370. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2371. session_type = SESSION_TYPE_RX;
  2372. ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
  2373. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2374. if (ret < 0)
  2375. goto fail;
  2376. }
  2377. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2378. session_type = SESSION_TYPE_TX;
  2379. ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
  2380. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2381. if (ret < 0)
  2382. goto fail;
  2383. }
  2384. return 0;
  2385. fail:
  2386. pr_err("%s: failed add platform ctl, err = %d\n",
  2387. __func__, ret);
  2388. return ret;
  2389. }
  2390. static int msm_pcm_add_channel_mixer_controls(struct snd_soc_pcm_runtime *rtd)
  2391. {
  2392. int i, ret = 0;
  2393. struct snd_pcm *pcm = NULL;
  2394. struct msm_plat_data *pdata = NULL;
  2395. struct snd_soc_component *component = NULL;
  2396. if (!rtd || !rtd->pcm) {
  2397. pr_err("%s invalid rtd or pcm\n", __func__);
  2398. return -EINVAL;
  2399. }
  2400. pcm = rtd->pcm;
  2401. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2402. if (!component) {
  2403. pr_err("%s: component is NULL\n", __func__);
  2404. return -EINVAL;
  2405. }
  2406. pdata = (struct msm_plat_data *)
  2407. dev_get_drvdata(component->dev);
  2408. if (!pdata) {
  2409. pr_err("%s: platform data not populated\n", __func__);
  2410. return -EINVAL;
  2411. }
  2412. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream &&
  2413. !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
  2414. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] =
  2415. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  2416. if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
  2417. ret = -ENOMEM;
  2418. goto fail;
  2419. }
  2420. }
  2421. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream &&
  2422. !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
  2423. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] =
  2424. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  2425. if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
  2426. ret = -ENOMEM;
  2427. goto fail;
  2428. }
  2429. }
  2430. ret = msm_pcm_add_channel_mixer_cfg_controls(rtd);
  2431. if (ret) {
  2432. pr_err("%s: pcm add channel mixer cfg controls failed:%d\n",
  2433. __func__, ret);
  2434. goto fail;
  2435. }
  2436. ret = msm_pcm_add_channel_mixer_input_map_controls(rtd);
  2437. if (ret) {
  2438. pr_err("%s: pcm add channel mixer input map controls failed:%d\n",
  2439. __func__, ret);
  2440. goto fail;
  2441. }
  2442. ret = msm_pcm_add_channel_mixer_output_map_controls(rtd);
  2443. if (ret) {
  2444. pr_err("%s: pcm add channel mixer output map controls failed:%d\n",
  2445. __func__, ret);
  2446. goto fail;
  2447. }
  2448. for (i = 1; i <= PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++) {
  2449. ret = msm_pcm_add_channel_mixer_weight_controls(rtd, i);
  2450. if (ret) {
  2451. pr_err("%s: pcm add channel mixer weight controls failed:%d\n",
  2452. __func__, ret);
  2453. goto fail;
  2454. }
  2455. }
  2456. return 0;
  2457. fail:
  2458. kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]);
  2459. kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]);
  2460. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] = NULL;
  2461. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] = NULL;
  2462. return ret;
  2463. }
  2464. static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
  2465. {
  2466. int ret = 0;
  2467. pr_debug("%s\n", __func__);
  2468. ret = msm_pcm_add_chmap_controls(rtd);
  2469. if (ret)
  2470. pr_err("%s: pcm add controls failed:%d\n", __func__, ret);
  2471. ret = msm_pcm_add_app_type_controls(rtd);
  2472. if (ret)
  2473. pr_err("%s: pcm add app type controls failed:%d\n",
  2474. __func__, ret);
  2475. ret = msm_pcm_add_channel_mixer_controls(rtd);
  2476. if (ret)
  2477. pr_err("%s: pcm add channel mixer controls failed:%d\n",
  2478. __func__, ret);
  2479. return ret;
  2480. }
  2481. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  2482. {
  2483. struct snd_card *card = rtd->card->snd_card;
  2484. int ret = 0;
  2485. if (!card->dev->coherent_dma_mask)
  2486. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  2487. ret = msm_pcm_add_controls(rtd);
  2488. if (ret) {
  2489. pr_err("%s, kctl add failed:%d\n", __func__, ret);
  2490. return ret;
  2491. }
  2492. ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_PLAYBACK);
  2493. if (ret)
  2494. pr_err("%s: Could not add pcm Volume Control %d\n",
  2495. __func__, ret);
  2496. ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_CAPTURE);
  2497. if (ret)
  2498. pr_err("%s: Could not add pcm Volume Control %d\n",
  2499. __func__, ret);
  2500. ret = msm_pcm_add_compress_control(rtd);
  2501. if (ret)
  2502. pr_err("%s: Could not add pcm Compress Control %d\n",
  2503. __func__, ret);
  2504. ret = msm_pcm_add_audio_adsp_stream_cmd_control(rtd);
  2505. if (ret)
  2506. pr_err("%s: Could not add pcm ADSP Stream Cmd Control\n",
  2507. __func__);
  2508. ret = msm_pcm_add_audio_adsp_stream_callback_control(rtd);
  2509. if (ret)
  2510. pr_err("%s: Could not add pcm ADSP Stream Callback Control\n",
  2511. __func__);
  2512. return ret;
  2513. }
  2514. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2515. static snd_pcm_sframes_t msm_pcm_delay_blk(struct snd_pcm_substream *substream,
  2516. struct snd_soc_dai *dai)
  2517. {
  2518. struct snd_pcm_runtime *runtime = substream->runtime;
  2519. struct msm_audio *prtd = runtime->private_data;
  2520. struct audio_client *ac = prtd->audio_client;
  2521. snd_pcm_sframes_t frames;
  2522. int ret;
  2523. ret = q6asm_get_path_delay(prtd->audio_client);
  2524. if (ret) {
  2525. pr_err("%s: get_path_delay failed, ret=%d\n", __func__, ret);
  2526. return 0;
  2527. }
  2528. /* convert microseconds to frames */
  2529. frames = ac->path_delay / 1000 * runtime->rate / 1000;
  2530. /* also convert the remainder from the initial division */
  2531. frames += ac->path_delay % 1000 * runtime->rate / 1000000;
  2532. /* overcompensate for the loss of precision (empirical) */
  2533. frames += 2;
  2534. return frames;
  2535. }
  2536. #endif /* CONFIG_AUDIO_QGKI */
  2537. static struct snd_soc_component_driver msm_soc_component = {
  2538. .name = DRV_NAME,
  2539. .ops = &msm_pcm_ops,
  2540. .pcm_new = msm_asoc_pcm_new,
  2541. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2542. .delay_blk = msm_pcm_delay_blk,
  2543. #endif /* CONFIG_AUDIO_QGKI */
  2544. };
  2545. static int msm_pcm_probe(struct platform_device *pdev)
  2546. {
  2547. int rc;
  2548. int id;
  2549. struct msm_plat_data *pdata;
  2550. const char *latency_level;
  2551. rc = of_property_read_u32(pdev->dev.of_node,
  2552. "qcom,msm-pcm-dsp-id", &id);
  2553. if (rc) {
  2554. dev_err(&pdev->dev, "%s: qcom,msm-pcm-dsp-id missing in DT node\n",
  2555. __func__);
  2556. return rc;
  2557. }
  2558. pdata = kzalloc(sizeof(struct msm_plat_data), GFP_KERNEL);
  2559. if (!pdata)
  2560. return -ENOMEM;
  2561. if (of_property_read_bool(pdev->dev.of_node,
  2562. "qcom,msm-pcm-low-latency")) {
  2563. pdata->perf_mode = LOW_LATENCY_PCM_MODE;
  2564. rc = of_property_read_string(pdev->dev.of_node,
  2565. "qcom,latency-level", &latency_level);
  2566. if (!rc) {
  2567. if (!strcmp(latency_level, "ultra"))
  2568. pdata->perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
  2569. else if (!strcmp(latency_level, "ull-pp"))
  2570. pdata->perf_mode =
  2571. ULL_POST_PROCESSING_PCM_MODE;
  2572. }
  2573. } else {
  2574. pdata->perf_mode = LEGACY_PCM_MODE;
  2575. }
  2576. mutex_init(&pdata->lock);
  2577. dev_set_drvdata(&pdev->dev, pdata);
  2578. dev_dbg(&pdev->dev, "%s: dev name %s\n",
  2579. __func__, dev_name(&pdev->dev));
  2580. return snd_soc_register_component(&pdev->dev,
  2581. &msm_soc_component,
  2582. NULL, 0);
  2583. }
  2584. static int msm_pcm_remove(struct platform_device *pdev)
  2585. {
  2586. struct msm_plat_data *pdata;
  2587. int i = 0;
  2588. pdata = dev_get_drvdata(&pdev->dev);
  2589. if (pdata) {
  2590. for (i = 0; i < MSM_FRONTEND_DAI_MM_SIZE; i++) {
  2591. kfree(pdata->chmixer_pspd[i][SESSION_TYPE_RX]);
  2592. kfree(pdata->chmixer_pspd[i][SESSION_TYPE_TX]);
  2593. }
  2594. }
  2595. mutex_destroy(&pdata->lock);
  2596. kfree(pdata);
  2597. snd_soc_unregister_component(&pdev->dev);
  2598. return 0;
  2599. }
  2600. static const struct of_device_id msm_pcm_dt_match[] = {
  2601. {.compatible = "qcom,msm-pcm-dsp"},
  2602. {}
  2603. };
  2604. MODULE_DEVICE_TABLE(of, msm_pcm_dt_match);
  2605. static struct platform_driver msm_pcm_driver = {
  2606. .driver = {
  2607. .name = "msm-pcm-dsp",
  2608. .owner = THIS_MODULE,
  2609. .of_match_table = msm_pcm_dt_match,
  2610. .suppress_bind_attrs = true,
  2611. },
  2612. .probe = msm_pcm_probe,
  2613. .remove = msm_pcm_remove,
  2614. };
  2615. int __init msm_pcm_dsp_init(void)
  2616. {
  2617. init_waitqueue_head(&the_locks.enable_wait);
  2618. init_waitqueue_head(&the_locks.eos_wait);
  2619. init_waitqueue_head(&the_locks.write_wait);
  2620. init_waitqueue_head(&the_locks.read_wait);
  2621. return platform_driver_register(&msm_pcm_driver);
  2622. }
  2623. void msm_pcm_dsp_exit(void)
  2624. {
  2625. platform_driver_unregister(&msm_pcm_driver);
  2626. }
  2627. MODULE_DESCRIPTION("PCM module platform driver");
  2628. MODULE_LICENSE("GPL v2");