msm-pcm-q6-v2.c 55 KB

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