2 * linux/sound/arm/pxa2xx-pcm.c -- ALSA PCM interface for the Intel PXA2xx chip
4 * Author: Nicolas Pitre
5 * Created: Nov 30, 2004
6 * Copyright: (C) 2004 MontaVista Software, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/dma-mapping.h>
19 #include <sound/driver.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
26 #include <asm/hardware.h>
27 #include <asm/arch/pxa-regs.h>
28 #include <asm/arch/audio.h>
30 #include "pxa2xx-pcm.h"
32 static const struct snd_pcm_hardware pxa2xx_pcm_hardware
= {
33 .info
= SNDRV_PCM_INFO_MMAP
|
34 SNDRV_PCM_INFO_MMAP_VALID
|
35 SNDRV_PCM_INFO_INTERLEAVED
|
36 SNDRV_PCM_INFO_PAUSE
|
37 SNDRV_PCM_INFO_RESUME
,
38 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
39 SNDRV_PCM_FMTBIT_S24_LE
|
40 SNDRV_PCM_FMTBIT_S32_LE
,
41 .period_bytes_min
= 32,
42 .period_bytes_max
= 8192 - 32,
44 .periods_max
= PAGE_SIZE
/sizeof(pxa_dma_desc
),
45 .buffer_bytes_max
= 128 * 1024,
49 struct pxa2xx_runtime_data
{
51 struct pxa2xx_pcm_dma_params
*params
;
52 pxa_dma_desc
*dma_desc_array
;
53 dma_addr_t dma_desc_array_phys
;
56 static void pxa2xx_pcm_dma_irq(int dma_ch
, void *dev_id
)
58 struct snd_pcm_substream
*substream
= dev_id
;
59 struct pxa2xx_runtime_data
*prtd
= substream
->runtime
->private_data
;
63 DCSR(dma_ch
) = dcsr
& ~DCSR_STOPIRQEN
;
65 if (dcsr
& DCSR_ENDINTR
) {
66 snd_pcm_period_elapsed(substream
);
68 printk( KERN_ERR
"%s: DMA error on channel %d (DCSR=%#x)\n",
69 prtd
->params
->name
, dma_ch
, dcsr
);
73 static int pxa2xx_pcm_hw_params(struct snd_pcm_substream
*substream
,
74 struct snd_pcm_hw_params
*params
)
76 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
77 struct pxa2xx_runtime_data
*prtd
= runtime
->private_data
;
78 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
79 struct pxa2xx_pcm_dma_params
*dma
= rtd
->cpu_dai
->dma_data
;
80 size_t totsize
= params_buffer_bytes(params
);
81 size_t period
= params_period_bytes(params
);
82 pxa_dma_desc
*dma_desc
;
83 dma_addr_t dma_buff_phys
, next_desc_phys
;
86 /* this may get called several times by oss emulation
87 * with different params */
88 if (prtd
->params
== NULL
) {
90 ret
= pxa_request_dma(prtd
->params
->name
, DMA_PRIO_LOW
,
91 pxa2xx_pcm_dma_irq
, substream
);
95 } else if (prtd
->params
!= dma
) {
96 pxa_free_dma(prtd
->dma_ch
);
98 ret
= pxa_request_dma(prtd
->params
->name
, DMA_PRIO_LOW
,
99 pxa2xx_pcm_dma_irq
, substream
);
105 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
106 runtime
->dma_bytes
= totsize
;
108 dma_desc
= prtd
->dma_desc_array
;
109 next_desc_phys
= prtd
->dma_desc_array_phys
;
110 dma_buff_phys
= runtime
->dma_addr
;
112 next_desc_phys
+= sizeof(pxa_dma_desc
);
113 dma_desc
->ddadr
= next_desc_phys
;
114 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
115 dma_desc
->dsadr
= dma_buff_phys
;
116 dma_desc
->dtadr
= prtd
->params
->dev_addr
;
118 dma_desc
->dsadr
= prtd
->params
->dev_addr
;
119 dma_desc
->dtadr
= dma_buff_phys
;
121 if (period
> totsize
)
123 dma_desc
->dcmd
= prtd
->params
->dcmd
| period
| DCMD_ENDIRQEN
;
125 dma_buff_phys
+= period
;
126 } while (totsize
-= period
);
127 dma_desc
[-1].ddadr
= prtd
->dma_desc_array_phys
;
132 static int pxa2xx_pcm_hw_free(struct snd_pcm_substream
*substream
)
134 struct pxa2xx_runtime_data
*prtd
= substream
->runtime
->private_data
;
136 if (prtd
&& prtd
->params
)
137 *prtd
->params
->drcmr
= 0;
140 snd_pcm_set_runtime_buffer(substream
, NULL
);
141 pxa_free_dma(prtd
->dma_ch
);
148 static int pxa2xx_pcm_prepare(struct snd_pcm_substream
*substream
)
150 struct pxa2xx_runtime_data
*prtd
= substream
->runtime
->private_data
;
152 DCSR(prtd
->dma_ch
) &= ~DCSR_RUN
;
153 DCSR(prtd
->dma_ch
) = 0;
154 DCMD(prtd
->dma_ch
) = 0;
155 *prtd
->params
->drcmr
= prtd
->dma_ch
| DRCMR_MAPVLD
;
160 static int pxa2xx_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
162 struct pxa2xx_runtime_data
*prtd
= substream
->runtime
->private_data
;
166 case SNDRV_PCM_TRIGGER_START
:
167 DDADR(prtd
->dma_ch
) = prtd
->dma_desc_array_phys
;
168 DCSR(prtd
->dma_ch
) = DCSR_RUN
;
171 case SNDRV_PCM_TRIGGER_STOP
:
172 case SNDRV_PCM_TRIGGER_SUSPEND
:
173 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
174 DCSR(prtd
->dma_ch
) &= ~DCSR_RUN
;
177 case SNDRV_PCM_TRIGGER_RESUME
:
178 DCSR(prtd
->dma_ch
) |= DCSR_RUN
;
180 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
181 DDADR(prtd
->dma_ch
) = prtd
->dma_desc_array_phys
;
182 DCSR(prtd
->dma_ch
) |= DCSR_RUN
;
192 static snd_pcm_uframes_t
193 pxa2xx_pcm_pointer(struct snd_pcm_substream
*substream
)
195 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
196 struct pxa2xx_runtime_data
*prtd
= runtime
->private_data
;
198 dma_addr_t ptr
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) ?
199 DSADR(prtd
->dma_ch
) : DTADR(prtd
->dma_ch
);
200 snd_pcm_uframes_t x
= bytes_to_frames(runtime
, ptr
- runtime
->dma_addr
);
202 if (x
== runtime
->buffer_size
)
207 static int pxa2xx_pcm_open(struct snd_pcm_substream
*substream
)
209 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
210 struct pxa2xx_runtime_data
*prtd
;
213 snd_soc_set_runtime_hwparams(substream
, &pxa2xx_pcm_hardware
);
216 * For mysterious reasons (and despite what the manual says)
217 * playback samples are lost if the DMA count is not a multiple
218 * of the DMA burst size. Let's add a rule to enforce that.
220 ret
= snd_pcm_hw_constraint_step(runtime
, 0,
221 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 32);
225 ret
= snd_pcm_hw_constraint_step(runtime
, 0,
226 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 32);
230 prtd
= kzalloc(sizeof(struct pxa2xx_runtime_data
), GFP_KERNEL
);
236 prtd
->dma_desc_array
=
237 dma_alloc_writecombine(substream
->pcm
->card
->dev
, PAGE_SIZE
,
238 &prtd
->dma_desc_array_phys
, GFP_KERNEL
);
239 if (!prtd
->dma_desc_array
) {
244 runtime
->private_data
= prtd
;
253 static int pxa2xx_pcm_close(struct snd_pcm_substream
*substream
)
255 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
256 struct pxa2xx_runtime_data
*prtd
= runtime
->private_data
;
258 dma_free_writecombine(substream
->pcm
->card
->dev
, PAGE_SIZE
,
259 prtd
->dma_desc_array
, prtd
->dma_desc_array_phys
);
264 static int pxa2xx_pcm_mmap(struct snd_pcm_substream
*substream
,
265 struct vm_area_struct
*vma
)
267 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
268 return dma_mmap_writecombine(substream
->pcm
->card
->dev
, vma
,
274 struct snd_pcm_ops pxa2xx_pcm_ops
= {
275 .open
= pxa2xx_pcm_open
,
276 .close
= pxa2xx_pcm_close
,
277 .ioctl
= snd_pcm_lib_ioctl
,
278 .hw_params
= pxa2xx_pcm_hw_params
,
279 .hw_free
= pxa2xx_pcm_hw_free
,
280 .prepare
= pxa2xx_pcm_prepare
,
281 .trigger
= pxa2xx_pcm_trigger
,
282 .pointer
= pxa2xx_pcm_pointer
,
283 .mmap
= pxa2xx_pcm_mmap
,
286 static int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm
*pcm
, int stream
)
288 struct snd_pcm_substream
*substream
= pcm
->streams
[stream
].substream
;
289 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
290 size_t size
= pxa2xx_pcm_hardware
.buffer_bytes_max
;
291 buf
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
292 buf
->dev
.dev
= pcm
->card
->dev
;
293 buf
->private_data
= NULL
;
294 buf
->area
= dma_alloc_writecombine(pcm
->card
->dev
, size
,
295 &buf
->addr
, GFP_KERNEL
);
302 static void pxa2xx_pcm_free_dma_buffers(struct snd_pcm
*pcm
)
304 struct snd_pcm_substream
*substream
;
305 struct snd_dma_buffer
*buf
;
308 for (stream
= 0; stream
< 2; stream
++) {
309 substream
= pcm
->streams
[stream
].substream
;
313 buf
= &substream
->dma_buffer
;
317 dma_free_writecombine(pcm
->card
->dev
, buf
->bytes
,
318 buf
->area
, buf
->addr
);
323 static u64 pxa2xx_pcm_dmamask
= DMA_32BIT_MASK
;
325 int pxa2xx_pcm_new(struct snd_card
*card
, struct snd_soc_codec_dai
*dai
,
330 if (!card
->dev
->dma_mask
)
331 card
->dev
->dma_mask
= &pxa2xx_pcm_dmamask
;
332 if (!card
->dev
->coherent_dma_mask
)
333 card
->dev
->coherent_dma_mask
= DMA_32BIT_MASK
;
335 if (dai
->playback
.channels_min
) {
336 ret
= pxa2xx_pcm_preallocate_dma_buffer(pcm
,
337 SNDRV_PCM_STREAM_PLAYBACK
);
342 if (dai
->capture
.channels_min
) {
343 ret
= pxa2xx_pcm_preallocate_dma_buffer(pcm
,
344 SNDRV_PCM_STREAM_CAPTURE
);
352 struct snd_soc_platform pxa2xx_soc_platform
= {
353 .name
= "pxa2xx-audio",
354 .pcm_ops
= &pxa2xx_pcm_ops
,
355 .pcm_new
= pxa2xx_pcm_new
,
356 .pcm_free
= pxa2xx_pcm_free_dma_buffers
,
359 EXPORT_SYMBOL_GPL(pxa2xx_soc_platform
);
361 MODULE_AUTHOR("Nicolas Pitre");
362 MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module");
363 MODULE_LICENSE("GPL");