diff options
Diffstat (limited to 'sound/soc/atmel/atmel-pcm.c')
-rw-r--r-- | sound/soc/atmel/atmel-pcm.c | 494 |
1 files changed, 494 insertions, 0 deletions
diff --git a/sound/soc/atmel/atmel-pcm.c b/sound/soc/atmel/atmel-pcm.c new file mode 100644 index 000000000000..1fac5efd285b --- /dev/null +++ b/sound/soc/atmel/atmel-pcm.c | |||
@@ -0,0 +1,494 @@ | |||
1 | /* | ||
2 | * atmel-pcm.c -- ALSA PCM interface for the Atmel atmel SoC. | ||
3 | * | ||
4 | * Copyright (C) 2005 SAN People | ||
5 | * Copyright (C) 2008 Atmel | ||
6 | * | ||
7 | * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com> | ||
8 | * | ||
9 | * Based on at91-pcm. by: | ||
10 | * Frank Mandarino <fmandarino@endrelia.com> | ||
11 | * Copyright 2006 Endrelia Technologies Inc. | ||
12 | * | ||
13 | * Based on pxa2xx-pcm.c by: | ||
14 | * | ||
15 | * Author: Nicolas Pitre | ||
16 | * Created: Nov 30, 2004 | ||
17 | * Copyright: (C) 2004 MontaVista Software, Inc. | ||
18 | * | ||
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. | ||
23 | * | ||
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. | ||
28 | * | ||
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 | ||
32 | */ | ||
33 | |||
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> | ||
41 | |||
42 | #include <sound/core.h> | ||
43 | #include <sound/pcm.h> | ||
44 | #include <sound/pcm_params.h> | ||
45 | #include <sound/soc.h> | ||
46 | |||
47 | #include <mach/hardware.h> | ||
48 | |||
49 | #include "atmel-pcm.h" | ||
50 | |||
51 | |||
52 | /*--------------------------------------------------------------------------*\ | ||
53 | * Hardware definition | ||
54 | \*--------------------------------------------------------------------------*/ | ||
55 | /* TODO: These values were taken from the AT91 platform driver, check | ||
56 | * them against real values for AT32 | ||
57 | */ | ||
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 | | ||
62 | SNDRV_PCM_INFO_PAUSE, | ||
63 | .formats = SNDRV_PCM_FMTBIT_S16_LE, | ||
64 | .period_bytes_min = 32, | ||
65 | .period_bytes_max = 8192, | ||
66 | .periods_min = 2, | ||
67 | .periods_max = 1024, | ||
68 | .buffer_bytes_max = 32 * 1024, | ||
69 | }; | ||
70 | |||
71 | |||
72 | /*--------------------------------------------------------------------------*\ | ||
73 | * Data types | ||
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 */ | ||
79 | size_t period_size; | ||
80 | |||
81 | dma_addr_t period_ptr; /* physical address of next period */ | ||
82 | int periods; /* period index of period_ptr */ | ||
83 | |||
84 | /* PDC register save */ | ||
85 | u32 pdc_xpr_save; | ||
86 | u32 pdc_xcr_save; | ||
87 | u32 pdc_xnpr_save; | ||
88 | u32 pdc_xncr_save; | ||
89 | }; | ||
90 | |||
91 | |||
92 | /*--------------------------------------------------------------------------*\ | ||
93 | * Helper functions | ||
94 | \*--------------------------------------------------------------------------*/ | ||
95 | static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, | ||
96 | int stream) | ||
97 | { | ||
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; | ||
101 | |||
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", | ||
109 | (void *) buf->area, | ||
110 | (void *) buf->addr, | ||
111 | size); | ||
112 | |||
113 | if (!buf->area) | ||
114 | return -ENOMEM; | ||
115 | |||
116 | buf->bytes = size; | ||
117 | return 0; | ||
118 | } | ||
119 | /*--------------------------------------------------------------------------*\ | ||
120 | * ISR | ||
121 | \*--------------------------------------------------------------------------*/ | ||
122 | static void atmel_pcm_dma_irq(u32 ssc_sr, | ||
123 | struct snd_pcm_substream *substream) | ||
124 | { | ||
125 | struct atmel_runtime_data *prtd = substream->runtime->private_data; | ||
126 | struct atmel_pcm_dma_params *params = prtd->params; | ||
127 | static int count; | ||
128 | |||
129 | count++; | ||
130 | |||
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); | ||
137 | |||
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; | ||
144 | |||
145 | ssc_writex(params->ssc->regs, params->pdc->xpr, | ||
146 | prtd->period_ptr); | ||
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); | ||
151 | } | ||
152 | |||
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; | ||
158 | |||
159 | ssc_writex(params->ssc->regs, params->pdc->xnpr, | ||
160 | prtd->period_ptr); | ||
161 | ssc_writex(params->ssc->regs, params->pdc->xncr, | ||
162 | prtd->period_size / params->pdc_xfer_size); | ||
163 | } | ||
164 | |||
165 | snd_pcm_period_elapsed(substream); | ||
166 | } | ||
167 | |||
168 | |||
169 | /*--------------------------------------------------------------------------*\ | ||
170 | * PCM operations | ||
171 | \*--------------------------------------------------------------------------*/ | ||
172 | static int atmel_pcm_hw_params(struct snd_pcm_substream *substream, | ||
173 | struct snd_pcm_hw_params *params) | ||
174 | { | ||
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; | ||
178 | |||
179 | /* this may get called several times by oss emulation | ||
180 | * with different params */ | ||
181 | |||
182 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); | ||
183 | runtime->dma_bytes = params_buffer_bytes(params); | ||
184 | |||
185 | prtd->params = rtd->dai->cpu_dai->dma_data; | ||
186 | prtd->params->dma_intr_handler = atmel_pcm_dma_irq; | ||
187 | |||
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); | ||
191 | |||
192 | pr_debug("atmel-pcm: " | ||
193 | "hw_params: DMA for %s initialized " | ||
194 | "(dma_bytes=%u, period_size=%u)\n", | ||
195 | prtd->params->name, | ||
196 | runtime->dma_bytes, | ||
197 | prtd->period_size); | ||
198 | return 0; | ||
199 | } | ||
200 | |||
201 | static int atmel_pcm_hw_free(struct snd_pcm_substream *substream) | ||
202 | { | ||
203 | struct atmel_runtime_data *prtd = substream->runtime->private_data; | ||
204 | struct atmel_pcm_dma_params *params = prtd->params; | ||
205 | |||
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; | ||
210 | } | ||
211 | |||
212 | return 0; | ||
213 | } | ||
214 | |||
215 | static int atmel_pcm_prepare(struct snd_pcm_substream *substream) | ||
216 | { | ||
217 | struct atmel_runtime_data *prtd = substream->runtime->private_data; | ||
218 | struct atmel_pcm_dma_params *params = prtd->params; | ||
219 | |||
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); | ||
224 | return 0; | ||
225 | } | ||
226 | |||
227 | static int atmel_pcm_trigger(struct snd_pcm_substream *substream, | ||
228 | int cmd) | ||
229 | { | ||
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; | ||
233 | int ret = 0; | ||
234 | |||
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); | ||
238 | |||
239 | switch (cmd) { | ||
240 | case SNDRV_PCM_TRIGGER_START: | ||
241 | prtd->period_ptr = prtd->dma_buffer; | ||
242 | |||
243 | ssc_writex(params->ssc->regs, params->pdc->xpr, | ||
244 | prtd->period_ptr); | ||
245 | ssc_writex(params->ssc->regs, params->pdc->xcr, | ||
246 | prtd->period_size / params->pdc_xfer_size); | ||
247 | |||
248 | prtd->period_ptr += prtd->period_size; | ||
249 | ssc_writex(params->ssc->regs, params->pdc->xnpr, | ||
250 | prtd->period_ptr); | ||
251 | ssc_writex(params->ssc->regs, params->pdc->xncr, | ||
252 | prtd->period_size / params->pdc_xfer_size); | ||
253 | |||
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)); | ||
262 | |||
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); | ||
267 | |||
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 */ | ||
272 | |||
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); | ||
278 | break; | ||
279 | |||
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); | ||
284 | break; | ||
285 | |||
286 | default: | ||
287 | ret = -EINVAL; | ||
288 | } | ||
289 | |||
290 | return ret; | ||
291 | } | ||
292 | |||
293 | static snd_pcm_uframes_t atmel_pcm_pointer( | ||
294 | struct snd_pcm_substream *substream) | ||
295 | { | ||
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; | ||
299 | dma_addr_t ptr; | ||
300 | snd_pcm_uframes_t x; | ||
301 | |||
302 | ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr); | ||
303 | x = bytes_to_frames(runtime, ptr - prtd->dma_buffer); | ||
304 | |||
305 | if (x == runtime->buffer_size) | ||
306 | x = 0; | ||
307 | |||
308 | return x; | ||
309 | } | ||
310 | |||
311 | static int atmel_pcm_open(struct snd_pcm_substream *substream) | ||
312 | { | ||
313 | struct snd_pcm_runtime *runtime = substream->runtime; | ||
314 | struct atmel_runtime_data *prtd; | ||
315 | int ret = 0; | ||
316 | |||
317 | snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware); | ||
318 | |||
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); | ||
322 | if (ret < 0) | ||
323 | goto out; | ||
324 | |||
325 | prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL); | ||
326 | if (prtd == NULL) { | ||
327 | ret = -ENOMEM; | ||
328 | goto out; | ||
329 | } | ||
330 | runtime->private_data = prtd; | ||
331 | |||
332 | out: | ||
333 | return ret; | ||
334 | } | ||
335 | |||
336 | static int atmel_pcm_close(struct snd_pcm_substream *substream) | ||
337 | { | ||
338 | struct atmel_runtime_data *prtd = substream->runtime->private_data; | ||
339 | |||
340 | kfree(prtd); | ||
341 | return 0; | ||
342 | } | ||
343 | |||
344 | static int atmel_pcm_mmap(struct snd_pcm_substream *substream, | ||
345 | struct vm_area_struct *vma) | ||
346 | { | ||
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); | ||
350 | } | ||
351 | |||
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, | ||
362 | }; | ||
363 | |||
364 | |||
365 | /*--------------------------------------------------------------------------*\ | ||
366 | * ASoC platform driver | ||
367 | \*--------------------------------------------------------------------------*/ | ||
368 | static u64 atmel_pcm_dmamask = 0xffffffff; | ||
369 | |||
370 | static int atmel_pcm_new(struct snd_card *card, | ||
371 | struct snd_soc_dai *dai, struct snd_pcm *pcm) | ||
372 | { | ||
373 | int ret = 0; | ||
374 | |||
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; | ||
379 | |||
380 | if (dai->playback.channels_min) { | ||
381 | ret = atmel_pcm_preallocate_dma_buffer(pcm, | ||
382 | SNDRV_PCM_STREAM_PLAYBACK); | ||
383 | if (ret) | ||
384 | goto out; | ||
385 | } | ||
386 | |||
387 | if (dai->capture.channels_min) { | ||
388 | pr_debug("at32-pcm:" | ||
389 | "Allocating PCM capture DMA buffer\n"); | ||
390 | ret = atmel_pcm_preallocate_dma_buffer(pcm, | ||
391 | SNDRV_PCM_STREAM_CAPTURE); | ||
392 | if (ret) | ||
393 | goto out; | ||
394 | } | ||
395 | out: | ||
396 | return ret; | ||
397 | } | ||
398 | |||
399 | static void atmel_pcm_free_dma_buffers(struct snd_pcm *pcm) | ||
400 | { | ||
401 | struct snd_pcm_substream *substream; | ||
402 | struct snd_dma_buffer *buf; | ||
403 | int stream; | ||
404 | |||
405 | for (stream = 0; stream < 2; stream++) { | ||
406 | substream = pcm->streams[stream].substream; | ||
407 | if (!substream) | ||
408 | continue; | ||
409 | |||
410 | buf = &substream->dma_buffer; | ||
411 | if (!buf->area) | ||
412 | continue; | ||
413 | dma_free_coherent(pcm->card->dev, buf->bytes, | ||
414 | buf->area, buf->addr); | ||
415 | buf->area = NULL; | ||
416 | } | ||
417 | } | ||
418 | |||
419 | #ifdef CONFIG_PM | ||
420 | static int atmel_pcm_suspend(struct snd_soc_dai *dai) | ||
421 | { | ||
422 | struct snd_pcm_runtime *runtime = dai->runtime; | ||
423 | struct atmel_runtime_data *prtd; | ||
424 | struct atmel_pcm_dma_params *params; | ||
425 | |||
426 | if (!runtime) | ||
427 | return 0; | ||
428 | |||
429 | prtd = runtime->private_data; | ||
430 | params = prtd->params; | ||
431 | |||
432 | /* disable the PDC and save the PDC registers */ | ||
433 | |||
434 | ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_disable); | ||
435 | |||
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); | ||
440 | |||
441 | return 0; | ||
442 | } | ||
443 | |||
444 | static int atmel_pcm_resume(struct snd_soc_dai *dai) | ||
445 | { | ||
446 | struct snd_pcm_runtime *runtime = dai->runtime; | ||
447 | struct atmel_runtime_data *prtd; | ||
448 | struct atmel_pcm_dma_params *params; | ||
449 | |||
450 | if (!runtime) | ||
451 | return 0; | ||
452 | |||
453 | prtd = runtime->private_data; | ||
454 | params = prtd->params; | ||
455 | |||
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); | ||
461 | |||
462 | ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_enable); | ||
463 | return 0; | ||
464 | } | ||
465 | #else | ||
466 | #define atmel_pcm_suspend NULL | ||
467 | #define atmel_pcm_resume NULL | ||
468 | #endif | ||
469 | |||
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, | ||
477 | }; | ||
478 | EXPORT_SYMBOL_GPL(atmel_soc_platform); | ||
479 | |||
480 | static int __init atmel_pcm_modinit(void) | ||
481 | { | ||
482 | return snd_soc_register_platform(&atmel_soc_platform); | ||
483 | } | ||
484 | module_init(atmel_pcm_modinit); | ||
485 | |||
486 | static void __exit atmel_pcm_modexit(void) | ||
487 | { | ||
488 | snd_soc_unregister_platform(&atmel_soc_platform); | ||
489 | } | ||
490 | module_exit(atmel_pcm_modexit); | ||
491 | |||
492 | MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>"); | ||
493 | MODULE_DESCRIPTION("Atmel PCM module"); | ||
494 | MODULE_LICENSE("GPL"); | ||