12ba28053SFabio Estevam // SPDX-License-Identifier: GPL-2.0 22ba28053SFabio Estevam // 32ba28053SFabio Estevam // Freescale ASRC ALSA SoC Platform (DMA) driver 42ba28053SFabio Estevam // 52ba28053SFabio Estevam // Copyright (C) 2014 Freescale Semiconductor, Inc. 62ba28053SFabio Estevam // 72ba28053SFabio Estevam // Author: Nicolin Chen <nicoleotsuka@gmail.com> 83117bb31SNicolin Chen 93117bb31SNicolin Chen #include <linux/dma-mapping.h> 103117bb31SNicolin Chen #include <linux/module.h> 113117bb31SNicolin Chen #include <linux/platform_data/dma-imx.h> 123117bb31SNicolin Chen #include <sound/dmaengine_pcm.h> 133117bb31SNicolin Chen #include <sound/pcm_params.h> 143117bb31SNicolin Chen 153117bb31SNicolin Chen #include "fsl_asrc.h" 163117bb31SNicolin Chen 173117bb31SNicolin Chen #define FSL_ASRC_DMABUF_SIZE (256 * 1024) 183117bb31SNicolin Chen 19703df441SShengjiu Wang static struct snd_pcm_hardware snd_imx_hardware = { 203117bb31SNicolin Chen .info = SNDRV_PCM_INFO_INTERLEAVED | 213117bb31SNicolin Chen SNDRV_PCM_INFO_BLOCK_TRANSFER | 223117bb31SNicolin Chen SNDRV_PCM_INFO_MMAP | 23703df441SShengjiu Wang SNDRV_PCM_INFO_MMAP_VALID, 243117bb31SNicolin Chen .buffer_bytes_max = FSL_ASRC_DMABUF_SIZE, 253117bb31SNicolin Chen .period_bytes_min = 128, 263117bb31SNicolin Chen .period_bytes_max = 65535, /* Limited by SDMA engine */ 273117bb31SNicolin Chen .periods_min = 2, 283117bb31SNicolin Chen .periods_max = 255, 293117bb31SNicolin Chen .fifo_size = 0, 303117bb31SNicolin Chen }; 313117bb31SNicolin Chen 323117bb31SNicolin Chen static bool filter(struct dma_chan *chan, void *param) 333117bb31SNicolin Chen { 343117bb31SNicolin Chen if (!imx_dma_is_general_purpose(chan)) 353117bb31SNicolin Chen return false; 363117bb31SNicolin Chen 373117bb31SNicolin Chen chan->private = param; 383117bb31SNicolin Chen 393117bb31SNicolin Chen return true; 403117bb31SNicolin Chen } 413117bb31SNicolin Chen 423117bb31SNicolin Chen static void fsl_asrc_dma_complete(void *arg) 433117bb31SNicolin Chen { 443117bb31SNicolin Chen struct snd_pcm_substream *substream = arg; 453117bb31SNicolin Chen struct snd_pcm_runtime *runtime = substream->runtime; 463117bb31SNicolin Chen struct fsl_asrc_pair *pair = runtime->private_data; 473117bb31SNicolin Chen 483117bb31SNicolin Chen pair->pos += snd_pcm_lib_period_bytes(substream); 493117bb31SNicolin Chen if (pair->pos >= snd_pcm_lib_buffer_bytes(substream)) 503117bb31SNicolin Chen pair->pos = 0; 513117bb31SNicolin Chen 523117bb31SNicolin Chen snd_pcm_period_elapsed(substream); 533117bb31SNicolin Chen } 543117bb31SNicolin Chen 558903ed25SKuninori Morimoto static int fsl_asrc_dma_prepare_and_submit(struct snd_pcm_substream *substream, 568903ed25SKuninori Morimoto struct snd_soc_component *component) 573117bb31SNicolin Chen { 583117bb31SNicolin Chen u8 dir = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? OUT : IN; 593117bb31SNicolin Chen struct snd_pcm_runtime *runtime = substream->runtime; 603117bb31SNicolin Chen struct fsl_asrc_pair *pair = runtime->private_data; 617f110661SKuninori Morimoto struct device *dev = component->dev; 623117bb31SNicolin Chen unsigned long flags = DMA_CTRL_ACK; 633117bb31SNicolin Chen 643117bb31SNicolin Chen /* Prepare and submit Front-End DMA channel */ 653117bb31SNicolin Chen if (!substream->runtime->no_period_wakeup) 663117bb31SNicolin Chen flags |= DMA_PREP_INTERRUPT; 673117bb31SNicolin Chen 683117bb31SNicolin Chen pair->pos = 0; 693117bb31SNicolin Chen pair->desc[!dir] = dmaengine_prep_dma_cyclic( 703117bb31SNicolin Chen pair->dma_chan[!dir], runtime->dma_addr, 713117bb31SNicolin Chen snd_pcm_lib_buffer_bytes(substream), 723117bb31SNicolin Chen snd_pcm_lib_period_bytes(substream), 7324dbd9edSStefan Agner dir == OUT ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM, flags); 743117bb31SNicolin Chen if (!pair->desc[!dir]) { 753117bb31SNicolin Chen dev_err(dev, "failed to prepare slave DMA for Front-End\n"); 763117bb31SNicolin Chen return -ENOMEM; 773117bb31SNicolin Chen } 783117bb31SNicolin Chen 793117bb31SNicolin Chen pair->desc[!dir]->callback = fsl_asrc_dma_complete; 803117bb31SNicolin Chen pair->desc[!dir]->callback_param = substream; 813117bb31SNicolin Chen 823117bb31SNicolin Chen dmaengine_submit(pair->desc[!dir]); 833117bb31SNicolin Chen 843117bb31SNicolin Chen /* Prepare and submit Back-End DMA channel */ 853117bb31SNicolin Chen pair->desc[dir] = dmaengine_prep_dma_cyclic( 863117bb31SNicolin Chen pair->dma_chan[dir], 0xffff, 64, 64, DMA_DEV_TO_DEV, 0); 873117bb31SNicolin Chen if (!pair->desc[dir]) { 883117bb31SNicolin Chen dev_err(dev, "failed to prepare slave DMA for Back-End\n"); 893117bb31SNicolin Chen return -ENOMEM; 903117bb31SNicolin Chen } 913117bb31SNicolin Chen 923117bb31SNicolin Chen dmaengine_submit(pair->desc[dir]); 933117bb31SNicolin Chen 943117bb31SNicolin Chen return 0; 953117bb31SNicolin Chen } 963117bb31SNicolin Chen 978903ed25SKuninori Morimoto static int fsl_asrc_dma_trigger(struct snd_soc_component *component, 988903ed25SKuninori Morimoto struct snd_pcm_substream *substream, int cmd) 993117bb31SNicolin Chen { 1003117bb31SNicolin Chen struct snd_pcm_runtime *runtime = substream->runtime; 1013117bb31SNicolin Chen struct fsl_asrc_pair *pair = runtime->private_data; 1023117bb31SNicolin Chen int ret; 1033117bb31SNicolin Chen 1043117bb31SNicolin Chen switch (cmd) { 1053117bb31SNicolin Chen case SNDRV_PCM_TRIGGER_START: 1063117bb31SNicolin Chen case SNDRV_PCM_TRIGGER_RESUME: 1073117bb31SNicolin Chen case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1088903ed25SKuninori Morimoto ret = fsl_asrc_dma_prepare_and_submit(substream, component); 1093117bb31SNicolin Chen if (ret) 1103117bb31SNicolin Chen return ret; 1113117bb31SNicolin Chen dma_async_issue_pending(pair->dma_chan[IN]); 1123117bb31SNicolin Chen dma_async_issue_pending(pair->dma_chan[OUT]); 1133117bb31SNicolin Chen break; 1143117bb31SNicolin Chen case SNDRV_PCM_TRIGGER_STOP: 1153117bb31SNicolin Chen case SNDRV_PCM_TRIGGER_SUSPEND: 1163117bb31SNicolin Chen case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1173117bb31SNicolin Chen dmaengine_terminate_all(pair->dma_chan[OUT]); 1183117bb31SNicolin Chen dmaengine_terminate_all(pair->dma_chan[IN]); 1193117bb31SNicolin Chen break; 1203117bb31SNicolin Chen default: 1213117bb31SNicolin Chen return -EINVAL; 1223117bb31SNicolin Chen } 1233117bb31SNicolin Chen 1243117bb31SNicolin Chen return 0; 1253117bb31SNicolin Chen } 1263117bb31SNicolin Chen 1278903ed25SKuninori Morimoto static int fsl_asrc_dma_hw_params(struct snd_soc_component *component, 1288903ed25SKuninori Morimoto struct snd_pcm_substream *substream, 1293117bb31SNicolin Chen struct snd_pcm_hw_params *params) 1303117bb31SNicolin Chen { 1313117bb31SNicolin Chen enum dma_slave_buswidth buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES; 1323117bb31SNicolin Chen struct snd_soc_pcm_runtime *rtd = substream->private_data; 1333117bb31SNicolin Chen bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 1343117bb31SNicolin Chen struct snd_dmaengine_dai_dma_data *dma_params_fe = NULL; 1353117bb31SNicolin Chen struct snd_dmaengine_dai_dma_data *dma_params_be = NULL; 1363117bb31SNicolin Chen struct snd_pcm_runtime *runtime = substream->runtime; 1373117bb31SNicolin Chen struct fsl_asrc_pair *pair = runtime->private_data; 1383117bb31SNicolin Chen struct fsl_asrc *asrc_priv = pair->asrc_priv; 1393117bb31SNicolin Chen struct dma_slave_config config_fe, config_be; 1403117bb31SNicolin Chen enum asrc_pair_index index = pair->index; 1417f110661SKuninori Morimoto struct device *dev = component->dev; 1423117bb31SNicolin Chen int stream = substream->stream; 1433117bb31SNicolin Chen struct imx_dma_data *tmp_data; 1443117bb31SNicolin Chen struct snd_soc_dpcm *dpcm; 1453117bb31SNicolin Chen struct dma_chan *tmp_chan; 1463117bb31SNicolin Chen struct device *dev_be; 1473117bb31SNicolin Chen u8 dir = tx ? OUT : IN; 1483117bb31SNicolin Chen dma_cap_mask_t mask; 1493117bb31SNicolin Chen int ret; 1503117bb31SNicolin Chen 1513117bb31SNicolin Chen /* Fetch the Back-End dma_data from DPCM */ 1528d6258a4SKuninori Morimoto for_each_dpcm_be(rtd, stream, dpcm) { 1533117bb31SNicolin Chen struct snd_soc_pcm_runtime *be = dpcm->be; 1543117bb31SNicolin Chen struct snd_pcm_substream *substream_be; 1553117bb31SNicolin Chen struct snd_soc_dai *dai = be->cpu_dai; 1563117bb31SNicolin Chen 1573117bb31SNicolin Chen if (dpcm->fe != rtd) 1583117bb31SNicolin Chen continue; 1593117bb31SNicolin Chen 1603117bb31SNicolin Chen substream_be = snd_soc_dpcm_get_substream(be, stream); 1613117bb31SNicolin Chen dma_params_be = snd_soc_dai_get_dma_data(dai, substream_be); 1623117bb31SNicolin Chen dev_be = dai->dev; 1633117bb31SNicolin Chen break; 1643117bb31SNicolin Chen } 1653117bb31SNicolin Chen 1663117bb31SNicolin Chen if (!dma_params_be) { 1673117bb31SNicolin Chen dev_err(dev, "failed to get the substream of Back-End\n"); 1683117bb31SNicolin Chen return -EINVAL; 1693117bb31SNicolin Chen } 1703117bb31SNicolin Chen 1713117bb31SNicolin Chen /* Override dma_data of the Front-End and config its dmaengine */ 1723117bb31SNicolin Chen dma_params_fe = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); 1733117bb31SNicolin Chen dma_params_fe->addr = asrc_priv->paddr + REG_ASRDx(!dir, index); 1743117bb31SNicolin Chen dma_params_fe->maxburst = dma_params_be->maxburst; 1753117bb31SNicolin Chen 1763117bb31SNicolin Chen pair->dma_chan[!dir] = fsl_asrc_get_dma_channel(pair, !dir); 1773117bb31SNicolin Chen if (!pair->dma_chan[!dir]) { 1783117bb31SNicolin Chen dev_err(dev, "failed to request DMA channel\n"); 1793117bb31SNicolin Chen return -EINVAL; 1803117bb31SNicolin Chen } 1813117bb31SNicolin Chen 1823117bb31SNicolin Chen memset(&config_fe, 0, sizeof(config_fe)); 1833117bb31SNicolin Chen ret = snd_dmaengine_pcm_prepare_slave_config(substream, params, &config_fe); 1843117bb31SNicolin Chen if (ret) { 1853117bb31SNicolin Chen dev_err(dev, "failed to prepare DMA config for Front-End\n"); 1863117bb31SNicolin Chen return ret; 1873117bb31SNicolin Chen } 1883117bb31SNicolin Chen 1893117bb31SNicolin Chen ret = dmaengine_slave_config(pair->dma_chan[!dir], &config_fe); 1903117bb31SNicolin Chen if (ret) { 1913117bb31SNicolin Chen dev_err(dev, "failed to config DMA channel for Front-End\n"); 1923117bb31SNicolin Chen return ret; 1933117bb31SNicolin Chen } 1943117bb31SNicolin Chen 1953117bb31SNicolin Chen /* Request and config DMA channel for Back-End */ 1963117bb31SNicolin Chen dma_cap_zero(mask); 1973117bb31SNicolin Chen dma_cap_set(DMA_SLAVE, mask); 1983117bb31SNicolin Chen dma_cap_set(DMA_CYCLIC, mask); 1993117bb31SNicolin Chen 200c05f10f2SShengjiu Wang /* 201c05f10f2SShengjiu Wang * An EDMA DEV_TO_DEV channel is fixed and bound with DMA event of each 202c05f10f2SShengjiu Wang * peripheral, unlike SDMA channel that is allocated dynamically. So no 203c05f10f2SShengjiu Wang * need to configure dma_request and dma_request2, but get dma_chan via 204c05f10f2SShengjiu Wang * dma_request_slave_channel directly with dma name of Front-End device 205c05f10f2SShengjiu Wang */ 206c05f10f2SShengjiu Wang if (!asrc_priv->soc->use_edma) { 2073117bb31SNicolin Chen /* Get DMA request of Back-End */ 2083117bb31SNicolin Chen tmp_chan = dma_request_slave_channel(dev_be, tx ? "tx" : "rx"); 2093117bb31SNicolin Chen tmp_data = tmp_chan->private; 2103117bb31SNicolin Chen pair->dma_data.dma_request = tmp_data->dma_request; 2113117bb31SNicolin Chen dma_release_channel(tmp_chan); 2123117bb31SNicolin Chen 2133117bb31SNicolin Chen /* Get DMA request of Front-End */ 2143117bb31SNicolin Chen tmp_chan = fsl_asrc_get_dma_channel(pair, dir); 2153117bb31SNicolin Chen tmp_data = tmp_chan->private; 2163117bb31SNicolin Chen pair->dma_data.dma_request2 = tmp_data->dma_request; 2173117bb31SNicolin Chen pair->dma_data.peripheral_type = tmp_data->peripheral_type; 2183117bb31SNicolin Chen pair->dma_data.priority = tmp_data->priority; 2193117bb31SNicolin Chen dma_release_channel(tmp_chan); 2203117bb31SNicolin Chen 221c05f10f2SShengjiu Wang pair->dma_chan[dir] = 222c05f10f2SShengjiu Wang dma_request_channel(mask, filter, &pair->dma_data); 223c05f10f2SShengjiu Wang } else { 224c05f10f2SShengjiu Wang pair->dma_chan[dir] = 225c05f10f2SShengjiu Wang fsl_asrc_get_dma_channel(pair, dir); 226c05f10f2SShengjiu Wang } 227c05f10f2SShengjiu Wang 2283117bb31SNicolin Chen if (!pair->dma_chan[dir]) { 2293117bb31SNicolin Chen dev_err(dev, "failed to request DMA channel for Back-End\n"); 2303117bb31SNicolin Chen return -EINVAL; 2313117bb31SNicolin Chen } 2323117bb31SNicolin Chen 2333117bb31SNicolin Chen if (asrc_priv->asrc_width == 16) 2343117bb31SNicolin Chen buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES; 2353117bb31SNicolin Chen else 2363117bb31SNicolin Chen buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES; 2373117bb31SNicolin Chen 2383117bb31SNicolin Chen config_be.direction = DMA_DEV_TO_DEV; 2393117bb31SNicolin Chen config_be.src_addr_width = buswidth; 2403117bb31SNicolin Chen config_be.src_maxburst = dma_params_be->maxburst; 2413117bb31SNicolin Chen config_be.dst_addr_width = buswidth; 2423117bb31SNicolin Chen config_be.dst_maxburst = dma_params_be->maxburst; 2433117bb31SNicolin Chen 2443117bb31SNicolin Chen if (tx) { 2453117bb31SNicolin Chen config_be.src_addr = asrc_priv->paddr + REG_ASRDO(index); 2463117bb31SNicolin Chen config_be.dst_addr = dma_params_be->addr; 2473117bb31SNicolin Chen } else { 2483117bb31SNicolin Chen config_be.dst_addr = asrc_priv->paddr + REG_ASRDI(index); 2493117bb31SNicolin Chen config_be.src_addr = dma_params_be->addr; 2503117bb31SNicolin Chen } 2513117bb31SNicolin Chen 2523117bb31SNicolin Chen ret = dmaengine_slave_config(pair->dma_chan[dir], &config_be); 2533117bb31SNicolin Chen if (ret) { 2543117bb31SNicolin Chen dev_err(dev, "failed to config DMA channel for Back-End\n"); 2553117bb31SNicolin Chen return ret; 2563117bb31SNicolin Chen } 2573117bb31SNicolin Chen 2583117bb31SNicolin Chen snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); 2593117bb31SNicolin Chen 2603117bb31SNicolin Chen return 0; 2613117bb31SNicolin Chen } 2623117bb31SNicolin Chen 2638903ed25SKuninori Morimoto static int fsl_asrc_dma_hw_free(struct snd_soc_component *component, 2648903ed25SKuninori Morimoto struct snd_pcm_substream *substream) 2653117bb31SNicolin Chen { 2663117bb31SNicolin Chen struct snd_pcm_runtime *runtime = substream->runtime; 2673117bb31SNicolin Chen struct fsl_asrc_pair *pair = runtime->private_data; 2683117bb31SNicolin Chen 2693117bb31SNicolin Chen snd_pcm_set_runtime_buffer(substream, NULL); 2703117bb31SNicolin Chen 2713117bb31SNicolin Chen if (pair->dma_chan[IN]) 2723117bb31SNicolin Chen dma_release_channel(pair->dma_chan[IN]); 2733117bb31SNicolin Chen 2743117bb31SNicolin Chen if (pair->dma_chan[OUT]) 2753117bb31SNicolin Chen dma_release_channel(pair->dma_chan[OUT]); 2763117bb31SNicolin Chen 2773117bb31SNicolin Chen pair->dma_chan[IN] = NULL; 2783117bb31SNicolin Chen pair->dma_chan[OUT] = NULL; 2793117bb31SNicolin Chen 2803117bb31SNicolin Chen return 0; 2813117bb31SNicolin Chen } 2823117bb31SNicolin Chen 2838903ed25SKuninori Morimoto static int fsl_asrc_dma_startup(struct snd_soc_component *component, 2848903ed25SKuninori Morimoto struct snd_pcm_substream *substream) 2853117bb31SNicolin Chen { 286703df441SShengjiu Wang bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 2873117bb31SNicolin Chen struct snd_soc_pcm_runtime *rtd = substream->private_data; 2883117bb31SNicolin Chen struct snd_pcm_runtime *runtime = substream->runtime; 289703df441SShengjiu Wang struct snd_dmaengine_dai_dma_data *dma_data; 2907f110661SKuninori Morimoto struct device *dev = component->dev; 2913117bb31SNicolin Chen struct fsl_asrc *asrc_priv = dev_get_drvdata(dev); 2923117bb31SNicolin Chen struct fsl_asrc_pair *pair; 293703df441SShengjiu Wang struct dma_chan *tmp_chan = NULL; 294703df441SShengjiu Wang u8 dir = tx ? OUT : IN; 295703df441SShengjiu Wang bool release_pair = true; 296703df441SShengjiu Wang int ret = 0; 297703df441SShengjiu Wang 298703df441SShengjiu Wang ret = snd_pcm_hw_constraint_integer(substream->runtime, 299703df441SShengjiu Wang SNDRV_PCM_HW_PARAM_PERIODS); 300703df441SShengjiu Wang if (ret < 0) { 301703df441SShengjiu Wang dev_err(dev, "failed to set pcm hw params periods\n"); 302703df441SShengjiu Wang return ret; 303703df441SShengjiu Wang } 3043117bb31SNicolin Chen 3053117bb31SNicolin Chen pair = kzalloc(sizeof(struct fsl_asrc_pair), GFP_KERNEL); 306443be77eSMarkus Elfring if (!pair) 3073117bb31SNicolin Chen return -ENOMEM; 3083117bb31SNicolin Chen 3093117bb31SNicolin Chen pair->asrc_priv = asrc_priv; 3103117bb31SNicolin Chen 3113117bb31SNicolin Chen runtime->private_data = pair; 3123117bb31SNicolin Chen 313703df441SShengjiu Wang /* Request a dummy pair, which will be released later. 314703df441SShengjiu Wang * Request pair function needs channel num as input, for this 315703df441SShengjiu Wang * dummy pair, we just request "1" channel temporarily. 316703df441SShengjiu Wang */ 317703df441SShengjiu Wang ret = fsl_asrc_request_pair(1, pair); 318703df441SShengjiu Wang if (ret < 0) { 319703df441SShengjiu Wang dev_err(dev, "failed to request asrc pair\n"); 320703df441SShengjiu Wang goto req_pair_err; 321703df441SShengjiu Wang } 322703df441SShengjiu Wang 323703df441SShengjiu Wang /* Request a dummy dma channel, which will be released later. */ 324703df441SShengjiu Wang tmp_chan = fsl_asrc_get_dma_channel(pair, dir); 325703df441SShengjiu Wang if (!tmp_chan) { 326703df441SShengjiu Wang dev_err(dev, "failed to get dma channel\n"); 327703df441SShengjiu Wang ret = -EINVAL; 328703df441SShengjiu Wang goto dma_chan_err; 329703df441SShengjiu Wang } 330703df441SShengjiu Wang 331703df441SShengjiu Wang dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); 332703df441SShengjiu Wang 333703df441SShengjiu Wang /* Refine the snd_imx_hardware according to caps of DMA. */ 334703df441SShengjiu Wang ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream, 335703df441SShengjiu Wang dma_data, 336703df441SShengjiu Wang &snd_imx_hardware, 337703df441SShengjiu Wang tmp_chan); 338703df441SShengjiu Wang if (ret < 0) { 339703df441SShengjiu Wang dev_err(dev, "failed to refine runtime hwparams\n"); 340703df441SShengjiu Wang goto out; 341703df441SShengjiu Wang } 342703df441SShengjiu Wang 343703df441SShengjiu Wang release_pair = false; 3443117bb31SNicolin Chen snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware); 3453117bb31SNicolin Chen 346703df441SShengjiu Wang out: 347703df441SShengjiu Wang dma_release_channel(tmp_chan); 348703df441SShengjiu Wang 349703df441SShengjiu Wang dma_chan_err: 350703df441SShengjiu Wang fsl_asrc_release_pair(pair); 351703df441SShengjiu Wang 352703df441SShengjiu Wang req_pair_err: 353703df441SShengjiu Wang if (release_pair) 354703df441SShengjiu Wang kfree(pair); 355703df441SShengjiu Wang 356703df441SShengjiu Wang return ret; 3573117bb31SNicolin Chen } 3583117bb31SNicolin Chen 3598903ed25SKuninori Morimoto static int fsl_asrc_dma_shutdown(struct snd_soc_component *component, 3608903ed25SKuninori Morimoto struct snd_pcm_substream *substream) 3613117bb31SNicolin Chen { 3623117bb31SNicolin Chen struct snd_pcm_runtime *runtime = substream->runtime; 3633117bb31SNicolin Chen struct fsl_asrc_pair *pair = runtime->private_data; 3646ccf62c7SNicolin Chen struct fsl_asrc *asrc_priv; 3653117bb31SNicolin Chen 3666ccf62c7SNicolin Chen if (!pair) 3676ccf62c7SNicolin Chen return 0; 3686ccf62c7SNicolin Chen 3696ccf62c7SNicolin Chen asrc_priv = pair->asrc_priv; 3706ccf62c7SNicolin Chen 3716ccf62c7SNicolin Chen if (asrc_priv->pair[pair->index] == pair) 3723117bb31SNicolin Chen asrc_priv->pair[pair->index] = NULL; 3733117bb31SNicolin Chen 3743117bb31SNicolin Chen kfree(pair); 3753117bb31SNicolin Chen 3763117bb31SNicolin Chen return 0; 3773117bb31SNicolin Chen } 3783117bb31SNicolin Chen 3798903ed25SKuninori Morimoto static snd_pcm_uframes_t 3808903ed25SKuninori Morimoto fsl_asrc_dma_pcm_pointer(struct snd_soc_component *component, 3818903ed25SKuninori Morimoto struct snd_pcm_substream *substream) 3823117bb31SNicolin Chen { 3833117bb31SNicolin Chen struct snd_pcm_runtime *runtime = substream->runtime; 3843117bb31SNicolin Chen struct fsl_asrc_pair *pair = runtime->private_data; 3853117bb31SNicolin Chen 3863117bb31SNicolin Chen return bytes_to_frames(substream->runtime, pair->pos); 3873117bb31SNicolin Chen } 3883117bb31SNicolin Chen 3898903ed25SKuninori Morimoto static int fsl_asrc_dma_pcm_new(struct snd_soc_component *component, 3908903ed25SKuninori Morimoto struct snd_soc_pcm_runtime *rtd) 3913117bb31SNicolin Chen { 3923117bb31SNicolin Chen struct snd_card *card = rtd->card->snd_card; 3933117bb31SNicolin Chen struct snd_pcm_substream *substream; 3943117bb31SNicolin Chen struct snd_pcm *pcm = rtd->pcm; 3953117bb31SNicolin Chen int ret, i; 3963117bb31SNicolin Chen 3973117bb31SNicolin Chen ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32)); 3983117bb31SNicolin Chen if (ret) { 3993117bb31SNicolin Chen dev_err(card->dev, "failed to set DMA mask\n"); 4003117bb31SNicolin Chen return ret; 4013117bb31SNicolin Chen } 4023117bb31SNicolin Chen 403fa7b2a1fSKuninori Morimoto for_each_pcm_streams(i) { 4043117bb31SNicolin Chen substream = pcm->streams[i].substream; 4053117bb31SNicolin Chen if (!substream) 4063117bb31SNicolin Chen continue; 4073117bb31SNicolin Chen 4083117bb31SNicolin Chen ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev, 4093117bb31SNicolin Chen FSL_ASRC_DMABUF_SIZE, &substream->dma_buffer); 4103117bb31SNicolin Chen if (ret) { 4113117bb31SNicolin Chen dev_err(card->dev, "failed to allocate DMA buffer\n"); 4123117bb31SNicolin Chen goto err; 4133117bb31SNicolin Chen } 4143117bb31SNicolin Chen } 4153117bb31SNicolin Chen 4163117bb31SNicolin Chen return 0; 4173117bb31SNicolin Chen 4183117bb31SNicolin Chen err: 4193117bb31SNicolin Chen if (--i == 0 && pcm->streams[i].substream) 4203117bb31SNicolin Chen snd_dma_free_pages(&pcm->streams[i].substream->dma_buffer); 4213117bb31SNicolin Chen 4223117bb31SNicolin Chen return ret; 4233117bb31SNicolin Chen } 4243117bb31SNicolin Chen 4258903ed25SKuninori Morimoto static void fsl_asrc_dma_pcm_free(struct snd_soc_component *component, 4268903ed25SKuninori Morimoto struct snd_pcm *pcm) 4273117bb31SNicolin Chen { 4283117bb31SNicolin Chen struct snd_pcm_substream *substream; 4293117bb31SNicolin Chen int i; 4303117bb31SNicolin Chen 431fa7b2a1fSKuninori Morimoto for_each_pcm_streams(i) { 4323117bb31SNicolin Chen substream = pcm->streams[i].substream; 4333117bb31SNicolin Chen if (!substream) 4343117bb31SNicolin Chen continue; 4353117bb31SNicolin Chen 4363117bb31SNicolin Chen snd_dma_free_pages(&substream->dma_buffer); 4373117bb31SNicolin Chen substream->dma_buffer.area = NULL; 4383117bb31SNicolin Chen substream->dma_buffer.addr = 0; 4393117bb31SNicolin Chen } 4403117bb31SNicolin Chen } 4413117bb31SNicolin Chen 4427f110661SKuninori Morimoto struct snd_soc_component_driver fsl_asrc_component = { 4437f110661SKuninori Morimoto .name = DRV_NAME, 4448903ed25SKuninori Morimoto .hw_params = fsl_asrc_dma_hw_params, 4458903ed25SKuninori Morimoto .hw_free = fsl_asrc_dma_hw_free, 4468903ed25SKuninori Morimoto .trigger = fsl_asrc_dma_trigger, 4478903ed25SKuninori Morimoto .open = fsl_asrc_dma_startup, 4488903ed25SKuninori Morimoto .close = fsl_asrc_dma_shutdown, 4498903ed25SKuninori Morimoto .pointer = fsl_asrc_dma_pcm_pointer, 4508903ed25SKuninori Morimoto .pcm_construct = fsl_asrc_dma_pcm_new, 4518903ed25SKuninori Morimoto .pcm_destruct = fsl_asrc_dma_pcm_free, 4523117bb31SNicolin Chen }; 4537f110661SKuninori Morimoto EXPORT_SYMBOL_GPL(fsl_asrc_component); 454