920T: Use specific 920t mtune=
[linux-2.6/mini2440.git] / sound / arm / pxa2xx-pcm-lib.c
blob743ac6a2906598fdbd70a04f6a3c6910fd86fdfe
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
5 */
7 #include <linux/module.h>
8 #include <linux/dma-mapping.h>
10 #include <sound/core.h>
11 #include <sound/pcm.h>
12 #include <sound/pcm_params.h>
13 #include <sound/pxa2xx-lib.h>
15 #include <mach/dma.h>
17 #include "pxa2xx-pcm.h"
19 static const struct snd_pcm_hardware pxa2xx_pcm_hardware = {
20 .info = SNDRV_PCM_INFO_MMAP |
21 SNDRV_PCM_INFO_MMAP_VALID |
22 SNDRV_PCM_INFO_INTERLEAVED |
23 SNDRV_PCM_INFO_PAUSE |
24 SNDRV_PCM_INFO_RESUME,
25 .formats = SNDRV_PCM_FMTBIT_S16_LE |
26 SNDRV_PCM_FMTBIT_S24_LE |
27 SNDRV_PCM_FMTBIT_S32_LE,
28 .period_bytes_min = 32,
29 .period_bytes_max = 8192 - 32,
30 .periods_min = 1,
31 .periods_max = PAGE_SIZE/sizeof(pxa_dma_desc),
32 .buffer_bytes_max = 128 * 1024,
33 .fifo_size = 32,
36 int __pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
37 struct snd_pcm_hw_params *params)
39 struct snd_pcm_runtime *runtime = substream->runtime;
40 struct pxa2xx_runtime_data *rtd = runtime->private_data;
41 size_t totsize = params_buffer_bytes(params);
42 size_t period = params_period_bytes(params);
43 pxa_dma_desc *dma_desc;
44 dma_addr_t dma_buff_phys, next_desc_phys;
46 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
47 runtime->dma_bytes = totsize;
49 dma_desc = rtd->dma_desc_array;
50 next_desc_phys = rtd->dma_desc_array_phys;
51 dma_buff_phys = runtime->dma_addr;
52 do {
53 next_desc_phys += sizeof(pxa_dma_desc);
54 dma_desc->ddadr = next_desc_phys;
55 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
56 dma_desc->dsadr = dma_buff_phys;
57 dma_desc->dtadr = rtd->params->dev_addr;
58 } else {
59 dma_desc->dsadr = rtd->params->dev_addr;
60 dma_desc->dtadr = dma_buff_phys;
62 if (period > totsize)
63 period = totsize;
64 dma_desc->dcmd = rtd->params->dcmd | period | DCMD_ENDIRQEN;
65 dma_desc++;
66 dma_buff_phys += period;
67 } while (totsize -= period);
68 dma_desc[-1].ddadr = rtd->dma_desc_array_phys;
70 return 0;
72 EXPORT_SYMBOL(__pxa2xx_pcm_hw_params);
74 int __pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
76 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
78 if (rtd && rtd->params && rtd->params->drcmr)
79 *rtd->params->drcmr = 0;
81 snd_pcm_set_runtime_buffer(substream, NULL);
82 return 0;
84 EXPORT_SYMBOL(__pxa2xx_pcm_hw_free);
86 int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
88 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
89 int ret = 0;
91 switch (cmd) {
92 case SNDRV_PCM_TRIGGER_START:
93 DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
94 DCSR(prtd->dma_ch) = DCSR_RUN;
95 break;
97 case SNDRV_PCM_TRIGGER_STOP:
98 case SNDRV_PCM_TRIGGER_SUSPEND:
99 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
100 DCSR(prtd->dma_ch) &= ~DCSR_RUN;
101 break;
103 case SNDRV_PCM_TRIGGER_RESUME:
104 DCSR(prtd->dma_ch) |= DCSR_RUN;
105 break;
106 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
107 DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
108 DCSR(prtd->dma_ch) |= DCSR_RUN;
109 break;
111 default:
112 ret = -EINVAL;
115 return ret;
117 EXPORT_SYMBOL(pxa2xx_pcm_trigger);
119 snd_pcm_uframes_t
120 pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
122 struct snd_pcm_runtime *runtime = substream->runtime;
123 struct pxa2xx_runtime_data *prtd = runtime->private_data;
125 dma_addr_t ptr = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
126 DSADR(prtd->dma_ch) : DTADR(prtd->dma_ch);
127 snd_pcm_uframes_t x = bytes_to_frames(runtime, ptr - runtime->dma_addr);
129 if (x == runtime->buffer_size)
130 x = 0;
131 return x;
133 EXPORT_SYMBOL(pxa2xx_pcm_pointer);
135 int __pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
137 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
139 if (!prtd || !prtd->params)
140 return 0;
142 DCSR(prtd->dma_ch) &= ~DCSR_RUN;
143 DCSR(prtd->dma_ch) = 0;
144 DCMD(prtd->dma_ch) = 0;
145 *prtd->params->drcmr = prtd->dma_ch | DRCMR_MAPVLD;
147 return 0;
149 EXPORT_SYMBOL(__pxa2xx_pcm_prepare);
151 void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id)
153 struct snd_pcm_substream *substream = dev_id;
154 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
155 int dcsr;
157 dcsr = DCSR(dma_ch);
158 DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
160 if (dcsr & DCSR_ENDINTR) {
161 snd_pcm_period_elapsed(substream);
162 } else {
163 printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
164 rtd->params->name, dma_ch, dcsr);
165 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
168 EXPORT_SYMBOL(pxa2xx_pcm_dma_irq);
170 int __pxa2xx_pcm_open(struct snd_pcm_substream *substream)
172 struct snd_pcm_runtime *runtime = substream->runtime;
173 struct pxa2xx_runtime_data *rtd;
174 int ret;
176 runtime->hw = pxa2xx_pcm_hardware;
179 * For mysterious reasons (and despite what the manual says)
180 * playback samples are lost if the DMA count is not a multiple
181 * of the DMA burst size. Let's add a rule to enforce that.
183 ret = snd_pcm_hw_constraint_step(runtime, 0,
184 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
185 if (ret)
186 goto out;
188 ret = snd_pcm_hw_constraint_step(runtime, 0,
189 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
190 if (ret)
191 goto out;
193 ret = snd_pcm_hw_constraint_integer(runtime,
194 SNDRV_PCM_HW_PARAM_PERIODS);
195 if (ret < 0)
196 goto out;
198 ret = -ENOMEM;
199 rtd = kzalloc(sizeof(*rtd), GFP_KERNEL);
200 if (!rtd)
201 goto out;
202 rtd->dma_desc_array =
203 dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE,
204 &rtd->dma_desc_array_phys, GFP_KERNEL);
205 if (!rtd->dma_desc_array)
206 goto err1;
208 runtime->private_data = rtd;
209 return 0;
211 err1:
212 kfree(rtd);
213 out:
214 return ret;
216 EXPORT_SYMBOL(__pxa2xx_pcm_open);
218 int __pxa2xx_pcm_close(struct snd_pcm_substream *substream)
220 struct snd_pcm_runtime *runtime = substream->runtime;
221 struct pxa2xx_runtime_data *rtd = runtime->private_data;
223 dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
224 rtd->dma_desc_array, rtd->dma_desc_array_phys);
225 kfree(rtd);
226 return 0;
228 EXPORT_SYMBOL(__pxa2xx_pcm_close);
230 int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
231 struct vm_area_struct *vma)
233 struct snd_pcm_runtime *runtime = substream->runtime;
234 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
235 runtime->dma_area,
236 runtime->dma_addr,
237 runtime->dma_bytes);
239 EXPORT_SYMBOL(pxa2xx_pcm_mmap);
241 int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
243 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
244 struct snd_dma_buffer *buf = &substream->dma_buffer;
245 size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
246 buf->dev.type = SNDRV_DMA_TYPE_DEV;
247 buf->dev.dev = pcm->card->dev;
248 buf->private_data = NULL;
249 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
250 &buf->addr, GFP_KERNEL);
251 if (!buf->area)
252 return -ENOMEM;
253 buf->bytes = size;
254 return 0;
256 EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer);
258 void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
260 struct snd_pcm_substream *substream;
261 struct snd_dma_buffer *buf;
262 int stream;
264 for (stream = 0; stream < 2; stream++) {
265 substream = pcm->streams[stream].substream;
266 if (!substream)
267 continue;
268 buf = &substream->dma_buffer;
269 if (!buf->area)
270 continue;
271 dma_free_writecombine(pcm->card->dev, buf->bytes,
272 buf->area, buf->addr);
273 buf->area = NULL;
276 EXPORT_SYMBOL(pxa2xx_pcm_free_dma_buffers);
278 MODULE_AUTHOR("Nicolas Pitre");
279 MODULE_DESCRIPTION("Intel PXA2xx sound library");
280 MODULE_LICENSE("GPL");