2 * at91-pcm.c -- ALSA PCM interface for the Atmel AT91 SoC
4 * Author: Frank Mandarino <fmandarino@endrelia.com>
5 * Endrelia Technologies Inc.
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>
24 #include <linux/atmel_pdc.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/hardware.h>
32 #include <asm/arch/at91_ssc.h>
37 #define DBG(x...) printk(KERN_INFO "at91-pcm: " x)
42 static 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
|
47 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
48 .period_bytes_min
= 32,
49 .period_bytes_max
= 8192,
52 .buffer_bytes_max
= 32 * 1024,
55 struct 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 */
60 dma_addr_t period_ptr
; /* physical address of next period */
61 u32 pdc_xpr_save
; /* PDC register save */
67 static void at91_pcm_dma_irq(u32 ssc_sr
,
68 struct snd_pcm_substream
*substream
)
70 struct at91_runtime_data
*prtd
= substream
->runtime
->private_data
;
71 struct at91_pcm_dma_params
*params
= prtd
->params
;
76 if (ssc_sr
& params
->mask
->ssc_endbuf
) {
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
);
84 /* re-start the PDC */
85 at91_ssc_write(params
->ssc_base
+ ATMEL_PDC_PTCR
, params
->mask
->pdc_disable
);
87 prtd
->period_ptr
+= prtd
->period_size
;
88 if (prtd
->period_ptr
>= prtd
->dma_buffer_end
) {
89 prtd
->period_ptr
= prtd
->dma_buffer
;
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
);
96 at91_ssc_write(params
->ssc_base
+ ATMEL_PDC_PTCR
, params
->mask
->pdc_enable
);
99 if (ssc_sr
& params
->mask
->ssc_endx
) {
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
;
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
);
111 snd_pcm_period_elapsed(substream
);
114 static int at91_pcm_hw_params(struct snd_pcm_substream
*substream
,
115 struct snd_pcm_hw_params
*params
)
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
;
121 /* this may get called several times by oss emulation
122 * with different params */
124 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
125 runtime
->dma_bytes
= params_buffer_bytes(params
);
127 prtd
->params
= rtd
->dai
->cpu_dai
->dma_data
;
128 prtd
->params
->dma_intr_handler
= at91_pcm_dma_irq
;
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
);
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
);
139 static int at91_pcm_hw_free(struct snd_pcm_substream
*substream
)
141 struct at91_runtime_data
*prtd
= substream
->runtime
->private_data
;
142 struct at91_pcm_dma_params
*params
= prtd
->params
;
144 if (params
!= NULL
) {
145 at91_ssc_write(params
->ssc_base
+ ATMEL_PDC_PTCR
, params
->mask
->pdc_disable
);
146 prtd
->params
->dma_intr_handler
= NULL
;
152 static int at91_pcm_prepare(struct snd_pcm_substream
*substream
)
154 struct at91_runtime_data
*prtd
= substream
->runtime
->private_data
;
155 struct at91_pcm_dma_params
*params
= prtd
->params
;
157 at91_ssc_write(params
->ssc_base
+ AT91_SSC_IDR
,
158 params
->mask
->ssc_endx
| params
->mask
->ssc_endbuf
);
160 at91_ssc_write(params
->ssc_base
+ ATMEL_PDC_PTCR
, params
->mask
->pdc_disable
);
164 static int at91_pcm_trigger(struct snd_pcm_substream
*substream
,
167 struct at91_runtime_data
*prtd
= substream
->runtime
->private_data
;
168 struct at91_pcm_dma_params
*params
= prtd
->params
;
172 case SNDRV_PCM_TRIGGER_START
:
173 prtd
->period_ptr
= prtd
->dma_buffer
;
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
);
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
);
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
));
191 at91_ssc_write(params
->ssc_base
+ AT91_SSC_IER
,
192 params
->mask
->ssc_endx
| params
->mask
->ssc_endbuf
);
194 at91_ssc_write(params
->ssc_base
+ ATMEL_PDC_PTCR
, params
->mask
->pdc_enable
);
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
));
200 case SNDRV_PCM_TRIGGER_STOP
:
201 case SNDRV_PCM_TRIGGER_SUSPEND
:
202 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
203 at91_ssc_write(params
->ssc_base
+ ATMEL_PDC_PTCR
, params
->mask
->pdc_disable
);
206 case SNDRV_PCM_TRIGGER_RESUME
:
207 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
208 at91_ssc_write(params
->ssc_base
+ ATMEL_PDC_PTCR
, params
->mask
->pdc_enable
);
218 static snd_pcm_uframes_t
at91_pcm_pointer(
219 struct snd_pcm_substream
*substream
)
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
;
227 ptr
= (dma_addr_t
) at91_ssc_read(params
->ssc_base
+ params
->pdc
->xpr
);
228 x
= bytes_to_frames(runtime
, ptr
- prtd
->dma_buffer
);
230 if (x
== runtime
->buffer_size
)
235 static int at91_pcm_open(struct snd_pcm_substream
*substream
)
237 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
238 struct at91_runtime_data
*prtd
;
241 snd_soc_set_runtime_hwparams(substream
, &at91_pcm_hardware
);
243 /* ensure that buffer size is a multiple of period size */
244 ret
= snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
248 prtd
= kzalloc(sizeof(struct at91_runtime_data
), GFP_KERNEL
);
253 runtime
->private_data
= prtd
;
259 static int at91_pcm_close(struct snd_pcm_substream
*substream
)
261 struct at91_runtime_data
*prtd
= substream
->runtime
->private_data
;
267 static int at91_pcm_mmap(struct snd_pcm_substream
*substream
,
268 struct vm_area_struct
*vma
)
270 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
272 return dma_mmap_writecombine(substream
->pcm
->card
->dev
, vma
,
278 struct 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
,
290 static int at91_pcm_preallocate_dma_buffer(struct snd_pcm
*pcm
,
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
;
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
);
303 DBG("preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
315 static u64 at91_pcm_dmamask
= 0xffffffff;
317 static int at91_pcm_new(struct snd_card
*card
,
318 struct snd_soc_codec_dai
*dai
, struct snd_pcm
*pcm
)
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;
327 if (dai
->playback
.channels_min
) {
328 ret
= at91_pcm_preallocate_dma_buffer(pcm
,
329 SNDRV_PCM_STREAM_PLAYBACK
);
334 if (dai
->capture
.channels_min
) {
335 ret
= at91_pcm_preallocate_dma_buffer(pcm
,
336 SNDRV_PCM_STREAM_CAPTURE
);
344 static void at91_pcm_free_dma_buffers(struct snd_pcm
*pcm
)
346 struct snd_pcm_substream
*substream
;
347 struct snd_dma_buffer
*buf
;
350 for (stream
= 0; stream
< 2; stream
++) {
351 substream
= pcm
->streams
[stream
].substream
;
355 buf
= &substream
->dma_buffer
;
359 dma_free_writecombine(pcm
->card
->dev
, buf
->bytes
,
360 buf
->area
, buf
->addr
);
366 static int at91_pcm_suspend(struct platform_device
*pdev
,
367 struct snd_soc_cpu_dai
*dai
)
369 struct snd_pcm_runtime
*runtime
= dai
->runtime
;
370 struct at91_runtime_data
*prtd
;
371 struct at91_pcm_dma_params
*params
;
376 prtd
= runtime
->private_data
;
377 params
= prtd
->params
;
379 /* disable the PDC and save the PDC registers */
381 at91_ssc_write(params
->ssc_base
+ ATMEL_PDC_PTCR
, params
->mask
->pdc_disable
);
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
);
391 static int at91_pcm_resume(struct platform_device
*pdev
,
392 struct snd_soc_cpu_dai
*dai
)
394 struct snd_pcm_runtime
*runtime
= dai
->runtime
;
395 struct at91_runtime_data
*prtd
;
396 struct at91_pcm_dma_params
*params
;
401 prtd
= runtime
->private_data
;
402 params
= prtd
->params
;
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
);
410 at91_ssc_write(params
->ssc_base
+ ATMEL_PDC_PTCR
, params
->mask
->pdc_enable
);
414 #define at91_pcm_suspend NULL
415 #define at91_pcm_resume NULL
418 struct 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
,
427 EXPORT_SYMBOL_GPL(at91_soc_platform
);
429 MODULE_AUTHOR("Frank Mandarino <fmandarino@endrelia.com>");
430 MODULE_DESCRIPTION("Atmel AT91 PCM module");
431 MODULE_LICENSE("GPL");