1 /* 2 * atmel-pcm-dma.c -- ALSA PCM DMA support for the Atmel SoC. 3 * 4 * Copyright (C) 2012 Atmel 5 * 6 * Author: Bo Shen <voice.shen@atmel.com> 7 * 8 * Based on atmel-pcm by: 9 * Sedji Gaouaou <sedji.gaouaou@atmel.com> 10 * Copyright 2008 Atmel 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25 */ 26 27 #include <linux/module.h> 28 #include <linux/init.h> 29 #include <linux/platform_device.h> 30 #include <linux/slab.h> 31 #include <linux/dma-mapping.h> 32 #include <linux/dmaengine.h> 33 #include <linux/atmel-ssc.h> 34 #include <linux/platform_data/dma-atmel.h> 35 36 #include <sound/core.h> 37 #include <sound/pcm.h> 38 #include <sound/pcm_params.h> 39 #include <sound/soc.h> 40 #include <sound/dmaengine_pcm.h> 41 42 #include "atmel-pcm.h" 43 44 /*--------------------------------------------------------------------------*\ 45 * Hardware definition 46 \*--------------------------------------------------------------------------*/ 47 static const struct snd_pcm_hardware atmel_pcm_dma_hardware = { 48 .info = SNDRV_PCM_INFO_MMAP | 49 SNDRV_PCM_INFO_MMAP_VALID | 50 SNDRV_PCM_INFO_INTERLEAVED | 51 SNDRV_PCM_INFO_RESUME | 52 SNDRV_PCM_INFO_PAUSE, 53 .formats = SNDRV_PCM_FMTBIT_S16_LE, 54 .period_bytes_min = 256, /* lighting DMA overhead */ 55 .period_bytes_max = 2 * 0xffff, /* if 2 bytes format */ 56 .periods_min = 8, 57 .periods_max = 1024, /* no limit */ 58 .buffer_bytes_max = ATMEL_SSC_DMABUF_SIZE, 59 }; 60 61 /** 62 * atmel_pcm_dma_irq: SSC interrupt handler for DMAENGINE enabled SSC 63 * 64 * We use DMAENGINE to send/receive data to/from SSC so this ISR is only to 65 * check if any overrun occured. 66 */ 67 static void atmel_pcm_dma_irq(u32 ssc_sr, 68 struct snd_pcm_substream *substream) 69 { 70 struct snd_soc_pcm_runtime *rtd = substream->private_data; 71 struct atmel_pcm_dma_params *prtd; 72 73 prtd = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); 74 75 if (ssc_sr & prtd->mask->ssc_error) { 76 if (snd_pcm_running(substream)) 77 pr_warn("atmel-pcm: buffer %s on %s (SSC_SR=%#x)\n", 78 substream->stream == SNDRV_PCM_STREAM_PLAYBACK 79 ? "underrun" : "overrun", prtd->name, 80 ssc_sr); 81 82 /* stop RX and capture: will be enabled again at restart */ 83 ssc_writex(prtd->ssc->regs, SSC_CR, prtd->mask->ssc_disable); 84 snd_pcm_stream_lock(substream); 85 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); 86 snd_pcm_stream_unlock(substream); 87 88 /* now drain RHR and read status to remove xrun condition */ 89 ssc_readx(prtd->ssc->regs, SSC_RHR); 90 ssc_readx(prtd->ssc->regs, SSC_SR); 91 } 92 } 93 94 static int atmel_pcm_configure_dma(struct snd_pcm_substream *substream, 95 struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config) 96 { 97 struct snd_soc_pcm_runtime *rtd = substream->private_data; 98 struct atmel_pcm_dma_params *prtd; 99 struct ssc_device *ssc; 100 int ret; 101 102 prtd = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); 103 ssc = prtd->ssc; 104 105 ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config); 106 if (ret) { 107 pr_err("atmel-pcm: hwparams to dma slave configure failed\n"); 108 return ret; 109 } 110 111 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 112 slave_config->dst_addr = ssc->phybase + SSC_THR; 113 slave_config->dst_maxburst = 1; 114 } else { 115 slave_config->src_addr = ssc->phybase + SSC_RHR; 116 slave_config->src_maxburst = 1; 117 } 118 119 prtd->dma_intr_handler = atmel_pcm_dma_irq; 120 121 return 0; 122 } 123 124 static const struct snd_dmaengine_pcm_config atmel_dmaengine_pcm_config = { 125 .prepare_slave_config = atmel_pcm_configure_dma, 126 .pcm_hardware = &atmel_pcm_dma_hardware, 127 .prealloc_buffer_size = ATMEL_SSC_DMABUF_SIZE, 128 }; 129 130 int atmel_pcm_dma_platform_register(struct device *dev) 131 { 132 return snd_dmaengine_pcm_register(dev, &atmel_dmaengine_pcm_config, 133 SND_DMAENGINE_PCM_FLAG_NO_RESIDUE); 134 } 135 EXPORT_SYMBOL(atmel_pcm_dma_platform_register); 136 137 void atmel_pcm_dma_platform_unregister(struct device *dev) 138 { 139 snd_dmaengine_pcm_unregister(dev); 140 } 141 EXPORT_SYMBOL(atmel_pcm_dma_platform_unregister); 142 143 MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>"); 144 MODULE_DESCRIPTION("Atmel DMA based PCM module"); 145 MODULE_LICENSE("GPL"); 146