1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * atmel-pcm.c -- ALSA PCM interface for the Atmel atmel SoC.
5 * Copyright (C) 2005 SAN People
6 * Copyright (C) 2008 Atmel
8 * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
10 * Based on at91-pcm. by:
11 * Frank Mandarino <fmandarino@endrelia.com>
12 * Copyright 2006 Endrelia Technologies Inc.
14 * Based on pxa2xx-pcm.c by:
16 * Author: Nicolas Pitre
17 * Created: Nov 30, 2004
18 * Copyright: (C) 2004 MontaVista Software, Inc.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/atmel_pdc.h>
27 #include <linux/atmel-ssc.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/soc.h>
34 #include "atmel-pcm.h"
37 static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm
*pcm
,
40 struct snd_pcm_substream
*substream
= pcm
->streams
[stream
].substream
;
41 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
42 size_t size
= ATMEL_SSC_DMABUF_SIZE
;
44 buf
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
45 buf
->dev
.dev
= pcm
->card
->dev
;
46 buf
->private_data
= NULL
;
47 buf
->area
= dma_alloc_coherent(pcm
->card
->dev
, size
,
48 &buf
->addr
, GFP_KERNEL
);
49 pr_debug("atmel-pcm: alloc dma buffer: area=%p, addr=%p, size=%zu\n",
50 (void *)buf
->area
, (void *)(long)buf
->addr
, size
);
59 static int atmel_pcm_mmap(struct snd_pcm_substream
*substream
,
60 struct vm_area_struct
*vma
)
62 return remap_pfn_range(vma
, vma
->vm_start
,
63 substream
->dma_buffer
.addr
>> PAGE_SHIFT
,
64 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
);
67 static int atmel_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
69 struct snd_card
*card
= rtd
->card
->snd_card
;
70 struct snd_pcm
*pcm
= rtd
->pcm
;
73 ret
= dma_coerce_mask_and_coherent(card
->dev
, DMA_BIT_MASK(32));
77 if (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
) {
78 pr_debug("atmel-pcm: allocating PCM playback DMA buffer\n");
79 ret
= atmel_pcm_preallocate_dma_buffer(pcm
,
80 SNDRV_PCM_STREAM_PLAYBACK
);
85 if (pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
) {
86 pr_debug("atmel-pcm: allocating PCM capture DMA buffer\n");
87 ret
= atmel_pcm_preallocate_dma_buffer(pcm
,
88 SNDRV_PCM_STREAM_CAPTURE
);
96 static void atmel_pcm_free(struct snd_pcm
*pcm
)
98 struct snd_pcm_substream
*substream
;
99 struct snd_dma_buffer
*buf
;
102 for (stream
= 0; stream
< 2; stream
++) {
103 substream
= pcm
->streams
[stream
].substream
;
107 buf
= &substream
->dma_buffer
;
110 dma_free_coherent(pcm
->card
->dev
, buf
->bytes
,
111 buf
->area
, buf
->addr
);
116 /*--------------------------------------------------------------------------*\
117 * Hardware definition
118 \*--------------------------------------------------------------------------*/
119 /* TODO: These values were taken from the AT91 platform driver, check
120 * them against real values for AT32
122 static const struct snd_pcm_hardware atmel_pcm_hardware
= {
123 .info
= SNDRV_PCM_INFO_MMAP
|
124 SNDRV_PCM_INFO_MMAP_VALID
|
125 SNDRV_PCM_INFO_INTERLEAVED
|
126 SNDRV_PCM_INFO_PAUSE
,
127 .period_bytes_min
= 32,
128 .period_bytes_max
= 8192,
131 .buffer_bytes_max
= ATMEL_SSC_DMABUF_SIZE
,
135 /*--------------------------------------------------------------------------*\
137 \*--------------------------------------------------------------------------*/
138 struct atmel_runtime_data
{
139 struct atmel_pcm_dma_params
*params
;
140 dma_addr_t dma_buffer
; /* physical address of dma buffer */
141 dma_addr_t dma_buffer_end
; /* first address beyond DMA buffer */
144 dma_addr_t period_ptr
; /* physical address of next period */
147 /*--------------------------------------------------------------------------*\
149 \*--------------------------------------------------------------------------*/
150 static void atmel_pcm_dma_irq(u32 ssc_sr
,
151 struct snd_pcm_substream
*substream
)
153 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
154 struct atmel_pcm_dma_params
*params
= prtd
->params
;
159 if (ssc_sr
& params
->mask
->ssc_endbuf
) {
160 pr_warn("atmel-pcm: buffer %s on %s (SSC_SR=%#x, count=%d)\n",
161 substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
162 ? "underrun" : "overrun",
163 params
->name
, ssc_sr
, count
);
165 /* re-start the PDC */
166 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
167 params
->mask
->pdc_disable
);
168 prtd
->period_ptr
+= prtd
->period_size
;
169 if (prtd
->period_ptr
>= prtd
->dma_buffer_end
)
170 prtd
->period_ptr
= prtd
->dma_buffer
;
172 ssc_writex(params
->ssc
->regs
, params
->pdc
->xpr
,
174 ssc_writex(params
->ssc
->regs
, params
->pdc
->xcr
,
175 prtd
->period_size
/ params
->pdc_xfer_size
);
176 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
177 params
->mask
->pdc_enable
);
180 if (ssc_sr
& params
->mask
->ssc_endx
) {
181 /* Load the PDC next pointer and counter registers */
182 prtd
->period_ptr
+= prtd
->period_size
;
183 if (prtd
->period_ptr
>= prtd
->dma_buffer_end
)
184 prtd
->period_ptr
= prtd
->dma_buffer
;
186 ssc_writex(params
->ssc
->regs
, params
->pdc
->xnpr
,
188 ssc_writex(params
->ssc
->regs
, params
->pdc
->xncr
,
189 prtd
->period_size
/ params
->pdc_xfer_size
);
192 snd_pcm_period_elapsed(substream
);
196 /*--------------------------------------------------------------------------*\
198 \*--------------------------------------------------------------------------*/
199 static int atmel_pcm_hw_params(struct snd_pcm_substream
*substream
,
200 struct snd_pcm_hw_params
*params
)
202 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
203 struct atmel_runtime_data
*prtd
= runtime
->private_data
;
204 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
206 /* this may get called several times by oss emulation
207 * with different params */
209 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
210 runtime
->dma_bytes
= params_buffer_bytes(params
);
212 prtd
->params
= snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
213 prtd
->params
->dma_intr_handler
= atmel_pcm_dma_irq
;
215 prtd
->dma_buffer
= runtime
->dma_addr
;
216 prtd
->dma_buffer_end
= runtime
->dma_addr
+ runtime
->dma_bytes
;
217 prtd
->period_size
= params_period_bytes(params
);
219 pr_debug("atmel-pcm: "
220 "hw_params: DMA for %s initialized "
221 "(dma_bytes=%zu, period_size=%zu)\n",
228 static int atmel_pcm_hw_free(struct snd_pcm_substream
*substream
)
230 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
231 struct atmel_pcm_dma_params
*params
= prtd
->params
;
233 if (params
!= NULL
) {
234 ssc_writex(params
->ssc
->regs
, SSC_PDC_PTCR
,
235 params
->mask
->pdc_disable
);
236 prtd
->params
->dma_intr_handler
= NULL
;
242 static int atmel_pcm_prepare(struct snd_pcm_substream
*substream
)
244 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
245 struct atmel_pcm_dma_params
*params
= prtd
->params
;
247 ssc_writex(params
->ssc
->regs
, SSC_IDR
,
248 params
->mask
->ssc_endx
| params
->mask
->ssc_endbuf
);
249 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
250 params
->mask
->pdc_disable
);
254 static int atmel_pcm_trigger(struct snd_pcm_substream
*substream
,
257 struct snd_pcm_runtime
*rtd
= substream
->runtime
;
258 struct atmel_runtime_data
*prtd
= rtd
->private_data
;
259 struct atmel_pcm_dma_params
*params
= prtd
->params
;
262 pr_debug("atmel-pcm:buffer_size = %ld,"
263 "dma_area = %p, dma_bytes = %zu\n",
264 rtd
->buffer_size
, rtd
->dma_area
, rtd
->dma_bytes
);
267 case SNDRV_PCM_TRIGGER_START
:
268 prtd
->period_ptr
= prtd
->dma_buffer
;
270 ssc_writex(params
->ssc
->regs
, params
->pdc
->xpr
,
272 ssc_writex(params
->ssc
->regs
, params
->pdc
->xcr
,
273 prtd
->period_size
/ params
->pdc_xfer_size
);
275 prtd
->period_ptr
+= prtd
->period_size
;
276 ssc_writex(params
->ssc
->regs
, params
->pdc
->xnpr
,
278 ssc_writex(params
->ssc
->regs
, params
->pdc
->xncr
,
279 prtd
->period_size
/ params
->pdc_xfer_size
);
281 pr_debug("atmel-pcm: trigger: "
282 "period_ptr=%lx, xpr=%u, "
283 "xcr=%u, xnpr=%u, xncr=%u\n",
284 (unsigned long)prtd
->period_ptr
,
285 ssc_readx(params
->ssc
->regs
, params
->pdc
->xpr
),
286 ssc_readx(params
->ssc
->regs
, params
->pdc
->xcr
),
287 ssc_readx(params
->ssc
->regs
, params
->pdc
->xnpr
),
288 ssc_readx(params
->ssc
->regs
, params
->pdc
->xncr
));
290 ssc_writex(params
->ssc
->regs
, SSC_IER
,
291 params
->mask
->ssc_endx
| params
->mask
->ssc_endbuf
);
292 ssc_writex(params
->ssc
->regs
, SSC_PDC_PTCR
,
293 params
->mask
->pdc_enable
);
295 pr_debug("sr=%u imr=%u\n",
296 ssc_readx(params
->ssc
->regs
, SSC_SR
),
297 ssc_readx(params
->ssc
->regs
, SSC_IER
));
298 break; /* SNDRV_PCM_TRIGGER_START */
300 case SNDRV_PCM_TRIGGER_STOP
:
301 case SNDRV_PCM_TRIGGER_SUSPEND
:
302 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
303 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
304 params
->mask
->pdc_disable
);
307 case SNDRV_PCM_TRIGGER_RESUME
:
308 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
309 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
310 params
->mask
->pdc_enable
);
320 static snd_pcm_uframes_t
atmel_pcm_pointer(
321 struct snd_pcm_substream
*substream
)
323 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
324 struct atmel_runtime_data
*prtd
= runtime
->private_data
;
325 struct atmel_pcm_dma_params
*params
= prtd
->params
;
329 ptr
= (dma_addr_t
) ssc_readx(params
->ssc
->regs
, params
->pdc
->xpr
);
330 x
= bytes_to_frames(runtime
, ptr
- prtd
->dma_buffer
);
332 if (x
== runtime
->buffer_size
)
338 static int atmel_pcm_open(struct snd_pcm_substream
*substream
)
340 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
341 struct atmel_runtime_data
*prtd
;
344 snd_soc_set_runtime_hwparams(substream
, &atmel_pcm_hardware
);
346 /* ensure that buffer size is a multiple of period size */
347 ret
= snd_pcm_hw_constraint_integer(runtime
,
348 SNDRV_PCM_HW_PARAM_PERIODS
);
352 prtd
= kzalloc(sizeof(struct atmel_runtime_data
), GFP_KERNEL
);
357 runtime
->private_data
= prtd
;
363 static int atmel_pcm_close(struct snd_pcm_substream
*substream
)
365 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
371 static const struct snd_pcm_ops atmel_pcm_ops
= {
372 .open
= atmel_pcm_open
,
373 .close
= atmel_pcm_close
,
374 .ioctl
= snd_pcm_lib_ioctl
,
375 .hw_params
= atmel_pcm_hw_params
,
376 .hw_free
= atmel_pcm_hw_free
,
377 .prepare
= atmel_pcm_prepare
,
378 .trigger
= atmel_pcm_trigger
,
379 .pointer
= atmel_pcm_pointer
,
380 .mmap
= atmel_pcm_mmap
,
383 static struct snd_soc_component_driver atmel_soc_platform
= {
384 .ops
= &atmel_pcm_ops
,
385 .pcm_new
= atmel_pcm_new
,
386 .pcm_free
= atmel_pcm_free
,
389 int atmel_pcm_pdc_platform_register(struct device
*dev
)
391 return devm_snd_soc_register_component(dev
, &atmel_soc_platform
,
394 EXPORT_SYMBOL(atmel_pcm_pdc_platform_register
);
396 MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
397 MODULE_DESCRIPTION("Atmel PCM module");
398 MODULE_LICENSE("GPL");