2 * atmel-pcm.c -- ALSA PCM interface for the Atmel atmel SoC.
4 * Copyright (C) 2005 SAN People
5 * Copyright (C) 2008 Atmel
7 * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
9 * Based on at91-pcm. by:
10 * Frank Mandarino <fmandarino@endrelia.com>
11 * Copyright 2006 Endrelia Technologies Inc.
13 * Based on pxa2xx-pcm.c by:
15 * Author: Nicolas Pitre
16 * Created: Nov 30, 2004
17 * Copyright: (C) 2004 MontaVista Software, Inc.
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.
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.
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
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>
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc.h>
47 #include "atmel-pcm.h"
50 /*--------------------------------------------------------------------------*\
52 \*--------------------------------------------------------------------------*/
53 /* TODO: These values were taken from the AT91 platform driver, check
54 * them against real values for AT32
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
|
61 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
62 .period_bytes_min
= 32,
63 .period_bytes_max
= 8192,
66 .buffer_bytes_max
= ATMEL_SSC_DMABUF_SIZE
,
70 /*--------------------------------------------------------------------------*\
72 \*--------------------------------------------------------------------------*/
73 struct atmel_runtime_data
{
74 struct atmel_pcm_dma_params
*params
;
75 dma_addr_t dma_buffer
; /* physical address of dma buffer */
76 dma_addr_t dma_buffer_end
; /* first address beyond DMA buffer */
79 dma_addr_t period_ptr
; /* physical address of next period */
81 /* PDC register save */
88 /*--------------------------------------------------------------------------*\
90 \*--------------------------------------------------------------------------*/
91 static void atmel_pcm_dma_irq(u32 ssc_sr
,
92 struct snd_pcm_substream
*substream
)
94 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
95 struct atmel_pcm_dma_params
*params
= prtd
->params
;
100 if (ssc_sr
& params
->mask
->ssc_endbuf
) {
101 pr_warn("atmel-pcm: buffer %s on %s (SSC_SR=%#x, count=%d)\n",
102 substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
103 ? "underrun" : "overrun",
104 params
->name
, ssc_sr
, count
);
106 /* re-start the PDC */
107 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
108 params
->mask
->pdc_disable
);
109 prtd
->period_ptr
+= prtd
->period_size
;
110 if (prtd
->period_ptr
>= prtd
->dma_buffer_end
)
111 prtd
->period_ptr
= prtd
->dma_buffer
;
113 ssc_writex(params
->ssc
->regs
, params
->pdc
->xpr
,
115 ssc_writex(params
->ssc
->regs
, params
->pdc
->xcr
,
116 prtd
->period_size
/ params
->pdc_xfer_size
);
117 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
118 params
->mask
->pdc_enable
);
121 if (ssc_sr
& params
->mask
->ssc_endx
) {
122 /* Load the PDC next pointer and counter registers */
123 prtd
->period_ptr
+= prtd
->period_size
;
124 if (prtd
->period_ptr
>= prtd
->dma_buffer_end
)
125 prtd
->period_ptr
= prtd
->dma_buffer
;
127 ssc_writex(params
->ssc
->regs
, params
->pdc
->xnpr
,
129 ssc_writex(params
->ssc
->regs
, params
->pdc
->xncr
,
130 prtd
->period_size
/ params
->pdc_xfer_size
);
133 snd_pcm_period_elapsed(substream
);
137 /*--------------------------------------------------------------------------*\
139 \*--------------------------------------------------------------------------*/
140 static int atmel_pcm_hw_params(struct snd_pcm_substream
*substream
,
141 struct snd_pcm_hw_params
*params
)
143 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
144 struct atmel_runtime_data
*prtd
= runtime
->private_data
;
145 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
147 /* this may get called several times by oss emulation
148 * with different params */
150 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
151 runtime
->dma_bytes
= params_buffer_bytes(params
);
153 prtd
->params
= snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
154 prtd
->params
->dma_intr_handler
= atmel_pcm_dma_irq
;
156 prtd
->dma_buffer
= runtime
->dma_addr
;
157 prtd
->dma_buffer_end
= runtime
->dma_addr
+ runtime
->dma_bytes
;
158 prtd
->period_size
= params_period_bytes(params
);
160 pr_debug("atmel-pcm: "
161 "hw_params: DMA for %s initialized "
162 "(dma_bytes=%zu, period_size=%zu)\n",
169 static int atmel_pcm_hw_free(struct snd_pcm_substream
*substream
)
171 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
172 struct atmel_pcm_dma_params
*params
= prtd
->params
;
174 if (params
!= NULL
) {
175 ssc_writex(params
->ssc
->regs
, SSC_PDC_PTCR
,
176 params
->mask
->pdc_disable
);
177 prtd
->params
->dma_intr_handler
= NULL
;
183 static int atmel_pcm_prepare(struct snd_pcm_substream
*substream
)
185 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
186 struct atmel_pcm_dma_params
*params
= prtd
->params
;
188 ssc_writex(params
->ssc
->regs
, SSC_IDR
,
189 params
->mask
->ssc_endx
| params
->mask
->ssc_endbuf
);
190 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
191 params
->mask
->pdc_disable
);
195 static int atmel_pcm_trigger(struct snd_pcm_substream
*substream
,
198 struct snd_pcm_runtime
*rtd
= substream
->runtime
;
199 struct atmel_runtime_data
*prtd
= rtd
->private_data
;
200 struct atmel_pcm_dma_params
*params
= prtd
->params
;
203 pr_debug("atmel-pcm:buffer_size = %ld,"
204 "dma_area = %p, dma_bytes = %zu\n",
205 rtd
->buffer_size
, rtd
->dma_area
, rtd
->dma_bytes
);
208 case SNDRV_PCM_TRIGGER_START
:
209 prtd
->period_ptr
= prtd
->dma_buffer
;
211 ssc_writex(params
->ssc
->regs
, params
->pdc
->xpr
,
213 ssc_writex(params
->ssc
->regs
, params
->pdc
->xcr
,
214 prtd
->period_size
/ params
->pdc_xfer_size
);
216 prtd
->period_ptr
+= prtd
->period_size
;
217 ssc_writex(params
->ssc
->regs
, params
->pdc
->xnpr
,
219 ssc_writex(params
->ssc
->regs
, params
->pdc
->xncr
,
220 prtd
->period_size
/ params
->pdc_xfer_size
);
222 pr_debug("atmel-pcm: trigger: "
223 "period_ptr=%lx, xpr=%u, "
224 "xcr=%u, xnpr=%u, xncr=%u\n",
225 (unsigned long)prtd
->period_ptr
,
226 ssc_readx(params
->ssc
->regs
, params
->pdc
->xpr
),
227 ssc_readx(params
->ssc
->regs
, params
->pdc
->xcr
),
228 ssc_readx(params
->ssc
->regs
, params
->pdc
->xnpr
),
229 ssc_readx(params
->ssc
->regs
, params
->pdc
->xncr
));
231 ssc_writex(params
->ssc
->regs
, SSC_IER
,
232 params
->mask
->ssc_endx
| params
->mask
->ssc_endbuf
);
233 ssc_writex(params
->ssc
->regs
, SSC_PDC_PTCR
,
234 params
->mask
->pdc_enable
);
236 pr_debug("sr=%u imr=%u\n",
237 ssc_readx(params
->ssc
->regs
, SSC_SR
),
238 ssc_readx(params
->ssc
->regs
, SSC_IER
));
239 break; /* SNDRV_PCM_TRIGGER_START */
241 case SNDRV_PCM_TRIGGER_STOP
:
242 case SNDRV_PCM_TRIGGER_SUSPEND
:
243 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
244 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
245 params
->mask
->pdc_disable
);
248 case SNDRV_PCM_TRIGGER_RESUME
:
249 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
250 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
251 params
->mask
->pdc_enable
);
261 static snd_pcm_uframes_t
atmel_pcm_pointer(
262 struct snd_pcm_substream
*substream
)
264 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
265 struct atmel_runtime_data
*prtd
= runtime
->private_data
;
266 struct atmel_pcm_dma_params
*params
= prtd
->params
;
270 ptr
= (dma_addr_t
) ssc_readx(params
->ssc
->regs
, params
->pdc
->xpr
);
271 x
= bytes_to_frames(runtime
, ptr
- prtd
->dma_buffer
);
273 if (x
== runtime
->buffer_size
)
279 static int atmel_pcm_open(struct snd_pcm_substream
*substream
)
281 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
282 struct atmel_runtime_data
*prtd
;
285 snd_soc_set_runtime_hwparams(substream
, &atmel_pcm_hardware
);
287 /* ensure that buffer size is a multiple of period size */
288 ret
= snd_pcm_hw_constraint_integer(runtime
,
289 SNDRV_PCM_HW_PARAM_PERIODS
);
293 prtd
= kzalloc(sizeof(struct atmel_runtime_data
), GFP_KERNEL
);
298 runtime
->private_data
= prtd
;
304 static int atmel_pcm_close(struct snd_pcm_substream
*substream
)
306 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
312 static struct snd_pcm_ops atmel_pcm_ops
= {
313 .open
= atmel_pcm_open
,
314 .close
= atmel_pcm_close
,
315 .ioctl
= snd_pcm_lib_ioctl
,
316 .hw_params
= atmel_pcm_hw_params
,
317 .hw_free
= atmel_pcm_hw_free
,
318 .prepare
= atmel_pcm_prepare
,
319 .trigger
= atmel_pcm_trigger
,
320 .pointer
= atmel_pcm_pointer
,
321 .mmap
= atmel_pcm_mmap
,
325 /*--------------------------------------------------------------------------*\
326 * ASoC platform driver
327 \*--------------------------------------------------------------------------*/
329 static int atmel_pcm_suspend(struct snd_soc_dai
*dai
)
331 struct snd_pcm_runtime
*runtime
= dai
->runtime
;
332 struct atmel_runtime_data
*prtd
;
333 struct atmel_pcm_dma_params
*params
;
338 prtd
= runtime
->private_data
;
339 params
= prtd
->params
;
341 /* disable the PDC and save the PDC registers */
343 ssc_writel(params
->ssc
->regs
, PDC_PTCR
, params
->mask
->pdc_disable
);
345 prtd
->pdc_xpr_save
= ssc_readx(params
->ssc
->regs
, params
->pdc
->xpr
);
346 prtd
->pdc_xcr_save
= ssc_readx(params
->ssc
->regs
, params
->pdc
->xcr
);
347 prtd
->pdc_xnpr_save
= ssc_readx(params
->ssc
->regs
, params
->pdc
->xnpr
);
348 prtd
->pdc_xncr_save
= ssc_readx(params
->ssc
->regs
, params
->pdc
->xncr
);
353 static int atmel_pcm_resume(struct snd_soc_dai
*dai
)
355 struct snd_pcm_runtime
*runtime
= dai
->runtime
;
356 struct atmel_runtime_data
*prtd
;
357 struct atmel_pcm_dma_params
*params
;
362 prtd
= runtime
->private_data
;
363 params
= prtd
->params
;
365 /* restore the PDC registers and enable the PDC */
366 ssc_writex(params
->ssc
->regs
, params
->pdc
->xpr
, prtd
->pdc_xpr_save
);
367 ssc_writex(params
->ssc
->regs
, params
->pdc
->xcr
, prtd
->pdc_xcr_save
);
368 ssc_writex(params
->ssc
->regs
, params
->pdc
->xnpr
, prtd
->pdc_xnpr_save
);
369 ssc_writex(params
->ssc
->regs
, params
->pdc
->xncr
, prtd
->pdc_xncr_save
);
371 ssc_writel(params
->ssc
->regs
, PDC_PTCR
, params
->mask
->pdc_enable
);
375 #define atmel_pcm_suspend NULL
376 #define atmel_pcm_resume NULL
379 static struct snd_soc_platform_driver atmel_soc_platform
= {
380 .ops
= &atmel_pcm_ops
,
381 .pcm_new
= atmel_pcm_new
,
382 .pcm_free
= atmel_pcm_free
,
383 .suspend
= atmel_pcm_suspend
,
384 .resume
= atmel_pcm_resume
,
387 int atmel_pcm_pdc_platform_register(struct device
*dev
)
389 return snd_soc_register_platform(dev
, &atmel_soc_platform
);
391 EXPORT_SYMBOL(atmel_pcm_pdc_platform_register
);
393 void atmel_pcm_pdc_platform_unregister(struct device
*dev
)
395 snd_soc_unregister_platform(dev
);
397 EXPORT_SYMBOL(atmel_pcm_pdc_platform_unregister
);
399 MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
400 MODULE_DESCRIPTION("Atmel PCM module");
401 MODULE_LICENSE("GPL");