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/driver.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
25 #include <asm/hardware.h>
26 #include <asm/arch/pxa-regs.h>
28 #include "pxa2xx-pcm.h"
31 static const struct snd_pcm_hardware pxa2xx_pcm_hardware
= {
32 .info
= SNDRV_PCM_INFO_MMAP
|
33 SNDRV_PCM_INFO_MMAP_VALID
|
34 SNDRV_PCM_INFO_INTERLEAVED
|
36 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
37 .period_bytes_min
= 32,
38 .period_bytes_max
= 8192 - 32,
40 .periods_max
= PAGE_SIZE
/sizeof(pxa_dma_desc
),
41 .buffer_bytes_max
= 128 * 1024,
45 struct pxa2xx_runtime_data
{
47 struct pxa2xx_pcm_dma_params
*params
;
48 pxa_dma_desc
*dma_desc_array
;
49 dma_addr_t dma_desc_array_phys
;
52 static int pxa2xx_pcm_hw_params(struct snd_pcm_substream
*substream
,
53 struct snd_pcm_hw_params
*params
)
55 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
56 struct pxa2xx_runtime_data
*rtd
= runtime
->private_data
;
57 size_t totsize
= params_buffer_bytes(params
);
58 size_t period
= params_period_bytes(params
);
59 pxa_dma_desc
*dma_desc
;
60 dma_addr_t dma_buff_phys
, next_desc_phys
;
62 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
63 runtime
->dma_bytes
= totsize
;
65 dma_desc
= rtd
->dma_desc_array
;
66 next_desc_phys
= rtd
->dma_desc_array_phys
;
67 dma_buff_phys
= runtime
->dma_addr
;
69 next_desc_phys
+= sizeof(pxa_dma_desc
);
70 dma_desc
->ddadr
= next_desc_phys
;
71 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
72 dma_desc
->dsadr
= dma_buff_phys
;
73 dma_desc
->dtadr
= rtd
->params
->dev_addr
;
75 dma_desc
->dsadr
= rtd
->params
->dev_addr
;
76 dma_desc
->dtadr
= dma_buff_phys
;
80 dma_desc
->dcmd
= rtd
->params
->dcmd
| period
| DCMD_ENDIRQEN
;
82 dma_buff_phys
+= period
;
83 } while (totsize
-= period
);
84 dma_desc
[-1].ddadr
= rtd
->dma_desc_array_phys
;
89 static int pxa2xx_pcm_hw_free(struct snd_pcm_substream
*substream
)
91 struct pxa2xx_runtime_data
*rtd
= substream
->runtime
->private_data
;
93 *rtd
->params
->drcmr
= 0;
94 snd_pcm_set_runtime_buffer(substream
, NULL
);
98 static int pxa2xx_pcm_prepare(struct snd_pcm_substream
*substream
)
100 struct pxa2xx_pcm_client
*client
= substream
->private_data
;
101 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
102 struct pxa2xx_runtime_data
*rtd
= runtime
->private_data
;
104 DCSR(rtd
->dma_ch
) &= ~DCSR_RUN
;
105 DCSR(rtd
->dma_ch
) = 0;
106 DCMD(rtd
->dma_ch
) = 0;
107 *rtd
->params
->drcmr
= rtd
->dma_ch
| DRCMR_MAPVLD
;
109 return client
->prepare(substream
);
112 static int pxa2xx_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
114 struct pxa2xx_runtime_data
*rtd
= substream
->runtime
->private_data
;
118 case SNDRV_PCM_TRIGGER_START
:
119 DDADR(rtd
->dma_ch
) = rtd
->dma_desc_array_phys
;
120 DCSR(rtd
->dma_ch
) = DCSR_RUN
;
123 case SNDRV_PCM_TRIGGER_STOP
:
124 case SNDRV_PCM_TRIGGER_SUSPEND
:
125 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
126 DCSR(rtd
->dma_ch
) &= ~DCSR_RUN
;
129 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
130 DCSR(rtd
->dma_ch
) |= DCSR_RUN
;
140 static void pxa2xx_pcm_dma_irq(int dma_ch
, void *dev_id
)
142 struct snd_pcm_substream
*substream
= dev_id
;
143 struct pxa2xx_runtime_data
*rtd
= substream
->runtime
->private_data
;
147 DCSR(dma_ch
) = dcsr
& ~DCSR_STOPIRQEN
;
149 if (dcsr
& DCSR_ENDINTR
) {
150 snd_pcm_period_elapsed(substream
);
152 printk( KERN_ERR
"%s: DMA error on channel %d (DCSR=%#x)\n",
153 rtd
->params
->name
, dma_ch
, dcsr
);
154 snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
158 static snd_pcm_uframes_t
pxa2xx_pcm_pointer(struct snd_pcm_substream
*substream
)
160 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
161 struct pxa2xx_runtime_data
*rtd
= runtime
->private_data
;
162 dma_addr_t ptr
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) ?
163 DSADR(rtd
->dma_ch
) : DTADR(rtd
->dma_ch
);
164 snd_pcm_uframes_t x
= bytes_to_frames(runtime
, ptr
- runtime
->dma_addr
);
165 if (x
== runtime
->buffer_size
)
171 pxa2xx_pcm_hw_rule_mult32(struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
173 struct snd_interval
*i
= hw_param_interval(params
, rule
->var
);
177 i
->min
= (i
->min
& ~31) + 32;
191 static int pxa2xx_pcm_open(struct snd_pcm_substream
*substream
)
193 struct pxa2xx_pcm_client
*client
= substream
->private_data
;
194 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
195 struct pxa2xx_runtime_data
*rtd
;
198 runtime
->hw
= pxa2xx_pcm_hardware
;
201 * For mysterious reasons (and despite what the manual says)
202 * playback samples are lost if the DMA count is not a multiple
203 * of the DMA burst size. Let's add a rule to enforce that.
205 ret
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
206 pxa2xx_pcm_hw_rule_mult32
, NULL
,
207 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, -1);
210 ret
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
211 pxa2xx_pcm_hw_rule_mult32
, NULL
,
212 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
217 rtd
= kmalloc(sizeof(*rtd
), GFP_KERNEL
);
220 rtd
->dma_desc_array
=
221 dma_alloc_writecombine(substream
->pcm
->card
->dev
, PAGE_SIZE
,
222 &rtd
->dma_desc_array_phys
, GFP_KERNEL
);
223 if (!rtd
->dma_desc_array
)
226 rtd
->params
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) ?
227 client
->playback_params
: client
->capture_params
;
228 ret
= pxa_request_dma(rtd
->params
->name
, DMA_PRIO_LOW
,
229 pxa2xx_pcm_dma_irq
, substream
);
234 runtime
->private_data
= rtd
;
235 ret
= client
->startup(substream
);
239 pxa_free_dma(rtd
->dma_ch
);
241 dma_free_writecombine(substream
->pcm
->card
->dev
, PAGE_SIZE
,
242 rtd
->dma_desc_array
, rtd
->dma_desc_array_phys
);
249 static int pxa2xx_pcm_close(struct snd_pcm_substream
*substream
)
251 struct pxa2xx_pcm_client
*client
= substream
->private_data
;
252 struct pxa2xx_runtime_data
*rtd
= substream
->runtime
->private_data
;
254 pxa_free_dma(rtd
->dma_ch
);
255 client
->shutdown(substream
);
256 dma_free_writecombine(substream
->pcm
->card
->dev
, PAGE_SIZE
,
257 rtd
->dma_desc_array
, rtd
->dma_desc_array_phys
);
263 pxa2xx_pcm_mmap(struct snd_pcm_substream
*substream
, struct vm_area_struct
*vma
)
265 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
266 return dma_mmap_writecombine(substream
->pcm
->card
->dev
, vma
,
272 static struct snd_pcm_ops pxa2xx_pcm_ops
= {
273 .open
= pxa2xx_pcm_open
,
274 .close
= pxa2xx_pcm_close
,
275 .ioctl
= snd_pcm_lib_ioctl
,
276 .hw_params
= pxa2xx_pcm_hw_params
,
277 .hw_free
= pxa2xx_pcm_hw_free
,
278 .prepare
= pxa2xx_pcm_prepare
,
279 .trigger
= pxa2xx_pcm_trigger
,
280 .pointer
= pxa2xx_pcm_pointer
,
281 .mmap
= pxa2xx_pcm_mmap
,
284 static int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm
*pcm
, int stream
)
286 struct snd_pcm_substream
*substream
= pcm
->streams
[stream
].substream
;
287 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
288 size_t size
= pxa2xx_pcm_hardware
.buffer_bytes_max
;
289 buf
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
290 buf
->dev
.dev
= pcm
->card
->dev
;
291 buf
->private_data
= NULL
;
292 buf
->area
= dma_alloc_writecombine(pcm
->card
->dev
, size
,
293 &buf
->addr
, GFP_KERNEL
);
300 static void pxa2xx_pcm_free_dma_buffers(struct snd_pcm
*pcm
)
302 struct snd_pcm_substream
*substream
;
303 struct snd_dma_buffer
*buf
;
306 for (stream
= 0; stream
< 2; stream
++) {
307 substream
= pcm
->streams
[stream
].substream
;
310 buf
= &substream
->dma_buffer
;
313 dma_free_writecombine(pcm
->card
->dev
, buf
->bytes
,
314 buf
->area
, buf
->addr
);
319 static u64 pxa2xx_pcm_dmamask
= 0xffffffff;
321 int pxa2xx_pcm_new(struct snd_card
*card
, struct pxa2xx_pcm_client
*client
,
322 struct snd_pcm
**rpcm
)
325 int play
= client
->playback_params
? 1 : 0;
326 int capt
= client
->capture_params
? 1 : 0;
329 ret
= snd_pcm_new(card
, "PXA2xx-PCM", 0, play
, capt
, &pcm
);
333 pcm
->private_data
= client
;
334 pcm
->private_free
= pxa2xx_pcm_free_dma_buffers
;
336 if (!card
->dev
->dma_mask
)
337 card
->dev
->dma_mask
= &pxa2xx_pcm_dmamask
;
338 if (!card
->dev
->coherent_dma_mask
)
339 card
->dev
->coherent_dma_mask
= 0xffffffff;
342 int stream
= SNDRV_PCM_STREAM_PLAYBACK
;
343 snd_pcm_set_ops(pcm
, stream
, &pxa2xx_pcm_ops
);
344 ret
= pxa2xx_pcm_preallocate_dma_buffer(pcm
, stream
);
349 int stream
= SNDRV_PCM_STREAM_CAPTURE
;
350 snd_pcm_set_ops(pcm
, stream
, &pxa2xx_pcm_ops
);
351 ret
= pxa2xx_pcm_preallocate_dma_buffer(pcm
, stream
);
364 EXPORT_SYMBOL(pxa2xx_pcm_new
);
366 MODULE_AUTHOR("Nicolas Pitre");
367 MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module");
368 MODULE_LICENSE("GPL");