msm-pcm-q6-v2.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  1. /* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/time.h>
  17. #include <linux/wait.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <sound/core.h>
  21. #include <sound/soc.h>
  22. #include <sound/soc-dapm.h>
  23. #include <sound/pcm.h>
  24. #include <sound/initval.h>
  25. #include <sound/control.h>
  26. #include <sound/timer.h>
  27. #include <asm/dma.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/msm_audio.h>
  30. #include <linux/of_device.h>
  31. #include <sound/tlv.h>
  32. #include <sound/pcm_params.h>
  33. #include <dsp/msm_audio_ion.h>
  34. #include <dsp/q6audio-v2.h>
  35. #include "msm-pcm-q6-v2.h"
  36. #include "msm-pcm-routing-v2.h"
  37. #include "msm-qti-pp-config.h"
  38. enum stream_state {
  39. IDLE = 0,
  40. STOPPED,
  41. RUNNING,
  42. };
  43. static struct audio_locks the_locks;
  44. #define PCM_MASTER_VOL_MAX_STEPS 0x2000
  45. static const DECLARE_TLV_DB_LINEAR(msm_pcm_vol_gain, 0,
  46. PCM_MASTER_VOL_MAX_STEPS);
  47. struct snd_msm {
  48. struct snd_card *card;
  49. struct snd_pcm *pcm;
  50. };
  51. #define CMD_EOS_MIN_TIMEOUT_LENGTH 50
  52. #define CMD_EOS_TIMEOUT_MULTIPLIER (HZ * 50)
  53. #define MAX_PB_COPY_RETRIES 3
  54. static struct snd_pcm_hardware msm_pcm_hardware_capture = {
  55. .info = (SNDRV_PCM_INFO_MMAP |
  56. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  57. SNDRV_PCM_INFO_MMAP_VALID |
  58. SNDRV_PCM_INFO_INTERLEAVED |
  59. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  60. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  61. SNDRV_PCM_FMTBIT_S24_LE |
  62. SNDRV_PCM_FMTBIT_S24_3LE |
  63. SNDRV_PCM_FMTBIT_S32_LE),
  64. .rates = SNDRV_PCM_RATE_8000_384000,
  65. .rate_min = 8000,
  66. .rate_max = 384000,
  67. .channels_min = 1,
  68. .channels_max = 4,
  69. .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
  70. CAPTURE_MAX_PERIOD_SIZE,
  71. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  72. .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
  73. .periods_min = CAPTURE_MIN_NUM_PERIODS,
  74. .periods_max = CAPTURE_MAX_NUM_PERIODS,
  75. .fifo_size = 0,
  76. };
  77. static struct snd_pcm_hardware msm_pcm_hardware_playback = {
  78. .info = (SNDRV_PCM_INFO_MMAP |
  79. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  80. SNDRV_PCM_INFO_MMAP_VALID |
  81. SNDRV_PCM_INFO_INTERLEAVED |
  82. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  83. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  84. SNDRV_PCM_FMTBIT_S24_LE |
  85. SNDRV_PCM_FMTBIT_S24_3LE |
  86. SNDRV_PCM_FMTBIT_S32_LE),
  87. .rates = SNDRV_PCM_RATE_8000_384000,
  88. .rate_min = 8000,
  89. .rate_max = 384000,
  90. .channels_min = 1,
  91. .channels_max = 8,
  92. .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS *
  93. PLAYBACK_MAX_PERIOD_SIZE,
  94. .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
  95. .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
  96. .periods_min = PLAYBACK_MIN_NUM_PERIODS,
  97. .periods_max = PLAYBACK_MAX_NUM_PERIODS,
  98. .fifo_size = 0,
  99. };
  100. /* Conventional and unconventional sample rate supported */
  101. static unsigned int supported_sample_rates[] = {
  102. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
  103. 88200, 96000, 176400, 192000, 352800, 384000
  104. };
  105. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  106. .count = ARRAY_SIZE(supported_sample_rates),
  107. .list = supported_sample_rates,
  108. .mask = 0,
  109. };
  110. static void msm_pcm_route_event_handler(enum msm_pcm_routing_event event,
  111. void *priv_data)
  112. {
  113. struct msm_audio *prtd = priv_data;
  114. WARN_ON(!prtd);
  115. pr_debug("%s: event %x\n", __func__, event);
  116. switch (event) {
  117. case MSM_PCM_RT_EVT_BUF_RECFG:
  118. q6asm_cmd(prtd->audio_client, CMD_PAUSE);
  119. q6asm_cmd(prtd->audio_client, CMD_FLUSH);
  120. q6asm_run(prtd->audio_client, 0, 0, 0);
  121. /* fallthrough */
  122. default:
  123. break;
  124. }
  125. }
  126. static void event_handler(uint32_t opcode,
  127. uint32_t token, uint32_t *payload, void *priv)
  128. {
  129. struct msm_audio *prtd = priv;
  130. struct snd_pcm_substream *substream = prtd->substream;
  131. uint32_t *ptrmem = (uint32_t *)payload;
  132. uint32_t idx = 0;
  133. uint32_t size = 0;
  134. uint8_t buf_index;
  135. struct snd_soc_pcm_runtime *rtd;
  136. int ret = 0;
  137. switch (opcode) {
  138. case ASM_DATA_EVENT_WRITE_DONE_V2: {
  139. pr_debug("ASM_DATA_EVENT_WRITE_DONE_V2\n");
  140. pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
  141. prtd->pcm_irq_pos += prtd->pcm_count;
  142. if (atomic_read(&prtd->start))
  143. snd_pcm_period_elapsed(substream);
  144. atomic_inc(&prtd->out_count);
  145. wake_up(&the_locks.write_wait);
  146. if (!atomic_read(&prtd->start))
  147. break;
  148. if (!prtd->mmap_flag || prtd->reset_event)
  149. break;
  150. if (q6asm_is_cpu_buf_avail_nolock(IN,
  151. prtd->audio_client,
  152. &size, &idx)) {
  153. pr_debug("%s:writing %d bytes of buffer to dsp 2\n",
  154. __func__, prtd->pcm_count);
  155. q6asm_write_nolock(prtd->audio_client,
  156. prtd->pcm_count, 0, 0, NO_TIMESTAMP);
  157. }
  158. break;
  159. }
  160. case ASM_DATA_EVENT_RENDERED_EOS:
  161. pr_debug("ASM_DATA_EVENT_RENDERED_EOS\n");
  162. clear_bit(CMD_EOS, &prtd->cmd_pending);
  163. wake_up(&the_locks.eos_wait);
  164. break;
  165. case ASM_DATA_EVENT_READ_DONE_V2: {
  166. pr_debug("ASM_DATA_EVENT_READ_DONE_V2\n");
  167. buf_index = q6asm_get_buf_index_from_token(token);
  168. pr_debug("%s: token=0x%08x buf_index=0x%08x\n",
  169. __func__, token, buf_index);
  170. prtd->in_frame_info[buf_index].size = payload[4];
  171. prtd->in_frame_info[buf_index].offset = payload[5];
  172. /* assume data size = 0 during flushing */
  173. if (prtd->in_frame_info[buf_index].size) {
  174. prtd->pcm_irq_pos +=
  175. prtd->in_frame_info[buf_index].size;
  176. pr_debug("pcm_irq_pos=%d\n", prtd->pcm_irq_pos);
  177. if (atomic_read(&prtd->start))
  178. snd_pcm_period_elapsed(substream);
  179. if (atomic_read(&prtd->in_count) <= prtd->periods)
  180. atomic_inc(&prtd->in_count);
  181. wake_up(&the_locks.read_wait);
  182. if (prtd->mmap_flag &&
  183. q6asm_is_cpu_buf_avail_nolock(OUT,
  184. prtd->audio_client,
  185. &size, &idx) &&
  186. (substream->runtime->status->state ==
  187. SNDRV_PCM_STATE_RUNNING))
  188. q6asm_read_nolock(prtd->audio_client);
  189. } else {
  190. pr_debug("%s: reclaim flushed buf in_count %x\n",
  191. __func__, atomic_read(&prtd->in_count));
  192. prtd->pcm_irq_pos += prtd->pcm_count;
  193. if (prtd->mmap_flag) {
  194. if (q6asm_is_cpu_buf_avail_nolock(OUT,
  195. prtd->audio_client,
  196. &size, &idx) &&
  197. (substream->runtime->status->state ==
  198. SNDRV_PCM_STATE_RUNNING))
  199. q6asm_read_nolock(prtd->audio_client);
  200. } else {
  201. atomic_inc(&prtd->in_count);
  202. }
  203. if (atomic_read(&prtd->in_count) == prtd->periods) {
  204. pr_info("%s: reclaimed all bufs\n", __func__);
  205. if (atomic_read(&prtd->start))
  206. snd_pcm_period_elapsed(substream);
  207. wake_up(&the_locks.read_wait);
  208. }
  209. }
  210. break;
  211. }
  212. case ASM_STREAM_PP_EVENT:
  213. case ASM_STREAM_CMD_ENCDEC_EVENTS: {
  214. pr_debug("%s: ASM_STREAM_EVENT (0x%x)\n", __func__, opcode);
  215. if (!substream) {
  216. pr_err("%s: substream is NULL.\n", __func__);
  217. return;
  218. }
  219. rtd = substream->private_data;
  220. if (!rtd) {
  221. pr_err("%s: rtd is NULL\n", __func__);
  222. return;
  223. }
  224. ret = msm_adsp_inform_mixer_ctl(rtd, payload);
  225. if (ret) {
  226. pr_err("%s: failed to inform mixer ctl. err = %d\n",
  227. __func__, ret);
  228. return;
  229. }
  230. break;
  231. }
  232. case APR_BASIC_RSP_RESULT: {
  233. switch (payload[0]) {
  234. case ASM_SESSION_CMD_RUN_V2:
  235. if (substream->stream
  236. != SNDRV_PCM_STREAM_PLAYBACK) {
  237. atomic_set(&prtd->start, 1);
  238. break;
  239. }
  240. if (prtd->mmap_flag) {
  241. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  242. __func__,
  243. prtd->pcm_count);
  244. q6asm_write_nolock(prtd->audio_client,
  245. prtd->pcm_count,
  246. 0, 0, NO_TIMESTAMP);
  247. } else {
  248. while (atomic_read(&prtd->out_needed)) {
  249. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  250. __func__,
  251. prtd->pcm_count);
  252. q6asm_write_nolock(prtd->audio_client,
  253. prtd->pcm_count,
  254. 0, 0, NO_TIMESTAMP);
  255. atomic_dec(&prtd->out_needed);
  256. wake_up(&the_locks.write_wait);
  257. };
  258. }
  259. atomic_set(&prtd->start, 1);
  260. break;
  261. case ASM_STREAM_CMD_REGISTER_PP_EVENTS:
  262. pr_debug("%s: ASM_STREAM_CMD_REGISTER_PP_EVENTS:",
  263. __func__);
  264. break;
  265. default:
  266. pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
  267. __func__, payload[0], payload[1]);
  268. break;
  269. }
  270. }
  271. break;
  272. case RESET_EVENTS:
  273. pr_debug("%s RESET_EVENTS\n", __func__);
  274. prtd->pcm_irq_pos += prtd->pcm_count;
  275. atomic_inc(&prtd->out_count);
  276. atomic_inc(&prtd->in_count);
  277. prtd->reset_event = true;
  278. if (atomic_read(&prtd->start))
  279. snd_pcm_period_elapsed(substream);
  280. wake_up(&the_locks.eos_wait);
  281. wake_up(&the_locks.write_wait);
  282. wake_up(&the_locks.read_wait);
  283. break;
  284. default:
  285. pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
  286. break;
  287. }
  288. }
  289. static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
  290. {
  291. struct snd_pcm_runtime *runtime = substream->runtime;
  292. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  293. struct msm_audio *prtd = runtime->private_data;
  294. struct msm_plat_data *pdata;
  295. struct snd_pcm_hw_params *params;
  296. int ret;
  297. uint32_t fmt_type = FORMAT_LINEAR_PCM;
  298. uint16_t bits_per_sample;
  299. uint16_t sample_word_size;
  300. pdata = (struct msm_plat_data *)
  301. dev_get_drvdata(soc_prtd->platform->dev);
  302. if (!pdata) {
  303. pr_err("%s: platform data not populated\n", __func__);
  304. return -EINVAL;
  305. }
  306. if (!prtd || !prtd->audio_client) {
  307. pr_err("%s: private data null or audio client freed\n",
  308. __func__);
  309. return -EINVAL;
  310. }
  311. params = &soc_prtd->dpcm[substream->stream].hw_params;
  312. pr_debug("%s\n", __func__);
  313. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  314. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  315. prtd->pcm_irq_pos = 0;
  316. /* rate and channels are sent to audio driver */
  317. prtd->samp_rate = runtime->rate;
  318. prtd->channel_mode = runtime->channels;
  319. if (prtd->enabled)
  320. return 0;
  321. prtd->audio_client->perf_mode = pdata->perf_mode;
  322. pr_debug("%s: perf: %x\n", __func__, pdata->perf_mode);
  323. switch (params_format(params)) {
  324. case SNDRV_PCM_FORMAT_S32_LE:
  325. bits_per_sample = 32;
  326. sample_word_size = 32;
  327. break;
  328. case SNDRV_PCM_FORMAT_S24_LE:
  329. bits_per_sample = 24;
  330. sample_word_size = 32;
  331. break;
  332. case SNDRV_PCM_FORMAT_S24_3LE:
  333. bits_per_sample = 24;
  334. sample_word_size = 24;
  335. break;
  336. case SNDRV_PCM_FORMAT_S16_LE:
  337. default:
  338. bits_per_sample = 16;
  339. sample_word_size = 16;
  340. break;
  341. }
  342. if (prtd->compress_enable) {
  343. fmt_type = FORMAT_GEN_COMPR;
  344. pr_debug("%s: Compressed enabled!\n", __func__);
  345. ret = q6asm_open_write_compressed(prtd->audio_client, fmt_type,
  346. COMPRESSED_PASSTHROUGH_GEN);
  347. if (ret < 0) {
  348. pr_err("%s: q6asm_open_write_compressed failed (%d)\n",
  349. __func__, ret);
  350. q6asm_audio_client_free(prtd->audio_client);
  351. prtd->audio_client = NULL;
  352. return -ENOMEM;
  353. }
  354. } else {
  355. ret = q6asm_open_write_v4(prtd->audio_client,
  356. fmt_type, bits_per_sample);
  357. if (ret < 0) {
  358. pr_err("%s: q6asm_open_write_v4 failed (%d)\n",
  359. __func__, ret);
  360. q6asm_audio_client_free(prtd->audio_client);
  361. prtd->audio_client = NULL;
  362. return -ENOMEM;
  363. }
  364. ret = q6asm_send_cal(prtd->audio_client);
  365. if (ret < 0)
  366. pr_debug("%s : Send cal failed : %d", __func__, ret);
  367. }
  368. pr_debug("%s: session ID %d\n", __func__,
  369. prtd->audio_client->session);
  370. prtd->session_id = prtd->audio_client->session;
  371. if (prtd->compress_enable) {
  372. ret = msm_pcm_routing_reg_phy_compr_stream(
  373. soc_prtd->dai_link->id,
  374. prtd->audio_client->perf_mode,
  375. prtd->session_id,
  376. SNDRV_PCM_STREAM_PLAYBACK,
  377. COMPRESSED_PASSTHROUGH_GEN);
  378. } else {
  379. ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
  380. prtd->audio_client->perf_mode,
  381. prtd->session_id, substream->stream);
  382. }
  383. if (ret) {
  384. pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
  385. return ret;
  386. }
  387. if (prtd->compress_enable) {
  388. ret = q6asm_media_format_block_gen_compr(
  389. prtd->audio_client, runtime->rate,
  390. runtime->channels, !prtd->set_channel_map,
  391. prtd->channel_map, bits_per_sample);
  392. } else {
  393. ret = q6asm_media_format_block_multi_ch_pcm_v4(
  394. prtd->audio_client, runtime->rate,
  395. runtime->channels, !prtd->set_channel_map,
  396. prtd->channel_map, bits_per_sample,
  397. sample_word_size, ASM_LITTLE_ENDIAN,
  398. DEFAULT_QF);
  399. }
  400. if (ret < 0)
  401. pr_info("%s: CMD Format block failed\n", __func__);
  402. atomic_set(&prtd->out_count, runtime->periods);
  403. prtd->enabled = 1;
  404. prtd->cmd_pending = 0;
  405. prtd->cmd_interrupt = 0;
  406. return 0;
  407. }
  408. static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
  409. {
  410. struct snd_pcm_runtime *runtime = substream->runtime;
  411. struct msm_audio *prtd = runtime->private_data;
  412. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  413. struct msm_plat_data *pdata;
  414. struct snd_pcm_hw_params *params;
  415. struct msm_pcm_routing_evt event;
  416. int ret = 0;
  417. int i = 0;
  418. uint16_t bits_per_sample = 16;
  419. uint16_t sample_word_size;
  420. pdata = (struct msm_plat_data *)
  421. dev_get_drvdata(soc_prtd->platform->dev);
  422. if (!pdata) {
  423. pr_err("%s: platform data not populated\n", __func__);
  424. return -EINVAL;
  425. }
  426. if (!prtd || !prtd->audio_client) {
  427. pr_err("%s: private data null or audio client freed\n",
  428. __func__);
  429. return -EINVAL;
  430. }
  431. if (prtd->enabled == IDLE) {
  432. pr_debug("%s:perf_mode=%d periods=%d\n", __func__,
  433. pdata->perf_mode, runtime->periods);
  434. params = &soc_prtd->dpcm[substream->stream].hw_params;
  435. if ((params_format(params) == SNDRV_PCM_FORMAT_S24_LE) ||
  436. (params_format(params) == SNDRV_PCM_FORMAT_S24_3LE))
  437. bits_per_sample = 24;
  438. else if (params_format(params) == SNDRV_PCM_FORMAT_S32_LE)
  439. bits_per_sample = 32;
  440. /* ULL mode is not supported in capture path */
  441. if (pdata->perf_mode == LEGACY_PCM_MODE)
  442. prtd->audio_client->perf_mode = LEGACY_PCM_MODE;
  443. else
  444. prtd->audio_client->perf_mode = LOW_LATENCY_PCM_MODE;
  445. pr_debug("%s Opening %d-ch PCM read stream, perf_mode %d\n",
  446. __func__, params_channels(params),
  447. prtd->audio_client->perf_mode);
  448. ret = q6asm_open_read_v4(prtd->audio_client, FORMAT_LINEAR_PCM,
  449. bits_per_sample, false);
  450. if (ret < 0) {
  451. pr_err("%s: q6asm_open_read failed\n", __func__);
  452. q6asm_audio_client_free(prtd->audio_client);
  453. prtd->audio_client = NULL;
  454. return -ENOMEM;
  455. }
  456. ret = q6asm_send_cal(prtd->audio_client);
  457. if (ret < 0)
  458. pr_debug("%s : Send cal failed : %d", __func__, ret);
  459. pr_debug("%s: session ID %d\n",
  460. __func__, prtd->audio_client->session);
  461. prtd->session_id = prtd->audio_client->session;
  462. event.event_func = msm_pcm_route_event_handler;
  463. event.priv_data = (void *) prtd;
  464. ret = msm_pcm_routing_reg_phy_stream_v2(
  465. soc_prtd->dai_link->id,
  466. prtd->audio_client->perf_mode,
  467. prtd->session_id, substream->stream,
  468. event);
  469. if (ret) {
  470. pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
  471. return ret;
  472. }
  473. }
  474. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  475. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  476. prtd->pcm_irq_pos = 0;
  477. /* rate and channels are sent to audio driver */
  478. prtd->samp_rate = runtime->rate;
  479. prtd->channel_mode = runtime->channels;
  480. if (prtd->enabled == IDLE || prtd->enabled == STOPPED) {
  481. for (i = 0; i < runtime->periods; i++)
  482. q6asm_read(prtd->audio_client);
  483. prtd->periods = runtime->periods;
  484. }
  485. if (prtd->enabled != IDLE)
  486. return 0;
  487. switch (runtime->format) {
  488. case SNDRV_PCM_FORMAT_S32_LE:
  489. bits_per_sample = 32;
  490. sample_word_size = 32;
  491. break;
  492. case SNDRV_PCM_FORMAT_S24_LE:
  493. bits_per_sample = 24;
  494. sample_word_size = 32;
  495. break;
  496. case SNDRV_PCM_FORMAT_S24_3LE:
  497. bits_per_sample = 24;
  498. sample_word_size = 24;
  499. break;
  500. case SNDRV_PCM_FORMAT_S16_LE:
  501. default:
  502. bits_per_sample = 16;
  503. sample_word_size = 16;
  504. break;
  505. }
  506. pr_debug("%s: Samp_rate = %d Channel = %d bit width = %d, word size = %d\n",
  507. __func__, prtd->samp_rate, prtd->channel_mode,
  508. bits_per_sample, sample_word_size);
  509. ret = q6asm_enc_cfg_blk_pcm_format_support_v4(prtd->audio_client,
  510. prtd->samp_rate,
  511. prtd->channel_mode,
  512. bits_per_sample,
  513. sample_word_size,
  514. ASM_LITTLE_ENDIAN,
  515. DEFAULT_QF);
  516. if (ret < 0)
  517. pr_debug("%s: cmd cfg pcm was block failed", __func__);
  518. prtd->enabled = RUNNING;
  519. return ret;
  520. }
  521. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  522. {
  523. int ret = 0;
  524. struct snd_pcm_runtime *runtime = substream->runtime;
  525. struct msm_audio *prtd = runtime->private_data;
  526. switch (cmd) {
  527. case SNDRV_PCM_TRIGGER_START:
  528. case SNDRV_PCM_TRIGGER_RESUME:
  529. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  530. pr_debug("%s: Trigger start\n", __func__);
  531. ret = q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  532. break;
  533. case SNDRV_PCM_TRIGGER_STOP:
  534. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  535. atomic_set(&prtd->start, 0);
  536. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) {
  537. prtd->enabled = STOPPED;
  538. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  539. break;
  540. }
  541. /* pending CMD_EOS isn't expected */
  542. WARN_ON_ONCE(test_bit(CMD_EOS, &prtd->cmd_pending));
  543. set_bit(CMD_EOS, &prtd->cmd_pending);
  544. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
  545. if (ret)
  546. clear_bit(CMD_EOS, &prtd->cmd_pending);
  547. break;
  548. case SNDRV_PCM_TRIGGER_SUSPEND:
  549. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  550. pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
  551. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  552. atomic_set(&prtd->start, 0);
  553. break;
  554. default:
  555. ret = -EINVAL;
  556. break;
  557. }
  558. return ret;
  559. }
  560. static int msm_pcm_open(struct snd_pcm_substream *substream)
  561. {
  562. struct snd_pcm_runtime *runtime = substream->runtime;
  563. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  564. struct msm_audio *prtd;
  565. int ret = 0;
  566. prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
  567. if (prtd == NULL)
  568. return -ENOMEM;
  569. prtd->substream = substream;
  570. prtd->audio_client = q6asm_audio_client_alloc(
  571. (app_cb)event_handler, prtd);
  572. if (!prtd->audio_client) {
  573. pr_info("%s: Could not allocate memory\n", __func__);
  574. kfree(prtd);
  575. return -ENOMEM;
  576. }
  577. prtd->audio_client->dev = soc_prtd->platform->dev;
  578. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  579. runtime->hw = msm_pcm_hardware_playback;
  580. /* Capture path */
  581. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  582. runtime->hw = msm_pcm_hardware_capture;
  583. else {
  584. pr_err("Invalid Stream type %d\n", substream->stream);
  585. return -EINVAL;
  586. }
  587. ret = snd_pcm_hw_constraint_list(runtime, 0,
  588. SNDRV_PCM_HW_PARAM_RATE,
  589. &constraints_sample_rates);
  590. if (ret < 0)
  591. pr_info("snd_pcm_hw_constraint_list failed\n");
  592. /* Ensure that buffer size is a multiple of period size */
  593. ret = snd_pcm_hw_constraint_integer(runtime,
  594. SNDRV_PCM_HW_PARAM_PERIODS);
  595. if (ret < 0)
  596. pr_info("snd_pcm_hw_constraint_integer failed\n");
  597. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  598. ret = snd_pcm_hw_constraint_minmax(runtime,
  599. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  600. PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
  601. PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
  602. if (ret < 0) {
  603. pr_err("constraint for buffer bytes min max ret = %d\n",
  604. ret);
  605. }
  606. }
  607. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  608. ret = snd_pcm_hw_constraint_minmax(runtime,
  609. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  610. CAPTURE_MIN_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
  611. CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
  612. if (ret < 0) {
  613. pr_err("constraint for buffer bytes min max ret = %d\n",
  614. ret);
  615. }
  616. }
  617. ret = snd_pcm_hw_constraint_step(runtime, 0,
  618. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  619. if (ret < 0) {
  620. pr_err("constraint for period bytes step ret = %d\n",
  621. ret);
  622. }
  623. ret = snd_pcm_hw_constraint_step(runtime, 0,
  624. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  625. if (ret < 0) {
  626. pr_err("constraint for buffer bytes step ret = %d\n",
  627. ret);
  628. }
  629. prtd->enabled = IDLE;
  630. prtd->dsp_cnt = 0;
  631. prtd->set_channel_map = false;
  632. prtd->reset_event = false;
  633. runtime->private_data = prtd;
  634. msm_adsp_init_mixer_ctl_pp_event_queue(soc_prtd);
  635. return 0;
  636. }
  637. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  638. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  639. {
  640. int ret = 0;
  641. int fbytes = 0;
  642. int xfer = 0;
  643. char *bufptr = NULL;
  644. void *data = NULL;
  645. uint32_t idx = 0;
  646. uint32_t size = 0;
  647. uint32_t retries = 0;
  648. struct snd_pcm_runtime *runtime = substream->runtime;
  649. struct msm_audio *prtd = runtime->private_data;
  650. fbytes = frames_to_bytes(runtime, frames);
  651. pr_debug("%s: prtd->out_count = %d\n",
  652. __func__, atomic_read(&prtd->out_count));
  653. while ((fbytes > 0) && (retries < MAX_PB_COPY_RETRIES)) {
  654. if (prtd->reset_event) {
  655. pr_err("%s: In SSR return ENETRESET before wait\n",
  656. __func__);
  657. return -ENETRESET;
  658. }
  659. ret = wait_event_timeout(the_locks.write_wait,
  660. (atomic_read(&prtd->out_count)), 5 * HZ);
  661. if (!ret) {
  662. pr_err("%s: wait_event_timeout failed\n", __func__);
  663. ret = -ETIMEDOUT;
  664. goto fail;
  665. }
  666. ret = 0;
  667. if (prtd->reset_event) {
  668. pr_err("%s: In SSR return ENETRESET after wait\n",
  669. __func__);
  670. return -ENETRESET;
  671. }
  672. if (!atomic_read(&prtd->out_count)) {
  673. pr_err("%s: pcm stopped out_count 0\n", __func__);
  674. return 0;
  675. }
  676. data = q6asm_is_cpu_buf_avail(IN, prtd->audio_client, &size,
  677. &idx);
  678. if (data == NULL) {
  679. retries++;
  680. continue;
  681. } else {
  682. retries = 0;
  683. }
  684. if (fbytes > size)
  685. xfer = size;
  686. else
  687. xfer = fbytes;
  688. bufptr = data;
  689. if (bufptr) {
  690. pr_debug("%s:fbytes =%d: xfer=%d size=%d\n",
  691. __func__, fbytes, xfer, size);
  692. if (copy_from_user(bufptr, buf, xfer)) {
  693. ret = -EFAULT;
  694. pr_err("%s: copy_from_user failed\n",
  695. __func__);
  696. q6asm_cpu_buf_release(IN, prtd->audio_client);
  697. goto fail;
  698. }
  699. buf += xfer;
  700. fbytes -= xfer;
  701. pr_debug("%s:fbytes = %d: xfer=%d\n", __func__, fbytes,
  702. xfer);
  703. if (atomic_read(&prtd->start)) {
  704. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  705. __func__, xfer);
  706. ret = q6asm_write(prtd->audio_client, xfer,
  707. 0, 0, NO_TIMESTAMP);
  708. if (ret < 0) {
  709. ret = -EFAULT;
  710. q6asm_cpu_buf_release(IN,
  711. prtd->audio_client);
  712. goto fail;
  713. }
  714. } else
  715. atomic_inc(&prtd->out_needed);
  716. atomic_dec(&prtd->out_count);
  717. }
  718. }
  719. fail:
  720. if (retries >= MAX_PB_COPY_RETRIES)
  721. ret = -ENOMEM;
  722. return ret;
  723. }
  724. static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
  725. {
  726. struct snd_pcm_runtime *runtime = substream->runtime;
  727. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  728. struct msm_audio *prtd = runtime->private_data;
  729. uint32_t timeout;
  730. int dir = 0;
  731. int ret = 0;
  732. pr_debug("%s: cmd_pending 0x%lx\n", __func__, prtd->cmd_pending);
  733. if (prtd->audio_client) {
  734. dir = IN;
  735. /* determine timeout length */
  736. if (runtime->frame_bits == 0 || runtime->rate == 0) {
  737. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  738. } else {
  739. timeout = (runtime->period_size *
  740. CMD_EOS_TIMEOUT_MULTIPLIER) /
  741. ((runtime->frame_bits / 8) *
  742. runtime->rate);
  743. if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
  744. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  745. }
  746. pr_debug("%s: CMD_EOS timeout is %d\n", __func__, timeout);
  747. ret = wait_event_timeout(the_locks.eos_wait,
  748. !test_bit(CMD_EOS, &prtd->cmd_pending),
  749. timeout);
  750. if (!ret)
  751. pr_err("%s: CMD_EOS failed, cmd_pending 0x%lx\n",
  752. __func__, prtd->cmd_pending);
  753. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  754. q6asm_audio_client_buf_free_contiguous(dir,
  755. prtd->audio_client);
  756. q6asm_audio_client_free(prtd->audio_client);
  757. }
  758. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  759. SNDRV_PCM_STREAM_PLAYBACK);
  760. msm_adsp_clean_mixer_ctl_pp_event_queue(soc_prtd);
  761. kfree(prtd);
  762. runtime->private_data = NULL;
  763. return 0;
  764. }
  765. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  766. int channel, snd_pcm_uframes_t hwoff, void __user *buf,
  767. snd_pcm_uframes_t frames)
  768. {
  769. int ret = 0;
  770. int fbytes = 0;
  771. int xfer;
  772. char *bufptr;
  773. void *data = NULL;
  774. static uint32_t idx;
  775. static uint32_t size;
  776. uint32_t offset = 0;
  777. struct snd_pcm_runtime *runtime = substream->runtime;
  778. struct msm_audio *prtd = substream->runtime->private_data;
  779. pr_debug("%s\n", __func__);
  780. fbytes = frames_to_bytes(runtime, frames);
  781. pr_debug("appl_ptr %d\n", (int)runtime->control->appl_ptr);
  782. pr_debug("hw_ptr %d\n", (int)runtime->status->hw_ptr);
  783. pr_debug("avail_min %d\n", (int)runtime->control->avail_min);
  784. if (prtd->reset_event) {
  785. pr_err("%s: In SSR return ENETRESET before wait\n", __func__);
  786. return -ENETRESET;
  787. }
  788. ret = wait_event_timeout(the_locks.read_wait,
  789. (atomic_read(&prtd->in_count)), 5 * HZ);
  790. if (!ret) {
  791. pr_debug("%s: wait_event_timeout failed\n", __func__);
  792. goto fail;
  793. }
  794. if (prtd->reset_event) {
  795. pr_err("%s: In SSR return ENETRESET after wait\n", __func__);
  796. return -ENETRESET;
  797. }
  798. if (!atomic_read(&prtd->in_count)) {
  799. pr_debug("%s: pcm stopped in_count 0\n", __func__);
  800. return 0;
  801. }
  802. pr_debug("Checking if valid buffer is available...%pK\n",
  803. data);
  804. data = q6asm_is_cpu_buf_avail(OUT, prtd->audio_client, &size, &idx);
  805. bufptr = data;
  806. pr_debug("Size = %d\n", size);
  807. pr_debug("fbytes = %d\n", fbytes);
  808. pr_debug("idx = %d\n", idx);
  809. if (bufptr) {
  810. xfer = fbytes;
  811. if (xfer > size)
  812. xfer = size;
  813. offset = prtd->in_frame_info[idx].offset;
  814. pr_debug("Offset value = %d\n", offset);
  815. if (copy_to_user(buf, bufptr+offset, xfer)) {
  816. pr_err("Failed to copy buf to user\n");
  817. ret = -EFAULT;
  818. q6asm_cpu_buf_release(OUT, prtd->audio_client);
  819. goto fail;
  820. }
  821. fbytes -= xfer;
  822. size -= xfer;
  823. prtd->in_frame_info[idx].offset += xfer;
  824. pr_debug("%s:fbytes = %d: size=%d: xfer=%d\n",
  825. __func__, fbytes, size, xfer);
  826. pr_debug(" Sending next buffer to dsp\n");
  827. memset(&prtd->in_frame_info[idx], 0,
  828. sizeof(struct msm_audio_in_frame_info));
  829. atomic_dec(&prtd->in_count);
  830. ret = q6asm_read(prtd->audio_client);
  831. if (ret < 0) {
  832. pr_err("q6asm read failed\n");
  833. ret = -EFAULT;
  834. q6asm_cpu_buf_release(OUT, prtd->audio_client);
  835. goto fail;
  836. }
  837. } else
  838. pr_err("No valid buffer\n");
  839. pr_debug("Returning from capture_copy... %d\n", ret);
  840. fail:
  841. return ret;
  842. }
  843. static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
  844. {
  845. struct snd_pcm_runtime *runtime = substream->runtime;
  846. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  847. struct msm_audio *prtd = runtime->private_data;
  848. int dir = OUT;
  849. pr_debug("%s\n", __func__);
  850. if (prtd->audio_client) {
  851. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  852. q6asm_audio_client_buf_free_contiguous(dir,
  853. prtd->audio_client);
  854. q6asm_audio_client_free(prtd->audio_client);
  855. }
  856. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  857. SNDRV_PCM_STREAM_CAPTURE);
  858. kfree(prtd);
  859. runtime->private_data = NULL;
  860. return 0;
  861. }
  862. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  863. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  864. {
  865. int ret = 0;
  866. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  867. ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
  868. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  869. ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
  870. return ret;
  871. }
  872. static int msm_pcm_close(struct snd_pcm_substream *substream)
  873. {
  874. int ret = 0;
  875. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  876. ret = msm_pcm_playback_close(substream);
  877. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  878. ret = msm_pcm_capture_close(substream);
  879. return ret;
  880. }
  881. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  882. {
  883. int ret = 0;
  884. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  885. ret = msm_pcm_playback_prepare(substream);
  886. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  887. ret = msm_pcm_capture_prepare(substream);
  888. return ret;
  889. }
  890. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  891. {
  892. struct snd_pcm_runtime *runtime = substream->runtime;
  893. struct msm_audio *prtd = runtime->private_data;
  894. if (prtd->pcm_irq_pos >= prtd->pcm_size)
  895. prtd->pcm_irq_pos = 0;
  896. pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
  897. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  898. }
  899. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  900. struct vm_area_struct *vma)
  901. {
  902. struct snd_pcm_runtime *runtime = substream->runtime;
  903. struct msm_audio *prtd = runtime->private_data;
  904. struct audio_client *ac = prtd->audio_client;
  905. struct audio_port_data *apd = ac->port;
  906. struct audio_buffer *ab;
  907. int dir = -1;
  908. prtd->mmap_flag = 1;
  909. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  910. dir = IN;
  911. else
  912. dir = OUT;
  913. ab = &(apd[dir].buf[0]);
  914. return msm_audio_ion_mmap(ab, vma);
  915. }
  916. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  917. struct snd_pcm_hw_params *params)
  918. {
  919. struct snd_pcm_runtime *runtime = substream->runtime;
  920. struct msm_audio *prtd = runtime->private_data;
  921. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  922. struct audio_buffer *buf;
  923. int dir, ret;
  924. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  925. dir = IN;
  926. else
  927. dir = OUT;
  928. ret = q6asm_audio_client_buf_alloc_contiguous(dir,
  929. prtd->audio_client,
  930. (params_buffer_bytes(params) / params_periods(params)),
  931. params_periods(params));
  932. if (ret < 0) {
  933. pr_err("Audio Start: Buffer Allocation failed rc = %d\n",
  934. ret);
  935. return -ENOMEM;
  936. }
  937. buf = prtd->audio_client->port[dir].buf;
  938. if (buf == NULL || buf[0].data == NULL)
  939. return -ENOMEM;
  940. pr_debug("%s:buf = %pK\n", __func__, buf);
  941. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  942. dma_buf->dev.dev = substream->pcm->card->dev;
  943. dma_buf->private_data = NULL;
  944. dma_buf->area = buf[0].data;
  945. dma_buf->addr = buf[0].phys;
  946. dma_buf->bytes = params_buffer_bytes(params);
  947. if (!dma_buf->area)
  948. return -ENOMEM;
  949. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  950. return 0;
  951. }
  952. static const struct snd_pcm_ops msm_pcm_ops = {
  953. .open = msm_pcm_open,
  954. .copy = msm_pcm_copy,
  955. .hw_params = msm_pcm_hw_params,
  956. .close = msm_pcm_close,
  957. .ioctl = snd_pcm_lib_ioctl,
  958. .prepare = msm_pcm_prepare,
  959. .trigger = msm_pcm_trigger,
  960. .pointer = msm_pcm_pointer,
  961. .mmap = msm_pcm_mmap,
  962. };
  963. static int msm_pcm_adsp_stream_cmd_put(struct snd_kcontrol *kcontrol,
  964. struct snd_ctl_elem_value *ucontrol)
  965. {
  966. struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
  967. struct snd_soc_platform *platform = snd_soc_component_to_platform(pcm);
  968. struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
  969. struct snd_pcm_substream *substream;
  970. struct msm_audio *prtd;
  971. int ret = 0;
  972. struct msm_adsp_event_data *event_data = NULL;
  973. if (!pdata) {
  974. pr_err("%s pdata is NULL\n", __func__);
  975. ret = -ENODEV;
  976. goto done;
  977. }
  978. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  979. if (!substream) {
  980. pr_err("%s substream not found\n", __func__);
  981. ret = -EINVAL;
  982. goto done;
  983. }
  984. if (!substream->runtime) {
  985. pr_err("%s substream runtime not found\n", __func__);
  986. ret = -EINVAL;
  987. goto done;
  988. }
  989. prtd = substream->runtime->private_data;
  990. if (prtd->audio_client == NULL) {
  991. pr_err("%s prtd is null.\n", __func__);
  992. ret = -EINVAL;
  993. goto done;
  994. }
  995. event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
  996. if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
  997. (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
  998. pr_err("%s: invalid event_type=%d",
  999. __func__, event_data->event_type);
  1000. ret = -EINVAL;
  1001. goto done;
  1002. }
  1003. if ((sizeof(struct msm_adsp_event_data) + event_data->payload_len) >=
  1004. sizeof(ucontrol->value.bytes.data)) {
  1005. pr_err("%s param length=%d exceeds limit",
  1006. __func__, event_data->payload_len);
  1007. ret = -EINVAL;
  1008. goto done;
  1009. }
  1010. ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
  1011. if (ret < 0)
  1012. pr_err("%s: failed to send stream event cmd, err = %d\n",
  1013. __func__, ret);
  1014. done:
  1015. return ret;
  1016. }
  1017. static int msm_pcm_add_audio_adsp_stream_cmd_control(
  1018. struct snd_soc_pcm_runtime *rtd)
  1019. {
  1020. const char *mixer_ctl_name = DSP_STREAM_CMD;
  1021. const char *deviceNo = "NN";
  1022. char *mixer_str = NULL;
  1023. int ctl_len = 0, ret = 0;
  1024. struct snd_kcontrol_new fe_audio_adsp_stream_cmd_config_control[1] = {
  1025. {
  1026. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1027. .name = "?",
  1028. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1029. .info = msm_adsp_stream_cmd_info,
  1030. .put = msm_pcm_adsp_stream_cmd_put,
  1031. .private_value = 0,
  1032. }
  1033. };
  1034. if (!rtd) {
  1035. pr_err("%s rtd is NULL\n", __func__);
  1036. ret = -EINVAL;
  1037. goto done;
  1038. }
  1039. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1040. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1041. if (!mixer_str) {
  1042. ret = -ENOMEM;
  1043. goto done;
  1044. }
  1045. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1046. fe_audio_adsp_stream_cmd_config_control[0].name = mixer_str;
  1047. fe_audio_adsp_stream_cmd_config_control[0].private_value =
  1048. rtd->dai_link->id;
  1049. pr_debug("Registering new mixer ctl %s\n", mixer_str);
  1050. ret = snd_soc_add_platform_controls(rtd->platform,
  1051. fe_audio_adsp_stream_cmd_config_control,
  1052. ARRAY_SIZE(fe_audio_adsp_stream_cmd_config_control));
  1053. if (ret < 0)
  1054. pr_err("%s: failed add ctl %s. err = %d\n",
  1055. __func__, mixer_str, ret);
  1056. kfree(mixer_str);
  1057. done:
  1058. return ret;
  1059. }
  1060. static int msm_pcm_add_audio_adsp_stream_callback_control(
  1061. struct snd_soc_pcm_runtime *rtd)
  1062. {
  1063. const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
  1064. const char *deviceNo = "NN";
  1065. char *mixer_str = NULL;
  1066. int ctl_len = 0, ret = 0;
  1067. struct snd_kcontrol *kctl;
  1068. struct snd_kcontrol_new fe_audio_adsp_callback_config_control[1] = {
  1069. {
  1070. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1071. .name = "?",
  1072. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1073. .info = msm_adsp_stream_callback_info,
  1074. .get = msm_adsp_stream_callback_get,
  1075. .private_value = 0,
  1076. }
  1077. };
  1078. if (!rtd) {
  1079. pr_err("%s NULL rtd\n", __func__);
  1080. ret = -EINVAL;
  1081. goto done;
  1082. }
  1083. pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
  1084. __func__, rtd->dai_link->name, rtd->dai_link->id,
  1085. rtd->dai_link->cpu_dai_name, rtd->pcm->device);
  1086. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1087. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1088. if (!mixer_str) {
  1089. ret = -ENOMEM;
  1090. goto done;
  1091. }
  1092. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1093. fe_audio_adsp_callback_config_control[0].name = mixer_str;
  1094. fe_audio_adsp_callback_config_control[0].private_value =
  1095. rtd->dai_link->id;
  1096. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1097. ret = snd_soc_add_platform_controls(rtd->platform,
  1098. fe_audio_adsp_callback_config_control,
  1099. ARRAY_SIZE(fe_audio_adsp_callback_config_control));
  1100. if (ret < 0) {
  1101. pr_err("%s: failed to add ctl %s. err = %d\n",
  1102. __func__, mixer_str, ret);
  1103. ret = -EINVAL;
  1104. goto free_mixer_str;
  1105. }
  1106. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  1107. if (!kctl) {
  1108. pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
  1109. ret = -EINVAL;
  1110. goto free_mixer_str;
  1111. }
  1112. kctl->private_data = NULL;
  1113. free_mixer_str:
  1114. kfree(mixer_str);
  1115. done:
  1116. return ret;
  1117. }
  1118. static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
  1119. {
  1120. int rc = 0;
  1121. if (prtd && prtd->audio_client) {
  1122. pr_debug("%s: channels %d volume 0x%x\n", __func__,
  1123. prtd->channel_mode, volume);
  1124. rc = q6asm_set_volume(prtd->audio_client, volume);
  1125. if (rc < 0) {
  1126. pr_err("%s: Send Volume command failed rc=%d\n",
  1127. __func__, rc);
  1128. }
  1129. }
  1130. return rc;
  1131. }
  1132. static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
  1133. struct snd_ctl_elem_value *ucontrol)
  1134. {
  1135. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  1136. struct snd_pcm_substream *substream =
  1137. vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1138. struct msm_audio *prtd;
  1139. pr_debug("%s\n", __func__);
  1140. if (!substream) {
  1141. pr_err("%s substream not found\n", __func__);
  1142. return -ENODEV;
  1143. }
  1144. if (!substream->runtime) {
  1145. pr_err("%s substream runtime not found\n", __func__);
  1146. return 0;
  1147. }
  1148. prtd = substream->runtime->private_data;
  1149. if (prtd)
  1150. ucontrol->value.integer.value[0] = prtd->volume;
  1151. return 0;
  1152. }
  1153. static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
  1154. struct snd_ctl_elem_value *ucontrol)
  1155. {
  1156. int rc = 0;
  1157. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  1158. struct snd_pcm_substream *substream =
  1159. vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1160. struct msm_audio *prtd;
  1161. int volume = ucontrol->value.integer.value[0];
  1162. pr_debug("%s: volume : 0x%x\n", __func__, volume);
  1163. if (!substream) {
  1164. pr_err("%s substream not found\n", __func__);
  1165. return -ENODEV;
  1166. }
  1167. if (!substream->runtime) {
  1168. pr_err("%s substream runtime not found\n", __func__);
  1169. return 0;
  1170. }
  1171. prtd = substream->runtime->private_data;
  1172. if (prtd) {
  1173. rc = msm_pcm_set_volume(prtd, volume);
  1174. prtd->volume = volume;
  1175. }
  1176. return rc;
  1177. }
  1178. static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd)
  1179. {
  1180. int ret = 0;
  1181. struct snd_pcm *pcm = rtd->pcm;
  1182. struct snd_pcm_volume *volume_info;
  1183. struct snd_kcontrol *kctl;
  1184. dev_dbg(rtd->dev, "%s, Volume control add\n", __func__);
  1185. ret = snd_pcm_add_volume_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1186. NULL, 1, rtd->dai_link->id,
  1187. &volume_info);
  1188. if (ret < 0) {
  1189. pr_err("%s volume control failed ret %d\n", __func__, ret);
  1190. return ret;
  1191. }
  1192. kctl = volume_info->kctl;
  1193. kctl->put = msm_pcm_volume_ctl_put;
  1194. kctl->get = msm_pcm_volume_ctl_get;
  1195. kctl->tlv.p = msm_pcm_vol_gain;
  1196. return 0;
  1197. }
  1198. static int msm_pcm_compress_ctl_info(struct snd_kcontrol *kcontrol,
  1199. struct snd_ctl_elem_info *uinfo)
  1200. {
  1201. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1202. uinfo->count = 1;
  1203. uinfo->value.integer.min = 0;
  1204. uinfo->value.integer.max = 0x2000;
  1205. return 0;
  1206. }
  1207. static int msm_pcm_compress_ctl_get(struct snd_kcontrol *kcontrol,
  1208. struct snd_ctl_elem_value *ucontrol)
  1209. {
  1210. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  1211. struct snd_soc_platform *platform = snd_soc_component_to_platform(comp);
  1212. struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
  1213. struct snd_pcm_substream *substream;
  1214. struct msm_audio *prtd;
  1215. if (!pdata) {
  1216. pr_err("%s pdata is NULL\n", __func__);
  1217. return -ENODEV;
  1218. }
  1219. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1220. if (!substream) {
  1221. pr_err("%s substream not found\n", __func__);
  1222. return -EINVAL;
  1223. }
  1224. if (!substream->runtime) {
  1225. pr_err("%s substream runtime not found\n", __func__);
  1226. return 0;
  1227. }
  1228. prtd = substream->runtime->private_data;
  1229. if (prtd)
  1230. ucontrol->value.integer.value[0] = prtd->compress_enable;
  1231. return 0;
  1232. }
  1233. static int msm_pcm_compress_ctl_put(struct snd_kcontrol *kcontrol,
  1234. struct snd_ctl_elem_value *ucontrol)
  1235. {
  1236. int rc = 0;
  1237. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  1238. struct snd_soc_platform *platform = snd_soc_component_to_platform(comp);
  1239. struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
  1240. struct snd_pcm_substream *substream;
  1241. struct msm_audio *prtd;
  1242. int compress = ucontrol->value.integer.value[0];
  1243. if (!pdata) {
  1244. pr_err("%s pdata is NULL\n", __func__);
  1245. return -ENODEV;
  1246. }
  1247. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1248. pr_debug("%s: compress : 0x%x\n", __func__, compress);
  1249. if (!substream) {
  1250. pr_err("%s substream not found\n", __func__);
  1251. return -EINVAL;
  1252. }
  1253. if (!substream->runtime) {
  1254. pr_err("%s substream runtime not found\n", __func__);
  1255. return 0;
  1256. }
  1257. prtd = substream->runtime->private_data;
  1258. if (prtd) {
  1259. pr_debug("%s: setting compress flag to 0x%x\n",
  1260. __func__, compress);
  1261. prtd->compress_enable = compress;
  1262. }
  1263. return rc;
  1264. }
  1265. static int msm_pcm_add_compress_control(struct snd_soc_pcm_runtime *rtd)
  1266. {
  1267. const char *mixer_ctl_name = "Playback ";
  1268. const char *mixer_ctl_end_name = " Compress";
  1269. const char *deviceNo = "NN";
  1270. char *mixer_str = NULL;
  1271. int ctl_len;
  1272. int ret = 0;
  1273. struct msm_plat_data *pdata;
  1274. struct snd_kcontrol_new pcm_compress_control[1] = {
  1275. {
  1276. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1277. .name = "?",
  1278. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1279. .info = msm_pcm_compress_ctl_info,
  1280. .get = msm_pcm_compress_ctl_get,
  1281. .put = msm_pcm_compress_ctl_put,
  1282. .private_value = 0,
  1283. }
  1284. };
  1285. if (!rtd) {
  1286. pr_err("%s: NULL rtd\n", __func__);
  1287. return -EINVAL;
  1288. }
  1289. ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) +
  1290. strlen(mixer_ctl_end_name) + 1;
  1291. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1292. if (!mixer_str)
  1293. return -ENOMEM;
  1294. snprintf(mixer_str, ctl_len, "%s%d%s", mixer_ctl_name,
  1295. rtd->pcm->device, mixer_ctl_end_name);
  1296. pcm_compress_control[0].name = mixer_str;
  1297. pcm_compress_control[0].private_value = rtd->dai_link->id;
  1298. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1299. pdata = dev_get_drvdata(rtd->platform->dev);
  1300. if (pdata) {
  1301. if (!pdata->pcm) {
  1302. pdata->pcm = rtd->pcm;
  1303. snd_soc_add_platform_controls(rtd->platform,
  1304. pcm_compress_control,
  1305. ARRAY_SIZE
  1306. (pcm_compress_control));
  1307. pr_debug("%s: add control success plt = %pK\n",
  1308. __func__, rtd->platform);
  1309. }
  1310. } else {
  1311. pr_err("%s: NULL pdata\n", __func__);
  1312. ret = -EINVAL;
  1313. }
  1314. kfree(mixer_str);
  1315. return ret;
  1316. }
  1317. static int msm_pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
  1318. struct snd_ctl_elem_value *ucontrol)
  1319. {
  1320. int i;
  1321. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1322. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1323. struct snd_pcm_substream *substream;
  1324. struct msm_audio *prtd;
  1325. pr_debug("%s", __func__);
  1326. substream = snd_pcm_chmap_substream(info, idx);
  1327. if (!substream)
  1328. return -ENODEV;
  1329. if (!substream->runtime)
  1330. return 0;
  1331. prtd = substream->runtime->private_data;
  1332. if (prtd) {
  1333. prtd->set_channel_map = true;
  1334. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  1335. prtd->channel_map[i] =
  1336. (char)(ucontrol->value.integer.value[i]);
  1337. }
  1338. return 0;
  1339. }
  1340. static int msm_pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  1341. struct snd_ctl_elem_value *ucontrol)
  1342. {
  1343. int i;
  1344. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1345. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1346. struct snd_pcm_substream *substream;
  1347. struct msm_audio *prtd;
  1348. pr_debug("%s", __func__);
  1349. substream = snd_pcm_chmap_substream(info, idx);
  1350. if (!substream)
  1351. return -ENODEV;
  1352. memset(ucontrol->value.integer.value, 0,
  1353. sizeof(ucontrol->value.integer.value));
  1354. if (!substream->runtime)
  1355. return 0; /* no channels set */
  1356. prtd = substream->runtime->private_data;
  1357. if (prtd && prtd->set_channel_map == true) {
  1358. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  1359. ucontrol->value.integer.value[i] =
  1360. (int)prtd->channel_map[i];
  1361. } else {
  1362. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  1363. ucontrol->value.integer.value[i] = 0;
  1364. }
  1365. return 0;
  1366. }
  1367. static int msm_pcm_add_chmap_controls(struct snd_soc_pcm_runtime *rtd)
  1368. {
  1369. struct snd_pcm *pcm = rtd->pcm;
  1370. struct snd_pcm_chmap *chmap_info;
  1371. struct snd_kcontrol *kctl;
  1372. char device_num[12];
  1373. int i, ret = 0;
  1374. pr_debug("%s, Channel map cntrl add\n", __func__);
  1375. ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1376. snd_pcm_std_chmaps,
  1377. PCM_FORMAT_MAX_NUM_CHANNEL, 0,
  1378. &chmap_info);
  1379. if (ret < 0) {
  1380. pr_err("%s, channel map cntrl add failed\n", __func__);
  1381. return ret;
  1382. }
  1383. kctl = chmap_info->kctl;
  1384. for (i = 0; i < kctl->count; i++)
  1385. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  1386. snprintf(device_num, sizeof(device_num), "%d", pcm->device);
  1387. strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
  1388. pr_debug("%s, Overwriting channel map control name to: %s\n",
  1389. __func__, kctl->id.name);
  1390. kctl->put = msm_pcm_chmap_ctl_put;
  1391. kctl->get = msm_pcm_chmap_ctl_get;
  1392. return 0;
  1393. }
  1394. static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1395. struct snd_ctl_elem_value *ucontrol)
  1396. {
  1397. u64 fe_id = kcontrol->private_value;
  1398. int session_type = SESSION_TYPE_RX;
  1399. int be_id = ucontrol->value.integer.value[3];
  1400. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1401. int ret = 0;
  1402. cfg_data.app_type = ucontrol->value.integer.value[0];
  1403. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1404. if (ucontrol->value.integer.value[2] != 0)
  1405. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1406. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1407. __func__, fe_id, session_type, be_id,
  1408. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1409. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1410. be_id, &cfg_data);
  1411. if (ret < 0)
  1412. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1413. __func__, ret);
  1414. return ret;
  1415. }
  1416. static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1417. struct snd_ctl_elem_value *ucontrol)
  1418. {
  1419. u64 fe_id = kcontrol->private_value;
  1420. int session_type = SESSION_TYPE_RX;
  1421. int be_id = 0;
  1422. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1423. int ret = 0;
  1424. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1425. &be_id, &cfg_data);
  1426. if (ret < 0) {
  1427. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1428. __func__, ret);
  1429. goto done;
  1430. }
  1431. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1432. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1433. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1434. ucontrol->value.integer.value[3] = be_id;
  1435. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1436. __func__, fe_id, session_type, be_id,
  1437. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1438. done:
  1439. return ret;
  1440. }
  1441. static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1442. struct snd_ctl_elem_value *ucontrol)
  1443. {
  1444. u64 fe_id = kcontrol->private_value;
  1445. int session_type = SESSION_TYPE_TX;
  1446. int be_id = ucontrol->value.integer.value[3];
  1447. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1448. int ret = 0;
  1449. cfg_data.app_type = ucontrol->value.integer.value[0];
  1450. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1451. if (ucontrol->value.integer.value[2] != 0)
  1452. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1453. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1454. __func__, fe_id, session_type, be_id,
  1455. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1456. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1457. be_id, &cfg_data);
  1458. if (ret < 0)
  1459. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1460. __func__, ret);
  1461. return ret;
  1462. }
  1463. static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1464. struct snd_ctl_elem_value *ucontrol)
  1465. {
  1466. u64 fe_id = kcontrol->private_value;
  1467. int session_type = SESSION_TYPE_TX;
  1468. int be_id = 0;
  1469. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1470. int ret = 0;
  1471. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1472. &be_id, &cfg_data);
  1473. if (ret < 0) {
  1474. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1475. __func__, ret);
  1476. goto done;
  1477. }
  1478. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1479. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1480. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1481. ucontrol->value.integer.value[3] = be_id;
  1482. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1483. __func__, fe_id, session_type, be_id,
  1484. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1485. done:
  1486. return ret;
  1487. }
  1488. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  1489. {
  1490. struct snd_pcm *pcm = rtd->pcm;
  1491. struct snd_pcm_usr *app_type_info;
  1492. struct snd_kcontrol *kctl;
  1493. const char *playback_mixer_ctl_name = "Audio Stream";
  1494. const char *capture_mixer_ctl_name = "Audio Stream Capture";
  1495. const char *deviceNo = "NN";
  1496. const char *suffix = "App Type Cfg";
  1497. int ctl_len, ret = 0;
  1498. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  1499. ctl_len = strlen(playback_mixer_ctl_name) + 1 +
  1500. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1501. pr_debug("%s: Playback app type cntrl add\n", __func__);
  1502. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1503. NULL, 1, ctl_len, rtd->dai_link->id,
  1504. &app_type_info);
  1505. if (ret < 0) {
  1506. pr_err("%s: playback app type cntrl add failed: %d\n",
  1507. __func__, ret);
  1508. return ret;
  1509. }
  1510. kctl = app_type_info->kctl;
  1511. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1512. playback_mixer_ctl_name, rtd->pcm->device, suffix);
  1513. kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
  1514. kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
  1515. }
  1516. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  1517. ctl_len = strlen(capture_mixer_ctl_name) + 1 +
  1518. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1519. pr_debug("%s: Capture app type cntrl add\n", __func__);
  1520. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1521. NULL, 1, ctl_len, rtd->dai_link->id,
  1522. &app_type_info);
  1523. if (ret < 0) {
  1524. pr_err("%s: capture app type cntrl add failed: %d\n",
  1525. __func__, ret);
  1526. return ret;
  1527. }
  1528. kctl = app_type_info->kctl;
  1529. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1530. capture_mixer_ctl_name, rtd->pcm->device, suffix);
  1531. kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
  1532. kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
  1533. }
  1534. return 0;
  1535. }
  1536. static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
  1537. {
  1538. int ret = 0;
  1539. pr_debug("%s\n", __func__);
  1540. ret = msm_pcm_add_chmap_controls(rtd);
  1541. if (ret)
  1542. pr_err("%s: pcm add controls failed:%d\n", __func__, ret);
  1543. ret = msm_pcm_add_app_type_controls(rtd);
  1544. if (ret)
  1545. pr_err("%s: pcm add app type controls failed:%d\n",
  1546. __func__, ret);
  1547. return ret;
  1548. }
  1549. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1550. {
  1551. struct snd_card *card = rtd->card->snd_card;
  1552. int ret = 0;
  1553. if (!card->dev->coherent_dma_mask)
  1554. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1555. ret = msm_pcm_add_controls(rtd);
  1556. if (ret) {
  1557. pr_err("%s, kctl add failed:%d\n", __func__, ret);
  1558. return ret;
  1559. }
  1560. ret = msm_pcm_add_volume_control(rtd);
  1561. if (ret)
  1562. pr_err("%s: Could not add pcm Volume Control %d\n",
  1563. __func__, ret);
  1564. ret = msm_pcm_add_compress_control(rtd);
  1565. if (ret)
  1566. pr_err("%s: Could not add pcm Compress Control %d\n",
  1567. __func__, ret);
  1568. ret = msm_pcm_add_audio_adsp_stream_cmd_control(rtd);
  1569. if (ret)
  1570. pr_err("%s: Could not add pcm ADSP Stream Cmd Control\n",
  1571. __func__);
  1572. ret = msm_pcm_add_audio_adsp_stream_callback_control(rtd);
  1573. if (ret)
  1574. pr_err("%s: Could not add pcm ADSP Stream Callback Control\n",
  1575. __func__);
  1576. return ret;
  1577. }
  1578. static snd_pcm_sframes_t msm_pcm_delay_blk(struct snd_pcm_substream *substream,
  1579. struct snd_soc_dai *dai)
  1580. {
  1581. struct snd_pcm_runtime *runtime = substream->runtime;
  1582. struct msm_audio *prtd = runtime->private_data;
  1583. struct audio_client *ac = prtd->audio_client;
  1584. snd_pcm_sframes_t frames;
  1585. int ret;
  1586. ret = q6asm_get_path_delay(prtd->audio_client);
  1587. if (ret) {
  1588. pr_err("%s: get_path_delay failed, ret=%d\n", __func__, ret);
  1589. return 0;
  1590. }
  1591. /* convert microseconds to frames */
  1592. frames = ac->path_delay / 1000 * runtime->rate / 1000;
  1593. /* also convert the remainder from the initial division */
  1594. frames += ac->path_delay % 1000 * runtime->rate / 1000000;
  1595. /* overcompensate for the loss of precision (empirical) */
  1596. frames += 2;
  1597. return frames;
  1598. }
  1599. static struct snd_soc_platform_driver msm_soc_platform = {
  1600. .ops = &msm_pcm_ops,
  1601. .pcm_new = msm_asoc_pcm_new,
  1602. .delay_blk = msm_pcm_delay_blk,
  1603. };
  1604. static int msm_pcm_probe(struct platform_device *pdev)
  1605. {
  1606. int rc;
  1607. int id;
  1608. struct msm_plat_data *pdata;
  1609. const char *latency_level;
  1610. rc = of_property_read_u32(pdev->dev.of_node,
  1611. "qcom,msm-pcm-dsp-id", &id);
  1612. if (rc) {
  1613. dev_err(&pdev->dev, "%s: qcom,msm-pcm-dsp-id missing in DT node\n",
  1614. __func__);
  1615. return rc;
  1616. }
  1617. pdata = kzalloc(sizeof(struct msm_plat_data), GFP_KERNEL);
  1618. if (!pdata)
  1619. return -ENOMEM;
  1620. if (of_property_read_bool(pdev->dev.of_node,
  1621. "qcom,msm-pcm-low-latency")) {
  1622. pdata->perf_mode = LOW_LATENCY_PCM_MODE;
  1623. rc = of_property_read_string(pdev->dev.of_node,
  1624. "qcom,latency-level", &latency_level);
  1625. if (!rc) {
  1626. if (!strcmp(latency_level, "ultra"))
  1627. pdata->perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
  1628. else if (!strcmp(latency_level, "ull-pp"))
  1629. pdata->perf_mode =
  1630. ULL_POST_PROCESSING_PCM_MODE;
  1631. }
  1632. } else {
  1633. pdata->perf_mode = LEGACY_PCM_MODE;
  1634. }
  1635. dev_set_drvdata(&pdev->dev, pdata);
  1636. dev_dbg(&pdev->dev, "%s: dev name %s\n",
  1637. __func__, dev_name(&pdev->dev));
  1638. return snd_soc_register_platform(&pdev->dev,
  1639. &msm_soc_platform);
  1640. }
  1641. static int msm_pcm_remove(struct platform_device *pdev)
  1642. {
  1643. struct msm_plat_data *pdata;
  1644. pdata = dev_get_drvdata(&pdev->dev);
  1645. kfree(pdata);
  1646. snd_soc_unregister_platform(&pdev->dev);
  1647. return 0;
  1648. }
  1649. static const struct of_device_id msm_pcm_dt_match[] = {
  1650. {.compatible = "qcom,msm-pcm-dsp"},
  1651. {}
  1652. };
  1653. MODULE_DEVICE_TABLE(of, msm_pcm_dt_match);
  1654. static struct platform_driver msm_pcm_driver = {
  1655. .driver = {
  1656. .name = "msm-pcm-dsp",
  1657. .owner = THIS_MODULE,
  1658. .of_match_table = msm_pcm_dt_match,
  1659. },
  1660. .probe = msm_pcm_probe,
  1661. .remove = msm_pcm_remove,
  1662. };
  1663. int __init msm_pcm_dsp_init(void)
  1664. {
  1665. init_waitqueue_head(&the_locks.enable_wait);
  1666. init_waitqueue_head(&the_locks.eos_wait);
  1667. init_waitqueue_head(&the_locks.write_wait);
  1668. init_waitqueue_head(&the_locks.read_wait);
  1669. return platform_driver_register(&msm_pcm_driver);
  1670. }
  1671. void __exit msm_pcm_dsp_exit(void)
  1672. {
  1673. platform_driver_unregister(&msm_pcm_driver);
  1674. }
  1675. MODULE_DESCRIPTION("PCM module platform driver");
  1676. MODULE_LICENSE("GPL v2");