xref: /openbmc/linux/sound/soc/atmel/atmel-pcm-pdc.c (revision c819e2cf)
1 /*
2  * atmel-pcm.c  --  ALSA PCM interface for the Atmel atmel SoC.
3  *
4  *  Copyright (C) 2005 SAN People
5  *  Copyright (C) 2008 Atmel
6  *
7  * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
8  *
9  * Based on at91-pcm. by:
10  * Frank Mandarino <fmandarino@endrelia.com>
11  * Copyright 2006 Endrelia Technologies Inc.
12  *
13  * Based on pxa2xx-pcm.c by:
14  *
15  * Author:	Nicolas Pitre
16  * Created:	Nov 30, 2004
17  * Copyright:	(C) 2004 MontaVista Software, Inc.
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
32  */
33 
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/platform_device.h>
37 #include <linux/slab.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/atmel_pdc.h>
40 #include <linux/atmel-ssc.h>
41 
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc.h>
46 
47 #include "atmel-pcm.h"
48 
49 
50 /*--------------------------------------------------------------------------*\
51  * Hardware definition
52 \*--------------------------------------------------------------------------*/
53 /* TODO: These values were taken from the AT91 platform driver, check
54  *	 them against real values for AT32
55  */
56 static const struct snd_pcm_hardware atmel_pcm_hardware = {
57 	.info			= SNDRV_PCM_INFO_MMAP |
58 				  SNDRV_PCM_INFO_MMAP_VALID |
59 				  SNDRV_PCM_INFO_INTERLEAVED |
60 				  SNDRV_PCM_INFO_PAUSE,
61 	.period_bytes_min	= 32,
62 	.period_bytes_max	= 8192,
63 	.periods_min		= 2,
64 	.periods_max		= 1024,
65 	.buffer_bytes_max	= ATMEL_SSC_DMABUF_SIZE,
66 };
67 
68 
69 /*--------------------------------------------------------------------------*\
70  * Data types
71 \*--------------------------------------------------------------------------*/
72 struct atmel_runtime_data {
73 	struct atmel_pcm_dma_params *params;
74 	dma_addr_t dma_buffer;		/* physical address of dma buffer */
75 	dma_addr_t dma_buffer_end;	/* first address beyond DMA buffer */
76 	size_t period_size;
77 
78 	dma_addr_t period_ptr;		/* physical address of next period */
79 };
80 
81 /*--------------------------------------------------------------------------*\
82  * ISR
83 \*--------------------------------------------------------------------------*/
84 static void atmel_pcm_dma_irq(u32 ssc_sr,
85 	struct snd_pcm_substream *substream)
86 {
87 	struct atmel_runtime_data *prtd = substream->runtime->private_data;
88 	struct atmel_pcm_dma_params *params = prtd->params;
89 	static int count;
90 
91 	count++;
92 
93 	if (ssc_sr & params->mask->ssc_endbuf) {
94 		pr_warn("atmel-pcm: buffer %s on %s (SSC_SR=%#x, count=%d)\n",
95 				substream->stream == SNDRV_PCM_STREAM_PLAYBACK
96 				? "underrun" : "overrun",
97 				params->name, ssc_sr, count);
98 
99 		/* re-start the PDC */
100 		ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
101 			   params->mask->pdc_disable);
102 		prtd->period_ptr += prtd->period_size;
103 		if (prtd->period_ptr >= prtd->dma_buffer_end)
104 			prtd->period_ptr = prtd->dma_buffer;
105 
106 		ssc_writex(params->ssc->regs, params->pdc->xpr,
107 			   prtd->period_ptr);
108 		ssc_writex(params->ssc->regs, params->pdc->xcr,
109 			   prtd->period_size / params->pdc_xfer_size);
110 		ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
111 			   params->mask->pdc_enable);
112 	}
113 
114 	if (ssc_sr & params->mask->ssc_endx) {
115 		/* Load the PDC next pointer and counter registers */
116 		prtd->period_ptr += prtd->period_size;
117 		if (prtd->period_ptr >= prtd->dma_buffer_end)
118 			prtd->period_ptr = prtd->dma_buffer;
119 
120 		ssc_writex(params->ssc->regs, params->pdc->xnpr,
121 			   prtd->period_ptr);
122 		ssc_writex(params->ssc->regs, params->pdc->xncr,
123 			   prtd->period_size / params->pdc_xfer_size);
124 	}
125 
126 	snd_pcm_period_elapsed(substream);
127 }
128 
129 
130 /*--------------------------------------------------------------------------*\
131  * PCM operations
132 \*--------------------------------------------------------------------------*/
133 static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
134 	struct snd_pcm_hw_params *params)
135 {
136 	struct snd_pcm_runtime *runtime = substream->runtime;
137 	struct atmel_runtime_data *prtd = runtime->private_data;
138 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
139 
140 	/* this may get called several times by oss emulation
141 	 * with different params */
142 
143 	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
144 	runtime->dma_bytes = params_buffer_bytes(params);
145 
146 	prtd->params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
147 	prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
148 
149 	prtd->dma_buffer = runtime->dma_addr;
150 	prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
151 	prtd->period_size = params_period_bytes(params);
152 
153 	pr_debug("atmel-pcm: "
154 		"hw_params: DMA for %s initialized "
155 		"(dma_bytes=%zu, period_size=%zu)\n",
156 		prtd->params->name,
157 		runtime->dma_bytes,
158 		prtd->period_size);
159 	return 0;
160 }
161 
162 static int atmel_pcm_hw_free(struct snd_pcm_substream *substream)
163 {
164 	struct atmel_runtime_data *prtd = substream->runtime->private_data;
165 	struct atmel_pcm_dma_params *params = prtd->params;
166 
167 	if (params != NULL) {
168 		ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
169 			   params->mask->pdc_disable);
170 		prtd->params->dma_intr_handler = NULL;
171 	}
172 
173 	return 0;
174 }
175 
176 static int atmel_pcm_prepare(struct snd_pcm_substream *substream)
177 {
178 	struct atmel_runtime_data *prtd = substream->runtime->private_data;
179 	struct atmel_pcm_dma_params *params = prtd->params;
180 
181 	ssc_writex(params->ssc->regs, SSC_IDR,
182 		   params->mask->ssc_endx | params->mask->ssc_endbuf);
183 	ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
184 		   params->mask->pdc_disable);
185 	return 0;
186 }
187 
188 static int atmel_pcm_trigger(struct snd_pcm_substream *substream,
189 	int cmd)
190 {
191 	struct snd_pcm_runtime *rtd = substream->runtime;
192 	struct atmel_runtime_data *prtd = rtd->private_data;
193 	struct atmel_pcm_dma_params *params = prtd->params;
194 	int ret = 0;
195 
196 	pr_debug("atmel-pcm:buffer_size = %ld,"
197 		"dma_area = %p, dma_bytes = %zu\n",
198 		rtd->buffer_size, rtd->dma_area, rtd->dma_bytes);
199 
200 	switch (cmd) {
201 	case SNDRV_PCM_TRIGGER_START:
202 		prtd->period_ptr = prtd->dma_buffer;
203 
204 		ssc_writex(params->ssc->regs, params->pdc->xpr,
205 			   prtd->period_ptr);
206 		ssc_writex(params->ssc->regs, params->pdc->xcr,
207 			   prtd->period_size / params->pdc_xfer_size);
208 
209 		prtd->period_ptr += prtd->period_size;
210 		ssc_writex(params->ssc->regs, params->pdc->xnpr,
211 			   prtd->period_ptr);
212 		ssc_writex(params->ssc->regs, params->pdc->xncr,
213 			   prtd->period_size / params->pdc_xfer_size);
214 
215 		pr_debug("atmel-pcm: trigger: "
216 			"period_ptr=%lx, xpr=%u, "
217 			"xcr=%u, xnpr=%u, xncr=%u\n",
218 			(unsigned long)prtd->period_ptr,
219 			ssc_readx(params->ssc->regs, params->pdc->xpr),
220 			ssc_readx(params->ssc->regs, params->pdc->xcr),
221 			ssc_readx(params->ssc->regs, params->pdc->xnpr),
222 			ssc_readx(params->ssc->regs, params->pdc->xncr));
223 
224 		ssc_writex(params->ssc->regs, SSC_IER,
225 			   params->mask->ssc_endx | params->mask->ssc_endbuf);
226 		ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
227 			   params->mask->pdc_enable);
228 
229 		pr_debug("sr=%u imr=%u\n",
230 			ssc_readx(params->ssc->regs, SSC_SR),
231 			ssc_readx(params->ssc->regs, SSC_IER));
232 		break;		/* SNDRV_PCM_TRIGGER_START */
233 
234 	case SNDRV_PCM_TRIGGER_STOP:
235 	case SNDRV_PCM_TRIGGER_SUSPEND:
236 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
237 		ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
238 			   params->mask->pdc_disable);
239 		break;
240 
241 	case SNDRV_PCM_TRIGGER_RESUME:
242 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
243 		ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
244 			   params->mask->pdc_enable);
245 		break;
246 
247 	default:
248 		ret = -EINVAL;
249 	}
250 
251 	return ret;
252 }
253 
254 static snd_pcm_uframes_t atmel_pcm_pointer(
255 	struct snd_pcm_substream *substream)
256 {
257 	struct snd_pcm_runtime *runtime = substream->runtime;
258 	struct atmel_runtime_data *prtd = runtime->private_data;
259 	struct atmel_pcm_dma_params *params = prtd->params;
260 	dma_addr_t ptr;
261 	snd_pcm_uframes_t x;
262 
263 	ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr);
264 	x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
265 
266 	if (x == runtime->buffer_size)
267 		x = 0;
268 
269 	return x;
270 }
271 
272 static int atmel_pcm_open(struct snd_pcm_substream *substream)
273 {
274 	struct snd_pcm_runtime *runtime = substream->runtime;
275 	struct atmel_runtime_data *prtd;
276 	int ret = 0;
277 
278 	snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware);
279 
280 	/* ensure that buffer size is a multiple of period size */
281 	ret = snd_pcm_hw_constraint_integer(runtime,
282 						SNDRV_PCM_HW_PARAM_PERIODS);
283 	if (ret < 0)
284 		goto out;
285 
286 	prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL);
287 	if (prtd == NULL) {
288 		ret = -ENOMEM;
289 		goto out;
290 	}
291 	runtime->private_data = prtd;
292 
293  out:
294 	return ret;
295 }
296 
297 static int atmel_pcm_close(struct snd_pcm_substream *substream)
298 {
299 	struct atmel_runtime_data *prtd = substream->runtime->private_data;
300 
301 	kfree(prtd);
302 	return 0;
303 }
304 
305 static struct snd_pcm_ops atmel_pcm_ops = {
306 	.open		= atmel_pcm_open,
307 	.close		= atmel_pcm_close,
308 	.ioctl		= snd_pcm_lib_ioctl,
309 	.hw_params	= atmel_pcm_hw_params,
310 	.hw_free	= atmel_pcm_hw_free,
311 	.prepare	= atmel_pcm_prepare,
312 	.trigger	= atmel_pcm_trigger,
313 	.pointer	= atmel_pcm_pointer,
314 	.mmap		= atmel_pcm_mmap,
315 };
316 
317 static struct snd_soc_platform_driver atmel_soc_platform = {
318 	.ops		= &atmel_pcm_ops,
319 	.pcm_new	= atmel_pcm_new,
320 	.pcm_free	= atmel_pcm_free,
321 };
322 
323 int atmel_pcm_pdc_platform_register(struct device *dev)
324 {
325 	return snd_soc_register_platform(dev, &atmel_soc_platform);
326 }
327 EXPORT_SYMBOL(atmel_pcm_pdc_platform_register);
328 
329 void atmel_pcm_pdc_platform_unregister(struct device *dev)
330 {
331 	snd_soc_unregister_platform(dev);
332 }
333 EXPORT_SYMBOL(atmel_pcm_pdc_platform_unregister);
334 
335 MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
336 MODULE_DESCRIPTION("Atmel PCM module");
337 MODULE_LICENSE("GPL");
338