Input: ct82c710 - remove a redundant serio_register_port()
[linux/fpc-iii.git] / sound / arm / pxa2xx-pcm-lib.c
blob8808b82311b1eb207a2fb3306548666ad6dd6a8b
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/slab.h>
8 #include <linux/module.h>
9 #include <linux/dma-mapping.h>
11 #include <sound/core.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_params.h>
14 #include <sound/pxa2xx-lib.h>
16 #include <mach/dma.h>
18 #include "pxa2xx-pcm.h"
20 static const struct snd_pcm_hardware pxa2xx_pcm_hardware = {
21 .info = SNDRV_PCM_INFO_MMAP |
22 SNDRV_PCM_INFO_MMAP_VALID |
23 SNDRV_PCM_INFO_INTERLEAVED |
24 SNDRV_PCM_INFO_PAUSE |
25 SNDRV_PCM_INFO_RESUME,
26 .formats = SNDRV_PCM_FMTBIT_S16_LE |
27 SNDRV_PCM_FMTBIT_S24_LE |
28 SNDRV_PCM_FMTBIT_S32_LE,
29 .period_bytes_min = 32,
30 .period_bytes_max = 8192 - 32,
31 .periods_min = 1,
32 .periods_max = PAGE_SIZE/sizeof(pxa_dma_desc),
33 .buffer_bytes_max = 128 * 1024,
34 .fifo_size = 32,
37 int __pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
38 struct snd_pcm_hw_params *params)
40 struct snd_pcm_runtime *runtime = substream->runtime;
41 struct pxa2xx_runtime_data *rtd = runtime->private_data;
42 size_t totsize = params_buffer_bytes(params);
43 size_t period = params_period_bytes(params);
44 pxa_dma_desc *dma_desc;
45 dma_addr_t dma_buff_phys, next_desc_phys;
47 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
48 runtime->dma_bytes = totsize;
50 dma_desc = rtd->dma_desc_array;
51 next_desc_phys = rtd->dma_desc_array_phys;
52 dma_buff_phys = runtime->dma_addr;
53 do {
54 next_desc_phys += sizeof(pxa_dma_desc);
55 dma_desc->ddadr = next_desc_phys;
56 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
57 dma_desc->dsadr = dma_buff_phys;
58 dma_desc->dtadr = rtd->params->dev_addr;
59 } else {
60 dma_desc->dsadr = rtd->params->dev_addr;
61 dma_desc->dtadr = dma_buff_phys;
63 if (period > totsize)
64 period = totsize;
65 dma_desc->dcmd = rtd->params->dcmd | period | DCMD_ENDIRQEN;
66 dma_desc++;
67 dma_buff_phys += period;
68 } while (totsize -= period);
69 dma_desc[-1].ddadr = rtd->dma_desc_array_phys;
71 return 0;
73 EXPORT_SYMBOL(__pxa2xx_pcm_hw_params);
75 int __pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
77 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
79 if (rtd && rtd->params && rtd->params->drcmr)
80 *rtd->params->drcmr = 0;
82 snd_pcm_set_runtime_buffer(substream, NULL);
83 return 0;
85 EXPORT_SYMBOL(__pxa2xx_pcm_hw_free);
87 int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
89 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
90 int ret = 0;
92 switch (cmd) {
93 case SNDRV_PCM_TRIGGER_START:
94 DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
95 DCSR(prtd->dma_ch) = DCSR_RUN;
96 break;
98 case SNDRV_PCM_TRIGGER_STOP:
99 case SNDRV_PCM_TRIGGER_SUSPEND:
100 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
101 DCSR(prtd->dma_ch) &= ~DCSR_RUN;
102 break;
104 case SNDRV_PCM_TRIGGER_RESUME:
105 DCSR(prtd->dma_ch) |= DCSR_RUN;
106 break;
107 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
108 DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
109 DCSR(prtd->dma_ch) |= DCSR_RUN;
110 break;
112 default:
113 ret = -EINVAL;
116 return ret;
118 EXPORT_SYMBOL(pxa2xx_pcm_trigger);
120 snd_pcm_uframes_t
121 pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
123 struct snd_pcm_runtime *runtime = substream->runtime;
124 struct pxa2xx_runtime_data *prtd = runtime->private_data;
126 dma_addr_t ptr = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
127 DSADR(prtd->dma_ch) : DTADR(prtd->dma_ch);
128 snd_pcm_uframes_t x = bytes_to_frames(runtime, ptr - runtime->dma_addr);
130 if (x == runtime->buffer_size)
131 x = 0;
132 return x;
134 EXPORT_SYMBOL(pxa2xx_pcm_pointer);
136 int __pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
138 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
140 if (!prtd || !prtd->params)
141 return 0;
143 DCSR(prtd->dma_ch) &= ~DCSR_RUN;
144 DCSR(prtd->dma_ch) = 0;
145 DCMD(prtd->dma_ch) = 0;
146 *prtd->params->drcmr = prtd->dma_ch | DRCMR_MAPVLD;
148 return 0;
150 EXPORT_SYMBOL(__pxa2xx_pcm_prepare);
152 void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id)
154 struct snd_pcm_substream *substream = dev_id;
155 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
156 int dcsr;
158 dcsr = DCSR(dma_ch);
159 DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
161 if (dcsr & DCSR_ENDINTR) {
162 snd_pcm_period_elapsed(substream);
163 } else {
164 printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
165 rtd->params->name, dma_ch, dcsr);
166 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
169 EXPORT_SYMBOL(pxa2xx_pcm_dma_irq);
171 int __pxa2xx_pcm_open(struct snd_pcm_substream *substream)
173 struct snd_pcm_runtime *runtime = substream->runtime;
174 struct pxa2xx_runtime_data *rtd;
175 int ret;
177 runtime->hw = pxa2xx_pcm_hardware;
180 * For mysterious reasons (and despite what the manual says)
181 * playback samples are lost if the DMA count is not a multiple
182 * of the DMA burst size. Let's add a rule to enforce that.
184 ret = snd_pcm_hw_constraint_step(runtime, 0,
185 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
186 if (ret)
187 goto out;
189 ret = snd_pcm_hw_constraint_step(runtime, 0,
190 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
191 if (ret)
192 goto out;
194 ret = snd_pcm_hw_constraint_integer(runtime,
195 SNDRV_PCM_HW_PARAM_PERIODS);
196 if (ret < 0)
197 goto out;
199 ret = -ENOMEM;
200 rtd = kzalloc(sizeof(*rtd), GFP_KERNEL);
201 if (!rtd)
202 goto out;
203 rtd->dma_desc_array =
204 dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE,
205 &rtd->dma_desc_array_phys, GFP_KERNEL);
206 if (!rtd->dma_desc_array)
207 goto err1;
209 rtd->dma_ch = -1;
210 runtime->private_data = rtd;
211 return 0;
213 err1:
214 kfree(rtd);
215 out:
216 return ret;
218 EXPORT_SYMBOL(__pxa2xx_pcm_open);
220 int __pxa2xx_pcm_close(struct snd_pcm_substream *substream)
222 struct snd_pcm_runtime *runtime = substream->runtime;
223 struct pxa2xx_runtime_data *rtd = runtime->private_data;
225 dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
226 rtd->dma_desc_array, rtd->dma_desc_array_phys);
227 kfree(rtd);
228 return 0;
230 EXPORT_SYMBOL(__pxa2xx_pcm_close);
232 int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
233 struct vm_area_struct *vma)
235 struct snd_pcm_runtime *runtime = substream->runtime;
236 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
237 runtime->dma_area,
238 runtime->dma_addr,
239 runtime->dma_bytes);
241 EXPORT_SYMBOL(pxa2xx_pcm_mmap);
243 int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
245 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
246 struct snd_dma_buffer *buf = &substream->dma_buffer;
247 size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
248 buf->dev.type = SNDRV_DMA_TYPE_DEV;
249 buf->dev.dev = pcm->card->dev;
250 buf->private_data = NULL;
251 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
252 &buf->addr, GFP_KERNEL);
253 if (!buf->area)
254 return -ENOMEM;
255 buf->bytes = size;
256 return 0;
258 EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer);
260 void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
262 struct snd_pcm_substream *substream;
263 struct snd_dma_buffer *buf;
264 int stream;
266 for (stream = 0; stream < 2; stream++) {
267 substream = pcm->streams[stream].substream;
268 if (!substream)
269 continue;
270 buf = &substream->dma_buffer;
271 if (!buf->area)
272 continue;
273 dma_free_writecombine(pcm->card->dev, buf->bytes,
274 buf->area, buf->addr);
275 buf->area = NULL;
278 EXPORT_SYMBOL(pxa2xx_pcm_free_dma_buffers);
280 MODULE_AUTHOR("Nicolas Pitre");
281 MODULE_DESCRIPTION("Intel PXA2xx sound library");
282 MODULE_LICENSE("GPL");