[ALSA] ASoC AT91RM92000 audio DMA
[usb.git] / sound / soc / at91 / at91rm9200-pcm.c
blob237bc5f075795e6f5875419f21f099c744349046
1 /*
2 * at91rm9200-pcm.c -- ALSA PCM interface for the Atmel AT91RM9200 chip.
4 * Author: Frank Mandarino <fmandarino@endrelia.com>
5 * Endrelia Technologies Inc.
6 * Created: Mar 3, 2006
8 * Based on pxa2xx-pcm.c by:
10 * Author: Nicolas Pitre
11 * Created: Nov 30, 2004
12 * Copyright: (C) 2004 MontaVista Software, Inc.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/dma-mapping.h>
25 #include <sound/driver.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
31 #include <asm/arch/at91rm9200.h>
32 #include <asm/arch/at91rm9200_ssc.h>
33 #include <asm/arch/at91rm9200_pdc.h>
34 #include <asm/arch/hardware.h>
36 #include "at91rm9200-pcm.h"
38 #if 0
39 #define DBG(x...) printk(KERN_INFO "at91rm9200-pcm: " x)
40 #else
41 #define DBG(x...)
42 #endif
44 static const snd_pcm_hardware_t at91rm9200_pcm_hardware = {
45 .info = SNDRV_PCM_INFO_MMAP |
46 SNDRV_PCM_INFO_MMAP_VALID |
47 SNDRV_PCM_INFO_INTERLEAVED |
48 SNDRV_PCM_INFO_PAUSE,
49 .formats = SNDRV_PCM_FMTBIT_S16_LE,
50 .period_bytes_min = 32,
51 .period_bytes_max = 8192,
52 .periods_min = 2,
53 .periods_max = 1024,
54 .buffer_bytes_max = 32 * 1024,
57 struct at91rm9200_runtime_data {
58 at91rm9200_pcm_dma_params_t *params;
59 dma_addr_t dma_buffer; /* physical address of dma buffer */
60 dma_addr_t dma_buffer_end; /* first address beyond DMA buffer */
61 size_t period_size;
62 dma_addr_t period_ptr; /* physical address of next period */
63 u32 pdc_xpr_save; /* PDC register save */
64 u32 pdc_xcr_save;
65 u32 pdc_xnpr_save;
66 u32 pdc_xncr_save;
69 static void at91rm9200_pcm_dma_irq(u32 ssc_sr,
70 struct snd_pcm_substream *substream)
72 struct at91rm9200_runtime_data *prtd = substream->runtime->private_data;
73 at91rm9200_pcm_dma_params_t *params = prtd->params;
74 static int count = 0;
76 count++;
78 if (ssc_sr & params->mask->ssc_endbuf) {
80 printk(KERN_WARNING
81 "at91rm9200-pcm: buffer %s on %s (SSC_SR=%#x, count=%d)\n",
82 substream->stream == SNDRV_PCM_STREAM_PLAYBACK
83 ? "underrun" : "overrun",
84 params->name, ssc_sr, count);
86 /* re-start the PDC */
87 at91_ssc_write(params->pdc->ptcr, params->mask->pdc_disable);
89 prtd->period_ptr += prtd->period_size;
90 if (prtd->period_ptr >= prtd->dma_buffer_end) {
91 prtd->period_ptr = prtd->dma_buffer;
94 at91_ssc_write(params->pdc->xpr, prtd->period_ptr);
95 at91_ssc_write(params->pdc->xcr,
96 prtd->period_size / params->pdc_xfer_size);
98 at91_ssc_write(params->pdc->ptcr, params->mask->pdc_enable);
101 if (ssc_sr & params->mask->ssc_endx) {
103 /* Load the PDC next pointer and counter registers */
104 prtd->period_ptr += prtd->period_size;
105 if (prtd->period_ptr >= prtd->dma_buffer_end) {
106 prtd->period_ptr = prtd->dma_buffer;
108 at91_ssc_write(params->pdc->xnpr, prtd->period_ptr);
109 at91_ssc_write(params->pdc->xncr,
110 prtd->period_size / params->pdc_xfer_size);
113 snd_pcm_period_elapsed(substream);
116 static int at91rm9200_pcm_hw_params(struct snd_pcm_substream *substream,
117 struct snd_pcm_hw_params *params)
119 snd_pcm_runtime_t *runtime = substream->runtime;
120 struct at91rm9200_runtime_data *prtd = runtime->private_data;
121 struct snd_soc_pcm_runtime *rtd = substream->private_data;
123 /* this may get called several times by oss emulation
124 * with different params */
126 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
127 runtime->dma_bytes = params_buffer_bytes(params);
129 prtd->params = rtd->cpu_dai->dma_data;
130 prtd->params->dma_intr_handler = at91rm9200_pcm_dma_irq;
132 prtd->dma_buffer = runtime->dma_addr;
133 prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
134 prtd->period_size = params_period_bytes(params);
136 DBG("hw_params: DMA for %s initialized (dma_bytes=%d, period_size=%d)\n",
137 prtd->params->name, runtime->dma_bytes, prtd->period_size);
138 return 0;
141 static int at91rm9200_pcm_hw_free(struct snd_pcm_substream *substream)
143 struct at91rm9200_runtime_data *prtd = substream->runtime->private_data;
144 at91rm9200_pcm_dma_params_t *params = prtd->params;
146 if (params != NULL) {
147 at91_ssc_write(params->pdc->ptcr, params->mask->pdc_disable);
148 prtd->params->dma_intr_handler = NULL;
151 return 0;
154 static int at91rm9200_pcm_prepare(struct snd_pcm_substream *substream)
156 struct at91rm9200_runtime_data *prtd = substream->runtime->private_data;
157 at91rm9200_pcm_dma_params_t *params = prtd->params;
159 at91_ssc_write(params->ssc->idr,
160 params->mask->ssc_endx | params->mask->ssc_endbuf);
162 at91_ssc_write(params->pdc->ptcr, params->mask->pdc_disable);
163 return 0;
166 static int at91rm9200_pcm_trigger(struct snd_pcm_substream *substream,
167 int cmd)
169 struct at91rm9200_runtime_data *prtd = substream->runtime->private_data;
170 at91rm9200_pcm_dma_params_t *params = prtd->params;
171 int ret = 0;
173 switch (cmd) {
174 case SNDRV_PCM_TRIGGER_START:
175 prtd->period_ptr = prtd->dma_buffer;
177 at91_ssc_write(params->pdc->xpr, prtd->period_ptr);
178 at91_ssc_write(params->pdc->xcr,
179 prtd->period_size / params->pdc_xfer_size);
181 prtd->period_ptr += prtd->period_size;
182 at91_ssc_write(params->pdc->xnpr, prtd->period_ptr);
183 at91_ssc_write(params->pdc->xncr,
184 prtd->period_size / params->pdc_xfer_size);
186 DBG("trigger: period_ptr=%lx, xpr=%lx, xcr=%ld, xnpr=%lx, xncr=%ld\n",
187 (unsigned long) prtd->period_ptr,
188 at91_ssc_read(params->pdc->xpr),
189 at91_ssc_read(params->pdc->xcr),
190 at91_ssc_read(params->pdc->xnpr),
191 at91_ssc_read(params->pdc->xncr));
193 at91_ssc_write(params->ssc->ier,
194 params->mask->ssc_endx | params->mask->ssc_endbuf);
196 at91_ssc_write(params->pdc->ptcr, params->mask->pdc_enable);
198 DBG("sr=%lx imr=%lx\n", at91_ssc_read(params->ssc->ier - 4),
199 at91_ssc_read(params->ssc->ier + 8));
200 break;
202 case SNDRV_PCM_TRIGGER_STOP:
203 case SNDRV_PCM_TRIGGER_SUSPEND:
204 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
205 at91_ssc_write(params->pdc->ptcr, params->mask->pdc_disable);
206 break;
208 case SNDRV_PCM_TRIGGER_RESUME:
209 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
210 at91_ssc_write(params->pdc->ptcr, params->mask->pdc_enable);
211 break;
213 default:
214 ret = -EINVAL;
217 return ret;
220 static snd_pcm_uframes_t at91rm9200_pcm_pointer(
221 struct snd_pcm_substream *substream)
223 struct snd_pcm_runtime *runtime = substream->runtime;
224 struct at91rm9200_runtime_data *prtd = runtime->private_data;
225 at91rm9200_pcm_dma_params_t *params = prtd->params;
226 dma_addr_t ptr;
227 snd_pcm_uframes_t x;
229 ptr = (dma_addr_t) at91_ssc_read(params->pdc->xpr);
230 x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
232 if (x == runtime->buffer_size)
233 x = 0;
234 return x;
237 static int at91rm9200_pcm_open(struct snd_pcm_substream *substream)
239 struct snd_pcm_runtime *runtime = substream->runtime;
240 struct at91rm9200_runtime_data *prtd;
241 int ret = 0;
243 snd_soc_set_runtime_hwparams(substream, &at91rm9200_pcm_hardware);
245 /* ensure that buffer size is a multiple of period size */
246 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
247 if (ret < 0)
248 goto out;
250 prtd = kzalloc(sizeof(struct at91rm9200_runtime_data), GFP_KERNEL);
251 if (prtd == NULL) {
252 ret = -ENOMEM;
253 goto out;
255 runtime->private_data = prtd;
257 out:
258 return ret;
261 static int at91rm9200_pcm_close(struct snd_pcm_substream *substream)
263 struct at91rm9200_runtime_data *prtd = substream->runtime->private_data;
265 kfree(prtd);
266 return 0;
269 static int at91rm9200_pcm_mmap(struct snd_pcm_substream *substream,
270 struct vm_area_struct *vma)
272 struct snd_pcm_runtime *runtime = substream->runtime;
274 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
275 runtime->dma_area,
276 runtime->dma_addr,
277 runtime->dma_bytes);
280 struct snd_pcm_ops at91rm9200_pcm_ops = {
281 .open = at91rm9200_pcm_open,
282 .close = at91rm9200_pcm_close,
283 .ioctl = snd_pcm_lib_ioctl,
284 .hw_params = at91rm9200_pcm_hw_params,
285 .hw_free = at91rm9200_pcm_hw_free,
286 .prepare = at91rm9200_pcm_prepare,
287 .trigger = at91rm9200_pcm_trigger,
288 .pointer = at91rm9200_pcm_pointer,
289 .mmap = at91rm9200_pcm_mmap,
292 static int at91rm9200_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
293 int stream)
295 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
296 struct snd_dma_buffer *buf = &substream->dma_buffer;
297 size_t size = at91rm9200_pcm_hardware.buffer_bytes_max;
299 buf->dev.type = SNDRV_DMA_TYPE_DEV;
300 buf->dev.dev = pcm->card->dev;
301 buf->private_data = NULL;
302 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
303 &buf->addr, GFP_KERNEL);
305 DBG("preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
306 (void *) buf->area,
307 (void *) buf->addr,
308 size);
310 if (!buf->area)
311 return -ENOMEM;
313 buf->bytes = size;
314 return 0;
317 static u64 at91rm9200_pcm_dmamask = 0xffffffff;
319 static int at91rm9200_pcm_new(struct snd_card *card,
320 struct snd_soc_codec_dai *dai, struct snd_pcm *pcm)
322 int ret = 0;
324 if (!card->dev->dma_mask)
325 card->dev->dma_mask = &at91rm9200_pcm_dmamask;
326 if (!card->dev->coherent_dma_mask)
327 card->dev->coherent_dma_mask = 0xffffffff;
329 if (dai->playback.channels_min) {
330 ret = at91rm9200_pcm_preallocate_dma_buffer(pcm,
331 SNDRV_PCM_STREAM_PLAYBACK);
332 if (ret)
333 goto out;
336 if (dai->capture.channels_min) {
337 ret = at91rm9200_pcm_preallocate_dma_buffer(pcm,
338 SNDRV_PCM_STREAM_CAPTURE);
339 if (ret)
340 goto out;
342 out:
343 return ret;
346 static void at91rm9200_pcm_free_dma_buffers(struct snd_pcm *pcm)
348 struct snd_pcm_substream *substream;
349 struct snd_dma_buffer *buf;
350 int stream;
352 for (stream = 0; stream < 2; stream++) {
353 substream = pcm->streams[stream].substream;
354 if (!substream)
355 continue;
357 buf = &substream->dma_buffer;
358 if (!buf->area)
359 continue;
361 dma_free_writecombine(pcm->card->dev, buf->bytes,
362 buf->area, buf->addr);
363 buf->area = NULL;
367 static int at91rm9200_pcm_suspend(struct platform_device *pdev,
368 struct snd_soc_cpu_dai *dai)
370 struct snd_pcm_runtime *runtime = dai->runtime;
371 struct at91rm9200_runtime_data *prtd;
372 at91rm9200_pcm_dma_params_t *params;
374 if (!runtime)
375 return 0;
377 prtd = runtime->private_data;
378 params = prtd->params;
380 /* disable the PDC and save the PDC registers */
382 at91_ssc_write(params->pdc->ptcr, params->mask->pdc_disable);
384 prtd->pdc_xpr_save = at91_ssc_read(params->pdc->xpr);
385 prtd->pdc_xcr_save = at91_ssc_read(params->pdc->xcr);
386 prtd->pdc_xnpr_save = at91_ssc_read(params->pdc->xnpr);
387 prtd->pdc_xncr_save = at91_ssc_read(params->pdc->xncr);
389 return 0;
392 static int at91rm9200_pcm_resume(struct platform_device *pdev,
393 struct snd_soc_cpu_dai *dai)
395 struct snd_pcm_runtime *runtime = dai->runtime;
396 struct at91rm9200_runtime_data *prtd;
397 at91rm9200_pcm_dma_params_t *params;
399 if (!runtime)
400 return 0;
402 prtd = runtime->private_data;
403 params = prtd->params;
405 /* restore the PDC registers and enable the PDC */
406 at91_ssc_write(params->pdc->xpr, prtd->pdc_xpr_save);
407 at91_ssc_write(params->pdc->xcr, prtd->pdc_xcr_save);
408 at91_ssc_write(params->pdc->xnpr, prtd->pdc_xnpr_save);
409 at91_ssc_write(params->pdc->xncr, prtd->pdc_xncr_save);
411 at91_ssc_write(params->pdc->ptcr, params->mask->pdc_enable);
412 return 0;
415 struct snd_soc_platform at91rm9200_soc_platform = {
416 .name = "at91rm9200-audio",
417 .pcm_ops = &at91rm9200_pcm_ops,
418 .pcm_new = at91rm9200_pcm_new,
419 .pcm_free = at91rm9200_pcm_free_dma_buffers,
420 .suspend = at91rm9200_pcm_suspend,
421 .resume = at91rm9200_pcm_resume,
424 EXPORT_SYMBOL_GPL(at91rm9200_soc_platform);
426 MODULE_AUTHOR("Frank Mandarino <fmandarino@endrelia.com>");
427 MODULE_DESCRIPTION("Atmel AT91RM9200 PCM module");
428 MODULE_LICENSE("GPL");