msm-pcm-afe-v2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/time.h>
  17. #include <linux/wait.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/msm_audio_ion.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/q6adm-v2.h>
  29. #include <asm/dma.h>
  30. #include <linux/memory_alloc.h>
  31. #include "msm-pcm-afe-v2.h"
  32. #define MIN_PLAYBACK_PERIOD_SIZE (128 * 2)
  33. #define MAX_PLAYBACK_PERIOD_SIZE (128 * 2 * 2 * 6)
  34. #define MIN_PLAYBACK_NUM_PERIODS (4)
  35. #define MAX_PLAYBACK_NUM_PERIODS (768)
  36. #define MIN_CAPTURE_PERIOD_SIZE (128 * 2 * 4)
  37. #define MAX_CAPTURE_PERIOD_SIZE (128 * 2 * 2 * 6 * 4)
  38. #define MIN_CAPTURE_NUM_PERIODS (4)
  39. #define MAX_CAPTURE_NUM_PERIODS (384)
  40. static struct snd_pcm_hardware msm_afe_hardware_playback = {
  41. .info = (SNDRV_PCM_INFO_MMAP |
  42. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  43. SNDRV_PCM_INFO_MMAP_VALID |
  44. SNDRV_PCM_INFO_INTERLEAVED),
  45. .formats = SNDRV_PCM_FMTBIT_S16_LE|
  46. SNDRV_PCM_FMTBIT_S24_LE,
  47. .rates = (SNDRV_PCM_RATE_8000 |
  48. SNDRV_PCM_RATE_16000 |
  49. SNDRV_PCM_RATE_48000),
  50. .rate_min = 8000,
  51. .rate_max = 48000,
  52. .channels_min = 1,
  53. .channels_max = 6,
  54. .buffer_bytes_max = MAX_PLAYBACK_PERIOD_SIZE *
  55. MAX_PLAYBACK_NUM_PERIODS,
  56. .period_bytes_min = MIN_PLAYBACK_PERIOD_SIZE,
  57. .period_bytes_max = MAX_PLAYBACK_PERIOD_SIZE,
  58. .periods_min = MIN_PLAYBACK_NUM_PERIODS,
  59. .periods_max = MAX_PLAYBACK_NUM_PERIODS,
  60. .fifo_size = 0,
  61. };
  62. static struct snd_pcm_hardware msm_afe_hardware_capture = {
  63. .info = (SNDRV_PCM_INFO_MMAP |
  64. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  65. SNDRV_PCM_INFO_MMAP_VALID |
  66. SNDRV_PCM_INFO_INTERLEAVED),
  67. .formats = SNDRV_PCM_FMTBIT_S16_LE|
  68. SNDRV_PCM_FMTBIT_S24_LE,
  69. .rates = (SNDRV_PCM_RATE_8000 |
  70. SNDRV_PCM_RATE_16000 |
  71. SNDRV_PCM_RATE_48000),
  72. .rate_min = 8000,
  73. .rate_max = 48000,
  74. .channels_min = 1,
  75. .channels_max = 6,
  76. .buffer_bytes_max = MAX_CAPTURE_PERIOD_SIZE *
  77. MAX_CAPTURE_NUM_PERIODS,
  78. .period_bytes_min = MIN_CAPTURE_PERIOD_SIZE,
  79. .period_bytes_max = MAX_CAPTURE_PERIOD_SIZE,
  80. .periods_min = MIN_CAPTURE_NUM_PERIODS,
  81. .periods_max = MAX_CAPTURE_NUM_PERIODS,
  82. .fifo_size = 0,
  83. };
  84. static enum hrtimer_restart afe_hrtimer_callback(struct hrtimer *hrt);
  85. static enum hrtimer_restart afe_hrtimer_rec_callback(struct hrtimer *hrt);
  86. static enum hrtimer_restart afe_hrtimer_callback(struct hrtimer *hrt)
  87. {
  88. struct pcm_afe_info *prtd =
  89. container_of(hrt, struct pcm_afe_info, hrt);
  90. struct snd_pcm_substream *substream = prtd->substream;
  91. struct snd_pcm_runtime *runtime = substream->runtime;
  92. u32 mem_map_handle = 0;
  93. mem_map_handle = afe_req_mmap_handle(prtd->audio_client);
  94. if (!mem_map_handle)
  95. pr_err("%s: mem_map_handle is NULL\n", __func__);
  96. if (prtd->start) {
  97. pr_debug("sending frame to DSP: poll_time: %d\n",
  98. prtd->poll_time);
  99. if (prtd->dsp_cnt == runtime->periods)
  100. prtd->dsp_cnt = 0;
  101. pr_debug("%s: mem_map_handle 0x%x\n", __func__, mem_map_handle);
  102. afe_rt_proxy_port_write(
  103. (prtd->dma_addr +
  104. (prtd->dsp_cnt *
  105. snd_pcm_lib_period_bytes(prtd->substream))), mem_map_handle,
  106. snd_pcm_lib_period_bytes(prtd->substream));
  107. prtd->dsp_cnt++;
  108. hrtimer_forward_now(hrt, ns_to_ktime(prtd->poll_time
  109. * 1000));
  110. return HRTIMER_RESTART;
  111. } else
  112. return HRTIMER_NORESTART;
  113. }
  114. static enum hrtimer_restart afe_hrtimer_rec_callback(struct hrtimer *hrt)
  115. {
  116. struct pcm_afe_info *prtd =
  117. container_of(hrt, struct pcm_afe_info, hrt);
  118. struct snd_pcm_substream *substream = prtd->substream;
  119. struct snd_pcm_runtime *runtime = substream->runtime;
  120. u32 mem_map_handle = 0;
  121. mem_map_handle = afe_req_mmap_handle(prtd->audio_client);
  122. if (!mem_map_handle)
  123. pr_err("%s: mem_map_handle is NULL\n", __func__);
  124. if (prtd->start) {
  125. if (prtd->dsp_cnt == runtime->periods)
  126. prtd->dsp_cnt = 0;
  127. pr_debug("%s: mem_map_handle 0x%x\n", __func__, mem_map_handle);
  128. afe_rt_proxy_port_read(
  129. (prtd->dma_addr + (prtd->dsp_cnt
  130. * snd_pcm_lib_period_bytes(prtd->substream))), mem_map_handle,
  131. snd_pcm_lib_period_bytes(prtd->substream));
  132. prtd->dsp_cnt++;
  133. pr_debug("sending frame rec to DSP: poll_time: %d\n",
  134. prtd->poll_time);
  135. hrtimer_forward_now(hrt, ns_to_ktime(prtd->poll_time
  136. * 1000));
  137. return HRTIMER_RESTART;
  138. } else
  139. return HRTIMER_NORESTART;
  140. }
  141. static void pcm_afe_process_tx_pkt(uint32_t opcode,
  142. uint32_t token, uint32_t *payload,
  143. void *priv)
  144. {
  145. struct pcm_afe_info *prtd = priv;
  146. unsigned long dsp_flags;
  147. struct snd_pcm_substream *substream = NULL;
  148. struct snd_pcm_runtime *runtime = NULL;
  149. uint16_t event;
  150. uint64_t period_bytes;
  151. uint64_t bytes_one_sec;
  152. if (prtd == NULL)
  153. return;
  154. substream = prtd->substream;
  155. runtime = substream->runtime;
  156. pr_debug("%s\n", __func__);
  157. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  158. switch (opcode) {
  159. case AFE_EVENT_RT_PROXY_PORT_STATUS: {
  160. event = (uint16_t)((0xFFFF0000 & payload[0]) >> 0x10);
  161. switch (event) {
  162. case AFE_EVENT_RTPORT_START: {
  163. prtd->dsp_cnt = 0;
  164. /* Calculate poll time.
  165. * Split steps to avoid overflow.
  166. * Poll time-time corresponding to one period
  167. * in bytes.
  168. * (Samplerate * channelcount * format) =
  169. * bytes in 1 sec.
  170. * Poll time =
  171. * (period bytes / bytes in one sec) *
  172. * 1000000 micro seconds.
  173. * Multiplication by 1000000 is done in two
  174. * steps to keep the accuracy of poll time.
  175. */
  176. period_bytes = ((uint64_t)(
  177. (snd_pcm_lib_period_bytes(
  178. prtd->substream)) *
  179. 1000));
  180. bytes_one_sec =
  181. (runtime->rate * runtime->channels * 2);
  182. bytes_one_sec =
  183. div_u64(bytes_one_sec, 1000);
  184. prtd->poll_time =
  185. div_u64(period_bytes, bytes_one_sec);
  186. pr_debug("prtd->poll_time: %d",
  187. prtd->poll_time);
  188. break;
  189. }
  190. case AFE_EVENT_RTPORT_STOP:
  191. pr_debug("%s: event!=0\n", __func__);
  192. prtd->start = 0;
  193. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  194. break;
  195. case AFE_EVENT_RTPORT_LOW_WM:
  196. pr_debug("%s: Underrun\n", __func__);
  197. break;
  198. case AFE_EVENT_RTPORT_HI_WM:
  199. pr_debug("%s: Overrun\n", __func__);
  200. break;
  201. default:
  202. break;
  203. }
  204. break;
  205. }
  206. case APR_BASIC_RSP_RESULT: {
  207. switch (payload[0]) {
  208. case AFE_PORT_DATA_CMD_RT_PROXY_PORT_WRITE_V2:
  209. pr_debug("write done\n");
  210. prtd->pcm_irq_pos += snd_pcm_lib_period_bytes
  211. (prtd->substream);
  212. snd_pcm_period_elapsed(prtd->substream);
  213. break;
  214. default:
  215. break;
  216. }
  217. break;
  218. }
  219. default:
  220. break;
  221. }
  222. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  223. }
  224. static void pcm_afe_process_rx_pkt(uint32_t opcode,
  225. uint32_t token, uint32_t *payload,
  226. void *priv)
  227. {
  228. struct pcm_afe_info *prtd = priv;
  229. unsigned long dsp_flags;
  230. struct snd_pcm_substream *substream = NULL;
  231. struct snd_pcm_runtime *runtime = NULL;
  232. uint16_t event;
  233. uint64_t period_bytes;
  234. uint64_t bytes_one_sec;
  235. if (prtd == NULL)
  236. return;
  237. substream = prtd->substream;
  238. runtime = substream->runtime;
  239. pr_debug("%s\n", __func__);
  240. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  241. switch (opcode) {
  242. case AFE_EVENT_RT_PROXY_PORT_STATUS: {
  243. event = (uint16_t)((0xFFFF0000 & payload[0]) >> 0x10);
  244. switch (event) {
  245. case AFE_EVENT_RTPORT_START: {
  246. prtd->dsp_cnt = 0;
  247. /* Calculate poll time. Split steps to avoid overflow.
  248. * Poll time-time corresponding to one period in bytes.
  249. * (Samplerate * channelcount * format)=bytes in 1 sec.
  250. * Poll time = (period bytes / bytes in one sec) *
  251. * 1000000 micro seconds.
  252. * Multiplication by 1000000 is done in two steps to
  253. * keep the accuracy of poll time.
  254. */
  255. period_bytes = ((uint64_t)(
  256. (snd_pcm_lib_period_bytes(prtd->substream)) *
  257. 1000));
  258. bytes_one_sec = (runtime->rate * runtime->channels * 2);
  259. bytes_one_sec = div_u64(bytes_one_sec , 1000);
  260. prtd->poll_time =
  261. div_u64(period_bytes, bytes_one_sec);
  262. pr_debug("prtd->poll_time : %d\n", prtd->poll_time);
  263. break;
  264. }
  265. case AFE_EVENT_RTPORT_STOP:
  266. pr_debug("%s: event!=0\n", __func__);
  267. prtd->start = 0;
  268. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  269. break;
  270. case AFE_EVENT_RTPORT_LOW_WM:
  271. pr_debug("%s: Underrun\n", __func__);
  272. break;
  273. case AFE_EVENT_RTPORT_HI_WM:
  274. pr_debug("%s: Overrun\n", __func__);
  275. break;
  276. default:
  277. break;
  278. }
  279. break;
  280. }
  281. case APR_BASIC_RSP_RESULT: {
  282. switch (payload[0]) {
  283. case AFE_PORT_DATA_CMD_RT_PROXY_PORT_READ_V2:
  284. pr_debug("Read done\n");
  285. prtd->pcm_irq_pos += snd_pcm_lib_period_bytes
  286. (prtd->substream);
  287. snd_pcm_period_elapsed(prtd->substream);
  288. break;
  289. default:
  290. break;
  291. }
  292. break;
  293. }
  294. default:
  295. break;
  296. }
  297. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  298. }
  299. static int msm_afe_playback_prepare(struct snd_pcm_substream *substream)
  300. {
  301. struct snd_pcm_runtime *runtime = substream->runtime;
  302. struct pcm_afe_info *prtd = runtime->private_data;
  303. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  304. struct snd_soc_dai *dai = rtd->cpu_dai;
  305. int ret = 0;
  306. pr_debug("%s: sample_rate=%d\n", __func__, runtime->rate);
  307. pr_debug("%s: dai->id =%x\n", __func__, dai->id);
  308. ret = afe_register_get_events(dai->id,
  309. pcm_afe_process_tx_pkt, prtd);
  310. if (ret < 0) {
  311. pr_err("afe-pcm:register for events failed\n");
  312. return ret;
  313. }
  314. pr_debug("%s:success\n", __func__);
  315. prtd->prepared++;
  316. return ret;
  317. }
  318. static int msm_afe_capture_prepare(struct snd_pcm_substream *substream)
  319. {
  320. struct snd_pcm_runtime *runtime = substream->runtime;
  321. struct pcm_afe_info *prtd = runtime->private_data;
  322. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  323. struct snd_soc_dai *dai = rtd->cpu_dai;
  324. int ret = 0;
  325. pr_debug("%s\n", __func__);
  326. pr_debug("%s: dai->id =%x\n", __func__, dai->id);
  327. ret = afe_register_get_events(dai->id,
  328. pcm_afe_process_rx_pkt, prtd);
  329. if (ret < 0) {
  330. pr_err("afe-pcm:register for events failed\n");
  331. return ret;
  332. }
  333. pr_debug("%s:success\n", __func__);
  334. prtd->prepared++;
  335. return 0;
  336. }
  337. /* Conventional and unconventional sample rate supported */
  338. static unsigned int supported_sample_rates[] = {
  339. 8000, 16000, 48000
  340. };
  341. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  342. .count = ARRAY_SIZE(supported_sample_rates),
  343. .list = supported_sample_rates,
  344. .mask = 0,
  345. };
  346. static int msm_afe_open(struct snd_pcm_substream *substream)
  347. {
  348. struct snd_pcm_runtime *runtime = substream->runtime;
  349. struct pcm_afe_info *prtd = NULL;
  350. int ret = 0;
  351. prtd = kzalloc(sizeof(struct pcm_afe_info), GFP_KERNEL);
  352. if (prtd == NULL) {
  353. pr_err("Failed to allocate memory for msm_audio\n");
  354. return -ENOMEM;
  355. } else
  356. pr_debug("prtd %x\n", (unsigned int)prtd);
  357. mutex_init(&prtd->lock);
  358. spin_lock_init(&prtd->dsp_lock);
  359. prtd->dsp_cnt = 0;
  360. mutex_lock(&prtd->lock);
  361. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  362. runtime->hw = msm_afe_hardware_playback;
  363. else
  364. runtime->hw = msm_afe_hardware_capture;
  365. prtd->substream = substream;
  366. runtime->private_data = prtd;
  367. prtd->audio_client = q6afe_audio_client_alloc(prtd);
  368. if (!prtd->audio_client) {
  369. pr_debug("%s: Could not allocate memory\n", __func__);
  370. mutex_unlock(&prtd->lock);
  371. kfree(prtd);
  372. return -ENOMEM;
  373. }
  374. hrtimer_init(&prtd->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  375. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  376. prtd->hrt.function = afe_hrtimer_callback;
  377. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  378. prtd->hrt.function = afe_hrtimer_rec_callback;
  379. mutex_unlock(&prtd->lock);
  380. ret = snd_pcm_hw_constraint_list(runtime, 0,
  381. SNDRV_PCM_HW_PARAM_RATE,
  382. &constraints_sample_rates);
  383. if (ret < 0)
  384. pr_err("snd_pcm_hw_constraint_list failed\n");
  385. /* Ensure that buffer size is a multiple of period size */
  386. ret = snd_pcm_hw_constraint_integer(runtime,
  387. SNDRV_PCM_HW_PARAM_PERIODS);
  388. if (ret < 0)
  389. pr_err("snd_pcm_hw_constraint_integer failed\n");
  390. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  391. ret = snd_pcm_hw_constraint_minmax(runtime,
  392. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  393. MIN_CAPTURE_NUM_PERIODS * MIN_CAPTURE_PERIOD_SIZE,
  394. MAX_CAPTURE_NUM_PERIODS * MAX_CAPTURE_PERIOD_SIZE);
  395. if (ret < 0) {
  396. pr_err("constraint for buffer bytes min max ret = %d\n",
  397. ret);
  398. }
  399. }
  400. return 0;
  401. }
  402. static int msm_afe_close(struct snd_pcm_substream *substream)
  403. {
  404. int rc = 0;
  405. struct snd_dma_buffer *dma_buf;
  406. struct snd_pcm_runtime *runtime;
  407. struct pcm_afe_info *prtd;
  408. struct snd_soc_pcm_runtime *rtd = NULL;
  409. struct snd_soc_dai *dai = NULL;
  410. int dir = IN;
  411. int ret = 0;
  412. pr_debug("%s\n", __func__);
  413. if (substream == NULL) {
  414. pr_err("substream is NULL\n");
  415. return -EINVAL;
  416. }
  417. rtd = substream->private_data;
  418. dai = rtd->cpu_dai;
  419. runtime = substream->runtime;
  420. prtd = runtime->private_data;
  421. mutex_lock(&prtd->lock);
  422. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  423. dir = IN;
  424. ret = afe_unregister_get_events(dai->id);
  425. if (ret < 0)
  426. pr_err("AFE unregister for events failed\n");
  427. } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  428. dir = OUT;
  429. ret = afe_unregister_get_events(dai->id);
  430. if (ret < 0)
  431. pr_err("AFE unregister for events failed\n");
  432. }
  433. hrtimer_cancel(&prtd->hrt);
  434. rc = afe_cmd_memory_unmap(afe_req_mmap_handle(prtd->audio_client));
  435. if (rc < 0)
  436. pr_err("AFE memory unmap failed\n");
  437. pr_debug("release all buffer\n");
  438. dma_buf = &substream->dma_buffer;
  439. if (dma_buf == NULL) {
  440. pr_debug("dma_buf is NULL\n");
  441. goto done;
  442. }
  443. if (dma_buf->area)
  444. dma_buf->area = NULL;
  445. q6afe_audio_client_buf_free_contiguous(dir, prtd->audio_client);
  446. done:
  447. pr_debug("%s: dai->id =%x\n", __func__, dai->id);
  448. q6afe_audio_client_free(prtd->audio_client);
  449. mutex_unlock(&prtd->lock);
  450. prtd->prepared--;
  451. kfree(prtd);
  452. runtime->private_data = NULL;
  453. return 0;
  454. }
  455. static int msm_afe_prepare(struct snd_pcm_substream *substream)
  456. {
  457. int ret = 0;
  458. struct snd_pcm_runtime *runtime = substream->runtime;
  459. struct pcm_afe_info *prtd = runtime->private_data;
  460. prtd->pcm_irq_pos = 0;
  461. if (prtd->prepared)
  462. return 0;
  463. mutex_lock(&prtd->lock);
  464. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  465. ret = msm_afe_playback_prepare(substream);
  466. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  467. ret = msm_afe_capture_prepare(substream);
  468. mutex_unlock(&prtd->lock);
  469. return ret;
  470. }
  471. static int msm_afe_mmap(struct snd_pcm_substream *substream,
  472. struct vm_area_struct *vma)
  473. {
  474. struct snd_pcm_runtime *runtime = substream->runtime;
  475. struct pcm_afe_info *prtd = runtime->private_data;
  476. struct afe_audio_client *ac = prtd->audio_client;
  477. struct afe_audio_port_data *apd = ac->port;
  478. struct afe_audio_buffer *ab;
  479. int dir = -1;
  480. pr_debug("%s\n", __func__);
  481. prtd->mmap_flag = 1;
  482. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  483. dir = IN;
  484. else
  485. dir = OUT;
  486. ab = &(apd[dir].buf[0]);
  487. return msm_audio_ion_mmap((struct audio_buffer *)ab, vma);
  488. }
  489. static int msm_afe_trigger(struct snd_pcm_substream *substream, int cmd)
  490. {
  491. int ret = 0;
  492. struct snd_pcm_runtime *runtime = substream->runtime;
  493. struct pcm_afe_info *prtd = runtime->private_data;
  494. switch (cmd) {
  495. case SNDRV_PCM_TRIGGER_START:
  496. case SNDRV_PCM_TRIGGER_RESUME:
  497. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  498. pr_debug("%s: SNDRV_PCM_TRIGGER_START\n", __func__);
  499. prtd->start = 1;
  500. hrtimer_start(&prtd->hrt, ns_to_ktime(0),
  501. HRTIMER_MODE_REL);
  502. break;
  503. case SNDRV_PCM_TRIGGER_STOP:
  504. case SNDRV_PCM_TRIGGER_SUSPEND:
  505. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  506. pr_debug("%s: SNDRV_PCM_TRIGGER_STOP\n", __func__);
  507. prtd->start = 0;
  508. break;
  509. default:
  510. ret = -EINVAL;
  511. break;
  512. }
  513. return ret;
  514. }
  515. static int msm_afe_hw_params(struct snd_pcm_substream *substream,
  516. struct snd_pcm_hw_params *params)
  517. {
  518. struct snd_pcm_runtime *runtime = substream->runtime;
  519. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  520. struct pcm_afe_info *prtd = runtime->private_data;
  521. struct afe_audio_buffer *buf;
  522. int dir, rc;
  523. pr_debug("%s:\n", __func__);
  524. mutex_lock(&prtd->lock);
  525. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  526. dir = IN;
  527. else
  528. dir = OUT;
  529. rc = q6afe_audio_client_buf_alloc_contiguous(dir,
  530. prtd->audio_client,
  531. (params_buffer_bytes(params) / params_periods(params)),
  532. params_periods(params));
  533. pr_debug("params_buffer_bytes(params) = %d\n",
  534. (params_buffer_bytes(params)));
  535. pr_debug("params_periods(params) = %d\n",
  536. (params_periods(params)));
  537. pr_debug("params_periodsize(params) = %d\n",
  538. (params_buffer_bytes(params) / params_periods(params)));
  539. if (rc < 0) {
  540. pr_err("Audio Start: Buffer Allocation failed rc = %d\n", rc);
  541. mutex_unlock(&prtd->lock);
  542. return -ENOMEM;
  543. }
  544. buf = prtd->audio_client->port[dir].buf;
  545. if (buf == NULL || buf[0].data == NULL) {
  546. mutex_unlock(&prtd->lock);
  547. return -ENOMEM;
  548. }
  549. pr_debug("%s:buf = %p\n", __func__, buf);
  550. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  551. dma_buf->dev.dev = substream->pcm->card->dev;
  552. dma_buf->private_data = NULL;
  553. dma_buf->area = buf[0].data;
  554. dma_buf->addr = buf[0].phys;
  555. dma_buf->bytes = params_buffer_bytes(params);
  556. if (!dma_buf->area) {
  557. pr_err("%s:MSM AFE physical memory allocation failed\n",
  558. __func__);
  559. mutex_unlock(&prtd->lock);
  560. return -ENOMEM;
  561. }
  562. memset(dma_buf->area, 0, params_buffer_bytes(params));
  563. prtd->dma_addr = (u32) dma_buf->addr;
  564. mutex_unlock(&prtd->lock);
  565. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  566. rc = afe_memory_map(dma_buf->addr, dma_buf->bytes, prtd->audio_client);
  567. if (rc < 0)
  568. pr_err("fail to map memory to DSP\n");
  569. return rc;
  570. }
  571. static snd_pcm_uframes_t msm_afe_pointer(struct snd_pcm_substream *substream)
  572. {
  573. struct snd_pcm_runtime *runtime = substream->runtime;
  574. struct pcm_afe_info *prtd = runtime->private_data;
  575. if (prtd->pcm_irq_pos >= snd_pcm_lib_buffer_bytes(substream))
  576. prtd->pcm_irq_pos = 0;
  577. pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
  578. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  579. }
  580. static struct snd_pcm_ops msm_afe_ops = {
  581. .open = msm_afe_open,
  582. .hw_params = msm_afe_hw_params,
  583. .trigger = msm_afe_trigger,
  584. .close = msm_afe_close,
  585. .prepare = msm_afe_prepare,
  586. .mmap = msm_afe_mmap,
  587. .pointer = msm_afe_pointer,
  588. };
  589. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  590. {
  591. struct snd_card *card = rtd->card->snd_card;
  592. int ret = 0;
  593. pr_debug("%s\n", __func__);
  594. if (!card->dev->coherent_dma_mask)
  595. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  596. return ret;
  597. }
  598. static int msm_afe_afe_probe(struct snd_soc_platform *platform)
  599. {
  600. pr_debug("%s\n", __func__);
  601. return 0;
  602. }
  603. static struct snd_soc_platform_driver msm_soc_platform = {
  604. .ops = &msm_afe_ops,
  605. .pcm_new = msm_asoc_pcm_new,
  606. .probe = msm_afe_afe_probe,
  607. };
  608. static __devinit int msm_afe_probe(struct platform_device *pdev)
  609. {
  610. if (pdev->dev.of_node)
  611. dev_set_name(&pdev->dev, "%s", "msm-pcm-afe");
  612. pr_debug("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  613. return snd_soc_register_platform(&pdev->dev,
  614. &msm_soc_platform);
  615. }
  616. static int msm_afe_remove(struct platform_device *pdev)
  617. {
  618. pr_debug("%s\n", __func__);
  619. snd_soc_unregister_platform(&pdev->dev);
  620. return 0;
  621. }
  622. static const struct of_device_id msm_pcm_afe_dt_match[] = {
  623. {.compatible = "qcom,msm-pcm-afe"},
  624. {}
  625. };
  626. MODULE_DEVICE_TABLE(of, msm_pcm_afe_dt_match);
  627. static struct platform_driver msm_afe_driver = {
  628. .driver = {
  629. .name = "msm-pcm-afe",
  630. .owner = THIS_MODULE,
  631. .of_match_table = msm_pcm_afe_dt_match,
  632. },
  633. .probe = msm_afe_probe,
  634. .remove = __devexit_p(msm_afe_remove),
  635. };
  636. static int __init msm_soc_platform_init(void)
  637. {
  638. pr_debug("%s\n", __func__);
  639. return platform_driver_register(&msm_afe_driver);
  640. }
  641. module_init(msm_soc_platform_init);
  642. static void __exit msm_soc_platform_exit(void)
  643. {
  644. pr_debug("%s\n", __func__);
  645. platform_driver_unregister(&msm_afe_driver);
  646. }
  647. module_exit(msm_soc_platform_exit);
  648. MODULE_DESCRIPTION("AFE PCM module platform driver");
  649. MODULE_LICENSE("GPL v2");