2 * atmel-pcm.c -- ALSA PCM interface for the Atmel atmel SoC.
4 * Copyright (C) 2005 SAN People
5 * Copyright (C) 2008 Atmel
7 * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
9 * Based on at91-pcm. by:
10 * Frank Mandarino <fmandarino@endrelia.com>
11 * Copyright 2006 Endrelia Technologies Inc.
13 * Based on pxa2xx-pcm.c by:
15 * Author: Nicolas Pitre
16 * Created: Nov 30, 2004
17 * Copyright: (C) 2004 MontaVista Software, Inc.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/platform_device.h>
37 #include <linux/slab.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/atmel_pdc.h>
40 #include <linux/atmel-ssc.h>
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc.h>
47 #include <mach/hardware.h>
49 #include "atmel-pcm.h"
52 /*--------------------------------------------------------------------------*\
54 \*--------------------------------------------------------------------------*/
55 /* TODO: These values were taken from the AT91 platform driver, check
56 * them against real values for AT32
58 static const struct snd_pcm_hardware atmel_pcm_hardware = {
59 .info = SNDRV_PCM_INFO_MMAP |
60 SNDRV_PCM_INFO_MMAP_VALID |
61 SNDRV_PCM_INFO_INTERLEAVED |
63 .formats = SNDRV_PCM_FMTBIT_S16_LE,
64 .period_bytes_min = 32,
65 .period_bytes_max = 8192,
68 .buffer_bytes_max = 32 * 1024,
72 /*--------------------------------------------------------------------------*\
74 \*--------------------------------------------------------------------------*/
75 struct atmel_runtime_data {
76 struct atmel_pcm_dma_params *params;
77 dma_addr_t dma_buffer; /* physical address of dma buffer */
78 dma_addr_t dma_buffer_end; /* first address beyond DMA buffer */
81 dma_addr_t period_ptr; /* physical address of next period */
82 int periods; /* period index of period_ptr */
84 /* PDC register save */
92 /*--------------------------------------------------------------------------*\
94 \*--------------------------------------------------------------------------*/
95 static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
98 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
99 struct snd_dma_buffer *buf = &substream->dma_buffer;
100 size_t size = atmel_pcm_hardware.buffer_bytes_max;
102 buf->dev.type = SNDRV_DMA_TYPE_DEV;
103 buf->dev.dev = pcm->card->dev;
104 buf->private_data = NULL;
105 buf->area = dma_alloc_coherent(pcm->card->dev, size,
106 &buf->addr, GFP_KERNEL);
107 pr_debug("atmel-pcm:"
108 "preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
119 /*--------------------------------------------------------------------------*\
121 \*--------------------------------------------------------------------------*/
122 static void atmel_pcm_dma_irq(u32 ssc_sr,
123 struct snd_pcm_substream *substream)
125 struct atmel_runtime_data *prtd = substream->runtime->private_data;
126 struct atmel_pcm_dma_params *params = prtd->params;
131 if (ssc_sr & params->mask->ssc_endbuf) {
132 pr_warning("atmel-pcm: buffer %s on %s"
133 " (SSC_SR=%#x, count=%d)\n",
134 substream->stream == SNDRV_PCM_STREAM_PLAYBACK
135 ? "underrun" : "overrun",
136 params->name, ssc_sr, count);
138 /* re-start the PDC */
139 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
140 params->mask->pdc_disable);
141 prtd->period_ptr += prtd->period_size;
142 if (prtd->period_ptr >= prtd->dma_buffer_end)
143 prtd->period_ptr = prtd->dma_buffer;
145 ssc_writex(params->ssc->regs, params->pdc->xpr,
147 ssc_writex(params->ssc->regs, params->pdc->xcr,
148 prtd->period_size / params->pdc_xfer_size);
149 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
150 params->mask->pdc_enable);
153 if (ssc_sr & params->mask->ssc_endx) {
154 /* Load the PDC next pointer and counter registers */
155 prtd->period_ptr += prtd->period_size;
156 if (prtd->period_ptr >= prtd->dma_buffer_end)
157 prtd->period_ptr = prtd->dma_buffer;
159 ssc_writex(params->ssc->regs, params->pdc->xnpr,
161 ssc_writex(params->ssc->regs, params->pdc->xncr,
162 prtd->period_size / params->pdc_xfer_size);
165 snd_pcm_period_elapsed(substream);
169 /*--------------------------------------------------------------------------*\
171 \*--------------------------------------------------------------------------*/
172 static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
173 struct snd_pcm_hw_params *params)
175 struct snd_pcm_runtime *runtime = substream->runtime;
176 struct atmel_runtime_data *prtd = runtime->private_data;
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
179 /* this may get called several times by oss emulation
180 * with different params */
182 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
183 runtime->dma_bytes = params_buffer_bytes(params);
185 prtd->params = rtd->dai->cpu_dai->dma_data;
186 prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
188 prtd->dma_buffer = runtime->dma_addr;
189 prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
190 prtd->period_size = params_period_bytes(params);
192 pr_debug("atmel-pcm: "
193 "hw_params: DMA for %s initialized "
194 "(dma_bytes=%u, period_size=%u)\n",
201 static int atmel_pcm_hw_free(struct snd_pcm_substream *substream)
203 struct atmel_runtime_data *prtd = substream->runtime->private_data;
204 struct atmel_pcm_dma_params *params = prtd->params;
206 if (params != NULL) {
207 ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
208 params->mask->pdc_disable);
209 prtd->params->dma_intr_handler = NULL;
215 static int atmel_pcm_prepare(struct snd_pcm_substream *substream)
217 struct atmel_runtime_data *prtd = substream->runtime->private_data;
218 struct atmel_pcm_dma_params *params = prtd->params;
220 ssc_writex(params->ssc->regs, SSC_IDR,
221 params->mask->ssc_endx | params->mask->ssc_endbuf);
222 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
223 params->mask->pdc_disable);
227 static int atmel_pcm_trigger(struct snd_pcm_substream *substream,
230 struct snd_pcm_runtime *rtd = substream->runtime;
231 struct atmel_runtime_data *prtd = rtd->private_data;
232 struct atmel_pcm_dma_params *params = prtd->params;
235 pr_debug("atmel-pcm:buffer_size = %ld,"
236 "dma_area = %p, dma_bytes = %u\n",
237 rtd->buffer_size, rtd->dma_area, rtd->dma_bytes);
240 case SNDRV_PCM_TRIGGER_START:
241 prtd->period_ptr = prtd->dma_buffer;
243 ssc_writex(params->ssc->regs, params->pdc->xpr,
245 ssc_writex(params->ssc->regs, params->pdc->xcr,
246 prtd->period_size / params->pdc_xfer_size);
248 prtd->period_ptr += prtd->period_size;
249 ssc_writex(params->ssc->regs, params->pdc->xnpr,
251 ssc_writex(params->ssc->regs, params->pdc->xncr,
252 prtd->period_size / params->pdc_xfer_size);
254 pr_debug("atmel-pcm: trigger: "
255 "period_ptr=%lx, xpr=%u, "
256 "xcr=%u, xnpr=%u, xncr=%u\n",
257 (unsigned long)prtd->period_ptr,
258 ssc_readx(params->ssc->regs, params->pdc->xpr),
259 ssc_readx(params->ssc->regs, params->pdc->xcr),
260 ssc_readx(params->ssc->regs, params->pdc->xnpr),
261 ssc_readx(params->ssc->regs, params->pdc->xncr));
263 ssc_writex(params->ssc->regs, SSC_IER,
264 params->mask->ssc_endx | params->mask->ssc_endbuf);
265 ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
266 params->mask->pdc_enable);
268 pr_debug("sr=%u imr=%u\n",
269 ssc_readx(params->ssc->regs, SSC_SR),
270 ssc_readx(params->ssc->regs, SSC_IER));
271 break; /* SNDRV_PCM_TRIGGER_START */
273 case SNDRV_PCM_TRIGGER_STOP:
274 case SNDRV_PCM_TRIGGER_SUSPEND:
275 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
276 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
277 params->mask->pdc_disable);
280 case SNDRV_PCM_TRIGGER_RESUME:
281 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
282 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
283 params->mask->pdc_enable);
293 static snd_pcm_uframes_t atmel_pcm_pointer(
294 struct snd_pcm_substream *substream)
296 struct snd_pcm_runtime *runtime = substream->runtime;
297 struct atmel_runtime_data *prtd = runtime->private_data;
298 struct atmel_pcm_dma_params *params = prtd->params;
302 ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr);
303 x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
305 if (x == runtime->buffer_size)
311 static int atmel_pcm_open(struct snd_pcm_substream *substream)
313 struct snd_pcm_runtime *runtime = substream->runtime;
314 struct atmel_runtime_data *prtd;
317 snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware);
319 /* ensure that buffer size is a multiple of period size */
320 ret = snd_pcm_hw_constraint_integer(runtime,
321 SNDRV_PCM_HW_PARAM_PERIODS);
325 prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL);
330 runtime->private_data = prtd;
336 static int atmel_pcm_close(struct snd_pcm_substream *substream)
338 struct atmel_runtime_data *prtd = substream->runtime->private_data;
344 static int atmel_pcm_mmap(struct snd_pcm_substream *substream,
345 struct vm_area_struct *vma)
347 return remap_pfn_range(vma, vma->vm_start,
348 substream->dma_buffer.addr >> PAGE_SHIFT,
349 vma->vm_end - vma->vm_start, vma->vm_page_prot);
352 struct snd_pcm_ops atmel_pcm_ops = {
353 .open = atmel_pcm_open,
354 .close = atmel_pcm_close,
355 .ioctl = snd_pcm_lib_ioctl,
356 .hw_params = atmel_pcm_hw_params,
357 .hw_free = atmel_pcm_hw_free,
358 .prepare = atmel_pcm_prepare,
359 .trigger = atmel_pcm_trigger,
360 .pointer = atmel_pcm_pointer,
361 .mmap = atmel_pcm_mmap,
365 /*--------------------------------------------------------------------------*\
366 * ASoC platform driver
367 \*--------------------------------------------------------------------------*/
368 static u64 atmel_pcm_dmamask = 0xffffffff;
370 static int atmel_pcm_new(struct snd_card *card,
371 struct snd_soc_dai *dai, struct snd_pcm *pcm)
375 if (!card->dev->dma_mask)
376 card->dev->dma_mask = &atmel_pcm_dmamask;
377 if (!card->dev->coherent_dma_mask)
378 card->dev->coherent_dma_mask = 0xffffffff;
380 if (dai->playback.channels_min) {
381 ret = atmel_pcm_preallocate_dma_buffer(pcm,
382 SNDRV_PCM_STREAM_PLAYBACK);
387 if (dai->capture.channels_min) {
389 "Allocating PCM capture DMA buffer\n");
390 ret = atmel_pcm_preallocate_dma_buffer(pcm,
391 SNDRV_PCM_STREAM_CAPTURE);
399 static void atmel_pcm_free_dma_buffers(struct snd_pcm *pcm)
401 struct snd_pcm_substream *substream;
402 struct snd_dma_buffer *buf;
405 for (stream = 0; stream < 2; stream++) {
406 substream = pcm->streams[stream].substream;
410 buf = &substream->dma_buffer;
413 dma_free_coherent(pcm->card->dev, buf->bytes,
414 buf->area, buf->addr);
420 static int atmel_pcm_suspend(struct snd_soc_dai *dai)
422 struct snd_pcm_runtime *runtime = dai->runtime;
423 struct atmel_runtime_data *prtd;
424 struct atmel_pcm_dma_params *params;
429 prtd = runtime->private_data;
430 params = prtd->params;
432 /* disable the PDC and save the PDC registers */
434 ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_disable);
436 prtd->pdc_xpr_save = ssc_readx(params->ssc->regs, params->pdc->xpr);
437 prtd->pdc_xcr_save = ssc_readx(params->ssc->regs, params->pdc->xcr);
438 prtd->pdc_xnpr_save = ssc_readx(params->ssc->regs, params->pdc->xnpr);
439 prtd->pdc_xncr_save = ssc_readx(params->ssc->regs, params->pdc->xncr);
444 static int atmel_pcm_resume(struct snd_soc_dai *dai)
446 struct snd_pcm_runtime *runtime = dai->runtime;
447 struct atmel_runtime_data *prtd;
448 struct atmel_pcm_dma_params *params;
453 prtd = runtime->private_data;
454 params = prtd->params;
456 /* restore the PDC registers and enable the PDC */
457 ssc_writex(params->ssc->regs, params->pdc->xpr, prtd->pdc_xpr_save);
458 ssc_writex(params->ssc->regs, params->pdc->xcr, prtd->pdc_xcr_save);
459 ssc_writex(params->ssc->regs, params->pdc->xnpr, prtd->pdc_xnpr_save);
460 ssc_writex(params->ssc->regs, params->pdc->xncr, prtd->pdc_xncr_save);
462 ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_enable);
466 #define atmel_pcm_suspend NULL
467 #define atmel_pcm_resume NULL
470 struct snd_soc_platform atmel_soc_platform = {
471 .name = "atmel-audio",
472 .pcm_ops = &atmel_pcm_ops,
473 .pcm_new = atmel_pcm_new,
474 .pcm_free = atmel_pcm_free_dma_buffers,
475 .suspend = atmel_pcm_suspend,
476 .resume = atmel_pcm_resume,
478 EXPORT_SYMBOL_GPL(atmel_soc_platform);
480 static int __init atmel_pcm_modinit(void)
482 return snd_soc_register_platform(&atmel_soc_platform);
484 module_init(atmel_pcm_modinit);
486 static void __exit atmel_pcm_modexit(void)
488 snd_soc_unregister_platform(&atmel_soc_platform);
490 module_exit(atmel_pcm_modexit);
492 MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
493 MODULE_DESCRIPTION("Atmel PCM module");
494 MODULE_LICENSE("GPL");