• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * atmel-pcm.c  --  ALSA PCM interface for the Atmel atmel SoC.
4  *
5  *  Copyright (C) 2005 SAN People
6  *  Copyright (C) 2008 Atmel
7  *
8  * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
9  *
10  * Based on at91-pcm. by:
11  * Frank Mandarino <fmandarino@endrelia.com>
12  * Copyright 2006 Endrelia Technologies Inc.
13  *
14  * Based on pxa2xx-pcm.c by:
15  *
16  * Author:	Nicolas Pitre
17  * Created:	Nov 30, 2004
18  * Copyright:	(C) 2004 MontaVista Software, Inc.
19  */
20 
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>
28 
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/soc.h>
33 
34 #include "atmel-pcm.h"
35 
36 
atmel_pcm_preallocate_dma_buffer(struct snd_pcm * pcm,int stream)37 static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
38 	int stream)
39 {
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;
43 
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);
51 
52 	if (!buf->area)
53 		return -ENOMEM;
54 
55 	buf->bytes = size;
56 	return 0;
57 }
58 
atmel_pcm_mmap(struct snd_pcm_substream * substream,struct vm_area_struct * vma)59 static int atmel_pcm_mmap(struct snd_pcm_substream *substream,
60 	struct vm_area_struct *vma)
61 {
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);
65 }
66 
atmel_pcm_new(struct snd_soc_pcm_runtime * rtd)67 static int atmel_pcm_new(struct snd_soc_pcm_runtime *rtd)
68 {
69 	struct snd_card *card = rtd->card->snd_card;
70 	struct snd_pcm *pcm = rtd->pcm;
71 	int ret;
72 
73 	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
74 	if (ret)
75 		return ret;
76 
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);
81 		if (ret)
82 			goto out;
83 	}
84 
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);
89 		if (ret)
90 			goto out;
91 	}
92  out:
93 	return ret;
94 }
95 
atmel_pcm_free(struct snd_pcm * pcm)96 static void atmel_pcm_free(struct snd_pcm *pcm)
97 {
98 	struct snd_pcm_substream *substream;
99 	struct snd_dma_buffer *buf;
100 	int stream;
101 
102 	for (stream = 0; stream < 2; stream++) {
103 		substream = pcm->streams[stream].substream;
104 		if (!substream)
105 			continue;
106 
107 		buf = &substream->dma_buffer;
108 		if (!buf->area)
109 			continue;
110 		dma_free_coherent(pcm->card->dev, buf->bytes,
111 				  buf->area, buf->addr);
112 		buf->area = NULL;
113 	}
114 }
115 
116 /*--------------------------------------------------------------------------*\
117  * Hardware definition
118 \*--------------------------------------------------------------------------*/
119 /* TODO: These values were taken from the AT91 platform driver, check
120  *	 them against real values for AT32
121  */
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,
129 	.periods_min		= 2,
130 	.periods_max		= 1024,
131 	.buffer_bytes_max	= ATMEL_SSC_DMABUF_SIZE,
132 };
133 
134 
135 /*--------------------------------------------------------------------------*\
136  * Data types
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 */
142 	size_t period_size;
143 
144 	dma_addr_t period_ptr;		/* physical address of next period */
145 };
146 
147 /*--------------------------------------------------------------------------*\
148  * ISR
149 \*--------------------------------------------------------------------------*/
atmel_pcm_dma_irq(u32 ssc_sr,struct snd_pcm_substream * substream)150 static void atmel_pcm_dma_irq(u32 ssc_sr,
151 	struct snd_pcm_substream *substream)
152 {
153 	struct atmel_runtime_data *prtd = substream->runtime->private_data;
154 	struct atmel_pcm_dma_params *params = prtd->params;
155 	static int count;
156 
157 	count++;
158 
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);
164 
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;
171 
172 		ssc_writex(params->ssc->regs, params->pdc->xpr,
173 			   prtd->period_ptr);
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);
178 	}
179 
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;
185 
186 		ssc_writex(params->ssc->regs, params->pdc->xnpr,
187 			   prtd->period_ptr);
188 		ssc_writex(params->ssc->regs, params->pdc->xncr,
189 			   prtd->period_size / params->pdc_xfer_size);
190 	}
191 
192 	snd_pcm_period_elapsed(substream);
193 }
194 
195 
196 /*--------------------------------------------------------------------------*\
197  * PCM operations
198 \*--------------------------------------------------------------------------*/
atmel_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)199 static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
200 	struct snd_pcm_hw_params *params)
201 {
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;
205 
206 	/* this may get called several times by oss emulation
207 	 * with different params */
208 
209 	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
210 	runtime->dma_bytes = params_buffer_bytes(params);
211 
212 	prtd->params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
213 	prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
214 
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);
218 
219 	pr_debug("atmel-pcm: "
220 		"hw_params: DMA for %s initialized "
221 		"(dma_bytes=%zu, period_size=%zu)\n",
222 		prtd->params->name,
223 		runtime->dma_bytes,
224 		prtd->period_size);
225 	return 0;
226 }
227 
atmel_pcm_hw_free(struct snd_pcm_substream * substream)228 static int atmel_pcm_hw_free(struct snd_pcm_substream *substream)
229 {
230 	struct atmel_runtime_data *prtd = substream->runtime->private_data;
231 	struct atmel_pcm_dma_params *params = prtd->params;
232 
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;
237 	}
238 
239 	return 0;
240 }
241 
atmel_pcm_prepare(struct snd_pcm_substream * substream)242 static int atmel_pcm_prepare(struct snd_pcm_substream *substream)
243 {
244 	struct atmel_runtime_data *prtd = substream->runtime->private_data;
245 	struct atmel_pcm_dma_params *params = prtd->params;
246 
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);
251 	return 0;
252 }
253 
atmel_pcm_trigger(struct snd_pcm_substream * substream,int cmd)254 static int atmel_pcm_trigger(struct snd_pcm_substream *substream,
255 	int cmd)
256 {
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;
260 	int ret = 0;
261 
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);
265 
266 	switch (cmd) {
267 	case SNDRV_PCM_TRIGGER_START:
268 		prtd->period_ptr = prtd->dma_buffer;
269 
270 		ssc_writex(params->ssc->regs, params->pdc->xpr,
271 			   prtd->period_ptr);
272 		ssc_writex(params->ssc->regs, params->pdc->xcr,
273 			   prtd->period_size / params->pdc_xfer_size);
274 
275 		prtd->period_ptr += prtd->period_size;
276 		ssc_writex(params->ssc->regs, params->pdc->xnpr,
277 			   prtd->period_ptr);
278 		ssc_writex(params->ssc->regs, params->pdc->xncr,
279 			   prtd->period_size / params->pdc_xfer_size);
280 
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));
289 
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);
294 
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 */
299 
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);
305 		break;
306 
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);
311 		break;
312 
313 	default:
314 		ret = -EINVAL;
315 	}
316 
317 	return ret;
318 }
319 
atmel_pcm_pointer(struct snd_pcm_substream * substream)320 static snd_pcm_uframes_t atmel_pcm_pointer(
321 	struct snd_pcm_substream *substream)
322 {
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;
326 	dma_addr_t ptr;
327 	snd_pcm_uframes_t x;
328 
329 	ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr);
330 	x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
331 
332 	if (x == runtime->buffer_size)
333 		x = 0;
334 
335 	return x;
336 }
337 
atmel_pcm_open(struct snd_pcm_substream * substream)338 static int atmel_pcm_open(struct snd_pcm_substream *substream)
339 {
340 	struct snd_pcm_runtime *runtime = substream->runtime;
341 	struct atmel_runtime_data *prtd;
342 	int ret = 0;
343 
344 	snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware);
345 
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);
349 	if (ret < 0)
350 		goto out;
351 
352 	prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL);
353 	if (prtd == NULL) {
354 		ret = -ENOMEM;
355 		goto out;
356 	}
357 	runtime->private_data = prtd;
358 
359  out:
360 	return ret;
361 }
362 
atmel_pcm_close(struct snd_pcm_substream * substream)363 static int atmel_pcm_close(struct snd_pcm_substream *substream)
364 {
365 	struct atmel_runtime_data *prtd = substream->runtime->private_data;
366 
367 	kfree(prtd);
368 	return 0;
369 }
370 
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,
381 };
382 
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,
387 };
388 
atmel_pcm_pdc_platform_register(struct device * dev)389 int atmel_pcm_pdc_platform_register(struct device *dev)
390 {
391 	return devm_snd_soc_register_component(dev, &atmel_soc_platform,
392 					       NULL, 0);
393 }
394 EXPORT_SYMBOL(atmel_pcm_pdc_platform_register);
395 
396 MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
397 MODULE_DESCRIPTION("Atmel PCM module");
398 MODULE_LICENSE("GPL");
399