msm-pcm-q6-v2.c 54 KB

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