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/device.h>
16 #include <linux/slab.h>
17 #include <linux/dma-mapping.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
24 #include <asm/hardware.h>
25 #include <asm/arch/pxa-regs.h>
27 #include "pxa2xx-pcm.h"
30 static const struct snd_pcm_hardware pxa2xx_pcm_hardware
= {
31 .info
= SNDRV_PCM_INFO_MMAP
|
32 SNDRV_PCM_INFO_MMAP_VALID
|
33 SNDRV_PCM_INFO_INTERLEAVED
|
35 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
36 .period_bytes_min
= 32,
37 .period_bytes_max
= 8192 - 32,
39 .periods_max
= PAGE_SIZE
/sizeof(pxa_dma_desc
),
40 .buffer_bytes_max
= 128 * 1024,
44 struct pxa2xx_runtime_data
{
46 struct pxa2xx_pcm_dma_params
*params
;
47 pxa_dma_desc
*dma_desc_array
;
48 dma_addr_t dma_desc_array_phys
;
51 static int pxa2xx_pcm_hw_params(struct snd_pcm_substream
*substream
,
52 struct snd_pcm_hw_params
*params
)
54 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
55 struct pxa2xx_runtime_data
*rtd
= runtime
->private_data
;
56 size_t totsize
= params_buffer_bytes(params
);
57 size_t period
= params_period_bytes(params
);
58 pxa_dma_desc
*dma_desc
;
59 dma_addr_t dma_buff_phys
, next_desc_phys
;
61 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
62 runtime
->dma_bytes
= totsize
;
64 dma_desc
= rtd
->dma_desc_array
;
65 next_desc_phys
= rtd
->dma_desc_array_phys
;
66 dma_buff_phys
= runtime
->dma_addr
;
68 next_desc_phys
+= sizeof(pxa_dma_desc
);
69 dma_desc
->ddadr
= next_desc_phys
;
70 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
71 dma_desc
->dsadr
= dma_buff_phys
;
72 dma_desc
->dtadr
= rtd
->params
->dev_addr
;
74 dma_desc
->dsadr
= rtd
->params
->dev_addr
;
75 dma_desc
->dtadr
= dma_buff_phys
;
79 dma_desc
->dcmd
= rtd
->params
->dcmd
| period
| DCMD_ENDIRQEN
;
81 dma_buff_phys
+= period
;
82 } while (totsize
-= period
);
83 dma_desc
[-1].ddadr
= rtd
->dma_desc_array_phys
;
88 static int pxa2xx_pcm_hw_free(struct snd_pcm_substream
*substream
)
90 struct pxa2xx_runtime_data
*rtd
= substream
->runtime
->private_data
;
92 *rtd
->params
->drcmr
= 0;
93 snd_pcm_set_runtime_buffer(substream
, NULL
);
97 static int pxa2xx_pcm_prepare(struct snd_pcm_substream
*substream
)
99 struct pxa2xx_pcm_client
*client
= substream
->private_data
;
100 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
101 struct pxa2xx_runtime_data
*rtd
= runtime
->private_data
;
103 DCSR(rtd
->dma_ch
) &= ~DCSR_RUN
;
104 DCSR(rtd
->dma_ch
) = 0;
105 DCMD(rtd
->dma_ch
) = 0;
106 *rtd
->params
->drcmr
= rtd
->dma_ch
| DRCMR_MAPVLD
;
108 return client
->prepare(substream
);
111 static int pxa2xx_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
113 struct pxa2xx_runtime_data
*rtd
= substream
->runtime
->private_data
;
117 case SNDRV_PCM_TRIGGER_START
:
118 DDADR(rtd
->dma_ch
) = rtd
->dma_desc_array_phys
;
119 DCSR(rtd
->dma_ch
) = DCSR_RUN
;
122 case SNDRV_PCM_TRIGGER_STOP
:
123 case SNDRV_PCM_TRIGGER_SUSPEND
:
124 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
125 DCSR(rtd
->dma_ch
) &= ~DCSR_RUN
;
128 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
129 DCSR(rtd
->dma_ch
) |= DCSR_RUN
;
139 static void pxa2xx_pcm_dma_irq(int dma_ch
, void *dev_id
)
141 struct snd_pcm_substream
*substream
= dev_id
;
142 struct pxa2xx_runtime_data
*rtd
= substream
->runtime
->private_data
;
146 DCSR(dma_ch
) = dcsr
& ~DCSR_STOPIRQEN
;
148 if (dcsr
& DCSR_ENDINTR
) {
149 snd_pcm_period_elapsed(substream
);
151 printk( KERN_ERR
"%s: DMA error on channel %d (DCSR=%#x)\n",
152 rtd
->params
->name
, dma_ch
, dcsr
);
153 snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
157 static snd_pcm_uframes_t
pxa2xx_pcm_pointer(struct snd_pcm_substream
*substream
)
159 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
160 struct pxa2xx_runtime_data
*rtd
= runtime
->private_data
;
161 dma_addr_t ptr
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) ?
162 DSADR(rtd
->dma_ch
) : DTADR(rtd
->dma_ch
);
163 snd_pcm_uframes_t x
= bytes_to_frames(runtime
, ptr
- runtime
->dma_addr
);
164 if (x
== runtime
->buffer_size
)
170 pxa2xx_pcm_hw_rule_mult32(struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
172 struct snd_interval
*i
= hw_param_interval(params
, rule
->var
);
176 i
->min
= (i
->min
& ~31) + 32;
190 static int pxa2xx_pcm_open(struct snd_pcm_substream
*substream
)
192 struct pxa2xx_pcm_client
*client
= substream
->private_data
;
193 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
194 struct pxa2xx_runtime_data
*rtd
;
197 runtime
->hw
= pxa2xx_pcm_hardware
;
200 * For mysterious reasons (and despite what the manual says)
201 * playback samples are lost if the DMA count is not a multiple
202 * of the DMA burst size. Let's add a rule to enforce that.
204 ret
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
205 pxa2xx_pcm_hw_rule_mult32
, NULL
,
206 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, -1);
209 ret
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
210 pxa2xx_pcm_hw_rule_mult32
, NULL
,
211 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
216 rtd
= kmalloc(sizeof(*rtd
), GFP_KERNEL
);
219 rtd
->dma_desc_array
=
220 dma_alloc_writecombine(substream
->pcm
->card
->dev
, PAGE_SIZE
,
221 &rtd
->dma_desc_array_phys
, GFP_KERNEL
);
222 if (!rtd
->dma_desc_array
)
225 rtd
->params
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) ?
226 client
->playback_params
: client
->capture_params
;
227 ret
= pxa_request_dma(rtd
->params
->name
, DMA_PRIO_LOW
,
228 pxa2xx_pcm_dma_irq
, substream
);
233 runtime
->private_data
= rtd
;
234 ret
= client
->startup(substream
);
238 pxa_free_dma(rtd
->dma_ch
);
240 dma_free_writecombine(substream
->pcm
->card
->dev
, PAGE_SIZE
,
241 rtd
->dma_desc_array
, rtd
->dma_desc_array_phys
);
248 static int pxa2xx_pcm_close(struct snd_pcm_substream
*substream
)
250 struct pxa2xx_pcm_client
*client
= substream
->private_data
;
251 struct pxa2xx_runtime_data
*rtd
= substream
->runtime
->private_data
;
253 pxa_free_dma(rtd
->dma_ch
);
254 client
->shutdown(substream
);
255 dma_free_writecombine(substream
->pcm
->card
->dev
, PAGE_SIZE
,
256 rtd
->dma_desc_array
, rtd
->dma_desc_array_phys
);
262 pxa2xx_pcm_mmap(struct snd_pcm_substream
*substream
, struct vm_area_struct
*vma
)
264 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
265 return dma_mmap_writecombine(substream
->pcm
->card
->dev
, vma
,
271 static struct snd_pcm_ops pxa2xx_pcm_ops
= {
272 .open
= pxa2xx_pcm_open
,
273 .close
= pxa2xx_pcm_close
,
274 .ioctl
= snd_pcm_lib_ioctl
,
275 .hw_params
= pxa2xx_pcm_hw_params
,
276 .hw_free
= pxa2xx_pcm_hw_free
,
277 .prepare
= pxa2xx_pcm_prepare
,
278 .trigger
= pxa2xx_pcm_trigger
,
279 .pointer
= pxa2xx_pcm_pointer
,
280 .mmap
= pxa2xx_pcm_mmap
,
283 static int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm
*pcm
, int stream
)
285 struct snd_pcm_substream
*substream
= pcm
->streams
[stream
].substream
;
286 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
287 size_t size
= pxa2xx_pcm_hardware
.buffer_bytes_max
;
288 buf
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
289 buf
->dev
.dev
= pcm
->card
->dev
;
290 buf
->private_data
= NULL
;
291 buf
->area
= dma_alloc_writecombine(pcm
->card
->dev
, size
,
292 &buf
->addr
, GFP_KERNEL
);
299 static void pxa2xx_pcm_free_dma_buffers(struct snd_pcm
*pcm
)
301 struct snd_pcm_substream
*substream
;
302 struct snd_dma_buffer
*buf
;
305 for (stream
= 0; stream
< 2; stream
++) {
306 substream
= pcm
->streams
[stream
].substream
;
309 buf
= &substream
->dma_buffer
;
312 dma_free_writecombine(pcm
->card
->dev
, buf
->bytes
,
313 buf
->area
, buf
->addr
);
318 static u64 pxa2xx_pcm_dmamask
= 0xffffffff;
320 int pxa2xx_pcm_new(struct snd_card
*card
, struct pxa2xx_pcm_client
*client
,
321 struct snd_pcm
**rpcm
)
324 int play
= client
->playback_params
? 1 : 0;
325 int capt
= client
->capture_params
? 1 : 0;
328 ret
= snd_pcm_new(card
, "PXA2xx-PCM", 0, play
, capt
, &pcm
);
332 pcm
->private_data
= client
;
333 pcm
->private_free
= pxa2xx_pcm_free_dma_buffers
;
335 if (!card
->dev
->dma_mask
)
336 card
->dev
->dma_mask
= &pxa2xx_pcm_dmamask
;
337 if (!card
->dev
->coherent_dma_mask
)
338 card
->dev
->coherent_dma_mask
= 0xffffffff;
341 int stream
= SNDRV_PCM_STREAM_PLAYBACK
;
342 snd_pcm_set_ops(pcm
, stream
, &pxa2xx_pcm_ops
);
343 ret
= pxa2xx_pcm_preallocate_dma_buffer(pcm
, stream
);
348 int stream
= SNDRV_PCM_STREAM_CAPTURE
;
349 snd_pcm_set_ops(pcm
, stream
, &pxa2xx_pcm_ops
);
350 ret
= pxa2xx_pcm_preallocate_dma_buffer(pcm
, stream
);
363 EXPORT_SYMBOL(pxa2xx_pcm_new
);
365 MODULE_AUTHOR("Nicolas Pitre");
366 MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module");
367 MODULE_LICENSE("GPL");