blob: 6205f37d547ce80e0315be9d97fbc7ce7fa9ebe5 [file] [log] [blame]
Dmitry Baryshkova6d77312008-09-10 05:01:20 +04001/*
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 */
6
7#include <linux/module.h>
8#include <linux/dma-mapping.h>
9
10#include <sound/core.h>
11#include <sound/pcm.h>
12#include <sound/pcm_params.h>
13#include <sound/pxa2xx-lib.h>
14
Eric Miao57429642009-01-19 15:28:07 +080015#include <mach/dma.h>
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040016
17#include "pxa2xx-pcm.h"
18
19static 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,
34};
35
36int __pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
37 struct snd_pcm_hw_params *params)
38{
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;
45
46 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
47 runtime->dma_bytes = totsize;
48
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;
61 }
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;
69
70 return 0;
71}
72EXPORT_SYMBOL(__pxa2xx_pcm_hw_params);
73
74int __pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
75{
76 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
77
Daniel Mackb7d4de72009-07-08 19:24:26 +020078 if (rtd && rtd->params && rtd->params->drcmr)
Dmitry Baryshkova6d77312008-09-10 05:01:20 +040079 *rtd->params->drcmr = 0;
80
81 snd_pcm_set_runtime_buffer(substream, NULL);
82 return 0;
83}
84EXPORT_SYMBOL(__pxa2xx_pcm_hw_free);
85
86int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
87{
88 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
89 int ret = 0;
90
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;
96
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;
102
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;
110
111 default:
112 ret = -EINVAL;
113 }
114
115 return ret;
116}
117EXPORT_SYMBOL(pxa2xx_pcm_trigger);
118
119snd_pcm_uframes_t
120pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
121{
122 struct snd_pcm_runtime *runtime = substream->runtime;
123 struct pxa2xx_runtime_data *prtd = runtime->private_data;
124
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);
128
129 if (x == runtime->buffer_size)
130 x = 0;
131 return x;
132}
133EXPORT_SYMBOL(pxa2xx_pcm_pointer);
134
135int __pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
136{
137 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
138
139 DCSR(prtd->dma_ch) &= ~DCSR_RUN;
140 DCSR(prtd->dma_ch) = 0;
141 DCMD(prtd->dma_ch) = 0;
142 *prtd->params->drcmr = prtd->dma_ch | DRCMR_MAPVLD;
143
144 return 0;
145}
146EXPORT_SYMBOL(__pxa2xx_pcm_prepare);
147
148void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id)
149{
150 struct snd_pcm_substream *substream = dev_id;
151 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
152 int dcsr;
153
154 dcsr = DCSR(dma_ch);
155 DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
156
157 if (dcsr & DCSR_ENDINTR) {
158 snd_pcm_period_elapsed(substream);
159 } else {
160 printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
161 rtd->params->name, dma_ch, dcsr);
162 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
163 }
164}
165EXPORT_SYMBOL(pxa2xx_pcm_dma_irq);
166
167int __pxa2xx_pcm_open(struct snd_pcm_substream *substream)
168{
169 struct snd_pcm_runtime *runtime = substream->runtime;
170 struct pxa2xx_runtime_data *rtd;
171 int ret;
172
173 runtime->hw = pxa2xx_pcm_hardware;
174
175 /*
176 * For mysterious reasons (and despite what the manual says)
177 * playback samples are lost if the DMA count is not a multiple
178 * of the DMA burst size. Let's add a rule to enforce that.
179 */
180 ret = snd_pcm_hw_constraint_step(runtime, 0,
181 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
182 if (ret)
183 goto out;
184
185 ret = snd_pcm_hw_constraint_step(runtime, 0,
186 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
187 if (ret)
188 goto out;
189
190 ret = snd_pcm_hw_constraint_integer(runtime,
191 SNDRV_PCM_HW_PARAM_PERIODS);
192 if (ret < 0)
193 goto out;
194
195 ret = -ENOMEM;
Mark Brown13095c32008-10-22 13:27:49 +0100196 rtd = kzalloc(sizeof(*rtd), GFP_KERNEL);
Dmitry Baryshkova6d77312008-09-10 05:01:20 +0400197 if (!rtd)
198 goto out;
199 rtd->dma_desc_array =
200 dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE,
201 &rtd->dma_desc_array_phys, GFP_KERNEL);
202 if (!rtd->dma_desc_array)
203 goto err1;
204
205 runtime->private_data = rtd;
206 return 0;
207
208 err1:
209 kfree(rtd);
210 out:
211 return ret;
212}
213EXPORT_SYMBOL(__pxa2xx_pcm_open);
214
215int __pxa2xx_pcm_close(struct snd_pcm_substream *substream)
216{
217 struct snd_pcm_runtime *runtime = substream->runtime;
218 struct pxa2xx_runtime_data *rtd = runtime->private_data;
219
220 dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
221 rtd->dma_desc_array, rtd->dma_desc_array_phys);
222 kfree(rtd);
223 return 0;
224}
225EXPORT_SYMBOL(__pxa2xx_pcm_close);
226
227int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
228 struct vm_area_struct *vma)
229{
230 struct snd_pcm_runtime *runtime = substream->runtime;
231 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
232 runtime->dma_area,
233 runtime->dma_addr,
234 runtime->dma_bytes);
235}
236EXPORT_SYMBOL(pxa2xx_pcm_mmap);
237
238int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
239{
240 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
241 struct snd_dma_buffer *buf = &substream->dma_buffer;
242 size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
243 buf->dev.type = SNDRV_DMA_TYPE_DEV;
244 buf->dev.dev = pcm->card->dev;
245 buf->private_data = NULL;
246 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
247 &buf->addr, GFP_KERNEL);
248 if (!buf->area)
249 return -ENOMEM;
250 buf->bytes = size;
251 return 0;
252}
253EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer);
254
255void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
256{
257 struct snd_pcm_substream *substream;
258 struct snd_dma_buffer *buf;
259 int stream;
260
261 for (stream = 0; stream < 2; stream++) {
262 substream = pcm->streams[stream].substream;
263 if (!substream)
264 continue;
265 buf = &substream->dma_buffer;
266 if (!buf->area)
267 continue;
268 dma_free_writecombine(pcm->card->dev, buf->bytes,
269 buf->area, buf->addr);
270 buf->area = NULL;
271 }
272}
273EXPORT_SYMBOL(pxa2xx_pcm_free_dma_buffers);
274
275MODULE_AUTHOR("Nicolas Pitre");
276MODULE_DESCRIPTION("Intel PXA2xx sound library");
277MODULE_LICENSE("GPL");