2019-05-27 09:55:05 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
2012-11-28 07:46:13 +04:00
/*
* atmel - pcm - dma . c - - ALSA PCM DMA support for the Atmel SoC .
*
* Copyright ( C ) 2012 Atmel
*
* Author : Bo Shen < voice . shen @ atmel . com >
*
* Based on atmel - pcm by :
* Sedji Gaouaou < sedji . gaouaou @ atmel . com >
* Copyright 2008 Atmel
*/
# include <linux/module.h>
# include <linux/init.h>
# include <linux/platform_device.h>
# include <linux/slab.h>
# include <linux/dma-mapping.h>
# include <linux/dmaengine.h>
# include <linux/atmel-ssc.h>
# include <sound/core.h>
# include <sound/pcm.h>
# include <sound/pcm_params.h>
# include <sound/soc.h>
# include <sound/dmaengine_pcm.h>
# include "atmel-pcm.h"
/*--------------------------------------------------------------------------*\
* Hardware definition
\ * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
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 ,
. period_bytes_min = 256 , /* lighting DMA overhead */
. period_bytes_max = 2 * 0xffff , /* if 2 bytes format */
. periods_min = 8 ,
. periods_max = 1024 , /* no limit */
2015-03-30 22:40:37 +03:00
. buffer_bytes_max = 512 * 1024 ,
2012-11-28 07:46:13 +04:00
} ;
2020-07-07 22:06:00 +03:00
/*
2012-11-28 07:46:13 +04:00
* 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 )
{
2020-07-20 04:18:52 +03:00
struct snd_soc_pcm_runtime * rtd = asoc_substream_to_rtd ( substream ) ;
2012-11-28 07:46:13 +04:00
struct atmel_pcm_dma_params * prtd ;
2020-03-23 08:17:22 +03:00
prtd = snd_soc_dai_get_dma_data ( asoc_rtd_to_cpu ( rtd , 0 ) , substream ) ;
2012-11-28 07:46:13 +04:00
if ( ssc_sr & prtd - > 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 " , prtd - > name ,
ssc_sr ) ;
/* stop RX and capture: will be enabled again at restart */
ssc_writex ( prtd - > ssc - > regs , SSC_CR , prtd - > mask - > ssc_disable ) ;
2014-11-07 19:08:28 +03:00
snd_pcm_stop_xrun ( substream ) ;
2012-11-28 07:46:13 +04:00
/* now drain RHR and read status to remove xrun condition */
ssc_readx ( prtd - > ssc - > regs , SSC_RHR ) ;
ssc_readx ( prtd - > ssc - > regs , SSC_SR ) ;
}
}
static int atmel_pcm_configure_dma ( struct snd_pcm_substream * substream ,
2013-07-03 12:38:00 +04:00
struct snd_pcm_hw_params * params , struct dma_slave_config * slave_config )
2012-11-28 07:46:13 +04:00
{
2020-07-20 04:18:52 +03:00
struct snd_soc_pcm_runtime * rtd = asoc_substream_to_rtd ( substream ) ;
2013-07-03 12:38:00 +04:00
struct atmel_pcm_dma_params * prtd ;
2012-11-28 07:46:13 +04:00
struct ssc_device * ssc ;
int ret ;
2020-03-23 08:17:22 +03:00
prtd = snd_soc_dai_get_dma_data ( asoc_rtd_to_cpu ( rtd , 0 ) , substream ) ;
2012-11-28 07:46:13 +04:00
ssc = prtd - > ssc ;
2013-07-03 12:38:00 +04:00
ret = snd_hwparams_to_dma_slave_config ( substream , params , slave_config ) ;
2012-11-28 07:46:13 +04:00
if ( ret ) {
pr_err ( " atmel-pcm: hwparams to dma slave configure failed \n " ) ;
return ret ;
}
2015-02-02 09:44:46 +03:00
slave_config - > dst_addr = ssc - > phybase + SSC_THR ;
slave_config - > dst_maxburst = 1 ;
slave_config - > src_addr = ssc - > phybase + SSC_RHR ;
slave_config - > src_maxburst = 1 ;
2012-11-28 07:46:13 +04:00
prtd - > dma_intr_handler = atmel_pcm_dma_irq ;
return 0 ;
}
2013-07-03 12:38:00 +04:00
static const struct snd_dmaengine_pcm_config atmel_dmaengine_pcm_config = {
. prepare_slave_config = atmel_pcm_configure_dma ,
. pcm_hardware = & atmel_pcm_dma_hardware ,
2015-03-30 22:40:37 +03:00
. prealloc_buffer_size = 64 * 1024 ,
2012-11-28 07:46:13 +04:00
} ;
int atmel_pcm_dma_platform_register ( struct device * dev )
{
2019-06-28 07:07:05 +03:00
return devm_snd_dmaengine_pcm_register ( dev ,
& atmel_dmaengine_pcm_config , 0 ) ;
2012-11-28 07:46:13 +04:00
}
EXPORT_SYMBOL ( atmel_pcm_dma_platform_register ) ;
MODULE_AUTHOR ( " Bo Shen <voice.shen@atmel.com> " ) ;
MODULE_DESCRIPTION ( " Atmel DMA based PCM module " ) ;
MODULE_LICENSE ( " GPL " ) ;