blob: e8cf904b835804dc7329ec35d0eaca216a9ce188 [file] [log] [blame]
Takashi Iwai2c484df2005-06-30 18:54:04 +02001/*
2 * linux/sound/arm/pxa2xx-pcm.c -- ALSA PCM interface for the Intel PXA2xx chip
3 *
4 * Author: Nicolas Pitre
5 * Created: Nov 30, 2004
6 * Copyright: (C) 2004 MontaVista Software, Inc.
7 *
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.
11 */
12
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>
18
19#include <sound/driver.h>
20#include <sound/core.h>
21#include <sound/pcm.h>
22#include <sound/pcm_params.h>
23
24#include <asm/dma.h>
25#include <asm/hardware.h>
26#include <asm/arch/pxa-regs.h>
27
28#include "pxa2xx-pcm.h"
29
30
Takashi Iwaid18f8372005-11-17 15:10:38 +010031static const struct snd_pcm_hardware pxa2xx_pcm_hardware = {
Takashi Iwai2c484df2005-06-30 18:54:04 +020032 .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,
43};
44
45struct pxa2xx_runtime_data {
46 int dma_ch;
Takashi Iwaid18f8372005-11-17 15:10:38 +010047 struct pxa2xx_pcm_dma_params *params;
Takashi Iwai2c484df2005-06-30 18:54:04 +020048 pxa_dma_desc *dma_desc_array;
49 dma_addr_t dma_desc_array_phys;
50};
51
Takashi Iwaid18f8372005-11-17 15:10:38 +010052static int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
53 struct snd_pcm_hw_params *params)
Takashi Iwai2c484df2005-06-30 18:54:04 +020054{
Takashi Iwaid18f8372005-11-17 15:10:38 +010055 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai2c484df2005-06-30 18:54:04 +020056 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;
61
62 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
63 runtime->dma_bytes = totsize;
64
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;
77 }
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;
85
86 return 0;
87}
88
Takashi Iwaid18f8372005-11-17 15:10:38 +010089static int pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
Takashi Iwai2c484df2005-06-30 18:54:04 +020090{
91 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
92
93 *rtd->params->drcmr = 0;
94 snd_pcm_set_runtime_buffer(substream, NULL);
95 return 0;
96}
97
Takashi Iwaid18f8372005-11-17 15:10:38 +010098static int pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
Takashi Iwai2c484df2005-06-30 18:54:04 +020099{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100100 struct pxa2xx_pcm_client *client = substream->private_data;
101 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200102 struct pxa2xx_runtime_data *rtd = runtime->private_data;
103
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;
108
109 return client->prepare(substream);
110}
111
Takashi Iwaid18f8372005-11-17 15:10:38 +0100112static int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200113{
114 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
115 int ret = 0;
116
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;
122
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;
128
129 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
130 DCSR(rtd->dma_ch) |= DCSR_RUN;
131 break;
132
133 default:
134 ret = -EINVAL;
135 }
136
137 return ret;
138}
139
David Howells7d12e782006-10-05 14:55:46 +0100140static void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200141{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100142 struct snd_pcm_substream *substream = dev_id;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200143 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
144 int dcsr;
145
146 dcsr = DCSR(dma_ch);
147 DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
148
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);
155 }
156}
157
Takashi Iwaid18f8372005-11-17 15:10:38 +0100158static snd_pcm_uframes_t pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200159{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100160 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200161 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;
168}
169
170static int
Takashi Iwaid18f8372005-11-17 15:10:38 +0100171pxa2xx_pcm_hw_rule_mult32(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200172{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100173 struct snd_interval *i = hw_param_interval(params, rule->var);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200174 int changed = 0;
175
176 if (i->min & 31) {
177 i->min = (i->min & ~31) + 32;
178 i->openmin = 0;
179 changed = 1;
180 }
181
182 if (i->max & 31) {
183 i->max &= ~31;
184 i->openmax = 0;
185 changed = 1;
186 }
187
188 return changed;
189}
190
Takashi Iwaid18f8372005-11-17 15:10:38 +0100191static int pxa2xx_pcm_open(struct snd_pcm_substream *substream)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200192{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100193 struct pxa2xx_pcm_client *client = substream->private_data;
194 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200195 struct pxa2xx_runtime_data *rtd;
196 int ret;
197
198 runtime->hw = pxa2xx_pcm_hardware;
199
200 /*
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.
204 */
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;
215
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;
225
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;
233
234 runtime->private_data = rtd;
235 ret = client->startup(substream);
236 if (!ret)
237 goto out;
238
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;
247}
248
Takashi Iwaid18f8372005-11-17 15:10:38 +0100249static int pxa2xx_pcm_close(struct snd_pcm_substream *substream)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200250{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100251 struct pxa2xx_pcm_client *client = substream->private_data;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200252 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
253
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;
260}
261
262static int
Takashi Iwaid18f8372005-11-17 15:10:38 +0100263pxa2xx_pcm_mmap(struct snd_pcm_substream *substream, struct vm_area_struct *vma)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200264{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100265 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200266 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
267 runtime->dma_area,
268 runtime->dma_addr,
269 runtime->dma_bytes);
270}
271
Takashi Iwaid18f8372005-11-17 15:10:38 +0100272static struct snd_pcm_ops pxa2xx_pcm_ops = {
Takashi Iwai2c484df2005-06-30 18:54:04 +0200273 .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,
282};
283
Takashi Iwaid18f8372005-11-17 15:10:38 +0100284static int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200285{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100286 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200287 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;
298}
299
Takashi Iwaid18f8372005-11-17 15:10:38 +0100300static void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200301{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100302 struct snd_pcm_substream *substream;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200303 struct snd_dma_buffer *buf;
304 int stream;
305
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;
316 }
317}
318
319static u64 pxa2xx_pcm_dmamask = 0xffffffff;
320
Takashi Iwaid18f8372005-11-17 15:10:38 +0100321int pxa2xx_pcm_new(struct snd_card *card, struct pxa2xx_pcm_client *client,
322 struct snd_pcm **rpcm)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200323{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100324 struct snd_pcm *pcm;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200325 int play = client->playback_params ? 1 : 0;
326 int capt = client->capture_params ? 1 : 0;
327 int ret;
328
329 ret = snd_pcm_new(card, "PXA2xx-PCM", 0, play, capt, &pcm);
330 if (ret)
331 goto out;
332
333 pcm->private_data = client;
334 pcm->private_free = pxa2xx_pcm_free_dma_buffers;
335
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;
340
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;
347 }
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;
354 }
355
356 if (rpcm)
357 *rpcm = pcm;
358 ret = 0;
359
360 out:
361 return ret;
362}
363
364EXPORT_SYMBOL(pxa2xx_pcm_new);
365
366MODULE_AUTHOR("Nicolas Pitre");
367MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module");
368MODULE_LICENSE("GPL");