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/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/dma-mapping.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
25 #include <mach/hardware.h>
26 #include <mach/pxa-regs.h>
27 #include <mach/audio.h>
29 #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 SNDRV_PCM_INFO_RESUME,
37 .formats = SNDRV_PCM_FMTBIT_S16_LE |
38 SNDRV_PCM_FMTBIT_S24_LE |
39 SNDRV_PCM_FMTBIT_S32_LE,
40 .period_bytes_min = 32,
41 .period_bytes_max = 8192 - 32,
43 .periods_max = PAGE_SIZE/sizeof(pxa_dma_desc),
44 .buffer_bytes_max = 128 * 1024,
48 struct pxa2xx_runtime_data {
50 struct pxa2xx_pcm_dma_params *params;
51 pxa_dma_desc *dma_desc_array;
52 dma_addr_t dma_desc_array_phys;
55 static void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id)
57 struct snd_pcm_substream *substream = dev_id;
58 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
62 DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
64 if (dcsr & DCSR_ENDINTR) {
65 snd_pcm_period_elapsed(substream);
67 printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
68 prtd->params->name, dma_ch, dcsr);
72 static int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
73 struct snd_pcm_hw_params *params)
75 struct snd_pcm_runtime *runtime = substream->runtime;
76 struct pxa2xx_runtime_data *prtd = runtime->private_data;
77 struct snd_soc_pcm_runtime *rtd = substream->private_data;
78 struct pxa2xx_pcm_dma_params *dma = rtd->dai->cpu_dai->dma_data;
79 size_t totsize = params_buffer_bytes(params);
80 size_t period = params_period_bytes(params);
81 pxa_dma_desc *dma_desc;
82 dma_addr_t dma_buff_phys, next_desc_phys;
85 /* return if this is a bufferless transfer e.g.
86 * codec <--> BT codec or GSM modem -- lg FIXME */
90 /* this may get called several times by oss emulation
91 * with different params */
92 if (prtd->params == NULL) {
94 ret = pxa_request_dma(prtd->params->name, DMA_PRIO_LOW,
95 pxa2xx_pcm_dma_irq, substream);
99 } else if (prtd->params != dma) {
100 pxa_free_dma(prtd->dma_ch);
102 ret = pxa_request_dma(prtd->params->name, DMA_PRIO_LOW,
103 pxa2xx_pcm_dma_irq, substream);
109 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
110 runtime->dma_bytes = totsize;
112 dma_desc = prtd->dma_desc_array;
113 next_desc_phys = prtd->dma_desc_array_phys;
114 dma_buff_phys = runtime->dma_addr;
116 next_desc_phys += sizeof(pxa_dma_desc);
117 dma_desc->ddadr = next_desc_phys;
118 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
119 dma_desc->dsadr = dma_buff_phys;
120 dma_desc->dtadr = prtd->params->dev_addr;
122 dma_desc->dsadr = prtd->params->dev_addr;
123 dma_desc->dtadr = dma_buff_phys;
125 if (period > totsize)
127 dma_desc->dcmd = prtd->params->dcmd | period | DCMD_ENDIRQEN;
129 dma_buff_phys += period;
130 } while (totsize -= period);
131 dma_desc[-1].ddadr = prtd->dma_desc_array_phys;
136 static int pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
138 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
140 if (prtd && prtd->params)
141 *prtd->params->drcmr = 0;
144 snd_pcm_set_runtime_buffer(substream, NULL);
145 pxa_free_dma(prtd->dma_ch);
152 static int pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
154 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
156 DCSR(prtd->dma_ch) &= ~DCSR_RUN;
157 DCSR(prtd->dma_ch) = 0;
158 DCMD(prtd->dma_ch) = 0;
159 *prtd->params->drcmr = prtd->dma_ch | DRCMR_MAPVLD;
164 static int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
166 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
170 case SNDRV_PCM_TRIGGER_START:
171 DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
172 DCSR(prtd->dma_ch) = DCSR_RUN;
175 case SNDRV_PCM_TRIGGER_STOP:
176 case SNDRV_PCM_TRIGGER_SUSPEND:
177 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
178 DCSR(prtd->dma_ch) &= ~DCSR_RUN;
181 case SNDRV_PCM_TRIGGER_RESUME:
182 DCSR(prtd->dma_ch) |= DCSR_RUN;
184 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
185 DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
186 DCSR(prtd->dma_ch) |= DCSR_RUN;
196 static snd_pcm_uframes_t
197 pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
199 struct snd_pcm_runtime *runtime = substream->runtime;
200 struct pxa2xx_runtime_data *prtd = runtime->private_data;
202 dma_addr_t ptr = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
203 DSADR(prtd->dma_ch) : DTADR(prtd->dma_ch);
204 snd_pcm_uframes_t x = bytes_to_frames(runtime, ptr - runtime->dma_addr);
206 if (x == runtime->buffer_size)
211 static int pxa2xx_pcm_open(struct snd_pcm_substream *substream)
213 struct snd_pcm_runtime *runtime = substream->runtime;
214 struct pxa2xx_runtime_data *prtd;
217 snd_soc_set_runtime_hwparams(substream, &pxa2xx_pcm_hardware);
220 * For mysterious reasons (and despite what the manual says)
221 * playback samples are lost if the DMA count is not a multiple
222 * of the DMA burst size. Let's add a rule to enforce that.
224 ret = snd_pcm_hw_constraint_step(runtime, 0,
225 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
229 ret = snd_pcm_hw_constraint_step(runtime, 0,
230 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
234 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
238 prtd = kzalloc(sizeof(struct pxa2xx_runtime_data), GFP_KERNEL);
244 prtd->dma_desc_array =
245 dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE,
246 &prtd->dma_desc_array_phys, GFP_KERNEL);
247 if (!prtd->dma_desc_array) {
252 runtime->private_data = prtd;
261 static int pxa2xx_pcm_close(struct snd_pcm_substream *substream)
263 struct snd_pcm_runtime *runtime = substream->runtime;
264 struct pxa2xx_runtime_data *prtd = runtime->private_data;
266 dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
267 prtd->dma_desc_array, prtd->dma_desc_array_phys);
272 static int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
273 struct vm_area_struct *vma)
275 struct snd_pcm_runtime *runtime = substream->runtime;
276 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
282 struct snd_pcm_ops pxa2xx_pcm_ops = {
283 .open = pxa2xx_pcm_open,
284 .close = pxa2xx_pcm_close,
285 .ioctl = snd_pcm_lib_ioctl,
286 .hw_params = pxa2xx_pcm_hw_params,
287 .hw_free = pxa2xx_pcm_hw_free,
288 .prepare = pxa2xx_pcm_prepare,
289 .trigger = pxa2xx_pcm_trigger,
290 .pointer = pxa2xx_pcm_pointer,
291 .mmap = pxa2xx_pcm_mmap,
294 static int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
296 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
297 struct snd_dma_buffer *buf = &substream->dma_buffer;
298 size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
299 buf->dev.type = SNDRV_DMA_TYPE_DEV;
300 buf->dev.dev = pcm->card->dev;
301 buf->private_data = NULL;
302 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
303 &buf->addr, GFP_KERNEL);
310 static void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
312 struct snd_pcm_substream *substream;
313 struct snd_dma_buffer *buf;
316 for (stream = 0; stream < 2; stream++) {
317 substream = pcm->streams[stream].substream;
321 buf = &substream->dma_buffer;
325 dma_free_writecombine(pcm->card->dev, buf->bytes,
326 buf->area, buf->addr);
331 static u64 pxa2xx_pcm_dmamask = DMA_32BIT_MASK;
333 int pxa2xx_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
338 if (!card->dev->dma_mask)
339 card->dev->dma_mask = &pxa2xx_pcm_dmamask;
340 if (!card->dev->coherent_dma_mask)
341 card->dev->coherent_dma_mask = DMA_32BIT_MASK;
343 if (dai->playback.channels_min) {
344 ret = pxa2xx_pcm_preallocate_dma_buffer(pcm,
345 SNDRV_PCM_STREAM_PLAYBACK);
350 if (dai->capture.channels_min) {
351 ret = pxa2xx_pcm_preallocate_dma_buffer(pcm,
352 SNDRV_PCM_STREAM_CAPTURE);
360 struct snd_soc_platform pxa2xx_soc_platform = {
361 .name = "pxa2xx-audio",
362 .pcm_ops = &pxa2xx_pcm_ops,
363 .pcm_new = pxa2xx_pcm_new,
364 .pcm_free = pxa2xx_pcm_free_dma_buffers,
366 EXPORT_SYMBOL_GPL(pxa2xx_soc_platform);
368 MODULE_AUTHOR("Nicolas Pitre");
369 MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module");
370 MODULE_LICENSE("GPL");