xref: /linux/sound/soc/pxa/pxa2xx-pcm.c (revision 0c17e4ceedd35c78b1c7413dbd16279a350be6bc)
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/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/dma-mapping.h>
18 
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 
24 #include <asm/dma.h>
25 #include <asm/hardware.h>
26 #include <asm/arch/pxa-regs.h>
27 #include <asm/arch/audio.h>
28 
29 #include "pxa2xx-pcm.h"
30 
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,
42 	.periods_min		= 1,
43 	.periods_max		= PAGE_SIZE/sizeof(pxa_dma_desc),
44 	.buffer_bytes_max	= 128 * 1024,
45 	.fifo_size		= 32,
46 };
47 
48 struct pxa2xx_runtime_data {
49 	int dma_ch;
50 	struct pxa2xx_pcm_dma_params *params;
51 	pxa_dma_desc *dma_desc_array;
52 	dma_addr_t dma_desc_array_phys;
53 };
54 
55 static void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id)
56 {
57 	struct snd_pcm_substream *substream = dev_id;
58 	struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
59 	int dcsr;
60 
61 	dcsr = DCSR(dma_ch);
62 	DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
63 
64 	if (dcsr & DCSR_ENDINTR) {
65 		snd_pcm_period_elapsed(substream);
66 	} else {
67 		printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
68 			prtd->params->name, dma_ch, dcsr);
69 	}
70 }
71 
72 static int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
73 	struct snd_pcm_hw_params *params)
74 {
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;
83 	int ret;
84 
85 	/* return if this is a bufferless transfer e.g.
86 	 * codec <--> BT codec or GSM modem -- lg FIXME */
87 	if (!dma)
88 		return 0;
89 
90 	/* this may get called several times by oss emulation
91 	 * with different params */
92 	if (prtd->params == NULL) {
93 		prtd->params = dma;
94 		ret = pxa_request_dma(prtd->params->name, DMA_PRIO_LOW,
95 			      pxa2xx_pcm_dma_irq, substream);
96 		if (ret < 0)
97 			return ret;
98 		prtd->dma_ch = ret;
99 	} else if (prtd->params != dma) {
100 		pxa_free_dma(prtd->dma_ch);
101 		prtd->params = dma;
102 		ret = pxa_request_dma(prtd->params->name, DMA_PRIO_LOW,
103 			      pxa2xx_pcm_dma_irq, substream);
104 		if (ret < 0)
105 			return ret;
106 		prtd->dma_ch = ret;
107 	}
108 
109 	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
110 	runtime->dma_bytes = totsize;
111 
112 	dma_desc = prtd->dma_desc_array;
113 	next_desc_phys = prtd->dma_desc_array_phys;
114 	dma_buff_phys = runtime->dma_addr;
115 	do {
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;
121 		} else {
122 			dma_desc->dsadr = prtd->params->dev_addr;
123 			dma_desc->dtadr = dma_buff_phys;
124 		}
125 		if (period > totsize)
126 			period = totsize;
127 		dma_desc->dcmd = prtd->params->dcmd | period | DCMD_ENDIRQEN;
128 		dma_desc++;
129 		dma_buff_phys += period;
130 	} while (totsize -= period);
131 	dma_desc[-1].ddadr = prtd->dma_desc_array_phys;
132 
133 	return 0;
134 }
135 
136 static int pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
137 {
138 	struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
139 
140 	if (prtd && prtd->params)
141 		*prtd->params->drcmr = 0;
142 
143 	if (prtd->dma_ch) {
144 		snd_pcm_set_runtime_buffer(substream, NULL);
145 		pxa_free_dma(prtd->dma_ch);
146 		prtd->dma_ch = 0;
147 	}
148 
149 	return 0;
150 }
151 
152 static int pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
153 {
154 	struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
155 
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;
160 
161 	return 0;
162 }
163 
164 static int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
165 {
166 	struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
167 	int ret = 0;
168 
169 	switch (cmd) {
170 	case SNDRV_PCM_TRIGGER_START:
171 		DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
172 		DCSR(prtd->dma_ch) = DCSR_RUN;
173 		break;
174 
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;
179 		break;
180 
181 	case SNDRV_PCM_TRIGGER_RESUME:
182 		DCSR(prtd->dma_ch) |= DCSR_RUN;
183 		break;
184 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
185 		DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
186 		DCSR(prtd->dma_ch) |= DCSR_RUN;
187 		break;
188 
189 	default:
190 		ret = -EINVAL;
191 	}
192 
193 	return ret;
194 }
195 
196 static snd_pcm_uframes_t
197 pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
198 {
199 	struct snd_pcm_runtime *runtime = substream->runtime;
200 	struct pxa2xx_runtime_data *prtd = runtime->private_data;
201 
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);
205 
206 	if (x == runtime->buffer_size)
207 		x = 0;
208 	return x;
209 }
210 
211 static int pxa2xx_pcm_open(struct snd_pcm_substream *substream)
212 {
213 	struct snd_pcm_runtime *runtime = substream->runtime;
214 	struct pxa2xx_runtime_data *prtd;
215 	int ret;
216 
217 	snd_soc_set_runtime_hwparams(substream, &pxa2xx_pcm_hardware);
218 
219 	/*
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.
223 	 */
224 	ret = snd_pcm_hw_constraint_step(runtime, 0,
225 		SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
226 	if (ret)
227 		goto out;
228 
229 	ret = snd_pcm_hw_constraint_step(runtime, 0,
230 		SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
231 	if (ret)
232 		goto out;
233 
234 	ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
235 	if (ret < 0)
236 		goto out;
237 
238 	prtd = kzalloc(sizeof(struct pxa2xx_runtime_data), GFP_KERNEL);
239 	if (prtd == NULL) {
240 		ret = -ENOMEM;
241 		goto out;
242 	}
243 
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) {
248 		ret = -ENOMEM;
249 		goto err1;
250 	}
251 
252 	runtime->private_data = prtd;
253 	return 0;
254 
255  err1:
256 	kfree(prtd);
257  out:
258 	return ret;
259 }
260 
261 static int pxa2xx_pcm_close(struct snd_pcm_substream *substream)
262 {
263 	struct snd_pcm_runtime *runtime = substream->runtime;
264 	struct pxa2xx_runtime_data *prtd = runtime->private_data;
265 
266 	dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
267 			      prtd->dma_desc_array, prtd->dma_desc_array_phys);
268 	kfree(prtd);
269 	return 0;
270 }
271 
272 static int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
273 	struct vm_area_struct *vma)
274 {
275 	struct snd_pcm_runtime *runtime = substream->runtime;
276 	return dma_mmap_writecombine(substream->pcm->card->dev, vma,
277 				     runtime->dma_area,
278 				     runtime->dma_addr,
279 				     runtime->dma_bytes);
280 }
281 
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,
292 };
293 
294 static int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
295 {
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);
304 	if (!buf->area)
305 		return -ENOMEM;
306 	buf->bytes = size;
307 	return 0;
308 }
309 
310 static void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
311 {
312 	struct snd_pcm_substream *substream;
313 	struct snd_dma_buffer *buf;
314 	int stream;
315 
316 	for (stream = 0; stream < 2; stream++) {
317 		substream = pcm->streams[stream].substream;
318 		if (!substream)
319 			continue;
320 
321 		buf = &substream->dma_buffer;
322 		if (!buf->area)
323 			continue;
324 
325 		dma_free_writecombine(pcm->card->dev, buf->bytes,
326 				      buf->area, buf->addr);
327 		buf->area = NULL;
328 	}
329 }
330 
331 static u64 pxa2xx_pcm_dmamask = DMA_32BIT_MASK;
332 
333 int pxa2xx_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
334 	struct snd_pcm *pcm)
335 {
336 	int ret = 0;
337 
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;
342 
343 	if (dai->playback.channels_min) {
344 		ret = pxa2xx_pcm_preallocate_dma_buffer(pcm,
345 			SNDRV_PCM_STREAM_PLAYBACK);
346 		if (ret)
347 			goto out;
348 	}
349 
350 	if (dai->capture.channels_min) {
351 		ret = pxa2xx_pcm_preallocate_dma_buffer(pcm,
352 			SNDRV_PCM_STREAM_CAPTURE);
353 		if (ret)
354 			goto out;
355 	}
356  out:
357 	return ret;
358 }
359 
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,
365 };
366 EXPORT_SYMBOL_GPL(pxa2xx_soc_platform);
367 
368 MODULE_AUTHOR("Nicolas Pitre");
369 MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module");
370 MODULE_LICENSE("GPL");
371