Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Freescale ASRC ALSA SoC Platform (DMA) driver
0004 //
0005 // Copyright (C) 2014 Freescale Semiconductor, Inc.
0006 //
0007 // Author: Nicolin Chen <nicoleotsuka@gmail.com>
0008 
0009 #include <linux/dma-mapping.h>
0010 #include <linux/module.h>
0011 #include <linux/dma/imx-dma.h>
0012 #include <sound/dmaengine_pcm.h>
0013 #include <sound/pcm_params.h>
0014 
0015 #include "fsl_asrc_common.h"
0016 
0017 #define FSL_ASRC_DMABUF_SIZE    (256 * 1024)
0018 
0019 static struct snd_pcm_hardware snd_imx_hardware = {
0020     .info = SNDRV_PCM_INFO_INTERLEAVED |
0021         SNDRV_PCM_INFO_BLOCK_TRANSFER |
0022         SNDRV_PCM_INFO_MMAP |
0023         SNDRV_PCM_INFO_MMAP_VALID,
0024     .buffer_bytes_max = FSL_ASRC_DMABUF_SIZE,
0025     .period_bytes_min = 128,
0026     .period_bytes_max = 65535, /* Limited by SDMA engine */
0027     .periods_min = 2,
0028     .periods_max = 255,
0029     .fifo_size = 0,
0030 };
0031 
0032 static bool filter(struct dma_chan *chan, void *param)
0033 {
0034     if (!imx_dma_is_general_purpose(chan))
0035         return false;
0036 
0037     chan->private = param;
0038 
0039     return true;
0040 }
0041 
0042 static void fsl_asrc_dma_complete(void *arg)
0043 {
0044     struct snd_pcm_substream *substream = arg;
0045     struct snd_pcm_runtime *runtime = substream->runtime;
0046     struct fsl_asrc_pair *pair = runtime->private_data;
0047 
0048     pair->pos += snd_pcm_lib_period_bytes(substream);
0049     if (pair->pos >= snd_pcm_lib_buffer_bytes(substream))
0050         pair->pos = 0;
0051 
0052     snd_pcm_period_elapsed(substream);
0053 }
0054 
0055 static int fsl_asrc_dma_prepare_and_submit(struct snd_pcm_substream *substream,
0056                        struct snd_soc_component *component)
0057 {
0058     u8 dir = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? OUT : IN;
0059     struct snd_pcm_runtime *runtime = substream->runtime;
0060     struct fsl_asrc_pair *pair = runtime->private_data;
0061     struct device *dev = component->dev;
0062     unsigned long flags = DMA_CTRL_ACK;
0063 
0064     /* Prepare and submit Front-End DMA channel */
0065     if (!substream->runtime->no_period_wakeup)
0066         flags |= DMA_PREP_INTERRUPT;
0067 
0068     pair->pos = 0;
0069     pair->desc[!dir] = dmaengine_prep_dma_cyclic(
0070             pair->dma_chan[!dir], runtime->dma_addr,
0071             snd_pcm_lib_buffer_bytes(substream),
0072             snd_pcm_lib_period_bytes(substream),
0073             dir == OUT ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM, flags);
0074     if (!pair->desc[!dir]) {
0075         dev_err(dev, "failed to prepare slave DMA for Front-End\n");
0076         return -ENOMEM;
0077     }
0078 
0079     pair->desc[!dir]->callback = fsl_asrc_dma_complete;
0080     pair->desc[!dir]->callback_param = substream;
0081 
0082     dmaengine_submit(pair->desc[!dir]);
0083 
0084     /* Prepare and submit Back-End DMA channel */
0085     pair->desc[dir] = dmaengine_prep_dma_cyclic(
0086             pair->dma_chan[dir], 0xffff, 64, 64, DMA_DEV_TO_DEV, 0);
0087     if (!pair->desc[dir]) {
0088         dev_err(dev, "failed to prepare slave DMA for Back-End\n");
0089         return -ENOMEM;
0090     }
0091 
0092     dmaengine_submit(pair->desc[dir]);
0093 
0094     return 0;
0095 }
0096 
0097 static int fsl_asrc_dma_trigger(struct snd_soc_component *component,
0098                 struct snd_pcm_substream *substream, int cmd)
0099 {
0100     struct snd_pcm_runtime *runtime = substream->runtime;
0101     struct fsl_asrc_pair *pair = runtime->private_data;
0102     int ret;
0103 
0104     switch (cmd) {
0105     case SNDRV_PCM_TRIGGER_START:
0106     case SNDRV_PCM_TRIGGER_RESUME:
0107     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0108         ret = fsl_asrc_dma_prepare_and_submit(substream, component);
0109         if (ret)
0110             return ret;
0111         dma_async_issue_pending(pair->dma_chan[IN]);
0112         dma_async_issue_pending(pair->dma_chan[OUT]);
0113         break;
0114     case SNDRV_PCM_TRIGGER_STOP:
0115     case SNDRV_PCM_TRIGGER_SUSPEND:
0116     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0117         dmaengine_terminate_async(pair->dma_chan[OUT]);
0118         dmaengine_terminate_async(pair->dma_chan[IN]);
0119         break;
0120     default:
0121         return -EINVAL;
0122     }
0123 
0124     return 0;
0125 }
0126 
0127 static int fsl_asrc_dma_hw_params(struct snd_soc_component *component,
0128                   struct snd_pcm_substream *substream,
0129                   struct snd_pcm_hw_params *params)
0130 {
0131     enum dma_slave_buswidth buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
0132     enum sdma_peripheral_type be_peripheral_type = IMX_DMATYPE_SSI;
0133     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0134     bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
0135     struct snd_dmaengine_dai_dma_data *dma_params_fe = NULL;
0136     struct snd_dmaengine_dai_dma_data *dma_params_be = NULL;
0137     struct snd_pcm_runtime *runtime = substream->runtime;
0138     struct fsl_asrc_pair *pair = runtime->private_data;
0139     struct dma_chan *tmp_chan = NULL, *be_chan = NULL;
0140     struct snd_soc_component *component_be = NULL;
0141     struct fsl_asrc *asrc = pair->asrc;
0142     struct dma_slave_config config_fe, config_be;
0143     struct sdma_peripheral_config audio_config;
0144     enum asrc_pair_index index = pair->index;
0145     struct device *dev = component->dev;
0146     struct device_node *of_dma_node;
0147     int stream = substream->stream;
0148     struct imx_dma_data *tmp_data;
0149     struct snd_soc_dpcm *dpcm;
0150     struct device *dev_be;
0151     u8 dir = tx ? OUT : IN;
0152     dma_cap_mask_t mask;
0153     int ret, width;
0154 
0155     /* Fetch the Back-End dma_data from DPCM */
0156     for_each_dpcm_be(rtd, stream, dpcm) {
0157         struct snd_soc_pcm_runtime *be = dpcm->be;
0158         struct snd_pcm_substream *substream_be;
0159         struct snd_soc_dai *dai = asoc_rtd_to_cpu(be, 0);
0160 
0161         if (dpcm->fe != rtd)
0162             continue;
0163 
0164         substream_be = snd_soc_dpcm_get_substream(be, stream);
0165         dma_params_be = snd_soc_dai_get_dma_data(dai, substream_be);
0166         dev_be = dai->dev;
0167         break;
0168     }
0169 
0170     if (!dma_params_be) {
0171         dev_err(dev, "failed to get the substream of Back-End\n");
0172         return -EINVAL;
0173     }
0174 
0175     /* Override dma_data of the Front-End and config its dmaengine */
0176     dma_params_fe = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
0177     dma_params_fe->addr = asrc->paddr + asrc->get_fifo_addr(!dir, index);
0178     dma_params_fe->maxburst = dma_params_be->maxburst;
0179 
0180     pair->dma_chan[!dir] = asrc->get_dma_channel(pair, !dir);
0181     if (!pair->dma_chan[!dir]) {
0182         dev_err(dev, "failed to request DMA channel\n");
0183         return -EINVAL;
0184     }
0185 
0186     memset(&config_fe, 0, sizeof(config_fe));
0187     ret = snd_dmaengine_pcm_prepare_slave_config(substream, params, &config_fe);
0188     if (ret) {
0189         dev_err(dev, "failed to prepare DMA config for Front-End\n");
0190         return ret;
0191     }
0192 
0193     ret = dmaengine_slave_config(pair->dma_chan[!dir], &config_fe);
0194     if (ret) {
0195         dev_err(dev, "failed to config DMA channel for Front-End\n");
0196         return ret;
0197     }
0198 
0199     /* Request and config DMA channel for Back-End */
0200     dma_cap_zero(mask);
0201     dma_cap_set(DMA_SLAVE, mask);
0202     dma_cap_set(DMA_CYCLIC, mask);
0203 
0204     /*
0205      * The Back-End device might have already requested a DMA channel,
0206      * so try to reuse it first, and then request a new one upon NULL.
0207      */
0208     component_be = snd_soc_lookup_component_nolocked(dev_be, SND_DMAENGINE_PCM_DRV_NAME);
0209     if (component_be) {
0210         be_chan = soc_component_to_pcm(component_be)->chan[substream->stream];
0211         tmp_chan = be_chan;
0212     }
0213     if (!tmp_chan)
0214         tmp_chan = dma_request_slave_channel(dev_be, tx ? "tx" : "rx");
0215 
0216     /*
0217      * An EDMA DEV_TO_DEV channel is fixed and bound with DMA event of each
0218      * peripheral, unlike SDMA channel that is allocated dynamically. So no
0219      * need to configure dma_request and dma_request2, but get dma_chan of
0220      * Back-End device directly via dma_request_slave_channel.
0221      */
0222     if (!asrc->use_edma) {
0223         /* Get DMA request of Back-End */
0224         tmp_data = tmp_chan->private;
0225         pair->dma_data.dma_request = tmp_data->dma_request;
0226         be_peripheral_type = tmp_data->peripheral_type;
0227         if (!be_chan)
0228             dma_release_channel(tmp_chan);
0229 
0230         /* Get DMA request of Front-End */
0231         tmp_chan = asrc->get_dma_channel(pair, dir);
0232         tmp_data = tmp_chan->private;
0233         pair->dma_data.dma_request2 = tmp_data->dma_request;
0234         pair->dma_data.peripheral_type = tmp_data->peripheral_type;
0235         pair->dma_data.priority = tmp_data->priority;
0236         dma_release_channel(tmp_chan);
0237 
0238         of_dma_node = pair->dma_chan[!dir]->device->dev->of_node;
0239         pair->dma_chan[dir] =
0240             __dma_request_channel(&mask, filter, &pair->dma_data,
0241                           of_dma_node);
0242         pair->req_dma_chan = true;
0243     } else {
0244         pair->dma_chan[dir] = tmp_chan;
0245         /* Do not flag to release if we are reusing the Back-End one */
0246         pair->req_dma_chan = !be_chan;
0247     }
0248 
0249     if (!pair->dma_chan[dir]) {
0250         dev_err(dev, "failed to request DMA channel for Back-End\n");
0251         return -EINVAL;
0252     }
0253 
0254     width = snd_pcm_format_physical_width(asrc->asrc_format);
0255     if (width < 8 || width > 64)
0256         return -EINVAL;
0257     else if (width == 8)
0258         buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
0259     else if (width == 16)
0260         buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
0261     else if (width == 24)
0262         buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
0263     else if (width <= 32)
0264         buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
0265     else
0266         buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
0267 
0268     config_be.direction = DMA_DEV_TO_DEV;
0269     config_be.src_addr_width = buswidth;
0270     config_be.src_maxburst = dma_params_be->maxburst;
0271     config_be.dst_addr_width = buswidth;
0272     config_be.dst_maxburst = dma_params_be->maxburst;
0273 
0274     memset(&audio_config, 0, sizeof(audio_config));
0275     config_be.peripheral_config = &audio_config;
0276     config_be.peripheral_size  = sizeof(audio_config);
0277 
0278     if (tx && (be_peripheral_type == IMX_DMATYPE_SSI_DUAL ||
0279            be_peripheral_type == IMX_DMATYPE_SPDIF))
0280         audio_config.n_fifos_dst = 2;
0281     if (!tx && (be_peripheral_type == IMX_DMATYPE_SSI_DUAL ||
0282             be_peripheral_type == IMX_DMATYPE_SPDIF))
0283         audio_config.n_fifos_src = 2;
0284 
0285     if (tx) {
0286         config_be.src_addr = asrc->paddr + asrc->get_fifo_addr(OUT, index);
0287         config_be.dst_addr = dma_params_be->addr;
0288     } else {
0289         config_be.dst_addr = asrc->paddr + asrc->get_fifo_addr(IN, index);
0290         config_be.src_addr = dma_params_be->addr;
0291     }
0292 
0293     ret = dmaengine_slave_config(pair->dma_chan[dir], &config_be);
0294     if (ret) {
0295         dev_err(dev, "failed to config DMA channel for Back-End\n");
0296         if (pair->req_dma_chan)
0297             dma_release_channel(pair->dma_chan[dir]);
0298         return ret;
0299     }
0300 
0301     return 0;
0302 }
0303 
0304 static int fsl_asrc_dma_hw_free(struct snd_soc_component *component,
0305                 struct snd_pcm_substream *substream)
0306 {
0307     bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
0308     struct snd_pcm_runtime *runtime = substream->runtime;
0309     struct fsl_asrc_pair *pair = runtime->private_data;
0310     u8 dir = tx ? OUT : IN;
0311 
0312     if (pair->dma_chan[!dir])
0313         dma_release_channel(pair->dma_chan[!dir]);
0314 
0315     /* release dev_to_dev chan if we aren't reusing the Back-End one */
0316     if (pair->dma_chan[dir] && pair->req_dma_chan)
0317         dma_release_channel(pair->dma_chan[dir]);
0318 
0319     pair->dma_chan[!dir] = NULL;
0320     pair->dma_chan[dir] = NULL;
0321 
0322     return 0;
0323 }
0324 
0325 static int fsl_asrc_dma_startup(struct snd_soc_component *component,
0326                 struct snd_pcm_substream *substream)
0327 {
0328     bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
0329     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0330     struct snd_pcm_runtime *runtime = substream->runtime;
0331     struct snd_dmaengine_dai_dma_data *dma_data;
0332     struct device *dev = component->dev;
0333     struct fsl_asrc *asrc = dev_get_drvdata(dev);
0334     struct fsl_asrc_pair *pair;
0335     struct dma_chan *tmp_chan = NULL;
0336     u8 dir = tx ? OUT : IN;
0337     bool release_pair = true;
0338     int ret = 0;
0339 
0340     ret = snd_pcm_hw_constraint_integer(substream->runtime,
0341                         SNDRV_PCM_HW_PARAM_PERIODS);
0342     if (ret < 0) {
0343         dev_err(dev, "failed to set pcm hw params periods\n");
0344         return ret;
0345     }
0346 
0347     pair = kzalloc(sizeof(*pair) + asrc->pair_priv_size, GFP_KERNEL);
0348     if (!pair)
0349         return -ENOMEM;
0350 
0351     pair->asrc = asrc;
0352     pair->private = (void *)pair + sizeof(struct fsl_asrc_pair);
0353 
0354     runtime->private_data = pair;
0355 
0356     /* Request a dummy pair, which will be released later.
0357      * Request pair function needs channel num as input, for this
0358      * dummy pair, we just request "1" channel temporarily.
0359      */
0360     ret = asrc->request_pair(1, pair);
0361     if (ret < 0) {
0362         dev_err(dev, "failed to request asrc pair\n");
0363         goto req_pair_err;
0364     }
0365 
0366     /* Request a dummy dma channel, which will be released later. */
0367     tmp_chan = asrc->get_dma_channel(pair, dir);
0368     if (!tmp_chan) {
0369         dev_err(dev, "failed to get dma channel\n");
0370         ret = -EINVAL;
0371         goto dma_chan_err;
0372     }
0373 
0374     dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
0375 
0376     /* Refine the snd_imx_hardware according to caps of DMA. */
0377     ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream,
0378                             dma_data,
0379                             &snd_imx_hardware,
0380                             tmp_chan);
0381     if (ret < 0) {
0382         dev_err(dev, "failed to refine runtime hwparams\n");
0383         goto out;
0384     }
0385 
0386     release_pair = false;
0387     snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
0388 
0389 out:
0390     dma_release_channel(tmp_chan);
0391 
0392 dma_chan_err:
0393     asrc->release_pair(pair);
0394 
0395 req_pair_err:
0396     if (release_pair)
0397         kfree(pair);
0398 
0399     return ret;
0400 }
0401 
0402 static int fsl_asrc_dma_shutdown(struct snd_soc_component *component,
0403                  struct snd_pcm_substream *substream)
0404 {
0405     struct snd_pcm_runtime *runtime = substream->runtime;
0406     struct fsl_asrc_pair *pair = runtime->private_data;
0407     struct fsl_asrc *asrc;
0408 
0409     if (!pair)
0410         return 0;
0411 
0412     asrc = pair->asrc;
0413 
0414     if (asrc->pair[pair->index] == pair)
0415         asrc->pair[pair->index] = NULL;
0416 
0417     kfree(pair);
0418 
0419     return 0;
0420 }
0421 
0422 static snd_pcm_uframes_t
0423 fsl_asrc_dma_pcm_pointer(struct snd_soc_component *component,
0424              struct snd_pcm_substream *substream)
0425 {
0426     struct snd_pcm_runtime *runtime = substream->runtime;
0427     struct fsl_asrc_pair *pair = runtime->private_data;
0428 
0429     return bytes_to_frames(substream->runtime, pair->pos);
0430 }
0431 
0432 static int fsl_asrc_dma_pcm_new(struct snd_soc_component *component,
0433                 struct snd_soc_pcm_runtime *rtd)
0434 {
0435     struct snd_card *card = rtd->card->snd_card;
0436     struct snd_pcm *pcm = rtd->pcm;
0437     int ret;
0438 
0439     ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
0440     if (ret) {
0441         dev_err(card->dev, "failed to set DMA mask\n");
0442         return ret;
0443     }
0444 
0445     return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
0446                         card->dev, FSL_ASRC_DMABUF_SIZE);
0447 }
0448 
0449 struct snd_soc_component_driver fsl_asrc_component = {
0450     .name       = DRV_NAME,
0451     .hw_params  = fsl_asrc_dma_hw_params,
0452     .hw_free    = fsl_asrc_dma_hw_free,
0453     .trigger    = fsl_asrc_dma_trigger,
0454     .open       = fsl_asrc_dma_startup,
0455     .close      = fsl_asrc_dma_shutdown,
0456     .pointer    = fsl_asrc_dma_pcm_pointer,
0457     .pcm_construct  = fsl_asrc_dma_pcm_new,
0458     .legacy_dai_naming = 1,
0459 };
0460 EXPORT_SYMBOL_GPL(fsl_asrc_component);