msm-multi-ch-pcm-q6.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /* Copyright (c) 2011-2012, 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 <linux/dma-mapping.h>
  21. #include <asm/dma.h>
  22. #include <sound/core.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dapm.h>
  25. #include <sound/pcm.h>
  26. #include <sound/initval.h>
  27. #include <sound/control.h>
  28. #include <sound/timer.h>
  29. #include "msm-pcm-q6.h"
  30. #include "msm-pcm-routing.h"
  31. static struct audio_locks the_locks;
  32. struct snd_msm {
  33. struct snd_card *card;
  34. struct snd_pcm *pcm;
  35. };
  36. struct snd_msm_volume {
  37. struct msm_audio *prtd;
  38. unsigned volume;
  39. };
  40. static struct snd_msm_volume multi_ch_pcm_audio = {NULL, 0x2000};
  41. #define PLAYBACK_NUM_PERIODS 8
  42. #define PLAYBACK_MAX_PERIOD_SIZE 12288
  43. #define PLAYBACK_MIN_PERIOD_SIZE 256
  44. #define CAPTURE_NUM_PERIODS 16
  45. #define CAPTURE_MIN_PERIOD_SIZE 320
  46. #define CAPTURE_MAX_PERIOD_SIZE 12288
  47. static struct snd_pcm_hardware msm_pcm_hardware_capture = {
  48. .info = (SNDRV_PCM_INFO_MMAP |
  49. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  50. SNDRV_PCM_INFO_MMAP_VALID |
  51. SNDRV_PCM_INFO_INTERLEAVED |
  52. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  53. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  54. .rates = SNDRV_PCM_RATE_8000_48000,
  55. .rate_min = 8000,
  56. .rate_max = 48000,
  57. .channels_min = 1,
  58. .channels_max = 8,
  59. .buffer_bytes_max = CAPTURE_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
  60. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  61. .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
  62. .periods_min = CAPTURE_NUM_PERIODS,
  63. .periods_max = CAPTURE_NUM_PERIODS,
  64. .fifo_size = 0,
  65. };
  66. static struct snd_pcm_hardware msm_pcm_hardware_playback = {
  67. .info = (SNDRV_PCM_INFO_MMAP |
  68. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  69. SNDRV_PCM_INFO_MMAP_VALID |
  70. SNDRV_PCM_INFO_INTERLEAVED |
  71. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  72. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  73. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_KNOT,
  74. .rate_min = 8000,
  75. .rate_max = 48000,
  76. .channels_min = 1,
  77. .channels_max = 8,
  78. .buffer_bytes_max = PLAYBACK_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE,
  79. .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
  80. .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
  81. .periods_min = PLAYBACK_NUM_PERIODS,
  82. .periods_max = PLAYBACK_NUM_PERIODS,
  83. .fifo_size = 0,
  84. };
  85. /* Conventional and unconventional sample rate supported */
  86. static unsigned int supported_sample_rates[] = {
  87. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
  88. };
  89. static uint32_t in_frame_info[CAPTURE_NUM_PERIODS][2];
  90. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  91. .count = ARRAY_SIZE(supported_sample_rates),
  92. .list = supported_sample_rates,
  93. .mask = 0,
  94. };
  95. static void event_handler(uint32_t opcode,
  96. uint32_t token, uint32_t *payload, void *priv)
  97. {
  98. struct msm_audio *prtd = priv;
  99. struct snd_pcm_substream *substream = prtd->substream;
  100. struct snd_pcm_runtime *runtime = substream->runtime;
  101. struct audio_aio_write_param param;
  102. struct audio_buffer *buf = NULL;
  103. uint32_t *ptrmem = (uint32_t *)payload;
  104. int i = 0;
  105. uint32_t idx = 0;
  106. uint32_t size = 0;
  107. pr_debug("%s\n", __func__);
  108. switch (opcode) {
  109. case ASM_DATA_EVENT_WRITE_DONE: {
  110. uint32_t *ptrmem = (uint32_t *)&param;
  111. pr_debug("ASM_DATA_EVENT_WRITE_DONE\n");
  112. pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
  113. prtd->pcm_irq_pos += prtd->pcm_count;
  114. if (atomic_read(&prtd->start))
  115. snd_pcm_period_elapsed(substream);
  116. atomic_inc(&prtd->out_count);
  117. wake_up(&the_locks.write_wait);
  118. if (!atomic_read(&prtd->start))
  119. break;
  120. if (!prtd->mmap_flag)
  121. break;
  122. buf = prtd->audio_client->port[IN].buf;
  123. pr_debug("%s:writing %d bytes of buffer[%d] to dsp 2\n",
  124. __func__, prtd->pcm_count, prtd->out_head);
  125. pr_debug("%s:writing buffer[%d] from 0x%08x\n",
  126. __func__, prtd->out_head,
  127. ((unsigned int)buf[0].phys
  128. + (prtd->out_head * prtd->pcm_count)));
  129. param.paddr = (unsigned long)buf[0].phys
  130. + (prtd->out_head * prtd->pcm_count);
  131. param.len = prtd->pcm_count;
  132. param.msw_ts = 0;
  133. param.lsw_ts = 0;
  134. param.flags = NO_TIMESTAMP;
  135. param.uid = (unsigned long)buf[0].phys
  136. + (prtd->out_head * prtd->pcm_count);
  137. for (i = 0; i < sizeof(struct audio_aio_write_param)/4;
  138. i++, ++ptrmem)
  139. pr_debug("cmd[%d]=0x%08x\n", i, *ptrmem);
  140. if (q6asm_async_write(prtd->audio_client,
  141. &param) < 0)
  142. pr_err("%s:q6asm_async_write failed\n",
  143. __func__);
  144. else
  145. prtd->out_head =
  146. (prtd->out_head + 1) & (runtime->periods - 1);
  147. break;
  148. }
  149. case ASM_DATA_CMDRSP_EOS:
  150. pr_debug("ASM_DATA_CMDRSP_EOS\n");
  151. prtd->cmd_ack = 1;
  152. wake_up(&the_locks.eos_wait);
  153. break;
  154. case ASM_DATA_EVENT_READ_DONE: {
  155. pr_debug("ASM_DATA_EVENT_READ_DONE\n");
  156. pr_debug("token = 0x%08x\n", token);
  157. for (i = 0; i < 8; i++, ++ptrmem)
  158. pr_debug("cmd[%d]=0x%08x\n", i, *ptrmem);
  159. in_frame_info[token][0] = payload[2];
  160. in_frame_info[token][1] = payload[3];
  161. prtd->pcm_irq_pos += in_frame_info[token][0];
  162. pr_debug("pcm_irq_pos=%d\n", prtd->pcm_irq_pos);
  163. if (atomic_read(&prtd->start))
  164. snd_pcm_period_elapsed(substream);
  165. if (atomic_read(&prtd->in_count) <= prtd->periods)
  166. atomic_inc(&prtd->in_count);
  167. wake_up(&the_locks.read_wait);
  168. if (prtd->mmap_flag
  169. && q6asm_is_cpu_buf_avail_nolock(OUT,
  170. prtd->audio_client,
  171. &size, &idx))
  172. q6asm_read_nolock(prtd->audio_client);
  173. break;
  174. }
  175. case APR_BASIC_RSP_RESULT: {
  176. switch (payload[0]) {
  177. case ASM_SESSION_CMD_RUN:
  178. if (substream->stream
  179. != SNDRV_PCM_STREAM_PLAYBACK) {
  180. atomic_set(&prtd->start, 1);
  181. break;
  182. }
  183. if (prtd->mmap_flag) {
  184. pr_debug("%s:writing %d bytes"\
  185. " of buffer[%d] to dsp\n",
  186. __func__, prtd->pcm_count,
  187. prtd->out_head);
  188. buf = prtd->audio_client->port[IN].buf;
  189. pr_debug("%s:writing buffer[%d] from 0x%08x\n",
  190. __func__, prtd->out_head,
  191. ((unsigned int)buf[0].phys
  192. + (prtd->out_head * prtd->pcm_count)));
  193. param.paddr = (unsigned long)
  194. buf[prtd->out_head].phys;
  195. param.len = prtd->pcm_count;
  196. param.msw_ts = 0;
  197. param.lsw_ts = 0;
  198. param.flags = NO_TIMESTAMP;
  199. param.uid = (unsigned long)
  200. buf[prtd->out_head].phys;
  201. if (q6asm_async_write(prtd->audio_client,
  202. &param) < 0)
  203. pr_err("%s:q6asm_async_write failed\n",
  204. __func__);
  205. else
  206. prtd->out_head =
  207. (prtd->out_head + 1)
  208. & (runtime->periods - 1);
  209. } else {
  210. while (atomic_read(&prtd->out_needed)) {
  211. pr_debug("%s:writing %d bytesi" \
  212. " of buffer to dsp\n", \
  213. __func__, \
  214. prtd->pcm_count);
  215. q6asm_write_nolock(prtd->audio_client,
  216. prtd->pcm_count,
  217. 0, 0, NO_TIMESTAMP);
  218. atomic_dec(&prtd->out_needed);
  219. wake_up(&the_locks.write_wait);
  220. };
  221. }
  222. atomic_set(&prtd->start, 1);
  223. break;
  224. case ASM_STREAM_CMD_FLUSH:
  225. pr_debug("ASM_STREAM_CMD_FLUSH\n");
  226. prtd->cmd_ack = 1;
  227. wake_up(&the_locks.flush_wait);
  228. break;
  229. default:
  230. break;
  231. }
  232. }
  233. break;
  234. default:
  235. pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
  236. break;
  237. }
  238. }
  239. static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
  240. {
  241. struct snd_pcm_runtime *runtime = substream->runtime;
  242. struct msm_audio *prtd = runtime->private_data;
  243. int ret;
  244. pr_debug("%s\n", __func__);
  245. if (prtd->mmap_flag) {
  246. ret = q6asm_set_io_mode(prtd->audio_client, ASYNC_IO_MODE);
  247. if (ret < 0) {
  248. pr_err("%s: Set IO mode failed\n", __func__);
  249. return -ENOMEM;
  250. }
  251. }
  252. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  253. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  254. prtd->pcm_irq_pos = 0;
  255. /* rate and channels are sent to audio driver */
  256. prtd->samp_rate = runtime->rate;
  257. prtd->channel_mode = runtime->channels;
  258. if (prtd->enabled)
  259. return 0;
  260. pr_debug("prtd->set_channel_map: %d", prtd->set_channel_map);
  261. if (!prtd->set_channel_map) {
  262. pr_debug("using default channel map");
  263. memset(prtd->channel_map, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  264. if (prtd->channel_mode == 1) {
  265. prtd->channel_map[0] = PCM_CHANNEL_FL;
  266. } else if (prtd->channel_mode == 2) {
  267. prtd->channel_map[0] = PCM_CHANNEL_FL;
  268. prtd->channel_map[1] = PCM_CHANNEL_FR;
  269. } else if (prtd->channel_mode == 6) {
  270. prtd->channel_map[0] = PCM_CHANNEL_FC;
  271. prtd->channel_map[1] = PCM_CHANNEL_FL;
  272. prtd->channel_map[2] = PCM_CHANNEL_FR;
  273. prtd->channel_map[3] = PCM_CHANNEL_LB;
  274. prtd->channel_map[4] = PCM_CHANNEL_RB;
  275. prtd->channel_map[5] = PCM_CHANNEL_LFE;
  276. } else {
  277. pr_err("%s: ERROR.unsupported num_ch = %u\n", __func__,
  278. prtd->channel_mode);
  279. }
  280. }
  281. ret = q6asm_media_format_block_multi_ch_pcm(prtd->audio_client,
  282. runtime->rate, runtime->channels, prtd->channel_map);
  283. if (ret < 0)
  284. pr_info("%s: CMD Format block failed\n", __func__);
  285. atomic_set(&prtd->out_count, runtime->periods);
  286. prtd->enabled = 1;
  287. prtd->cmd_ack = 0;
  288. return 0;
  289. }
  290. static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
  291. {
  292. struct snd_pcm_runtime *runtime = substream->runtime;
  293. struct msm_audio *prtd = runtime->private_data;
  294. int ret = 0;
  295. int i = 0;
  296. pr_debug("%s\n", __func__);
  297. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  298. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  299. prtd->pcm_irq_pos = 0;
  300. /* rate and channels are sent to audio driver */
  301. prtd->samp_rate = runtime->rate;
  302. prtd->channel_mode = runtime->channels;
  303. if (prtd->enabled)
  304. return 0;
  305. pr_debug("Samp_rate = %d\n", prtd->samp_rate);
  306. pr_debug("Channel = %d\n", prtd->channel_mode);
  307. ret = q6asm_enc_cfg_blk_multi_ch_pcm(prtd->audio_client,
  308. prtd->samp_rate, prtd->channel_mode);
  309. if (ret < 0)
  310. pr_debug("%s: cmd cfg pcm was block failed", __func__);
  311. for (i = 0; i < runtime->periods; i++)
  312. q6asm_read(prtd->audio_client);
  313. prtd->periods = runtime->periods;
  314. prtd->enabled = 1;
  315. return ret;
  316. }
  317. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  318. {
  319. int ret = 0;
  320. struct snd_pcm_runtime *runtime = substream->runtime;
  321. struct msm_audio *prtd = runtime->private_data;
  322. switch (cmd) {
  323. case SNDRV_PCM_TRIGGER_START:
  324. case SNDRV_PCM_TRIGGER_RESUME:
  325. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  326. pr_debug("%s: Trigger start\n", __func__);
  327. q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  328. break;
  329. case SNDRV_PCM_TRIGGER_STOP:
  330. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  331. atomic_set(&prtd->start, 0);
  332. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  333. break;
  334. prtd->cmd_ack = 0;
  335. q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
  336. break;
  337. case SNDRV_PCM_TRIGGER_SUSPEND:
  338. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  339. pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
  340. q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  341. atomic_set(&prtd->start, 0);
  342. break;
  343. default:
  344. ret = -EINVAL;
  345. break;
  346. }
  347. return ret;
  348. }
  349. static int msm_pcm_open(struct snd_pcm_substream *substream)
  350. {
  351. struct snd_pcm_runtime *runtime = substream->runtime;
  352. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  353. struct msm_audio *prtd;
  354. int ret = 0;
  355. struct asm_softpause_params softpause = {
  356. .enable = SOFT_PAUSE_ENABLE,
  357. .period = SOFT_PAUSE_PERIOD,
  358. .step = SOFT_PAUSE_STEP,
  359. .rampingcurve = SOFT_PAUSE_CURVE_LINEAR,
  360. };
  361. struct asm_softvolume_params softvol = {
  362. .period = SOFT_VOLUME_PERIOD,
  363. .step = SOFT_VOLUME_STEP,
  364. .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
  365. };
  366. pr_debug("%s\n", __func__);
  367. prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
  368. if (prtd == NULL) {
  369. pr_err("Failed to allocate memory for msm_audio\n");
  370. return -ENOMEM;
  371. }
  372. prtd->substream = substream;
  373. prtd->audio_client = q6asm_audio_client_alloc(
  374. (app_cb)event_handler, prtd);
  375. if (!prtd->audio_client) {
  376. pr_err("%s: Could not allocate memory\n", __func__);
  377. kfree(prtd);
  378. return -ENOMEM;
  379. }
  380. prtd->audio_client->perf_mode = false;
  381. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  382. runtime->hw = msm_pcm_hardware_playback;
  383. ret = q6asm_open_write(prtd->audio_client,
  384. FORMAT_MULTI_CHANNEL_LINEAR_PCM);
  385. if (ret < 0) {
  386. pr_err("%s: pcm out open failed\n", __func__);
  387. q6asm_audio_client_free(prtd->audio_client);
  388. kfree(prtd);
  389. return -ENOMEM;
  390. }
  391. }
  392. /* Capture path */
  393. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  394. runtime->hw = msm_pcm_hardware_capture;
  395. ret = q6asm_open_read(prtd->audio_client,
  396. FORMAT_MULTI_CHANNEL_LINEAR_PCM);
  397. if (ret < 0) {
  398. pr_err("%s: pcm in open failed\n", __func__);
  399. q6asm_audio_client_free(prtd->audio_client);
  400. kfree(prtd);
  401. return -ENOMEM;
  402. }
  403. }
  404. pr_debug("%s: session ID %d\n", __func__, prtd->audio_client->session);
  405. prtd->session_id = prtd->audio_client->session;
  406. msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->be_id,
  407. prtd->audio_client->perf_mode,
  408. prtd->session_id, substream->stream);
  409. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  410. prtd->cmd_ack = 1;
  411. ret = snd_pcm_hw_constraint_list(runtime, 0,
  412. SNDRV_PCM_HW_PARAM_RATE,
  413. &constraints_sample_rates);
  414. if (ret < 0)
  415. pr_err("snd_pcm_hw_constraint_list failed\n");
  416. /* Ensure that buffer size is a multiple of period size */
  417. ret = snd_pcm_hw_constraint_integer(runtime,
  418. SNDRV_PCM_HW_PARAM_PERIODS);
  419. if (ret < 0)
  420. pr_err("snd_pcm_hw_constraint_integer failed\n");
  421. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  422. ret = snd_pcm_hw_constraint_minmax(runtime,
  423. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  424. PLAYBACK_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
  425. PLAYBACK_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
  426. if (ret < 0) {
  427. pr_err("constraint for buffer bytes min max ret = %d\n",
  428. ret);
  429. }
  430. }
  431. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  432. ret = snd_pcm_hw_constraint_minmax(runtime,
  433. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  434. CAPTURE_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
  435. CAPTURE_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
  436. if (ret < 0) {
  437. pr_err("constraint for buffer bytes min max ret = %d\n",
  438. ret);
  439. }
  440. }
  441. prtd->dsp_cnt = 0;
  442. prtd->set_channel_map = false;
  443. runtime->private_data = prtd;
  444. pr_debug("substream->pcm->device = %d\n", substream->pcm->device);
  445. pr_debug("soc_prtd->dai_link->be_id = %d\n", soc_prtd->dai_link->be_id);
  446. multi_ch_pcm_audio.prtd = prtd;
  447. ret = multi_ch_pcm_set_volume(multi_ch_pcm_audio.volume);
  448. if (ret < 0)
  449. pr_err("%s : Set Volume failed : %d", __func__, ret);
  450. ret = q6asm_set_softpause(multi_ch_pcm_audio.prtd->audio_client,
  451. &softpause);
  452. if (ret < 0)
  453. pr_err("%s: Send SoftPause Param failed ret=%d\n",
  454. __func__, ret);
  455. ret = q6asm_set_softvolume(multi_ch_pcm_audio.prtd->audio_client,
  456. &softvol);
  457. if (ret < 0)
  458. pr_err("%s: Send SoftVolume Param failed ret=%d\n",
  459. __func__, ret);
  460. return 0;
  461. }
  462. int multi_ch_pcm_set_volume(unsigned volume)
  463. {
  464. int rc = 0;
  465. pr_err("multi_ch_pcm_set_volume\n");
  466. if (multi_ch_pcm_audio.prtd && multi_ch_pcm_audio.prtd->audio_client) {
  467. pr_err("%s q6asm_set_volume\n", __func__);
  468. rc = q6asm_set_volume(multi_ch_pcm_audio.prtd->audio_client,
  469. volume);
  470. if (rc < 0) {
  471. pr_err("%s: Send Volume command failed"
  472. " rc=%d\n", __func__, rc);
  473. }
  474. }
  475. multi_ch_pcm_audio.volume = volume;
  476. return rc;
  477. }
  478. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  479. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  480. {
  481. int ret = 0;
  482. int fbytes = 0;
  483. int xfer = 0;
  484. char *bufptr = NULL;
  485. void *data = NULL;
  486. uint32_t idx = 0;
  487. uint32_t size = 0;
  488. struct snd_pcm_runtime *runtime = substream->runtime;
  489. struct msm_audio *prtd = runtime->private_data;
  490. fbytes = frames_to_bytes(runtime, frames);
  491. pr_debug("%s: prtd->out_count = %d\n",
  492. __func__, atomic_read(&prtd->out_count));
  493. ret = wait_event_timeout(the_locks.write_wait,
  494. (atomic_read(&prtd->out_count)), 5 * HZ);
  495. if (!ret) {
  496. pr_err("%s: wait_event_timeout failed\n", __func__);
  497. goto fail;
  498. }
  499. if (!atomic_read(&prtd->out_count)) {
  500. pr_err("%s: pcm stopped out_count 0\n", __func__);
  501. return 0;
  502. }
  503. data = q6asm_is_cpu_buf_avail(IN, prtd->audio_client, &size, &idx);
  504. bufptr = data;
  505. if (bufptr) {
  506. pr_debug("%s:fbytes =%d: xfer=%d size=%d\n",
  507. __func__, fbytes, xfer, size);
  508. xfer = fbytes;
  509. if (copy_from_user(bufptr, buf, xfer)) {
  510. ret = -EFAULT;
  511. goto fail;
  512. }
  513. buf += xfer;
  514. fbytes -= xfer;
  515. pr_debug("%s:fbytes = %d: xfer=%d\n", __func__, fbytes, xfer);
  516. if (atomic_read(&prtd->start)) {
  517. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  518. __func__, xfer);
  519. ret = q6asm_write(prtd->audio_client, xfer,
  520. 0, 0, NO_TIMESTAMP);
  521. if (ret < 0) {
  522. ret = -EFAULT;
  523. goto fail;
  524. }
  525. } else
  526. atomic_inc(&prtd->out_needed);
  527. atomic_dec(&prtd->out_count);
  528. }
  529. fail:
  530. return ret;
  531. }
  532. static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
  533. {
  534. struct snd_pcm_runtime *runtime = substream->runtime;
  535. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  536. struct msm_audio *prtd = runtime->private_data;
  537. int dir = 0;
  538. int ret = 0;
  539. pr_debug("%s\n", __func__);
  540. dir = IN;
  541. ret = wait_event_timeout(the_locks.eos_wait,
  542. prtd->cmd_ack, 5 * HZ);
  543. if (!ret)
  544. pr_err("%s: CMD_EOS failed\n", __func__);
  545. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  546. q6asm_audio_client_buf_free_contiguous(dir,
  547. prtd->audio_client);
  548. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->be_id,
  549. SNDRV_PCM_STREAM_PLAYBACK);
  550. multi_ch_pcm_audio.prtd = NULL;
  551. q6asm_audio_client_free(prtd->audio_client);
  552. kfree(prtd);
  553. return 0;
  554. }
  555. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  556. int channel, snd_pcm_uframes_t hwoff, void __user *buf,
  557. snd_pcm_uframes_t frames)
  558. {
  559. int ret = 0;
  560. int fbytes = 0;
  561. int xfer;
  562. char *bufptr;
  563. void *data = NULL;
  564. static uint32_t idx;
  565. static uint32_t size;
  566. uint32_t offset = 0;
  567. struct snd_pcm_runtime *runtime = substream->runtime;
  568. struct msm_audio *prtd = substream->runtime->private_data;
  569. pr_debug("%s\n", __func__);
  570. fbytes = frames_to_bytes(runtime, frames);
  571. pr_debug("appl_ptr %d\n", (int)runtime->control->appl_ptr);
  572. pr_debug("hw_ptr %d\n", (int)runtime->status->hw_ptr);
  573. pr_debug("avail_min %d\n", (int)runtime->control->avail_min);
  574. ret = wait_event_timeout(the_locks.read_wait,
  575. (atomic_read(&prtd->in_count)), 5 * HZ);
  576. if (!ret) {
  577. pr_debug("%s: wait_event_timeout failed\n", __func__);
  578. goto fail;
  579. }
  580. if (!atomic_read(&prtd->in_count)) {
  581. pr_debug("%s: pcm stopped in_count 0\n", __func__);
  582. return 0;
  583. }
  584. pr_debug("Checking if valid buffer is available...%08x\n",
  585. (unsigned int) data);
  586. data = q6asm_is_cpu_buf_avail(OUT, prtd->audio_client, &size, &idx);
  587. bufptr = data;
  588. pr_debug("Size = %d\n", size);
  589. pr_debug("fbytes = %d\n", fbytes);
  590. pr_debug("idx = %d\n", idx);
  591. if (bufptr) {
  592. xfer = fbytes;
  593. if (xfer > size)
  594. xfer = size;
  595. offset = in_frame_info[idx][1];
  596. pr_debug("Offset value = %d\n", offset);
  597. if (copy_to_user(buf, bufptr+offset, xfer)) {
  598. pr_err("Failed to copy buf to user\n");
  599. ret = -EFAULT;
  600. goto fail;
  601. }
  602. fbytes -= xfer;
  603. size -= xfer;
  604. in_frame_info[idx][1] += xfer;
  605. pr_debug("%s:fbytes = %d: size=%d: xfer=%d\n",
  606. __func__, fbytes, size, xfer);
  607. pr_debug(" Sending next buffer to dsp\n");
  608. memset(&in_frame_info[idx], 0,
  609. sizeof(uint32_t) * 2);
  610. atomic_dec(&prtd->in_count);
  611. ret = q6asm_read(prtd->audio_client);
  612. if (ret < 0) {
  613. pr_err("q6asm read failed\n");
  614. ret = -EFAULT;
  615. goto fail;
  616. }
  617. } else
  618. pr_err("No valid buffer\n");
  619. pr_debug("Returning from capture_copy... %d\n", ret);
  620. fail:
  621. return ret;
  622. }
  623. static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
  624. {
  625. struct snd_pcm_runtime *runtime = substream->runtime;
  626. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  627. struct msm_audio *prtd = runtime->private_data;
  628. int dir = OUT;
  629. pr_debug("%s\n", __func__);
  630. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  631. q6asm_audio_client_buf_free_contiguous(dir,
  632. prtd->audio_client);
  633. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->be_id,
  634. SNDRV_PCM_STREAM_CAPTURE);
  635. q6asm_audio_client_free(prtd->audio_client);
  636. kfree(prtd);
  637. return 0;
  638. }
  639. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  640. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  641. {
  642. int ret = 0;
  643. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  644. ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
  645. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  646. ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
  647. return ret;
  648. }
  649. static int msm_pcm_close(struct snd_pcm_substream *substream)
  650. {
  651. int ret = 0;
  652. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  653. ret = msm_pcm_playback_close(substream);
  654. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  655. ret = msm_pcm_capture_close(substream);
  656. return ret;
  657. }
  658. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  659. {
  660. int ret = 0;
  661. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  662. ret = msm_pcm_playback_prepare(substream);
  663. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  664. ret = msm_pcm_capture_prepare(substream);
  665. return ret;
  666. }
  667. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  668. {
  669. struct snd_pcm_runtime *runtime = substream->runtime;
  670. struct msm_audio *prtd = runtime->private_data;
  671. if (prtd->pcm_irq_pos >= prtd->pcm_size)
  672. prtd->pcm_irq_pos = 0;
  673. pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
  674. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  675. }
  676. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  677. struct vm_area_struct *vma)
  678. {
  679. int result = 0;
  680. struct snd_pcm_runtime *runtime = substream->runtime;
  681. struct msm_audio *prtd = runtime->private_data;
  682. pr_debug("%s\n", __func__);
  683. prtd->mmap_flag = 1;
  684. if (runtime->dma_addr && runtime->dma_bytes) {
  685. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  686. result = remap_pfn_range(vma, vma->vm_start,
  687. runtime->dma_addr >> PAGE_SHIFT,
  688. runtime->dma_bytes,
  689. vma->vm_page_prot);
  690. } else {
  691. pr_err("Physical address or size of buf is NULL");
  692. return -EINVAL;
  693. }
  694. return result;
  695. }
  696. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  697. struct snd_pcm_hw_params *params)
  698. {
  699. struct snd_pcm_runtime *runtime = substream->runtime;
  700. struct msm_audio *prtd = runtime->private_data;
  701. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  702. struct audio_buffer *buf;
  703. int dir, ret;
  704. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  705. dir = IN;
  706. else
  707. dir = OUT;
  708. ret = q6asm_audio_client_buf_alloc_contiguous(dir,
  709. prtd->audio_client,
  710. (params_buffer_bytes(params) / params_periods(params)),
  711. params_periods(params));
  712. if (ret < 0) {
  713. pr_err("Audio Start: Buffer Allocation failed rc = %d\n", ret);
  714. return -ENOMEM;
  715. }
  716. buf = prtd->audio_client->port[dir].buf;
  717. pr_debug("%s:buf = %p\n", __func__, buf);
  718. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  719. dma_buf->dev.dev = substream->pcm->card->dev;
  720. dma_buf->private_data = NULL;
  721. dma_buf->area = buf[0].data;
  722. dma_buf->addr = buf[0].phys;
  723. dma_buf->bytes = params_buffer_bytes(params);
  724. if (!dma_buf->area)
  725. return -ENOMEM;
  726. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  727. return 0;
  728. }
  729. static int msm_pcm_ioctl(struct snd_pcm_substream *substream,
  730. unsigned int cmd, void *arg)
  731. {
  732. struct snd_pcm_runtime *runtime = substream->runtime;
  733. struct msm_audio *prtd = runtime->private_data;
  734. int ret = 0, rc;
  735. pr_debug("%s\n", __func__);
  736. ret = snd_pcm_lib_ioctl(substream, cmd, arg);
  737. if (ret < 0) {
  738. pr_err("%s, snd_pcm_lib_ioctl error\n", __func__);
  739. return ret;
  740. }
  741. switch (cmd) {
  742. case SNDRV_PCM_IOCTL1_RESET:
  743. pr_debug("%s, SNDRV_PCM_IOCTL1_RESET\n", __func__);
  744. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  745. prtd->cmd_ack = 0;
  746. rc = q6asm_cmd(prtd->audio_client, CMD_FLUSH);
  747. if (rc < 0) {
  748. pr_err("%s: flush cmd failed rc=%d\n",
  749. __func__, rc);
  750. break;
  751. }
  752. rc = wait_event_timeout(the_locks.flush_wait,
  753. prtd->cmd_ack, 5 * HZ);
  754. if (rc < 0)
  755. pr_err("Flush cmd timeout\n");
  756. prtd->pcm_irq_pos = 0;
  757. atomic_set(&prtd->out_count, runtime->periods);
  758. }
  759. break;
  760. default:
  761. break;
  762. }
  763. return ret;
  764. }
  765. static struct snd_pcm_ops msm_pcm_ops = {
  766. .open = msm_pcm_open,
  767. .copy = msm_pcm_copy,
  768. .hw_params = msm_pcm_hw_params,
  769. .close = msm_pcm_close,
  770. .ioctl = msm_pcm_ioctl,
  771. .prepare = msm_pcm_prepare,
  772. .trigger = msm_pcm_trigger,
  773. .pointer = msm_pcm_pointer,
  774. .mmap = msm_pcm_mmap,
  775. };
  776. static int pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
  777. struct snd_ctl_elem_value *ucontrol)
  778. {
  779. int i;
  780. char channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL];
  781. pr_debug("%s", __func__);
  782. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  783. channel_mapping[i] = (char)(ucontrol->value.integer.value[i]);
  784. if (multi_ch_pcm_audio.prtd) {
  785. multi_ch_pcm_audio.prtd->set_channel_map = true;
  786. memcpy(multi_ch_pcm_audio.prtd->channel_map, channel_mapping,
  787. PCM_FORMAT_MAX_NUM_CHANNEL);
  788. }
  789. return 0;
  790. }
  791. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  792. {
  793. struct snd_card *card = rtd->card->snd_card;
  794. struct snd_pcm *pcm = rtd->pcm->streams[0].pcm;
  795. struct snd_pcm_chmap *chmap_info;
  796. struct snd_kcontrol *kctl;
  797. char device_num[3];
  798. int i, ret = 0;
  799. if (!card->dev->coherent_dma_mask)
  800. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  801. pr_debug("%s, Channel map cntrl add\n", __func__);
  802. ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  803. NULL, PCM_FORMAT_MAX_NUM_CHANNEL, 0,
  804. &chmap_info);
  805. if (ret < 0)
  806. return ret;
  807. kctl = chmap_info->kctl;
  808. for (i = 0; i < kctl->count; i++)
  809. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  810. snprintf(device_num, sizeof(device_num), "%d", pcm->device);
  811. strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
  812. pr_debug("%s, Overwriting channel map control name to: %s",
  813. __func__, kctl->id.name);
  814. kctl->put = pcm_chmap_ctl_put;
  815. return ret;
  816. }
  817. static struct snd_soc_platform_driver msm_soc_platform = {
  818. .ops = &msm_pcm_ops,
  819. .pcm_new = msm_asoc_pcm_new,
  820. };
  821. static __devinit int msm_pcm_probe(struct platform_device *pdev)
  822. {
  823. pr_info("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  824. return snd_soc_register_platform(&pdev->dev,
  825. &msm_soc_platform);
  826. }
  827. static int msm_pcm_remove(struct platform_device *pdev)
  828. {
  829. snd_soc_unregister_platform(&pdev->dev);
  830. return 0;
  831. }
  832. static struct platform_driver msm_pcm_driver = {
  833. .driver = {
  834. .name = "msm-multi-ch-pcm-dsp",
  835. .owner = THIS_MODULE,
  836. },
  837. .probe = msm_pcm_probe,
  838. .remove = __devexit_p(msm_pcm_remove),
  839. };
  840. static int __init msm_soc_platform_init(void)
  841. {
  842. init_waitqueue_head(&the_locks.enable_wait);
  843. init_waitqueue_head(&the_locks.eos_wait);
  844. init_waitqueue_head(&the_locks.write_wait);
  845. init_waitqueue_head(&the_locks.read_wait);
  846. init_waitqueue_head(&the_locks.flush_wait);
  847. return platform_driver_register(&msm_pcm_driver);
  848. }
  849. module_init(msm_soc_platform_init);
  850. static void __exit msm_soc_platform_exit(void)
  851. {
  852. platform_driver_unregister(&msm_pcm_driver);
  853. }
  854. module_exit(msm_soc_platform_exit);
  855. MODULE_DESCRIPTION("Multi channel PCM module platform driver");
  856. MODULE_LICENSE("GPL v2");