Commit | Line | Data |
---|---|---|
2c484df0 TI |
1 | /* |
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 | ||
31 | static const snd_pcm_hardware_t 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, | |
43 | }; | |
44 | ||
45 | struct pxa2xx_runtime_data { | |
46 | int dma_ch; | |
47 | pxa2xx_pcm_dma_params_t *params; | |
48 | pxa_dma_desc *dma_desc_array; | |
49 | dma_addr_t dma_desc_array_phys; | |
50 | }; | |
51 | ||
52 | static int pxa2xx_pcm_hw_params(snd_pcm_substream_t *substream, | |
53 | snd_pcm_hw_params_t *params) | |
54 | { | |
55 | snd_pcm_runtime_t *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; | |
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 | ||
89 | static int pxa2xx_pcm_hw_free(snd_pcm_substream_t *substream) | |
90 | { | |
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 | ||
98 | static int pxa2xx_pcm_prepare(snd_pcm_substream_t *substream) | |
99 | { | |
100 | pxa2xx_pcm_client_t *client = substream->private_data; | |
101 | snd_pcm_runtime_t *runtime = substream->runtime; | |
102 | 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 | ||
112 | static int pxa2xx_pcm_trigger(snd_pcm_substream_t *substream, int cmd) | |
113 | { | |
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 | ||
140 | static void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id, struct pt_regs *regs) | |
141 | { | |
142 | snd_pcm_substream_t *substream = dev_id; | |
143 | 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 | ||
158 | static snd_pcm_uframes_t pxa2xx_pcm_pointer(snd_pcm_substream_t *substream) | |
159 | { | |
160 | snd_pcm_runtime_t *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; | |
168 | } | |
169 | ||
170 | static int | |
171 | pxa2xx_pcm_hw_rule_mult32(snd_pcm_hw_params_t *params, snd_pcm_hw_rule_t *rule) | |
172 | { | |
173 | snd_interval_t *i = hw_param_interval(params, rule->var); | |
174 | 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 | ||
191 | static int pxa2xx_pcm_open(snd_pcm_substream_t *substream) | |
192 | { | |
193 | pxa2xx_pcm_client_t *client = substream->private_data; | |
194 | snd_pcm_runtime_t *runtime = substream->runtime; | |
195 | 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 | ||
249 | static int pxa2xx_pcm_close(snd_pcm_substream_t *substream) | |
250 | { | |
251 | pxa2xx_pcm_client_t *client = substream->private_data; | |
252 | 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 | ||
262 | static int | |
263 | pxa2xx_pcm_mmap(snd_pcm_substream_t *substream, struct vm_area_struct *vma) | |
264 | { | |
265 | snd_pcm_runtime_t *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); | |
270 | } | |
271 | ||
272 | static snd_pcm_ops_t 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, | |
282 | }; | |
283 | ||
284 | static int pxa2xx_pcm_preallocate_dma_buffer(snd_pcm_t *pcm, int stream) | |
285 | { | |
286 | snd_pcm_substream_t *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; | |
298 | } | |
299 | ||
300 | static void pxa2xx_pcm_free_dma_buffers(snd_pcm_t *pcm) | |
301 | { | |
302 | snd_pcm_substream_t *substream; | |
303 | 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 | ||
319 | static u64 pxa2xx_pcm_dmamask = 0xffffffff; | |
320 | ||
321 | int pxa2xx_pcm_new(snd_card_t *card, pxa2xx_pcm_client_t *client, snd_pcm_t **rpcm) | |
322 | { | |
323 | snd_pcm_t *pcm; | |
324 | int play = client->playback_params ? 1 : 0; | |
325 | int capt = client->capture_params ? 1 : 0; | |
326 | int ret; | |
327 | ||
328 | ret = snd_pcm_new(card, "PXA2xx-PCM", 0, play, capt, &pcm); | |
329 | if (ret) | |
330 | goto out; | |
331 | ||
332 | pcm->private_data = client; | |
333 | pcm->private_free = pxa2xx_pcm_free_dma_buffers; | |
334 | ||
335 | if (!card->dev->dma_mask) | |
336 | card->dev->dma_mask = &pxa2xx_pcm_dmamask; | |
337 | if (!card->dev->coherent_dma_mask) | |
338 | card->dev->coherent_dma_mask = 0xffffffff; | |
339 | ||
340 | if (play) { | |
341 | int stream = SNDRV_PCM_STREAM_PLAYBACK; | |
342 | snd_pcm_set_ops(pcm, stream, &pxa2xx_pcm_ops); | |
343 | ret = pxa2xx_pcm_preallocate_dma_buffer(pcm, stream); | |
344 | if (ret) | |
345 | goto out; | |
346 | } | |
347 | if (capt) { | |
348 | int stream = SNDRV_PCM_STREAM_CAPTURE; | |
349 | snd_pcm_set_ops(pcm, stream, &pxa2xx_pcm_ops); | |
350 | ret = pxa2xx_pcm_preallocate_dma_buffer(pcm, stream); | |
351 | if (ret) | |
352 | goto out; | |
353 | } | |
354 | ||
355 | if (rpcm) | |
356 | *rpcm = pcm; | |
357 | ret = 0; | |
358 | ||
359 | out: | |
360 | return ret; | |
361 | } | |
362 | ||
363 | EXPORT_SYMBOL(pxa2xx_pcm_new); | |
364 | ||
365 | MODULE_AUTHOR("Nicolas Pitre"); | |
366 | MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module"); | |
367 | MODULE_LICENSE("GPL"); |