xref: /openbmc/linux/sound/soc/fsl/fsl_asrc_dma.c (revision 3117bb31)
13117bb31SNicolin Chen /*
23117bb31SNicolin Chen  * Freescale ASRC ALSA SoC Platform (DMA) driver
33117bb31SNicolin Chen  *
43117bb31SNicolin Chen  * Copyright (C) 2014 Freescale Semiconductor, Inc.
53117bb31SNicolin Chen  *
63117bb31SNicolin Chen  * Author: Nicolin Chen <nicoleotsuka@gmail.com>
73117bb31SNicolin Chen  *
83117bb31SNicolin Chen  * This file is licensed under the terms of the GNU General Public License
93117bb31SNicolin Chen  * version 2. This program is licensed "as is" without any warranty of any
103117bb31SNicolin Chen  * kind, whether express or implied.
113117bb31SNicolin Chen  */
123117bb31SNicolin Chen 
133117bb31SNicolin Chen #include <linux/dma-mapping.h>
143117bb31SNicolin Chen #include <linux/module.h>
153117bb31SNicolin Chen #include <linux/platform_data/dma-imx.h>
163117bb31SNicolin Chen #include <sound/dmaengine_pcm.h>
173117bb31SNicolin Chen #include <sound/pcm_params.h>
183117bb31SNicolin Chen 
193117bb31SNicolin Chen #include "fsl_asrc.h"
203117bb31SNicolin Chen 
213117bb31SNicolin Chen #define FSL_ASRC_DMABUF_SIZE	(256 * 1024)
223117bb31SNicolin Chen 
233117bb31SNicolin Chen static struct snd_pcm_hardware snd_imx_hardware = {
243117bb31SNicolin Chen 	.info = SNDRV_PCM_INFO_INTERLEAVED |
253117bb31SNicolin Chen 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
263117bb31SNicolin Chen 		SNDRV_PCM_INFO_MMAP |
273117bb31SNicolin Chen 		SNDRV_PCM_INFO_MMAP_VALID |
283117bb31SNicolin Chen 		SNDRV_PCM_INFO_PAUSE |
293117bb31SNicolin Chen 		SNDRV_PCM_INFO_RESUME,
303117bb31SNicolin Chen 	.buffer_bytes_max = FSL_ASRC_DMABUF_SIZE,
313117bb31SNicolin Chen 	.period_bytes_min = 128,
323117bb31SNicolin Chen 	.period_bytes_max = 65535, /* Limited by SDMA engine */
333117bb31SNicolin Chen 	.periods_min = 2,
343117bb31SNicolin Chen 	.periods_max = 255,
353117bb31SNicolin Chen 	.fifo_size = 0,
363117bb31SNicolin Chen };
373117bb31SNicolin Chen 
383117bb31SNicolin Chen static bool filter(struct dma_chan *chan, void *param)
393117bb31SNicolin Chen {
403117bb31SNicolin Chen 	if (!imx_dma_is_general_purpose(chan))
413117bb31SNicolin Chen 		return false;
423117bb31SNicolin Chen 
433117bb31SNicolin Chen 	chan->private = param;
443117bb31SNicolin Chen 
453117bb31SNicolin Chen 	return true;
463117bb31SNicolin Chen }
473117bb31SNicolin Chen 
483117bb31SNicolin Chen static void fsl_asrc_dma_complete(void *arg)
493117bb31SNicolin Chen {
503117bb31SNicolin Chen 	struct snd_pcm_substream *substream = arg;
513117bb31SNicolin Chen 	struct snd_pcm_runtime *runtime = substream->runtime;
523117bb31SNicolin Chen 	struct fsl_asrc_pair *pair = runtime->private_data;
533117bb31SNicolin Chen 
543117bb31SNicolin Chen 	pair->pos += snd_pcm_lib_period_bytes(substream);
553117bb31SNicolin Chen 	if (pair->pos >= snd_pcm_lib_buffer_bytes(substream))
563117bb31SNicolin Chen 		pair->pos = 0;
573117bb31SNicolin Chen 
583117bb31SNicolin Chen 	snd_pcm_period_elapsed(substream);
593117bb31SNicolin Chen }
603117bb31SNicolin Chen 
613117bb31SNicolin Chen static int fsl_asrc_dma_prepare_and_submit(struct snd_pcm_substream *substream)
623117bb31SNicolin Chen {
633117bb31SNicolin Chen 	u8 dir = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? OUT : IN;
643117bb31SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
653117bb31SNicolin Chen 	struct snd_pcm_runtime *runtime = substream->runtime;
663117bb31SNicolin Chen 	struct fsl_asrc_pair *pair = runtime->private_data;
673117bb31SNicolin Chen 	struct device *dev = rtd->platform->dev;
683117bb31SNicolin Chen 	unsigned long flags = DMA_CTRL_ACK;
693117bb31SNicolin Chen 
703117bb31SNicolin Chen 	/* Prepare and submit Front-End DMA channel */
713117bb31SNicolin Chen 	if (!substream->runtime->no_period_wakeup)
723117bb31SNicolin Chen 		flags |= DMA_PREP_INTERRUPT;
733117bb31SNicolin Chen 
743117bb31SNicolin Chen 	pair->pos = 0;
753117bb31SNicolin Chen 	pair->desc[!dir] = dmaengine_prep_dma_cyclic(
763117bb31SNicolin Chen 			pair->dma_chan[!dir], runtime->dma_addr,
773117bb31SNicolin Chen 			snd_pcm_lib_buffer_bytes(substream),
783117bb31SNicolin Chen 			snd_pcm_lib_period_bytes(substream),
793117bb31SNicolin Chen 			dir == OUT ? DMA_TO_DEVICE : DMA_FROM_DEVICE, flags);
803117bb31SNicolin Chen 	if (!pair->desc[!dir]) {
813117bb31SNicolin Chen 		dev_err(dev, "failed to prepare slave DMA for Front-End\n");
823117bb31SNicolin Chen 		return -ENOMEM;
833117bb31SNicolin Chen 	}
843117bb31SNicolin Chen 
853117bb31SNicolin Chen 	pair->desc[!dir]->callback = fsl_asrc_dma_complete;
863117bb31SNicolin Chen 	pair->desc[!dir]->callback_param = substream;
873117bb31SNicolin Chen 
883117bb31SNicolin Chen 	dmaengine_submit(pair->desc[!dir]);
893117bb31SNicolin Chen 
903117bb31SNicolin Chen 	/* Prepare and submit Back-End DMA channel */
913117bb31SNicolin Chen 	pair->desc[dir] = dmaengine_prep_dma_cyclic(
923117bb31SNicolin Chen 			pair->dma_chan[dir], 0xffff, 64, 64, DMA_DEV_TO_DEV, 0);
933117bb31SNicolin Chen 	if (!pair->desc[dir]) {
943117bb31SNicolin Chen 		dev_err(dev, "failed to prepare slave DMA for Back-End\n");
953117bb31SNicolin Chen 		return -ENOMEM;
963117bb31SNicolin Chen 	}
973117bb31SNicolin Chen 
983117bb31SNicolin Chen 	dmaengine_submit(pair->desc[dir]);
993117bb31SNicolin Chen 
1003117bb31SNicolin Chen 	return 0;
1013117bb31SNicolin Chen }
1023117bb31SNicolin Chen 
1033117bb31SNicolin Chen static int fsl_asrc_dma_trigger(struct snd_pcm_substream *substream, int cmd)
1043117bb31SNicolin Chen {
1053117bb31SNicolin Chen 	struct snd_pcm_runtime *runtime = substream->runtime;
1063117bb31SNicolin Chen 	struct fsl_asrc_pair *pair = runtime->private_data;
1073117bb31SNicolin Chen 	int ret;
1083117bb31SNicolin Chen 
1093117bb31SNicolin Chen 	switch (cmd) {
1103117bb31SNicolin Chen 	case SNDRV_PCM_TRIGGER_START:
1113117bb31SNicolin Chen 	case SNDRV_PCM_TRIGGER_RESUME:
1123117bb31SNicolin Chen 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1133117bb31SNicolin Chen 		ret = fsl_asrc_dma_prepare_and_submit(substream);
1143117bb31SNicolin Chen 		if (ret)
1153117bb31SNicolin Chen 			return ret;
1163117bb31SNicolin Chen 		dma_async_issue_pending(pair->dma_chan[IN]);
1173117bb31SNicolin Chen 		dma_async_issue_pending(pair->dma_chan[OUT]);
1183117bb31SNicolin Chen 		break;
1193117bb31SNicolin Chen 	case SNDRV_PCM_TRIGGER_STOP:
1203117bb31SNicolin Chen 	case SNDRV_PCM_TRIGGER_SUSPEND:
1213117bb31SNicolin Chen 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1223117bb31SNicolin Chen 		dmaengine_terminate_all(pair->dma_chan[OUT]);
1233117bb31SNicolin Chen 		dmaengine_terminate_all(pair->dma_chan[IN]);
1243117bb31SNicolin Chen 		break;
1253117bb31SNicolin Chen 	default:
1263117bb31SNicolin Chen 		return -EINVAL;
1273117bb31SNicolin Chen 	}
1283117bb31SNicolin Chen 
1293117bb31SNicolin Chen 	return 0;
1303117bb31SNicolin Chen }
1313117bb31SNicolin Chen 
1323117bb31SNicolin Chen static int fsl_asrc_dma_hw_params(struct snd_pcm_substream *substream,
1333117bb31SNicolin Chen 				  struct snd_pcm_hw_params *params)
1343117bb31SNicolin Chen {
1353117bb31SNicolin Chen 	enum dma_slave_buswidth buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1363117bb31SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1373117bb31SNicolin Chen 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1383117bb31SNicolin Chen 	struct snd_dmaengine_dai_dma_data *dma_params_fe = NULL;
1393117bb31SNicolin Chen 	struct snd_dmaengine_dai_dma_data *dma_params_be = NULL;
1403117bb31SNicolin Chen 	struct snd_pcm_runtime *runtime = substream->runtime;
1413117bb31SNicolin Chen 	struct fsl_asrc_pair *pair = runtime->private_data;
1423117bb31SNicolin Chen 	struct fsl_asrc *asrc_priv = pair->asrc_priv;
1433117bb31SNicolin Chen 	struct dma_slave_config config_fe, config_be;
1443117bb31SNicolin Chen 	enum asrc_pair_index index = pair->index;
1453117bb31SNicolin Chen 	struct device *dev = rtd->platform->dev;
1463117bb31SNicolin Chen 	int stream = substream->stream;
1473117bb31SNicolin Chen 	struct imx_dma_data *tmp_data;
1483117bb31SNicolin Chen 	struct snd_soc_dpcm *dpcm;
1493117bb31SNicolin Chen 	struct dma_chan *tmp_chan;
1503117bb31SNicolin Chen 	struct device *dev_be;
1513117bb31SNicolin Chen 	u8 dir = tx ? OUT : IN;
1523117bb31SNicolin Chen 	dma_cap_mask_t mask;
1533117bb31SNicolin Chen 	int ret;
1543117bb31SNicolin Chen 
1553117bb31SNicolin Chen 	/* Fetch the Back-End dma_data from DPCM */
1563117bb31SNicolin Chen 	list_for_each_entry(dpcm, &rtd->dpcm[stream].be_clients, list_be) {
1573117bb31SNicolin Chen 		struct snd_soc_pcm_runtime *be = dpcm->be;
1583117bb31SNicolin Chen 		struct snd_pcm_substream *substream_be;
1593117bb31SNicolin Chen 		struct snd_soc_dai *dai = be->cpu_dai;
1603117bb31SNicolin Chen 
1613117bb31SNicolin Chen 		if (dpcm->fe != rtd)
1623117bb31SNicolin Chen 			continue;
1633117bb31SNicolin Chen 
1643117bb31SNicolin Chen 		substream_be = snd_soc_dpcm_get_substream(be, stream);
1653117bb31SNicolin Chen 		dma_params_be = snd_soc_dai_get_dma_data(dai, substream_be);
1663117bb31SNicolin Chen 		dev_be = dai->dev;
1673117bb31SNicolin Chen 		break;
1683117bb31SNicolin Chen 	}
1693117bb31SNicolin Chen 
1703117bb31SNicolin Chen 	if (!dma_params_be) {
1713117bb31SNicolin Chen 		dev_err(dev, "failed to get the substream of Back-End\n");
1723117bb31SNicolin Chen 		return -EINVAL;
1733117bb31SNicolin Chen 	}
1743117bb31SNicolin Chen 
1753117bb31SNicolin Chen 	/* Override dma_data of the Front-End and config its dmaengine */
1763117bb31SNicolin Chen 	dma_params_fe = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
1773117bb31SNicolin Chen 	dma_params_fe->addr = asrc_priv->paddr + REG_ASRDx(!dir, index);
1783117bb31SNicolin Chen 	dma_params_fe->maxburst = dma_params_be->maxburst;
1793117bb31SNicolin Chen 
1803117bb31SNicolin Chen 	pair->dma_chan[!dir] = fsl_asrc_get_dma_channel(pair, !dir);
1813117bb31SNicolin Chen 	if (!pair->dma_chan[!dir]) {
1823117bb31SNicolin Chen 		dev_err(dev, "failed to request DMA channel\n");
1833117bb31SNicolin Chen 		return -EINVAL;
1843117bb31SNicolin Chen 	}
1853117bb31SNicolin Chen 
1863117bb31SNicolin Chen 	memset(&config_fe, 0, sizeof(config_fe));
1873117bb31SNicolin Chen 	ret = snd_dmaengine_pcm_prepare_slave_config(substream, params, &config_fe);
1883117bb31SNicolin Chen 	if (ret) {
1893117bb31SNicolin Chen 		dev_err(dev, "failed to prepare DMA config for Front-End\n");
1903117bb31SNicolin Chen 		return ret;
1913117bb31SNicolin Chen 	}
1923117bb31SNicolin Chen 
1933117bb31SNicolin Chen 	ret = dmaengine_slave_config(pair->dma_chan[!dir], &config_fe);
1943117bb31SNicolin Chen 	if (ret) {
1953117bb31SNicolin Chen 		dev_err(dev, "failed to config DMA channel for Front-End\n");
1963117bb31SNicolin Chen 		return ret;
1973117bb31SNicolin Chen 	}
1983117bb31SNicolin Chen 
1993117bb31SNicolin Chen 	/* Request and config DMA channel for Back-End */
2003117bb31SNicolin Chen 	dma_cap_zero(mask);
2013117bb31SNicolin Chen 	dma_cap_set(DMA_SLAVE, mask);
2023117bb31SNicolin Chen 	dma_cap_set(DMA_CYCLIC, mask);
2033117bb31SNicolin Chen 
2043117bb31SNicolin Chen 	/* Get DMA request of Back-End */
2053117bb31SNicolin Chen 	tmp_chan = dma_request_slave_channel(dev_be, tx ? "tx" : "rx");
2063117bb31SNicolin Chen 	tmp_data = tmp_chan->private;
2073117bb31SNicolin Chen 	pair->dma_data.dma_request = tmp_data->dma_request;
2083117bb31SNicolin Chen 	dma_release_channel(tmp_chan);
2093117bb31SNicolin Chen 
2103117bb31SNicolin Chen 	/* Get DMA request of Front-End */
2113117bb31SNicolin Chen 	tmp_chan = fsl_asrc_get_dma_channel(pair, dir);
2123117bb31SNicolin Chen 	tmp_data = tmp_chan->private;
2133117bb31SNicolin Chen 	pair->dma_data.dma_request2 = tmp_data->dma_request;
2143117bb31SNicolin Chen 	pair->dma_data.peripheral_type = tmp_data->peripheral_type;
2153117bb31SNicolin Chen 	pair->dma_data.priority = tmp_data->priority;
2163117bb31SNicolin Chen 	dma_release_channel(tmp_chan);
2173117bb31SNicolin Chen 
2183117bb31SNicolin Chen 	pair->dma_chan[dir] = dma_request_channel(mask, filter, &pair->dma_data);
2193117bb31SNicolin Chen 	if (!pair->dma_chan[dir]) {
2203117bb31SNicolin Chen 		dev_err(dev, "failed to request DMA channel for Back-End\n");
2213117bb31SNicolin Chen 		return -EINVAL;
2223117bb31SNicolin Chen 	}
2233117bb31SNicolin Chen 
2243117bb31SNicolin Chen 	if (asrc_priv->asrc_width == 16)
2253117bb31SNicolin Chen 		buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
2263117bb31SNicolin Chen 	else
2273117bb31SNicolin Chen 		buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
2283117bb31SNicolin Chen 
2293117bb31SNicolin Chen 	config_be.direction = DMA_DEV_TO_DEV;
2303117bb31SNicolin Chen 	config_be.src_addr_width = buswidth;
2313117bb31SNicolin Chen 	config_be.src_maxburst = dma_params_be->maxburst;
2323117bb31SNicolin Chen 	config_be.dst_addr_width = buswidth;
2333117bb31SNicolin Chen 	config_be.dst_maxburst = dma_params_be->maxburst;
2343117bb31SNicolin Chen 
2353117bb31SNicolin Chen 	if (tx) {
2363117bb31SNicolin Chen 		config_be.src_addr = asrc_priv->paddr + REG_ASRDO(index);
2373117bb31SNicolin Chen 		config_be.dst_addr = dma_params_be->addr;
2383117bb31SNicolin Chen 	} else {
2393117bb31SNicolin Chen 		config_be.dst_addr = asrc_priv->paddr + REG_ASRDI(index);
2403117bb31SNicolin Chen 		config_be.src_addr = dma_params_be->addr;
2413117bb31SNicolin Chen 	}
2423117bb31SNicolin Chen 
2433117bb31SNicolin Chen 	ret = dmaengine_slave_config(pair->dma_chan[dir], &config_be);
2443117bb31SNicolin Chen 	if (ret) {
2453117bb31SNicolin Chen 		dev_err(dev, "failed to config DMA channel for Back-End\n");
2463117bb31SNicolin Chen 		return ret;
2473117bb31SNicolin Chen 	}
2483117bb31SNicolin Chen 
2493117bb31SNicolin Chen 	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
2503117bb31SNicolin Chen 
2513117bb31SNicolin Chen 	return 0;
2523117bb31SNicolin Chen }
2533117bb31SNicolin Chen 
2543117bb31SNicolin Chen static int fsl_asrc_dma_hw_free(struct snd_pcm_substream *substream)
2553117bb31SNicolin Chen {
2563117bb31SNicolin Chen 	struct snd_pcm_runtime *runtime = substream->runtime;
2573117bb31SNicolin Chen 	struct fsl_asrc_pair *pair = runtime->private_data;
2583117bb31SNicolin Chen 
2593117bb31SNicolin Chen 	snd_pcm_set_runtime_buffer(substream, NULL);
2603117bb31SNicolin Chen 
2613117bb31SNicolin Chen 	if (pair->dma_chan[IN])
2623117bb31SNicolin Chen 		dma_release_channel(pair->dma_chan[IN]);
2633117bb31SNicolin Chen 
2643117bb31SNicolin Chen 	if (pair->dma_chan[OUT])
2653117bb31SNicolin Chen 		dma_release_channel(pair->dma_chan[OUT]);
2663117bb31SNicolin Chen 
2673117bb31SNicolin Chen 	pair->dma_chan[IN] = NULL;
2683117bb31SNicolin Chen 	pair->dma_chan[OUT] = NULL;
2693117bb31SNicolin Chen 
2703117bb31SNicolin Chen 	return 0;
2713117bb31SNicolin Chen }
2723117bb31SNicolin Chen 
2733117bb31SNicolin Chen static int fsl_asrc_dma_startup(struct snd_pcm_substream *substream)
2743117bb31SNicolin Chen {
2753117bb31SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
2763117bb31SNicolin Chen 	struct snd_pcm_runtime *runtime = substream->runtime;
2773117bb31SNicolin Chen 	struct device *dev = rtd->platform->dev;
2783117bb31SNicolin Chen 	struct fsl_asrc *asrc_priv = dev_get_drvdata(dev);
2793117bb31SNicolin Chen 	struct fsl_asrc_pair *pair;
2803117bb31SNicolin Chen 
2813117bb31SNicolin Chen 	pair = kzalloc(sizeof(struct fsl_asrc_pair), GFP_KERNEL);
2823117bb31SNicolin Chen 	if (!pair) {
2833117bb31SNicolin Chen 		dev_err(dev, "failed to allocate pair\n");
2843117bb31SNicolin Chen 		return -ENOMEM;
2853117bb31SNicolin Chen 	}
2863117bb31SNicolin Chen 
2873117bb31SNicolin Chen 	pair->asrc_priv = asrc_priv;
2883117bb31SNicolin Chen 
2893117bb31SNicolin Chen 	runtime->private_data = pair;
2903117bb31SNicolin Chen 
2913117bb31SNicolin Chen 	snd_pcm_hw_constraint_integer(substream->runtime,
2923117bb31SNicolin Chen 				      SNDRV_PCM_HW_PARAM_PERIODS);
2933117bb31SNicolin Chen 	snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
2943117bb31SNicolin Chen 
2953117bb31SNicolin Chen 	return 0;
2963117bb31SNicolin Chen }
2973117bb31SNicolin Chen 
2983117bb31SNicolin Chen static int fsl_asrc_dma_shutdown(struct snd_pcm_substream *substream)
2993117bb31SNicolin Chen {
3003117bb31SNicolin Chen 	struct snd_pcm_runtime *runtime = substream->runtime;
3013117bb31SNicolin Chen 	struct fsl_asrc_pair *pair = runtime->private_data;
3023117bb31SNicolin Chen 	struct fsl_asrc *asrc_priv = pair->asrc_priv;
3033117bb31SNicolin Chen 
3043117bb31SNicolin Chen 	if (pair && asrc_priv->pair[pair->index] == pair)
3053117bb31SNicolin Chen 		asrc_priv->pair[pair->index] = NULL;
3063117bb31SNicolin Chen 
3073117bb31SNicolin Chen 	kfree(pair);
3083117bb31SNicolin Chen 
3093117bb31SNicolin Chen 	return 0;
3103117bb31SNicolin Chen }
3113117bb31SNicolin Chen 
3123117bb31SNicolin Chen static snd_pcm_uframes_t fsl_asrc_dma_pcm_pointer(struct snd_pcm_substream *substream)
3133117bb31SNicolin Chen {
3143117bb31SNicolin Chen 	struct snd_pcm_runtime *runtime = substream->runtime;
3153117bb31SNicolin Chen 	struct fsl_asrc_pair *pair = runtime->private_data;
3163117bb31SNicolin Chen 
3173117bb31SNicolin Chen 	return bytes_to_frames(substream->runtime, pair->pos);
3183117bb31SNicolin Chen }
3193117bb31SNicolin Chen 
3203117bb31SNicolin Chen static struct snd_pcm_ops fsl_asrc_dma_pcm_ops = {
3213117bb31SNicolin Chen 	.ioctl		= snd_pcm_lib_ioctl,
3223117bb31SNicolin Chen 	.hw_params	= fsl_asrc_dma_hw_params,
3233117bb31SNicolin Chen 	.hw_free	= fsl_asrc_dma_hw_free,
3243117bb31SNicolin Chen 	.trigger	= fsl_asrc_dma_trigger,
3253117bb31SNicolin Chen 	.open		= fsl_asrc_dma_startup,
3263117bb31SNicolin Chen 	.close		= fsl_asrc_dma_shutdown,
3273117bb31SNicolin Chen 	.pointer	= fsl_asrc_dma_pcm_pointer,
3283117bb31SNicolin Chen };
3293117bb31SNicolin Chen 
3303117bb31SNicolin Chen static int fsl_asrc_dma_pcm_new(struct snd_soc_pcm_runtime *rtd)
3313117bb31SNicolin Chen {
3323117bb31SNicolin Chen 	struct snd_card *card = rtd->card->snd_card;
3333117bb31SNicolin Chen 	struct snd_pcm_substream *substream;
3343117bb31SNicolin Chen 	struct snd_pcm *pcm = rtd->pcm;
3353117bb31SNicolin Chen 	int ret, i;
3363117bb31SNicolin Chen 
3373117bb31SNicolin Chen 	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
3383117bb31SNicolin Chen 	if (ret) {
3393117bb31SNicolin Chen 		dev_err(card->dev, "failed to set DMA mask\n");
3403117bb31SNicolin Chen 		return ret;
3413117bb31SNicolin Chen 	}
3423117bb31SNicolin Chen 
3433117bb31SNicolin Chen 	for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_LAST; i++) {
3443117bb31SNicolin Chen 		substream = pcm->streams[i].substream;
3453117bb31SNicolin Chen 		if (!substream)
3463117bb31SNicolin Chen 			continue;
3473117bb31SNicolin Chen 
3483117bb31SNicolin Chen 		ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev,
3493117bb31SNicolin Chen 				FSL_ASRC_DMABUF_SIZE, &substream->dma_buffer);
3503117bb31SNicolin Chen 		if (ret) {
3513117bb31SNicolin Chen 			dev_err(card->dev, "failed to allocate DMA buffer\n");
3523117bb31SNicolin Chen 			goto err;
3533117bb31SNicolin Chen 		}
3543117bb31SNicolin Chen 	}
3553117bb31SNicolin Chen 
3563117bb31SNicolin Chen 	return 0;
3573117bb31SNicolin Chen 
3583117bb31SNicolin Chen err:
3593117bb31SNicolin Chen 	if (--i == 0 && pcm->streams[i].substream)
3603117bb31SNicolin Chen 		snd_dma_free_pages(&pcm->streams[i].substream->dma_buffer);
3613117bb31SNicolin Chen 
3623117bb31SNicolin Chen 	return ret;
3633117bb31SNicolin Chen }
3643117bb31SNicolin Chen 
3653117bb31SNicolin Chen static void fsl_asrc_dma_pcm_free(struct snd_pcm *pcm)
3663117bb31SNicolin Chen {
3673117bb31SNicolin Chen 	struct snd_pcm_substream *substream;
3683117bb31SNicolin Chen 	int i;
3693117bb31SNicolin Chen 
3703117bb31SNicolin Chen 	for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_LAST; i++) {
3713117bb31SNicolin Chen 		substream = pcm->streams[i].substream;
3723117bb31SNicolin Chen 		if (!substream)
3733117bb31SNicolin Chen 			continue;
3743117bb31SNicolin Chen 
3753117bb31SNicolin Chen 		snd_dma_free_pages(&substream->dma_buffer);
3763117bb31SNicolin Chen 		substream->dma_buffer.area = NULL;
3773117bb31SNicolin Chen 		substream->dma_buffer.addr = 0;
3783117bb31SNicolin Chen 	}
3793117bb31SNicolin Chen }
3803117bb31SNicolin Chen 
3813117bb31SNicolin Chen struct snd_soc_platform_driver fsl_asrc_platform = {
3823117bb31SNicolin Chen 	.ops		= &fsl_asrc_dma_pcm_ops,
3833117bb31SNicolin Chen 	.pcm_new	= fsl_asrc_dma_pcm_new,
3843117bb31SNicolin Chen 	.pcm_free	= fsl_asrc_dma_pcm_free,
3853117bb31SNicolin Chen };
3863117bb31SNicolin Chen EXPORT_SYMBOL_GPL(fsl_asrc_platform);
387