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_new(struct snd_soc_component
*component
,
38 struct snd_soc_pcm_runtime
*rtd
)
40 struct snd_card
*card
= rtd
->card
->snd_card
;
43 ret
= dma_coerce_mask_and_coherent(card
->dev
, DMA_BIT_MASK(32));
47 snd_pcm_set_managed_buffer_all(rtd
->pcm
, SNDRV_DMA_TYPE_DEV
,
48 card
->dev
, ATMEL_SSC_DMABUF_SIZE
,
49 ATMEL_SSC_DMABUF_SIZE
);
54 /*--------------------------------------------------------------------------*\
56 \*--------------------------------------------------------------------------*/
57 /* TODO: These values were taken from the AT91 platform driver, check
58 * them against real values for AT32
60 static const struct snd_pcm_hardware atmel_pcm_hardware
= {
61 .info
= SNDRV_PCM_INFO_MMAP
|
62 SNDRV_PCM_INFO_MMAP_VALID
|
63 SNDRV_PCM_INFO_INTERLEAVED
|
65 .period_bytes_min
= 32,
66 .period_bytes_max
= 8192,
69 .buffer_bytes_max
= ATMEL_SSC_DMABUF_SIZE
,
73 /*--------------------------------------------------------------------------*\
75 \*--------------------------------------------------------------------------*/
76 struct atmel_runtime_data
{
77 struct atmel_pcm_dma_params
*params
;
78 dma_addr_t dma_buffer
; /* physical address of dma buffer */
79 dma_addr_t dma_buffer_end
; /* first address beyond DMA buffer */
82 dma_addr_t period_ptr
; /* physical address of next period */
85 /*--------------------------------------------------------------------------*\
87 \*--------------------------------------------------------------------------*/
88 static void atmel_pcm_dma_irq(u32 ssc_sr
,
89 struct snd_pcm_substream
*substream
)
91 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
92 struct atmel_pcm_dma_params
*params
= prtd
->params
;
97 if (ssc_sr
& params
->mask
->ssc_endbuf
) {
98 pr_warn("atmel-pcm: buffer %s on %s (SSC_SR=%#x, count=%d)\n",
99 substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
100 ? "underrun" : "overrun",
101 params
->name
, ssc_sr
, count
);
103 /* re-start the PDC */
104 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
105 params
->mask
->pdc_disable
);
106 prtd
->period_ptr
+= prtd
->period_size
;
107 if (prtd
->period_ptr
>= prtd
->dma_buffer_end
)
108 prtd
->period_ptr
= prtd
->dma_buffer
;
110 ssc_writex(params
->ssc
->regs
, params
->pdc
->xpr
,
112 ssc_writex(params
->ssc
->regs
, params
->pdc
->xcr
,
113 prtd
->period_size
/ params
->pdc_xfer_size
);
114 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
115 params
->mask
->pdc_enable
);
118 if (ssc_sr
& params
->mask
->ssc_endx
) {
119 /* Load the PDC next pointer and counter registers */
120 prtd
->period_ptr
+= prtd
->period_size
;
121 if (prtd
->period_ptr
>= prtd
->dma_buffer_end
)
122 prtd
->period_ptr
= prtd
->dma_buffer
;
124 ssc_writex(params
->ssc
->regs
, params
->pdc
->xnpr
,
126 ssc_writex(params
->ssc
->regs
, params
->pdc
->xncr
,
127 prtd
->period_size
/ params
->pdc_xfer_size
);
130 snd_pcm_period_elapsed(substream
);
134 /*--------------------------------------------------------------------------*\
136 \*--------------------------------------------------------------------------*/
137 static int atmel_pcm_hw_params(struct snd_soc_component
*component
,
138 struct snd_pcm_substream
*substream
,
139 struct snd_pcm_hw_params
*params
)
141 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
142 struct atmel_runtime_data
*prtd
= runtime
->private_data
;
143 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
145 /* this may get called several times by oss emulation
146 * with different params */
148 prtd
->params
= snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd
, 0), substream
);
149 prtd
->params
->dma_intr_handler
= atmel_pcm_dma_irq
;
151 prtd
->dma_buffer
= runtime
->dma_addr
;
152 prtd
->dma_buffer_end
= runtime
->dma_addr
+ runtime
->dma_bytes
;
153 prtd
->period_size
= params_period_bytes(params
);
155 pr_debug("atmel-pcm: "
156 "hw_params: DMA for %s initialized "
157 "(dma_bytes=%zu, period_size=%zu)\n",
164 static int atmel_pcm_hw_free(struct snd_soc_component
*component
,
165 struct snd_pcm_substream
*substream
)
167 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
168 struct atmel_pcm_dma_params
*params
= prtd
->params
;
170 if (params
!= NULL
) {
171 ssc_writex(params
->ssc
->regs
, SSC_PDC_PTCR
,
172 params
->mask
->pdc_disable
);
173 prtd
->params
->dma_intr_handler
= NULL
;
179 static int atmel_pcm_prepare(struct snd_soc_component
*component
,
180 struct snd_pcm_substream
*substream
)
182 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
183 struct atmel_pcm_dma_params
*params
= prtd
->params
;
185 ssc_writex(params
->ssc
->regs
, SSC_IDR
,
186 params
->mask
->ssc_endx
| params
->mask
->ssc_endbuf
);
187 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
188 params
->mask
->pdc_disable
);
192 static int atmel_pcm_trigger(struct snd_soc_component
*component
,
193 struct snd_pcm_substream
*substream
, int cmd
)
195 struct snd_pcm_runtime
*rtd
= substream
->runtime
;
196 struct atmel_runtime_data
*prtd
= rtd
->private_data
;
197 struct atmel_pcm_dma_params
*params
= prtd
->params
;
200 pr_debug("atmel-pcm:buffer_size = %ld,"
201 "dma_area = %p, dma_bytes = %zu\n",
202 rtd
->buffer_size
, rtd
->dma_area
, rtd
->dma_bytes
);
205 case SNDRV_PCM_TRIGGER_START
:
206 prtd
->period_ptr
= prtd
->dma_buffer
;
208 ssc_writex(params
->ssc
->regs
, params
->pdc
->xpr
,
210 ssc_writex(params
->ssc
->regs
, params
->pdc
->xcr
,
211 prtd
->period_size
/ params
->pdc_xfer_size
);
213 prtd
->period_ptr
+= prtd
->period_size
;
214 ssc_writex(params
->ssc
->regs
, params
->pdc
->xnpr
,
216 ssc_writex(params
->ssc
->regs
, params
->pdc
->xncr
,
217 prtd
->period_size
/ params
->pdc_xfer_size
);
219 pr_debug("atmel-pcm: trigger: "
220 "period_ptr=%lx, xpr=%u, "
221 "xcr=%u, xnpr=%u, xncr=%u\n",
222 (unsigned long)prtd
->period_ptr
,
223 ssc_readx(params
->ssc
->regs
, params
->pdc
->xpr
),
224 ssc_readx(params
->ssc
->regs
, params
->pdc
->xcr
),
225 ssc_readx(params
->ssc
->regs
, params
->pdc
->xnpr
),
226 ssc_readx(params
->ssc
->regs
, params
->pdc
->xncr
));
228 ssc_writex(params
->ssc
->regs
, SSC_IER
,
229 params
->mask
->ssc_endx
| params
->mask
->ssc_endbuf
);
230 ssc_writex(params
->ssc
->regs
, SSC_PDC_PTCR
,
231 params
->mask
->pdc_enable
);
233 pr_debug("sr=%u imr=%u\n",
234 ssc_readx(params
->ssc
->regs
, SSC_SR
),
235 ssc_readx(params
->ssc
->regs
, SSC_IER
));
236 break; /* SNDRV_PCM_TRIGGER_START */
238 case SNDRV_PCM_TRIGGER_STOP
:
239 case SNDRV_PCM_TRIGGER_SUSPEND
:
240 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
241 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
242 params
->mask
->pdc_disable
);
245 case SNDRV_PCM_TRIGGER_RESUME
:
246 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
247 ssc_writex(params
->ssc
->regs
, ATMEL_PDC_PTCR
,
248 params
->mask
->pdc_enable
);
258 static snd_pcm_uframes_t
atmel_pcm_pointer(struct snd_soc_component
*component
,
259 struct snd_pcm_substream
*substream
)
261 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
262 struct atmel_runtime_data
*prtd
= runtime
->private_data
;
263 struct atmel_pcm_dma_params
*params
= prtd
->params
;
267 ptr
= (dma_addr_t
) ssc_readx(params
->ssc
->regs
, params
->pdc
->xpr
);
268 x
= bytes_to_frames(runtime
, ptr
- prtd
->dma_buffer
);
270 if (x
== runtime
->buffer_size
)
276 static int atmel_pcm_open(struct snd_soc_component
*component
,
277 struct snd_pcm_substream
*substream
)
279 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
280 struct atmel_runtime_data
*prtd
;
283 snd_soc_set_runtime_hwparams(substream
, &atmel_pcm_hardware
);
285 /* ensure that buffer size is a multiple of period size */
286 ret
= snd_pcm_hw_constraint_integer(runtime
,
287 SNDRV_PCM_HW_PARAM_PERIODS
);
291 prtd
= kzalloc(sizeof(struct atmel_runtime_data
), GFP_KERNEL
);
296 runtime
->private_data
= prtd
;
302 static int atmel_pcm_close(struct snd_soc_component
*component
,
303 struct snd_pcm_substream
*substream
)
305 struct atmel_runtime_data
*prtd
= substream
->runtime
->private_data
;
311 static const struct snd_soc_component_driver atmel_soc_platform
= {
312 .open
= atmel_pcm_open
,
313 .close
= atmel_pcm_close
,
314 .hw_params
= atmel_pcm_hw_params
,
315 .hw_free
= atmel_pcm_hw_free
,
316 .prepare
= atmel_pcm_prepare
,
317 .trigger
= atmel_pcm_trigger
,
318 .pointer
= atmel_pcm_pointer
,
319 .pcm_construct
= atmel_pcm_new
,
322 int atmel_pcm_pdc_platform_register(struct device
*dev
)
324 return devm_snd_soc_register_component(dev
, &atmel_soc_platform
,
327 EXPORT_SYMBOL(atmel_pcm_pdc_platform_register
);
329 MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
330 MODULE_DESCRIPTION("Atmel PCM module");
331 MODULE_LICENSE("GPL");