msm-pcm-afe-v2.c 24 KB

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