blob: b8570e3e1b574ba880be4787212a8a04b32d8246 [file] [log] [blame]
Bo Shen3951e4a2012-11-28 11:46:13 +08001/*
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\*--------------------------------------------------------------------------*/
47static 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 */
67static void atmel_pcm_dma_irq(u32 ssc_sr,
68 struct snd_pcm_substream *substream)
69{
Lars-Peter Clausen5fe668a2013-03-22 14:12:09 +010070 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Bo Shen3951e4a2012-11-28 11:46:13 +080071 struct atmel_pcm_dma_params *prtd;
72
Lars-Peter Clausen5fe668a2013-03-22 14:12:09 +010073 prtd = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Bo Shen3951e4a2012-11-28 11:46:13 +080074
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_stop(substream, SNDRV_PCM_STATE_XRUN);
85
86 /* now drain RHR and read status to remove xrun condition */
87 ssc_readx(prtd->ssc->regs, SSC_RHR);
88 ssc_readx(prtd->ssc->regs, SSC_SR);
89 }
90}
91
92/*--------------------------------------------------------------------------*\
93 * DMAENGINE operations
94\*--------------------------------------------------------------------------*/
95static bool filter(struct dma_chan *chan, void *slave)
96{
97 struct at_dma_slave *sl = slave;
98
99 if (sl->dma_dev == chan->device->dev) {
100 chan->private = sl;
101 return true;
102 } else {
103 return false;
104 }
105}
106
107static int atmel_pcm_configure_dma(struct snd_pcm_substream *substream,
Lars-Peter Clausen5fe668a2013-03-22 14:12:09 +0100108 struct snd_pcm_hw_params *params, struct atmel_pcm_dma_params *prtd)
Bo Shen3951e4a2012-11-28 11:46:13 +0800109{
Bo Shen3951e4a2012-11-28 11:46:13 +0800110 struct ssc_device *ssc;
111 struct dma_chan *dma_chan;
112 struct dma_slave_config slave_config;
113 int ret;
114
Bo Shen3951e4a2012-11-28 11:46:13 +0800115 ssc = prtd->ssc;
116
117 ret = snd_hwparams_to_dma_slave_config(substream, params,
118 &slave_config);
119 if (ret) {
120 pr_err("atmel-pcm: hwparams to dma slave configure failed\n");
121 return ret;
122 }
123
124 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
125 slave_config.dst_addr = (dma_addr_t)ssc->phybase + SSC_THR;
126 slave_config.dst_maxburst = 1;
127 } else {
128 slave_config.src_addr = (dma_addr_t)ssc->phybase + SSC_RHR;
129 slave_config.src_maxburst = 1;
130 }
131
132 slave_config.device_fc = false;
133
134 dma_chan = snd_dmaengine_pcm_get_chan(substream);
135 if (dmaengine_slave_config(dma_chan, &slave_config)) {
136 pr_err("atmel-pcm: failed to configure dma channel\n");
137 ret = -EBUSY;
138 return ret;
139 }
140
141 return 0;
142}
143
144static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
145 struct snd_pcm_hw_params *params)
146{
147 struct snd_soc_pcm_runtime *rtd = substream->private_data;
148 struct atmel_pcm_dma_params *prtd;
149 struct ssc_device *ssc;
150 struct at_dma_slave *sdata = NULL;
151 int ret;
152
153 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
154
155 prtd = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
156 ssc = prtd->ssc;
157 if (ssc->pdev)
158 sdata = ssc->pdev->dev.platform_data;
159
160 ret = snd_dmaengine_pcm_open(substream, filter, sdata);
161 if (ret) {
162 pr_err("atmel-pcm: dmaengine pcm open failed\n");
163 return -EINVAL;
164 }
165
Lars-Peter Clausen5fe668a2013-03-22 14:12:09 +0100166 ret = atmel_pcm_configure_dma(substream, params, prtd);
Bo Shen3951e4a2012-11-28 11:46:13 +0800167 if (ret) {
168 pr_err("atmel-pcm: failed to configure dmai\n");
169 goto err;
170 }
171
172 prtd->dma_intr_handler = atmel_pcm_dma_irq;
173
174 return 0;
175err:
176 snd_dmaengine_pcm_close(substream);
177 return ret;
178}
179
180static int atmel_pcm_dma_prepare(struct snd_pcm_substream *substream)
181{
Lars-Peter Clausen5fe668a2013-03-22 14:12:09 +0100182 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Bo Shen3951e4a2012-11-28 11:46:13 +0800183 struct atmel_pcm_dma_params *prtd;
184
Lars-Peter Clausen5fe668a2013-03-22 14:12:09 +0100185 prtd = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Bo Shen3951e4a2012-11-28 11:46:13 +0800186
187 ssc_writex(prtd->ssc->regs, SSC_IER, prtd->mask->ssc_error);
188 ssc_writex(prtd->ssc->regs, SSC_CR, prtd->mask->ssc_enable);
189
190 return 0;
191}
192
193static int atmel_pcm_open(struct snd_pcm_substream *substream)
194{
195 snd_soc_set_runtime_hwparams(substream, &atmel_pcm_dma_hardware);
196
197 return 0;
198}
199
Bo Shen3951e4a2012-11-28 11:46:13 +0800200static struct snd_pcm_ops atmel_pcm_ops = {
201 .open = atmel_pcm_open,
Lars-Peter Clausena85fc1b2013-03-22 14:12:06 +0100202 .close = snd_dmaengine_pcm_close,
Bo Shen3951e4a2012-11-28 11:46:13 +0800203 .ioctl = snd_pcm_lib_ioctl,
204 .hw_params = atmel_pcm_hw_params,
205 .prepare = atmel_pcm_dma_prepare,
206 .trigger = snd_dmaengine_pcm_trigger,
207 .pointer = snd_dmaengine_pcm_pointer_no_residue,
208 .mmap = atmel_pcm_mmap,
209};
210
211static struct snd_soc_platform_driver atmel_soc_platform = {
212 .ops = &atmel_pcm_ops,
213 .pcm_new = atmel_pcm_new,
214 .pcm_free = atmel_pcm_free,
215};
216
217int atmel_pcm_dma_platform_register(struct device *dev)
218{
219 return snd_soc_register_platform(dev, &atmel_soc_platform);
220}
221EXPORT_SYMBOL(atmel_pcm_dma_platform_register);
222
223void atmel_pcm_dma_platform_unregister(struct device *dev)
224{
225 snd_soc_unregister_platform(dev);
226}
227EXPORT_SYMBOL(atmel_pcm_dma_platform_unregister);
228
229MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
230MODULE_DESCRIPTION("Atmel DMA based PCM module");
231MODULE_LICENSE("GPL");