From patchwork Fri Nov 23 06:14:17 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bo Shen X-Patchwork-Id: 1793851 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork2.kernel.org (Postfix) with ESMTP id 767C7DF23A for ; Fri, 23 Nov 2012 06:22:27 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1Tbmc4-0007Kl-6w; Fri, 23 Nov 2012 06:19:44 +0000 Received: from newsmtp5.atmel.com ([204.2.163.5] helo=sjogate2.atmel.com) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1TbmbR-0007GM-Dw for linux-arm-kernel@lists.infradead.org; Fri, 23 Nov 2012 06:19:10 +0000 Received: from shaarm01.corp.atmel.com ([10.217.6.34]) by sjogate2.atmel.com (8.13.6/8.13.6) with ESMTP id qAN6CYUO012858; Thu, 22 Nov 2012 22:13:23 -0800 (PST) From: Bo Shen To: broonie@opensource.wolfsonmicro.com, plagnioj@jcrosoft.com, nicolas.ferre@atmel.com Subject: [PATCH 3/3] ASoC: atmel-pcm: add dma support Date: Fri, 23 Nov 2012 14:14:17 +0800 Message-Id: <1353651257-2476-4-git-send-email-voice.shen@atmel.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1353651257-2476-1-git-send-email-voice.shen@atmel.com> References: <1353651257-2476-1-git-send-email-voice.shen@atmel.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20121123_011905_833394_5F8145E0 X-CRM114-Status: GOOD ( 18.61 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.7 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: Bo Shen , devicetree-discuss@lists.ozlabs.org, alsa-devel@alsa-project.org, linux-sound@vger.kernel.org, linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Add dmaengine specific routines and replace PDC ones in pcm_ops if appropriate. Signed-off-by: Nicolas Ferre [voice.shen@atmel.com: adapt to soc dmaengine framework] Signed-off-by: Bo Shen --- sound/soc/atmel/atmel-pcm.c | 215 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 201 insertions(+), 14 deletions(-) diff --git a/sound/soc/atmel/atmel-pcm.c b/sound/soc/atmel/atmel-pcm.c index a3471e9..d18a241 100644 --- a/sound/soc/atmel/atmel-pcm.c +++ b/sound/soc/atmel/atmel-pcm.c @@ -37,15 +37,19 @@ #include #include #include +#include #include +#include #include #include #include #include +#include #include "atmel-pcm.h" +static int use_dma; /*--------------------------------------------------------------------------*\ * Hardware definition @@ -66,6 +70,20 @@ static const struct snd_pcm_hardware atmel_pcm_pdc_hardware = { .buffer_bytes_max = 32 * 1024, }; +static const struct snd_pcm_hardware atmel_pcm_dma_hardware = { + .info = SNDRV_PCM_INFO_MMAP | + SNDRV_PCM_INFO_MMAP_VALID | + SNDRV_PCM_INFO_INTERLEAVED | + SNDRV_PCM_INFO_RESUME | + SNDRV_PCM_INFO_PAUSE, + .formats = SNDRV_PCM_FMTBIT_S16_LE, + .period_bytes_min = 256, /* lighting DMA overhead */ + .period_bytes_max = 2 * 0xffff, /* if 2 bytes format */ + .periods_min = 8, + .periods_max = 1024, /* no limit */ + .buffer_bytes_max = 64 * 1024, /* 64KiB */ +}; + static const struct snd_pcm_hardware *atmel_pcm_hardware; @@ -85,6 +103,9 @@ struct atmel_runtime_data { u32 pdc_xcr_save; u32 pdc_xnpr_save; u32 pdc_xncr_save; + + /* dmaengine data */ + struct at_dma_slave atslave; }; @@ -164,6 +185,101 @@ static void atmel_pcm_pdc_irq(u32 ssc_sr, snd_pcm_period_elapsed(substream); } +/** + * atmel_pcm_dma_irq: SSC interrupt handler for DMAENGINE enabled SSC + * + * We use DMAENGINE to send/receive data to/from SSC so this ISR is only to + * check if any overrun occured. + */ +static void atmel_pcm_dma_irq(u32 ssc_sr, + struct snd_pcm_substream *substream) +{ + struct atmel_runtime_data *prtd = snd_dmaengine_pcm_get_data(substream); + struct atmel_pcm_dma_params *params = prtd->params; + + if (ssc_sr & params->mask->ssc_error) { + if (snd_pcm_running(substream)) + pr_warn("atmel-pcm: buffer %s on %s (SSC_SR=%#x)\n", + substream->stream == SNDRV_PCM_STREAM_PLAYBACK + ? "underrun" : "overrun", params->name, + ssc_sr); + + /* stop RX and capture: will be enabled again at restart */ + ssc_writex(params->ssc->regs, SSC_CR, + params->mask->ssc_disable); + snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); + + /* now drain RHR and read status to remove xrun condition */ + ssc_readx(params->ssc->regs, SSC_RHR); + ssc_readx(params->ssc->regs, SSC_SR); + } +} + +/*--------------------------------------------------------------------------*\ + * DMAENGINE operations +\*--------------------------------------------------------------------------*/ +static bool filter(struct dma_chan *chan, void *slave) +{ + struct at_dma_slave *sl = slave; + + if (sl->dma_dev == chan->device->dev) { + chan->private = sl; + return true; + } else { + return false; + } +} + +static int atmel_pcm_configure_dma(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params) +{ + struct snd_pcm_runtime *runtime = substream->runtime; + struct atmel_runtime_data *prtd = runtime->private_data; + struct ssc_device *ssc = prtd->params->ssc; + struct dma_chan *dma_chan; + struct dma_slave_config slave_config; + struct at_dma_slave *sdata = NULL; + int ret; + + if (ssc->pdev) + sdata = ssc->pdev->dev.platform_data; + + ret = snd_dmaengine_pcm_open(substream, filter, sdata); + if (ret) { + pr_err("atmel-pcm: dmaengine pcm open failed\n"); + return -EINVAL; + } + snd_dmaengine_pcm_set_data(substream, prtd); + + ret = snd_hwparams_to_dma_slave_config(substream, params, + &slave_config); + if (ret) { + pr_err("atmel-pcm: hwparams to dma slave configure failed\n"); + goto err; + } + + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { + slave_config.dst_addr = (dma_addr_t)ssc->phybase + SSC_THR; + slave_config.dst_maxburst = 1; + } else { + slave_config.src_addr = (dma_addr_t)ssc->phybase + SSC_RHR; + slave_config.src_maxburst = 1; + } + slave_config.device_fc = false; + + dma_chan = snd_dmaengine_pcm_get_chan(substream); + if (dmaengine_slave_config(dma_chan, &slave_config)) { + pr_err("atmel-pcm: failed to configure dma channel\n"); + ret = -EBUSY; + goto err; + } + + return 0; + +err: + snd_dmaengine_pcm_close(substream); + return ret; +} /*--------------------------------------------------------------------------*\ * PCM operations @@ -174,43 +290,74 @@ static int atmel_pcm_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_runtime *runtime = substream->runtime; struct atmel_runtime_data *prtd = runtime->private_data; struct snd_soc_pcm_runtime *rtd = substream->private_data; + struct dma_chan *dma_chan = NULL; + int ret; /* this may get called several times by oss emulation * with different params */ snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); - runtime->dma_bytes = params_buffer_bytes(params); prtd->params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); - prtd->params->dma_intr_handler = atmel_pcm_pdc_irq; prtd->dma_buffer = runtime->dma_addr; prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes; prtd->period_size = params_period_bytes(params); + if (use_dma) { + ret = atmel_pcm_configure_dma(substream, params); + if (ret) { + pr_err("atmel-pcm: failed to configure dma"); + return ret; + } + + dma_chan = snd_dmaengine_pcm_get_chan(substream); + + prtd->params->dma_intr_handler = atmel_pcm_dma_irq; + } else { + prtd->params->dma_intr_handler = atmel_pcm_pdc_irq; + } + pr_debug("atmel-pcm: " - "hw_params: DMA for %s initialized " + "hw_params: %s%s for %s initialized " "(dma_bytes=%u, period_size=%u)\n", + dma_chan ? "DMA " : "PDC", + dma_chan ? dma_chan_name(dma_chan) : "", prtd->params->name, runtime->dma_bytes, prtd->period_size); + return 0; } static int atmel_pcm_hw_free(struct snd_pcm_substream *substream) { - struct atmel_runtime_data *prtd = substream->runtime->private_data; - struct atmel_pcm_dma_params *params = prtd->params; + struct atmel_runtime_data *prtd; + struct atmel_pcm_dma_params *params; - if (params != NULL) { - ssc_writex(params->ssc->regs, SSC_PDC_PTCR, - params->mask->pdc_disable); - prtd->params->dma_intr_handler = NULL; + if (use_dma) { + prtd = snd_dmaengine_pcm_get_data(substream); + params = prtd->params; + + if (params != NULL) + prtd->params->dma_intr_handler = NULL; + } else { + prtd = substream->runtime->private_data; + params = prtd->params; + + if (params != NULL) { + ssc_writex(params->ssc->regs, SSC_PDC_PTCR, + params->mask->pdc_disable); + prtd->params->dma_intr_handler = NULL; + } } return 0; } +/*--------------------------------------------------------------------------*\ + * PCM callbacks using PDC +\*--------------------------------------------------------------------------*/ static int atmel_pcm_pdc_prepare(struct snd_pcm_substream *substream) { struct atmel_runtime_data *prtd = substream->runtime->private_data; @@ -307,6 +454,22 @@ static snd_pcm_uframes_t atmel_pcm_pdc_pointer( return x; } +/*--------------------------------------------------------------------------*\ + * PCM callbacks using DMAENGINE +\*--------------------------------------------------------------------------*/ +static int atmel_pcm_dma_prepare(struct snd_pcm_substream *substream) +{ + struct atmel_runtime_data *prtd = snd_dmaengine_pcm_get_data(substream); + struct atmel_pcm_dma_params *params = prtd->params; + + ssc_writex(params->ssc->regs, SSC_IER, params->mask->ssc_error); + ssc_writex(params->ssc->regs, SSC_CR, params->mask->ssc_enable); + return 0; +} + +/*--------------------------------------------------------------------------*\ + * PCM open/close/mmap +\*--------------------------------------------------------------------------*/ static int atmel_pcm_open(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; @@ -334,7 +497,14 @@ static int atmel_pcm_open(struct snd_pcm_substream *substream) static int atmel_pcm_close(struct snd_pcm_substream *substream) { - struct atmel_runtime_data *prtd = substream->runtime->private_data; + struct atmel_runtime_data *prtd; + + if (use_dma) { + prtd = snd_dmaengine_pcm_get_data(substream); + snd_dmaengine_pcm_close(substream); + } else { + prtd = substream->runtime->private_data; + } kfree(prtd); return 0; @@ -426,12 +596,14 @@ static int atmel_pcm_suspend(struct snd_soc_dai *dai) if (!runtime) return 0; + if (!use_dma) + return 0; + prtd = runtime->private_data; params = prtd->params; /* disable the PDC and save the PDC registers */ - - ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_disable); + ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_disable); prtd->pdc_xpr_save = ssc_readx(params->ssc->regs, params->pdc->xpr); prtd->pdc_xcr_save = ssc_readx(params->ssc->regs, params->pdc->xcr); @@ -450,6 +622,9 @@ static int atmel_pcm_resume(struct snd_soc_dai *dai) if (!runtime) return 0; + if (use_dma) + return 0; + prtd = runtime->private_data; params = prtd->params; @@ -458,8 +633,8 @@ static int atmel_pcm_resume(struct snd_soc_dai *dai) ssc_writex(params->ssc->regs, params->pdc->xcr, prtd->pdc_xcr_save); ssc_writex(params->ssc->regs, params->pdc->xnpr, prtd->pdc_xnpr_save); ssc_writex(params->ssc->regs, params->pdc->xncr, prtd->pdc_xncr_save); - ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_enable); + return 0; } #else @@ -477,7 +652,19 @@ static struct snd_soc_platform_driver atmel_soc_platform = { int atmel_pcm_platform_register(struct device *dev) { - atmel_pcm_hardware = &atmel_pcm_pdc_hardware; + struct platform_device *pdev = to_platform_device(dev); + struct ssc_device *ssc = platform_get_drvdata(pdev); + + if (ssc->pdata->use_dma) { + use_dma = 1; + atmel_pcm_ops.prepare = atmel_pcm_dma_prepare; + atmel_pcm_ops.trigger = snd_dmaengine_pcm_trigger; + atmel_pcm_ops.pointer = snd_dmaengine_pcm_pointer_no_residue; + + atmel_pcm_hardware = &atmel_pcm_dma_hardware; + } else { + atmel_pcm_hardware = &atmel_pcm_pdc_hardware; + } return snd_soc_register_platform(dev, &atmel_soc_platform); }