fsl_asrc_dma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Freescale ASRC ALSA SoC Platform (DMA) driver
  4. //
  5. // Copyright (C) 2014 Freescale Semiconductor, Inc.
  6. //
  7. // Author: Nicolin Chen <nicoleotsuka@gmail.com>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_data/dma-imx.h>
  11. #include <sound/dmaengine_pcm.h>
  12. #include <sound/pcm_params.h>
  13. #include "fsl_asrc_common.h"
  14. #define FSL_ASRC_DMABUF_SIZE (256 * 1024)
  15. static struct snd_pcm_hardware snd_imx_hardware = {
  16. .info = SNDRV_PCM_INFO_INTERLEAVED |
  17. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  18. SNDRV_PCM_INFO_MMAP |
  19. SNDRV_PCM_INFO_MMAP_VALID,
  20. .buffer_bytes_max = FSL_ASRC_DMABUF_SIZE,
  21. .period_bytes_min = 128,
  22. .period_bytes_max = 65535, /* Limited by SDMA engine */
  23. .periods_min = 2,
  24. .periods_max = 255,
  25. .fifo_size = 0,
  26. };
  27. static bool filter(struct dma_chan *chan, void *param)
  28. {
  29. if (!imx_dma_is_general_purpose(chan))
  30. return false;
  31. chan->private = param;
  32. return true;
  33. }
  34. static void fsl_asrc_dma_complete(void *arg)
  35. {
  36. struct snd_pcm_substream *substream = arg;
  37. struct snd_pcm_runtime *runtime = substream->runtime;
  38. struct fsl_asrc_pair *pair = runtime->private_data;
  39. pair->pos += snd_pcm_lib_period_bytes(substream);
  40. if (pair->pos >= snd_pcm_lib_buffer_bytes(substream))
  41. pair->pos = 0;
  42. snd_pcm_period_elapsed(substream);
  43. }
  44. static int fsl_asrc_dma_prepare_and_submit(struct snd_pcm_substream *substream,
  45. struct snd_soc_component *component)
  46. {
  47. u8 dir = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? OUT : IN;
  48. struct snd_pcm_runtime *runtime = substream->runtime;
  49. struct fsl_asrc_pair *pair = runtime->private_data;
  50. struct device *dev = component->dev;
  51. unsigned long flags = DMA_CTRL_ACK;
  52. /* Prepare and submit Front-End DMA channel */
  53. if (!substream->runtime->no_period_wakeup)
  54. flags |= DMA_PREP_INTERRUPT;
  55. pair->pos = 0;
  56. pair->desc[!dir] = dmaengine_prep_dma_cyclic(
  57. pair->dma_chan[!dir], runtime->dma_addr,
  58. snd_pcm_lib_buffer_bytes(substream),
  59. snd_pcm_lib_period_bytes(substream),
  60. dir == OUT ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM, flags);
  61. if (!pair->desc[!dir]) {
  62. dev_err(dev, "failed to prepare slave DMA for Front-End\n");
  63. return -ENOMEM;
  64. }
  65. pair->desc[!dir]->callback = fsl_asrc_dma_complete;
  66. pair->desc[!dir]->callback_param = substream;
  67. dmaengine_submit(pair->desc[!dir]);
  68. /* Prepare and submit Back-End DMA channel */
  69. pair->desc[dir] = dmaengine_prep_dma_cyclic(
  70. pair->dma_chan[dir], 0xffff, 64, 64, DMA_DEV_TO_DEV, 0);
  71. if (!pair->desc[dir]) {
  72. dev_err(dev, "failed to prepare slave DMA for Back-End\n");
  73. return -ENOMEM;
  74. }
  75. dmaengine_submit(pair->desc[dir]);
  76. return 0;
  77. }
  78. static int fsl_asrc_dma_trigger(struct snd_soc_component *component,
  79. struct snd_pcm_substream *substream, int cmd)
  80. {
  81. struct snd_pcm_runtime *runtime = substream->runtime;
  82. struct fsl_asrc_pair *pair = runtime->private_data;
  83. int ret;
  84. switch (cmd) {
  85. case SNDRV_PCM_TRIGGER_START:
  86. case SNDRV_PCM_TRIGGER_RESUME:
  87. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  88. ret = fsl_asrc_dma_prepare_and_submit(substream, component);
  89. if (ret)
  90. return ret;
  91. dma_async_issue_pending(pair->dma_chan[IN]);
  92. dma_async_issue_pending(pair->dma_chan[OUT]);
  93. break;
  94. case SNDRV_PCM_TRIGGER_STOP:
  95. case SNDRV_PCM_TRIGGER_SUSPEND:
  96. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  97. dmaengine_terminate_all(pair->dma_chan[OUT]);
  98. dmaengine_terminate_all(pair->dma_chan[IN]);
  99. break;
  100. default:
  101. return -EINVAL;
  102. }
  103. return 0;
  104. }
  105. static int fsl_asrc_dma_hw_params(struct snd_soc_component *component,
  106. struct snd_pcm_substream *substream,
  107. struct snd_pcm_hw_params *params)
  108. {
  109. enum dma_slave_buswidth buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  110. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  111. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  112. struct snd_dmaengine_dai_dma_data *dma_params_fe = NULL;
  113. struct snd_dmaengine_dai_dma_data *dma_params_be = NULL;
  114. struct snd_pcm_runtime *runtime = substream->runtime;
  115. struct fsl_asrc_pair *pair = runtime->private_data;
  116. struct dma_chan *tmp_chan = NULL, *be_chan = NULL;
  117. struct snd_soc_component *component_be = NULL;
  118. struct fsl_asrc *asrc = pair->asrc;
  119. struct dma_slave_config config_fe, config_be;
  120. enum asrc_pair_index index = pair->index;
  121. struct device *dev = component->dev;
  122. int stream = substream->stream;
  123. struct imx_dma_data *tmp_data;
  124. struct snd_soc_dpcm *dpcm;
  125. struct device *dev_be;
  126. u8 dir = tx ? OUT : IN;
  127. dma_cap_mask_t mask;
  128. int ret, width;
  129. /* Fetch the Back-End dma_data from DPCM */
  130. for_each_dpcm_be(rtd, stream, dpcm) {
  131. struct snd_soc_pcm_runtime *be = dpcm->be;
  132. struct snd_pcm_substream *substream_be;
  133. struct snd_soc_dai *dai = asoc_rtd_to_cpu(be, 0);
  134. if (dpcm->fe != rtd)
  135. continue;
  136. substream_be = snd_soc_dpcm_get_substream(be, stream);
  137. dma_params_be = snd_soc_dai_get_dma_data(dai, substream_be);
  138. dev_be = dai->dev;
  139. break;
  140. }
  141. if (!dma_params_be) {
  142. dev_err(dev, "failed to get the substream of Back-End\n");
  143. return -EINVAL;
  144. }
  145. /* Override dma_data of the Front-End and config its dmaengine */
  146. dma_params_fe = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  147. dma_params_fe->addr = asrc->paddr + asrc->get_fifo_addr(!dir, index);
  148. dma_params_fe->maxburst = dma_params_be->maxburst;
  149. pair->dma_chan[!dir] = asrc->get_dma_channel(pair, !dir);
  150. if (!pair->dma_chan[!dir]) {
  151. dev_err(dev, "failed to request DMA channel\n");
  152. return -EINVAL;
  153. }
  154. memset(&config_fe, 0, sizeof(config_fe));
  155. ret = snd_dmaengine_pcm_prepare_slave_config(substream, params, &config_fe);
  156. if (ret) {
  157. dev_err(dev, "failed to prepare DMA config for Front-End\n");
  158. return ret;
  159. }
  160. ret = dmaengine_slave_config(pair->dma_chan[!dir], &config_fe);
  161. if (ret) {
  162. dev_err(dev, "failed to config DMA channel for Front-End\n");
  163. return ret;
  164. }
  165. /* Request and config DMA channel for Back-End */
  166. dma_cap_zero(mask);
  167. dma_cap_set(DMA_SLAVE, mask);
  168. dma_cap_set(DMA_CYCLIC, mask);
  169. /*
  170. * The Back-End device might have already requested a DMA channel,
  171. * so try to reuse it first, and then request a new one upon NULL.
  172. */
  173. component_be = snd_soc_lookup_component_nolocked(dev_be, SND_DMAENGINE_PCM_DRV_NAME);
  174. if (component_be) {
  175. be_chan = soc_component_to_pcm(component_be)->chan[substream->stream];
  176. tmp_chan = be_chan;
  177. }
  178. if (!tmp_chan)
  179. tmp_chan = dma_request_slave_channel(dev_be, tx ? "tx" : "rx");
  180. /*
  181. * An EDMA DEV_TO_DEV channel is fixed and bound with DMA event of each
  182. * peripheral, unlike SDMA channel that is allocated dynamically. So no
  183. * need to configure dma_request and dma_request2, but get dma_chan of
  184. * Back-End device directly via dma_request_slave_channel.
  185. */
  186. if (!asrc->use_edma) {
  187. /* Get DMA request of Back-End */
  188. tmp_data = tmp_chan->private;
  189. pair->dma_data.dma_request = tmp_data->dma_request;
  190. if (!be_chan)
  191. dma_release_channel(tmp_chan);
  192. /* Get DMA request of Front-End */
  193. tmp_chan = asrc->get_dma_channel(pair, dir);
  194. tmp_data = tmp_chan->private;
  195. pair->dma_data.dma_request2 = tmp_data->dma_request;
  196. pair->dma_data.peripheral_type = tmp_data->peripheral_type;
  197. pair->dma_data.priority = tmp_data->priority;
  198. dma_release_channel(tmp_chan);
  199. pair->dma_chan[dir] =
  200. dma_request_channel(mask, filter, &pair->dma_data);
  201. pair->req_dma_chan = true;
  202. } else {
  203. pair->dma_chan[dir] = tmp_chan;
  204. /* Do not flag to release if we are reusing the Back-End one */
  205. pair->req_dma_chan = !be_chan;
  206. }
  207. if (!pair->dma_chan[dir]) {
  208. dev_err(dev, "failed to request DMA channel for Back-End\n");
  209. return -EINVAL;
  210. }
  211. width = snd_pcm_format_physical_width(asrc->asrc_format);
  212. if (width < 8 || width > 64)
  213. return -EINVAL;
  214. else if (width == 8)
  215. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  216. else if (width == 16)
  217. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  218. else if (width == 24)
  219. buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
  220. else if (width <= 32)
  221. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  222. else
  223. buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
  224. config_be.direction = DMA_DEV_TO_DEV;
  225. config_be.src_addr_width = buswidth;
  226. config_be.src_maxburst = dma_params_be->maxburst;
  227. config_be.dst_addr_width = buswidth;
  228. config_be.dst_maxburst = dma_params_be->maxburst;
  229. if (tx) {
  230. config_be.src_addr = asrc->paddr + asrc->get_fifo_addr(OUT, index);
  231. config_be.dst_addr = dma_params_be->addr;
  232. } else {
  233. config_be.dst_addr = asrc->paddr + asrc->get_fifo_addr(IN, index);
  234. config_be.src_addr = dma_params_be->addr;
  235. }
  236. ret = dmaengine_slave_config(pair->dma_chan[dir], &config_be);
  237. if (ret) {
  238. dev_err(dev, "failed to config DMA channel for Back-End\n");
  239. if (pair->req_dma_chan)
  240. dma_release_channel(pair->dma_chan[dir]);
  241. return ret;
  242. }
  243. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  244. return 0;
  245. }
  246. static int fsl_asrc_dma_hw_free(struct snd_soc_component *component,
  247. struct snd_pcm_substream *substream)
  248. {
  249. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  250. struct snd_pcm_runtime *runtime = substream->runtime;
  251. struct fsl_asrc_pair *pair = runtime->private_data;
  252. u8 dir = tx ? OUT : IN;
  253. snd_pcm_set_runtime_buffer(substream, NULL);
  254. if (pair->dma_chan[!dir])
  255. dma_release_channel(pair->dma_chan[!dir]);
  256. /* release dev_to_dev chan if we aren't reusing the Back-End one */
  257. if (pair->dma_chan[dir] && pair->req_dma_chan)
  258. dma_release_channel(pair->dma_chan[dir]);
  259. pair->dma_chan[!dir] = NULL;
  260. pair->dma_chan[dir] = NULL;
  261. return 0;
  262. }
  263. static int fsl_asrc_dma_startup(struct snd_soc_component *component,
  264. struct snd_pcm_substream *substream)
  265. {
  266. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  267. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  268. struct snd_pcm_runtime *runtime = substream->runtime;
  269. struct snd_dmaengine_dai_dma_data *dma_data;
  270. struct device *dev = component->dev;
  271. struct fsl_asrc *asrc = dev_get_drvdata(dev);
  272. struct fsl_asrc_pair *pair;
  273. struct dma_chan *tmp_chan = NULL;
  274. u8 dir = tx ? OUT : IN;
  275. bool release_pair = true;
  276. int ret = 0;
  277. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  278. SNDRV_PCM_HW_PARAM_PERIODS);
  279. if (ret < 0) {
  280. dev_err(dev, "failed to set pcm hw params periods\n");
  281. return ret;
  282. }
  283. pair = kzalloc(sizeof(*pair) + asrc->pair_priv_size, GFP_KERNEL);
  284. if (!pair)
  285. return -ENOMEM;
  286. pair->asrc = asrc;
  287. pair->private = (void *)pair + sizeof(struct fsl_asrc_pair);
  288. runtime->private_data = pair;
  289. /* Request a dummy pair, which will be released later.
  290. * Request pair function needs channel num as input, for this
  291. * dummy pair, we just request "1" channel temporarily.
  292. */
  293. ret = asrc->request_pair(1, pair);
  294. if (ret < 0) {
  295. dev_err(dev, "failed to request asrc pair\n");
  296. goto req_pair_err;
  297. }
  298. /* Request a dummy dma channel, which will be released later. */
  299. tmp_chan = asrc->get_dma_channel(pair, dir);
  300. if (!tmp_chan) {
  301. dev_err(dev, "failed to get dma channel\n");
  302. ret = -EINVAL;
  303. goto dma_chan_err;
  304. }
  305. dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  306. /* Refine the snd_imx_hardware according to caps of DMA. */
  307. ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream,
  308. dma_data,
  309. &snd_imx_hardware,
  310. tmp_chan);
  311. if (ret < 0) {
  312. dev_err(dev, "failed to refine runtime hwparams\n");
  313. goto out;
  314. }
  315. release_pair = false;
  316. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  317. out:
  318. dma_release_channel(tmp_chan);
  319. dma_chan_err:
  320. asrc->release_pair(pair);
  321. req_pair_err:
  322. if (release_pair)
  323. kfree(pair);
  324. return ret;
  325. }
  326. static int fsl_asrc_dma_shutdown(struct snd_soc_component *component,
  327. struct snd_pcm_substream *substream)
  328. {
  329. struct snd_pcm_runtime *runtime = substream->runtime;
  330. struct fsl_asrc_pair *pair = runtime->private_data;
  331. struct fsl_asrc *asrc;
  332. if (!pair)
  333. return 0;
  334. asrc = pair->asrc;
  335. if (asrc->pair[pair->index] == pair)
  336. asrc->pair[pair->index] = NULL;
  337. kfree(pair);
  338. return 0;
  339. }
  340. static snd_pcm_uframes_t
  341. fsl_asrc_dma_pcm_pointer(struct snd_soc_component *component,
  342. struct snd_pcm_substream *substream)
  343. {
  344. struct snd_pcm_runtime *runtime = substream->runtime;
  345. struct fsl_asrc_pair *pair = runtime->private_data;
  346. return bytes_to_frames(substream->runtime, pair->pos);
  347. }
  348. static int fsl_asrc_dma_pcm_new(struct snd_soc_component *component,
  349. struct snd_soc_pcm_runtime *rtd)
  350. {
  351. struct snd_card *card = rtd->card->snd_card;
  352. struct snd_pcm_substream *substream;
  353. struct snd_pcm *pcm = rtd->pcm;
  354. int ret, i;
  355. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  356. if (ret) {
  357. dev_err(card->dev, "failed to set DMA mask\n");
  358. return ret;
  359. }
  360. for_each_pcm_streams(i) {
  361. substream = pcm->streams[i].substream;
  362. if (!substream)
  363. continue;
  364. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev,
  365. FSL_ASRC_DMABUF_SIZE, &substream->dma_buffer);
  366. if (ret) {
  367. dev_err(card->dev, "failed to allocate DMA buffer\n");
  368. goto err;
  369. }
  370. }
  371. return 0;
  372. err:
  373. if (--i == 0 && pcm->streams[i].substream)
  374. snd_dma_free_pages(&pcm->streams[i].substream->dma_buffer);
  375. return ret;
  376. }
  377. static void fsl_asrc_dma_pcm_free(struct snd_soc_component *component,
  378. struct snd_pcm *pcm)
  379. {
  380. struct snd_pcm_substream *substream;
  381. int i;
  382. for_each_pcm_streams(i) {
  383. substream = pcm->streams[i].substream;
  384. if (!substream)
  385. continue;
  386. snd_dma_free_pages(&substream->dma_buffer);
  387. substream->dma_buffer.area = NULL;
  388. substream->dma_buffer.addr = 0;
  389. }
  390. }
  391. struct snd_soc_component_driver fsl_asrc_component = {
  392. .name = DRV_NAME,
  393. .hw_params = fsl_asrc_dma_hw_params,
  394. .hw_free = fsl_asrc_dma_hw_free,
  395. .trigger = fsl_asrc_dma_trigger,
  396. .open = fsl_asrc_dma_startup,
  397. .close = fsl_asrc_dma_shutdown,
  398. .pointer = fsl_asrc_dma_pcm_pointer,
  399. .pcm_construct = fsl_asrc_dma_pcm_new,
  400. .pcm_destruct = fsl_asrc_dma_pcm_free,
  401. };
  402. EXPORT_SYMBOL_GPL(fsl_asrc_component);