fsl_asrc_dma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 <[email protected]>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/module.h>
  10. #include <linux/dma/imx-dma.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_async(pair->dma_chan[OUT]);
  98. dmaengine_terminate_async(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. enum sdma_peripheral_type be_peripheral_type = IMX_DMATYPE_SSI;
  111. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  112. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  113. struct snd_dmaengine_dai_dma_data *dma_params_fe = NULL;
  114. struct snd_dmaengine_dai_dma_data *dma_params_be = NULL;
  115. struct snd_pcm_runtime *runtime = substream->runtime;
  116. struct fsl_asrc_pair *pair = runtime->private_data;
  117. struct dma_chan *tmp_chan = NULL, *be_chan = NULL;
  118. struct snd_soc_component *component_be = NULL;
  119. struct fsl_asrc *asrc = pair->asrc;
  120. struct dma_slave_config config_fe = {}, config_be = {};
  121. struct sdma_peripheral_config audio_config;
  122. enum asrc_pair_index index = pair->index;
  123. struct device *dev = component->dev;
  124. struct device_node *of_dma_node;
  125. int stream = substream->stream;
  126. struct imx_dma_data *tmp_data;
  127. struct snd_soc_dpcm *dpcm;
  128. struct device *dev_be;
  129. u8 dir = tx ? OUT : IN;
  130. dma_cap_mask_t mask;
  131. int ret, width;
  132. /* Fetch the Back-End dma_data from DPCM */
  133. for_each_dpcm_be(rtd, stream, dpcm) {
  134. struct snd_soc_pcm_runtime *be = dpcm->be;
  135. struct snd_pcm_substream *substream_be;
  136. struct snd_soc_dai *dai = asoc_rtd_to_cpu(be, 0);
  137. if (dpcm->fe != rtd)
  138. continue;
  139. substream_be = snd_soc_dpcm_get_substream(be, stream);
  140. dma_params_be = snd_soc_dai_get_dma_data(dai, substream_be);
  141. dev_be = dai->dev;
  142. break;
  143. }
  144. if (!dma_params_be) {
  145. dev_err(dev, "failed to get the substream of Back-End\n");
  146. return -EINVAL;
  147. }
  148. /* Override dma_data of the Front-End and config its dmaengine */
  149. dma_params_fe = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  150. dma_params_fe->addr = asrc->paddr + asrc->get_fifo_addr(!dir, index);
  151. dma_params_fe->maxburst = dma_params_be->maxburst;
  152. pair->dma_chan[!dir] = asrc->get_dma_channel(pair, !dir);
  153. if (!pair->dma_chan[!dir]) {
  154. dev_err(dev, "failed to request DMA channel\n");
  155. return -EINVAL;
  156. }
  157. ret = snd_dmaengine_pcm_prepare_slave_config(substream, params, &config_fe);
  158. if (ret) {
  159. dev_err(dev, "failed to prepare DMA config for Front-End\n");
  160. return ret;
  161. }
  162. ret = dmaengine_slave_config(pair->dma_chan[!dir], &config_fe);
  163. if (ret) {
  164. dev_err(dev, "failed to config DMA channel for Front-End\n");
  165. return ret;
  166. }
  167. /* Request and config DMA channel for Back-End */
  168. dma_cap_zero(mask);
  169. dma_cap_set(DMA_SLAVE, mask);
  170. dma_cap_set(DMA_CYCLIC, mask);
  171. /*
  172. * The Back-End device might have already requested a DMA channel,
  173. * so try to reuse it first, and then request a new one upon NULL.
  174. */
  175. component_be = snd_soc_lookup_component_nolocked(dev_be, SND_DMAENGINE_PCM_DRV_NAME);
  176. if (component_be) {
  177. be_chan = soc_component_to_pcm(component_be)->chan[substream->stream];
  178. tmp_chan = be_chan;
  179. }
  180. if (!tmp_chan) {
  181. tmp_chan = dma_request_chan(dev_be, tx ? "tx" : "rx");
  182. if (IS_ERR(tmp_chan)) {
  183. dev_err(dev, "failed to request DMA channel for Back-End\n");
  184. return -EINVAL;
  185. }
  186. }
  187. /*
  188. * An EDMA DEV_TO_DEV channel is fixed and bound with DMA event of each
  189. * peripheral, unlike SDMA channel that is allocated dynamically. So no
  190. * need to configure dma_request and dma_request2, but get dma_chan of
  191. * Back-End device directly via dma_request_chan.
  192. */
  193. if (!asrc->use_edma) {
  194. /* Get DMA request of Back-End */
  195. tmp_data = tmp_chan->private;
  196. pair->dma_data.dma_request = tmp_data->dma_request;
  197. be_peripheral_type = tmp_data->peripheral_type;
  198. if (!be_chan)
  199. dma_release_channel(tmp_chan);
  200. /* Get DMA request of Front-End */
  201. tmp_chan = asrc->get_dma_channel(pair, dir);
  202. tmp_data = tmp_chan->private;
  203. pair->dma_data.dma_request2 = tmp_data->dma_request;
  204. pair->dma_data.peripheral_type = tmp_data->peripheral_type;
  205. pair->dma_data.priority = tmp_data->priority;
  206. dma_release_channel(tmp_chan);
  207. of_dma_node = pair->dma_chan[!dir]->device->dev->of_node;
  208. pair->dma_chan[dir] =
  209. __dma_request_channel(&mask, filter, &pair->dma_data,
  210. of_dma_node);
  211. pair->req_dma_chan = true;
  212. } else {
  213. pair->dma_chan[dir] = tmp_chan;
  214. /* Do not flag to release if we are reusing the Back-End one */
  215. pair->req_dma_chan = !be_chan;
  216. }
  217. if (!pair->dma_chan[dir]) {
  218. dev_err(dev, "failed to request DMA channel for Back-End\n");
  219. return -EINVAL;
  220. }
  221. width = snd_pcm_format_physical_width(asrc->asrc_format);
  222. if (width < 8 || width > 64)
  223. return -EINVAL;
  224. else if (width == 8)
  225. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  226. else if (width == 16)
  227. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  228. else if (width == 24)
  229. buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
  230. else if (width <= 32)
  231. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  232. else
  233. buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
  234. config_be.direction = DMA_DEV_TO_DEV;
  235. config_be.src_addr_width = buswidth;
  236. config_be.src_maxburst = dma_params_be->maxburst;
  237. config_be.dst_addr_width = buswidth;
  238. config_be.dst_maxburst = dma_params_be->maxburst;
  239. memset(&audio_config, 0, sizeof(audio_config));
  240. config_be.peripheral_config = &audio_config;
  241. config_be.peripheral_size = sizeof(audio_config);
  242. if (tx && (be_peripheral_type == IMX_DMATYPE_SSI_DUAL ||
  243. be_peripheral_type == IMX_DMATYPE_SPDIF))
  244. audio_config.n_fifos_dst = 2;
  245. if (!tx && (be_peripheral_type == IMX_DMATYPE_SSI_DUAL ||
  246. be_peripheral_type == IMX_DMATYPE_SPDIF))
  247. audio_config.n_fifos_src = 2;
  248. if (tx) {
  249. config_be.src_addr = asrc->paddr + asrc->get_fifo_addr(OUT, index);
  250. config_be.dst_addr = dma_params_be->addr;
  251. } else {
  252. config_be.dst_addr = asrc->paddr + asrc->get_fifo_addr(IN, index);
  253. config_be.src_addr = dma_params_be->addr;
  254. }
  255. ret = dmaengine_slave_config(pair->dma_chan[dir], &config_be);
  256. if (ret) {
  257. dev_err(dev, "failed to config DMA channel for Back-End\n");
  258. if (pair->req_dma_chan)
  259. dma_release_channel(pair->dma_chan[dir]);
  260. return ret;
  261. }
  262. return 0;
  263. }
  264. static int fsl_asrc_dma_hw_free(struct snd_soc_component *component,
  265. struct snd_pcm_substream *substream)
  266. {
  267. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  268. struct snd_pcm_runtime *runtime = substream->runtime;
  269. struct fsl_asrc_pair *pair = runtime->private_data;
  270. u8 dir = tx ? OUT : IN;
  271. if (pair->dma_chan[!dir])
  272. dma_release_channel(pair->dma_chan[!dir]);
  273. /* release dev_to_dev chan if we aren't reusing the Back-End one */
  274. if (pair->dma_chan[dir] && pair->req_dma_chan)
  275. dma_release_channel(pair->dma_chan[dir]);
  276. pair->dma_chan[!dir] = NULL;
  277. pair->dma_chan[dir] = NULL;
  278. return 0;
  279. }
  280. static int fsl_asrc_dma_startup(struct snd_soc_component *component,
  281. struct snd_pcm_substream *substream)
  282. {
  283. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  284. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  285. struct snd_pcm_runtime *runtime = substream->runtime;
  286. struct snd_dmaengine_dai_dma_data *dma_data;
  287. struct device *dev = component->dev;
  288. struct fsl_asrc *asrc = dev_get_drvdata(dev);
  289. struct fsl_asrc_pair *pair;
  290. struct dma_chan *tmp_chan = NULL;
  291. u8 dir = tx ? OUT : IN;
  292. bool release_pair = true;
  293. int ret = 0;
  294. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  295. SNDRV_PCM_HW_PARAM_PERIODS);
  296. if (ret < 0) {
  297. dev_err(dev, "failed to set pcm hw params periods\n");
  298. return ret;
  299. }
  300. pair = kzalloc(sizeof(*pair) + asrc->pair_priv_size, GFP_KERNEL);
  301. if (!pair)
  302. return -ENOMEM;
  303. pair->asrc = asrc;
  304. pair->private = (void *)pair + sizeof(struct fsl_asrc_pair);
  305. runtime->private_data = pair;
  306. /* Request a dummy pair, which will be released later.
  307. * Request pair function needs channel num as input, for this
  308. * dummy pair, we just request "1" channel temporarily.
  309. */
  310. ret = asrc->request_pair(1, pair);
  311. if (ret < 0) {
  312. dev_err(dev, "failed to request asrc pair\n");
  313. goto req_pair_err;
  314. }
  315. /* Request a dummy dma channel, which will be released later. */
  316. tmp_chan = asrc->get_dma_channel(pair, dir);
  317. if (!tmp_chan) {
  318. dev_err(dev, "failed to get dma channel\n");
  319. ret = -EINVAL;
  320. goto dma_chan_err;
  321. }
  322. dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  323. /* Refine the snd_imx_hardware according to caps of DMA. */
  324. ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream,
  325. dma_data,
  326. &snd_imx_hardware,
  327. tmp_chan);
  328. if (ret < 0) {
  329. dev_err(dev, "failed to refine runtime hwparams\n");
  330. goto out;
  331. }
  332. release_pair = false;
  333. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  334. out:
  335. dma_release_channel(tmp_chan);
  336. dma_chan_err:
  337. asrc->release_pair(pair);
  338. req_pair_err:
  339. if (release_pair)
  340. kfree(pair);
  341. return ret;
  342. }
  343. static int fsl_asrc_dma_shutdown(struct snd_soc_component *component,
  344. struct snd_pcm_substream *substream)
  345. {
  346. struct snd_pcm_runtime *runtime = substream->runtime;
  347. struct fsl_asrc_pair *pair = runtime->private_data;
  348. struct fsl_asrc *asrc;
  349. if (!pair)
  350. return 0;
  351. asrc = pair->asrc;
  352. if (asrc->pair[pair->index] == pair)
  353. asrc->pair[pair->index] = NULL;
  354. kfree(pair);
  355. return 0;
  356. }
  357. static snd_pcm_uframes_t
  358. fsl_asrc_dma_pcm_pointer(struct snd_soc_component *component,
  359. struct snd_pcm_substream *substream)
  360. {
  361. struct snd_pcm_runtime *runtime = substream->runtime;
  362. struct fsl_asrc_pair *pair = runtime->private_data;
  363. return bytes_to_frames(substream->runtime, pair->pos);
  364. }
  365. static int fsl_asrc_dma_pcm_new(struct snd_soc_component *component,
  366. struct snd_soc_pcm_runtime *rtd)
  367. {
  368. struct snd_card *card = rtd->card->snd_card;
  369. struct snd_pcm *pcm = rtd->pcm;
  370. int ret;
  371. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  372. if (ret) {
  373. dev_err(card->dev, "failed to set DMA mask\n");
  374. return ret;
  375. }
  376. return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
  377. card->dev, FSL_ASRC_DMABUF_SIZE);
  378. }
  379. struct snd_soc_component_driver fsl_asrc_component = {
  380. .name = DRV_NAME,
  381. .hw_params = fsl_asrc_dma_hw_params,
  382. .hw_free = fsl_asrc_dma_hw_free,
  383. .trigger = fsl_asrc_dma_trigger,
  384. .open = fsl_asrc_dma_startup,
  385. .close = fsl_asrc_dma_shutdown,
  386. .pointer = fsl_asrc_dma_pcm_pointer,
  387. .pcm_construct = fsl_asrc_dma_pcm_new,
  388. .legacy_dai_naming = 1,
  389. };
  390. EXPORT_SYMBOL_GPL(fsl_asrc_component);