mac80211: Fix behavior of ieee80211_open and ieee80211_close
[linux-2.6/sactl.git] / sound / arm / pxa2xx-pcm.c
blobe8cf904b835804dc7329ec35d0eaca216a9ce188
1 /*
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>
24 #include <asm/dma.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 |
35 SNDRV_PCM_INFO_PAUSE,
36 .formats = SNDRV_PCM_FMTBIT_S16_LE,
37 .period_bytes_min = 32,
38 .period_bytes_max = 8192 - 32,
39 .periods_min = 1,
40 .periods_max = PAGE_SIZE/sizeof(pxa_dma_desc),
41 .buffer_bytes_max = 128 * 1024,
42 .fifo_size = 32,
45 struct pxa2xx_runtime_data {
46 int dma_ch;
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;
68 do {
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;
74 } else {
75 dma_desc->dsadr = rtd->params->dev_addr;
76 dma_desc->dtadr = dma_buff_phys;
78 if (period > totsize)
79 period = totsize;
80 dma_desc->dcmd = rtd->params->dcmd | period | DCMD_ENDIRQEN;
81 dma_desc++;
82 dma_buff_phys += period;
83 } while (totsize -= period);
84 dma_desc[-1].ddadr = rtd->dma_desc_array_phys;
86 return 0;
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);
95 return 0;
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;
115 int ret = 0;
117 switch (cmd) {
118 case SNDRV_PCM_TRIGGER_START:
119 DDADR(rtd->dma_ch) = rtd->dma_desc_array_phys;
120 DCSR(rtd->dma_ch) = DCSR_RUN;
121 break;
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;
127 break;
129 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
130 DCSR(rtd->dma_ch) |= DCSR_RUN;
131 break;
133 default:
134 ret = -EINVAL;
137 return ret;
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;
144 int dcsr;
146 dcsr = DCSR(dma_ch);
147 DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
149 if (dcsr & DCSR_ENDINTR) {
150 snd_pcm_period_elapsed(substream);
151 } else {
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)
166 x = 0;
167 return x;
170 static int
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);
174 int changed = 0;
176 if (i->min & 31) {
177 i->min = (i->min & ~31) + 32;
178 i->openmin = 0;
179 changed = 1;
182 if (i->max & 31) {
183 i->max &= ~31;
184 i->openmax = 0;
185 changed = 1;
188 return changed;
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;
196 int ret;
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);
208 if (ret)
209 goto out;
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);
213 if (ret)
214 goto out;
216 ret = -ENOMEM;
217 rtd = kmalloc(sizeof(*rtd), GFP_KERNEL);
218 if (!rtd)
219 goto out;
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)
224 goto err1;
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);
230 if (ret < 0)
231 goto err2;
232 rtd->dma_ch = ret;
234 runtime->private_data = rtd;
235 ret = client->startup(substream);
236 if (!ret)
237 goto out;
239 pxa_free_dma(rtd->dma_ch);
240 err2:
241 dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
242 rtd->dma_desc_array, rtd->dma_desc_array_phys);
243 err1:
244 kfree(rtd);
245 out:
246 return ret;
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);
258 kfree(rtd);
259 return 0;
262 static int
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,
267 runtime->dma_area,
268 runtime->dma_addr,
269 runtime->dma_bytes);
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);
294 if (!buf->area)
295 return -ENOMEM;
296 buf->bytes = size;
297 return 0;
300 static void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
302 struct snd_pcm_substream *substream;
303 struct snd_dma_buffer *buf;
304 int stream;
306 for (stream = 0; stream < 2; stream++) {
307 substream = pcm->streams[stream].substream;
308 if (!substream)
309 continue;
310 buf = &substream->dma_buffer;
311 if (!buf->area)
312 continue;
313 dma_free_writecombine(pcm->card->dev, buf->bytes,
314 buf->area, buf->addr);
315 buf->area = NULL;
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)
324 struct snd_pcm *pcm;
325 int play = client->playback_params ? 1 : 0;
326 int capt = client->capture_params ? 1 : 0;
327 int ret;
329 ret = snd_pcm_new(card, "PXA2xx-PCM", 0, play, capt, &pcm);
330 if (ret)
331 goto out;
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;
341 if (play) {
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);
345 if (ret)
346 goto out;
348 if (capt) {
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);
352 if (ret)
353 goto out;
356 if (rpcm)
357 *rpcm = pcm;
358 ret = 0;
360 out:
361 return ret;
364 EXPORT_SYMBOL(pxa2xx_pcm_new);
366 MODULE_AUTHOR("Nicolas Pitre");
367 MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module");
368 MODULE_LICENSE("GPL");