msm-pcm-afe-v2.c 25 KB

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