blob: 67c88e322fb1259b5ab265cc385bb0e3a6e31f10 [file] [log] [blame]
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +01001/*
2 * at91-pcm.c -- ALSA PCM interface for the Atmel AT91 SoC
3 *
4 * Author: Frank Mandarino <fmandarino@endrelia.com>
5 * Endrelia Technologies Inc.
6 * Created: Mar 3, 2006
7 *
8 * Based on pxa2xx-pcm.c by:
9 *
10 * Author: Nicolas Pitre
11 * Created: Nov 30, 2004
12 * Copyright: (C) 2004 MontaVista Software, Inc.
13 *
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.
17 */
18
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>
Frank Mandarinofa8f8782007-02-12 14:06:22 +010024#include <linux/atmel_pdc.h>
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010025
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010026#include <sound/core.h>
27#include <sound/pcm.h>
28#include <sound/pcm_params.h>
29#include <sound/soc.h>
30
31#include <asm/arch/hardware.h>
32#include <asm/arch/at91_ssc.h>
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010033
34#include "at91-pcm.h"
35
36#if 0
37#define DBG(x...) printk(KERN_INFO "at91-pcm: " x)
38#else
39#define DBG(x...)
40#endif
41
42static const struct snd_pcm_hardware at91_pcm_hardware = {
43 .info = SNDRV_PCM_INFO_MMAP |
44 SNDRV_PCM_INFO_MMAP_VALID |
45 SNDRV_PCM_INFO_INTERLEAVED |
46 SNDRV_PCM_INFO_PAUSE,
47 .formats = SNDRV_PCM_FMTBIT_S16_LE,
48 .period_bytes_min = 32,
49 .period_bytes_max = 8192,
50 .periods_min = 2,
51 .periods_max = 1024,
52 .buffer_bytes_max = 32 * 1024,
53};
54
55struct at91_runtime_data {
56 struct at91_pcm_dma_params *params;
57 dma_addr_t dma_buffer; /* physical address of dma buffer */
58 dma_addr_t dma_buffer_end; /* first address beyond DMA buffer */
59 size_t period_size;
60 dma_addr_t period_ptr; /* physical address of next period */
61 u32 pdc_xpr_save; /* PDC register save */
62 u32 pdc_xcr_save;
63 u32 pdc_xnpr_save;
64 u32 pdc_xncr_save;
65};
66
67static void at91_pcm_dma_irq(u32 ssc_sr,
68 struct snd_pcm_substream *substream)
69{
70 struct at91_runtime_data *prtd = substream->runtime->private_data;
71 struct at91_pcm_dma_params *params = prtd->params;
72 static int count = 0;
73
74 count++;
75
76 if (ssc_sr & params->mask->ssc_endbuf) {
77
78 printk(KERN_WARNING
79 "at91-pcm: buffer %s on %s (SSC_SR=%#x, count=%d)\n",
80 substream->stream == SNDRV_PCM_STREAM_PLAYBACK
81 ? "underrun" : "overrun",
82 params->name, ssc_sr, count);
83
84 /* re-start the PDC */
Frank Mandarinofa8f8782007-02-12 14:06:22 +010085 at91_ssc_write(params->ssc_base + ATMEL_PDC_PTCR, params->mask->pdc_disable);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010086
87 prtd->period_ptr += prtd->period_size;
88 if (prtd->period_ptr >= prtd->dma_buffer_end) {
89 prtd->period_ptr = prtd->dma_buffer;
90 }
91
92 at91_ssc_write(params->ssc_base + params->pdc->xpr, prtd->period_ptr);
93 at91_ssc_write(params->ssc_base + params->pdc->xcr,
94 prtd->period_size / params->pdc_xfer_size);
95
Frank Mandarinofa8f8782007-02-12 14:06:22 +010096 at91_ssc_write(params->ssc_base + ATMEL_PDC_PTCR, params->mask->pdc_enable);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010097 }
98
99 if (ssc_sr & params->mask->ssc_endx) {
100
101 /* Load the PDC next pointer and counter registers */
102 prtd->period_ptr += prtd->period_size;
103 if (prtd->period_ptr >= prtd->dma_buffer_end) {
104 prtd->period_ptr = prtd->dma_buffer;
105 }
106 at91_ssc_write(params->ssc_base + params->pdc->xnpr, prtd->period_ptr);
107 at91_ssc_write(params->ssc_base + params->pdc->xncr,
108 prtd->period_size / params->pdc_xfer_size);
109 }
110
111 snd_pcm_period_elapsed(substream);
112}
113
114static int at91_pcm_hw_params(struct snd_pcm_substream *substream,
115 struct snd_pcm_hw_params *params)
116{
117 struct snd_pcm_runtime *runtime = substream->runtime;
118 struct at91_runtime_data *prtd = runtime->private_data;
119 struct snd_soc_pcm_runtime *rtd = substream->private_data;
120
121 /* this may get called several times by oss emulation
122 * with different params */
123
124 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
125 runtime->dma_bytes = params_buffer_bytes(params);
126
Frank Mandarino62970272007-02-02 17:19:24 +0100127 prtd->params = rtd->dai->cpu_dai->dma_data;
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100128 prtd->params->dma_intr_handler = at91_pcm_dma_irq;
129
130 prtd->dma_buffer = runtime->dma_addr;
131 prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
132 prtd->period_size = params_period_bytes(params);
133
134 DBG("hw_params: DMA for %s initialized (dma_bytes=%d, period_size=%d)\n",
135 prtd->params->name, runtime->dma_bytes, prtd->period_size);
136 return 0;
137}
138
139static int at91_pcm_hw_free(struct snd_pcm_substream *substream)
140{
141 struct at91_runtime_data *prtd = substream->runtime->private_data;
142 struct at91_pcm_dma_params *params = prtd->params;
143
144 if (params != NULL) {
Frank Mandarinofa8f8782007-02-12 14:06:22 +0100145 at91_ssc_write(params->ssc_base + ATMEL_PDC_PTCR, params->mask->pdc_disable);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100146 prtd->params->dma_intr_handler = NULL;
147 }
148
149 return 0;
150}
151
152static int at91_pcm_prepare(struct snd_pcm_substream *substream)
153{
154 struct at91_runtime_data *prtd = substream->runtime->private_data;
155 struct at91_pcm_dma_params *params = prtd->params;
156
157 at91_ssc_write(params->ssc_base + AT91_SSC_IDR,
158 params->mask->ssc_endx | params->mask->ssc_endbuf);
159
Frank Mandarinofa8f8782007-02-12 14:06:22 +0100160 at91_ssc_write(params->ssc_base + ATMEL_PDC_PTCR, params->mask->pdc_disable);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100161 return 0;
162}
163
164static int at91_pcm_trigger(struct snd_pcm_substream *substream,
165 int cmd)
166{
167 struct at91_runtime_data *prtd = substream->runtime->private_data;
168 struct at91_pcm_dma_params *params = prtd->params;
169 int ret = 0;
170
171 switch (cmd) {
172 case SNDRV_PCM_TRIGGER_START:
173 prtd->period_ptr = prtd->dma_buffer;
174
175 at91_ssc_write(params->ssc_base + params->pdc->xpr, prtd->period_ptr);
176 at91_ssc_write(params->ssc_base + params->pdc->xcr,
177 prtd->period_size / params->pdc_xfer_size);
178
179 prtd->period_ptr += prtd->period_size;
180 at91_ssc_write(params->ssc_base + params->pdc->xnpr, prtd->period_ptr);
181 at91_ssc_write(params->ssc_base + params->pdc->xncr,
182 prtd->period_size / params->pdc_xfer_size);
183
184 DBG("trigger: period_ptr=%lx, xpr=%lx, xcr=%ld, xnpr=%lx, xncr=%ld\n",
185 (unsigned long) prtd->period_ptr,
186 at91_ssc_read(params->ssc_base + params->pdc->xpr),
187 at91_ssc_read(params->ssc_base + params->pdc->xcr),
188 at91_ssc_read(params->ssc_base + params->pdc->xnpr),
189 at91_ssc_read(params->ssc_base + params->pdc->xncr));
190
191 at91_ssc_write(params->ssc_base + AT91_SSC_IER,
192 params->mask->ssc_endx | params->mask->ssc_endbuf);
193
Frank Mandarinofa8f8782007-02-12 14:06:22 +0100194 at91_ssc_write(params->ssc_base + ATMEL_PDC_PTCR, params->mask->pdc_enable);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100195
196 DBG("sr=%lx imr=%lx\n", at91_ssc_read(params->ssc_base + AT91_SSC_SR),
197 at91_ssc_read(params->ssc_base + AT91_SSC_IER));
198 break;
199
200 case SNDRV_PCM_TRIGGER_STOP:
201 case SNDRV_PCM_TRIGGER_SUSPEND:
202 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Frank Mandarinofa8f8782007-02-12 14:06:22 +0100203 at91_ssc_write(params->ssc_base + ATMEL_PDC_PTCR, params->mask->pdc_disable);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100204 break;
205
206 case SNDRV_PCM_TRIGGER_RESUME:
207 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Frank Mandarinofa8f8782007-02-12 14:06:22 +0100208 at91_ssc_write(params->ssc_base + ATMEL_PDC_PTCR, params->mask->pdc_enable);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100209 break;
210
211 default:
212 ret = -EINVAL;
213 }
214
215 return ret;
216}
217
218static snd_pcm_uframes_t at91_pcm_pointer(
219 struct snd_pcm_substream *substream)
220{
221 struct snd_pcm_runtime *runtime = substream->runtime;
222 struct at91_runtime_data *prtd = runtime->private_data;
223 struct at91_pcm_dma_params *params = prtd->params;
224 dma_addr_t ptr;
225 snd_pcm_uframes_t x;
226
227 ptr = (dma_addr_t) at91_ssc_read(params->ssc_base + params->pdc->xpr);
228 x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
229
230 if (x == runtime->buffer_size)
231 x = 0;
232 return x;
233}
234
235static int at91_pcm_open(struct snd_pcm_substream *substream)
236{
237 struct snd_pcm_runtime *runtime = substream->runtime;
238 struct at91_runtime_data *prtd;
239 int ret = 0;
240
241 snd_soc_set_runtime_hwparams(substream, &at91_pcm_hardware);
242
243 /* ensure that buffer size is a multiple of period size */
244 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
245 if (ret < 0)
246 goto out;
247
248 prtd = kzalloc(sizeof(struct at91_runtime_data), GFP_KERNEL);
249 if (prtd == NULL) {
250 ret = -ENOMEM;
251 goto out;
252 }
253 runtime->private_data = prtd;
254
255 out:
256 return ret;
257}
258
259static int at91_pcm_close(struct snd_pcm_substream *substream)
260{
261 struct at91_runtime_data *prtd = substream->runtime->private_data;
262
263 kfree(prtd);
264 return 0;
265}
266
267static int at91_pcm_mmap(struct snd_pcm_substream *substream,
268 struct vm_area_struct *vma)
269{
270 struct snd_pcm_runtime *runtime = substream->runtime;
271
272 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
273 runtime->dma_area,
274 runtime->dma_addr,
275 runtime->dma_bytes);
276}
277
278struct snd_pcm_ops at91_pcm_ops = {
279 .open = at91_pcm_open,
280 .close = at91_pcm_close,
281 .ioctl = snd_pcm_lib_ioctl,
282 .hw_params = at91_pcm_hw_params,
283 .hw_free = at91_pcm_hw_free,
284 .prepare = at91_pcm_prepare,
285 .trigger = at91_pcm_trigger,
286 .pointer = at91_pcm_pointer,
287 .mmap = at91_pcm_mmap,
288};
289
290static int at91_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
291 int stream)
292{
293 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
294 struct snd_dma_buffer *buf = &substream->dma_buffer;
295 size_t size = at91_pcm_hardware.buffer_bytes_max;
296
297 buf->dev.type = SNDRV_DMA_TYPE_DEV;
298 buf->dev.dev = pcm->card->dev;
299 buf->private_data = NULL;
300 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
301 &buf->addr, GFP_KERNEL);
302
303 DBG("preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
304 (void *) buf->area,
305 (void *) buf->addr,
306 size);
307
308 if (!buf->area)
309 return -ENOMEM;
310
311 buf->bytes = size;
312 return 0;
313}
314
315static u64 at91_pcm_dmamask = 0xffffffff;
316
317static int at91_pcm_new(struct snd_card *card,
318 struct snd_soc_codec_dai *dai, struct snd_pcm *pcm)
319{
320 int ret = 0;
321
322 if (!card->dev->dma_mask)
323 card->dev->dma_mask = &at91_pcm_dmamask;
324 if (!card->dev->coherent_dma_mask)
325 card->dev->coherent_dma_mask = 0xffffffff;
326
327 if (dai->playback.channels_min) {
328 ret = at91_pcm_preallocate_dma_buffer(pcm,
329 SNDRV_PCM_STREAM_PLAYBACK);
330 if (ret)
331 goto out;
332 }
333
334 if (dai->capture.channels_min) {
335 ret = at91_pcm_preallocate_dma_buffer(pcm,
336 SNDRV_PCM_STREAM_CAPTURE);
337 if (ret)
338 goto out;
339 }
340 out:
341 return ret;
342}
343
344static void at91_pcm_free_dma_buffers(struct snd_pcm *pcm)
345{
346 struct snd_pcm_substream *substream;
347 struct snd_dma_buffer *buf;
348 int stream;
349
350 for (stream = 0; stream < 2; stream++) {
351 substream = pcm->streams[stream].substream;
352 if (!substream)
353 continue;
354
355 buf = &substream->dma_buffer;
356 if (!buf->area)
357 continue;
358
359 dma_free_writecombine(pcm->card->dev, buf->bytes,
360 buf->area, buf->addr);
361 buf->area = NULL;
362 }
363}
364
Frank Mandarino62970272007-02-02 17:19:24 +0100365#ifdef CONFIG_PM
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100366static int at91_pcm_suspend(struct platform_device *pdev,
367 struct snd_soc_cpu_dai *dai)
368{
369 struct snd_pcm_runtime *runtime = dai->runtime;
370 struct at91_runtime_data *prtd;
371 struct at91_pcm_dma_params *params;
372
373 if (!runtime)
374 return 0;
375
376 prtd = runtime->private_data;
377 params = prtd->params;
378
379 /* disable the PDC and save the PDC registers */
380
Frank Mandarinofa8f8782007-02-12 14:06:22 +0100381 at91_ssc_write(params->ssc_base + ATMEL_PDC_PTCR, params->mask->pdc_disable);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100382
383 prtd->pdc_xpr_save = at91_ssc_read(params->ssc_base + params->pdc->xpr);
384 prtd->pdc_xcr_save = at91_ssc_read(params->ssc_base + params->pdc->xcr);
385 prtd->pdc_xnpr_save = at91_ssc_read(params->ssc_base + params->pdc->xnpr);
386 prtd->pdc_xncr_save = at91_ssc_read(params->ssc_base + params->pdc->xncr);
387
388 return 0;
389}
390
391static int at91_pcm_resume(struct platform_device *pdev,
392 struct snd_soc_cpu_dai *dai)
393{
394 struct snd_pcm_runtime *runtime = dai->runtime;
395 struct at91_runtime_data *prtd;
396 struct at91_pcm_dma_params *params;
397
398 if (!runtime)
399 return 0;
400
401 prtd = runtime->private_data;
402 params = prtd->params;
403
404 /* restore the PDC registers and enable the PDC */
405 at91_ssc_write(params->ssc_base + params->pdc->xpr, prtd->pdc_xpr_save);
406 at91_ssc_write(params->ssc_base + params->pdc->xcr, prtd->pdc_xcr_save);
407 at91_ssc_write(params->ssc_base + params->pdc->xnpr, prtd->pdc_xnpr_save);
408 at91_ssc_write(params->ssc_base + params->pdc->xncr, prtd->pdc_xncr_save);
409
Frank Mandarinofa8f8782007-02-12 14:06:22 +0100410 at91_ssc_write(params->ssc_base + ATMEL_PDC_PTCR, params->mask->pdc_enable);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100411 return 0;
412}
Frank Mandarino62970272007-02-02 17:19:24 +0100413#else
414#define at91_pcm_suspend NULL
415#define at91_pcm_resume NULL
416#endif
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100417
418struct snd_soc_platform at91_soc_platform = {
419 .name = "at91-audio",
420 .pcm_ops = &at91_pcm_ops,
421 .pcm_new = at91_pcm_new,
422 .pcm_free = at91_pcm_free_dma_buffers,
423 .suspend = at91_pcm_suspend,
424 .resume = at91_pcm_resume,
425};
426
427EXPORT_SYMBOL_GPL(at91_soc_platform);
428
429MODULE_AUTHOR("Frank Mandarino <fmandarino@endrelia.com>");
430MODULE_DESCRIPTION("Atmel AT91 PCM module");
431MODULE_LICENSE("GPL");