blob: f6735169477ef785143be4d2526d9cf1477b6ab0 [file] [log] [blame]
Sedji Gaouaou6c742502008-10-03 16:57:50 +02001/*
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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070021 * the Free Software Foundation; only version 2 of the License.
Sedji Gaouaou6c742502008-10-03 16:57:50 +020022 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 */
32
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/platform_device.h>
36#include <linux/slab.h>
37#include <linux/dma-mapping.h>
38#include <linux/atmel_pdc.h>
39#include <linux/atmel-ssc.h>
40
41#include <sound/core.h>
42#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc.h>
45
Sedji Gaouaou6c742502008-10-03 16:57:50 +020046#include "atmel-pcm.h"
47
48
49/*--------------------------------------------------------------------------*\
50 * Hardware definition
51\*--------------------------------------------------------------------------*/
52/* TODO: These values were taken from the AT91 platform driver, check
53 * them against real values for AT32
54 */
55static const struct snd_pcm_hardware atmel_pcm_hardware = {
56 .info = SNDRV_PCM_INFO_MMAP |
57 SNDRV_PCM_INFO_MMAP_VALID |
58 SNDRV_PCM_INFO_INTERLEAVED |
59 SNDRV_PCM_INFO_PAUSE,
60 .formats = SNDRV_PCM_FMTBIT_S16_LE,
61 .period_bytes_min = 32,
62 .period_bytes_max = 8192,
63 .periods_min = 2,
64 .periods_max = 1024,
65 .buffer_bytes_max = 32 * 1024,
66};
67
68
69/*--------------------------------------------------------------------------*\
70 * Data types
71\*--------------------------------------------------------------------------*/
72struct 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 */
Sedji Gaouaou6c742502008-10-03 16:57:50 +020079
80 /* PDC register save */
81 u32 pdc_xpr_save;
82 u32 pdc_xcr_save;
83 u32 pdc_xnpr_save;
84 u32 pdc_xncr_save;
85};
86
87
88/*--------------------------------------------------------------------------*\
89 * Helper functions
90\*--------------------------------------------------------------------------*/
91static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
92 int stream)
93{
94 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
95 struct snd_dma_buffer *buf = &substream->dma_buffer;
96 size_t size = atmel_pcm_hardware.buffer_bytes_max;
97
98 buf->dev.type = SNDRV_DMA_TYPE_DEV;
99 buf->dev.dev = pcm->card->dev;
100 buf->private_data = NULL;
101 buf->area = dma_alloc_coherent(pcm->card->dev, size,
102 &buf->addr, GFP_KERNEL);
103 pr_debug("atmel-pcm:"
104 "preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
105 (void *) buf->area,
106 (void *) buf->addr,
107 size);
108
109 if (!buf->area)
110 return -ENOMEM;
111
112 buf->bytes = size;
113 return 0;
114}
115/*--------------------------------------------------------------------------*\
116 * ISR
117\*--------------------------------------------------------------------------*/
118static void atmel_pcm_dma_irq(u32 ssc_sr,
119 struct snd_pcm_substream *substream)
120{
121 struct atmel_runtime_data *prtd = substream->runtime->private_data;
122 struct atmel_pcm_dma_params *params = prtd->params;
123 static int count;
124
125 count++;
126
127 if (ssc_sr & params->mask->ssc_endbuf) {
128 pr_warning("atmel-pcm: buffer %s on %s"
129 " (SSC_SR=%#x, count=%d)\n",
130 substream->stream == SNDRV_PCM_STREAM_PLAYBACK
131 ? "underrun" : "overrun",
132 params->name, ssc_sr, count);
133
134 /* re-start the PDC */
135 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
136 params->mask->pdc_disable);
137 prtd->period_ptr += prtd->period_size;
138 if (prtd->period_ptr >= prtd->dma_buffer_end)
139 prtd->period_ptr = prtd->dma_buffer;
140
141 ssc_writex(params->ssc->regs, params->pdc->xpr,
142 prtd->period_ptr);
143 ssc_writex(params->ssc->regs, params->pdc->xcr,
144 prtd->period_size / params->pdc_xfer_size);
145 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
146 params->mask->pdc_enable);
147 }
148
149 if (ssc_sr & params->mask->ssc_endx) {
150 /* Load the PDC next pointer and counter registers */
151 prtd->period_ptr += prtd->period_size;
152 if (prtd->period_ptr >= prtd->dma_buffer_end)
153 prtd->period_ptr = prtd->dma_buffer;
154
155 ssc_writex(params->ssc->regs, params->pdc->xnpr,
156 prtd->period_ptr);
157 ssc_writex(params->ssc->regs, params->pdc->xncr,
158 prtd->period_size / params->pdc_xfer_size);
159 }
160
161 snd_pcm_period_elapsed(substream);
162}
163
164
165/*--------------------------------------------------------------------------*\
166 * PCM operations
167\*--------------------------------------------------------------------------*/
168static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
169 struct snd_pcm_hw_params *params)
170{
171 struct snd_pcm_runtime *runtime = substream->runtime;
172 struct atmel_runtime_data *prtd = runtime->private_data;
173 struct snd_soc_pcm_runtime *rtd = substream->private_data;
174
175 /* this may get called several times by oss emulation
176 * with different params */
177
178 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
179 runtime->dma_bytes = params_buffer_bytes(params);
180
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000181 prtd->params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200182 prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
183
184 prtd->dma_buffer = runtime->dma_addr;
185 prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
186 prtd->period_size = params_period_bytes(params);
187
188 pr_debug("atmel-pcm: "
189 "hw_params: DMA for %s initialized "
190 "(dma_bytes=%u, period_size=%u)\n",
191 prtd->params->name,
192 runtime->dma_bytes,
193 prtd->period_size);
194 return 0;
195}
196
197static int atmel_pcm_hw_free(struct snd_pcm_substream *substream)
198{
199 struct atmel_runtime_data *prtd = substream->runtime->private_data;
200 struct atmel_pcm_dma_params *params = prtd->params;
201
202 if (params != NULL) {
203 ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
204 params->mask->pdc_disable);
205 prtd->params->dma_intr_handler = NULL;
206 }
207
208 return 0;
209}
210
211static int atmel_pcm_prepare(struct snd_pcm_substream *substream)
212{
213 struct atmel_runtime_data *prtd = substream->runtime->private_data;
214 struct atmel_pcm_dma_params *params = prtd->params;
215
216 ssc_writex(params->ssc->regs, SSC_IDR,
217 params->mask->ssc_endx | params->mask->ssc_endbuf);
218 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
219 params->mask->pdc_disable);
220 return 0;
221}
222
223static int atmel_pcm_trigger(struct snd_pcm_substream *substream,
224 int cmd)
225{
226 struct snd_pcm_runtime *rtd = substream->runtime;
227 struct atmel_runtime_data *prtd = rtd->private_data;
228 struct atmel_pcm_dma_params *params = prtd->params;
229 int ret = 0;
230
231 pr_debug("atmel-pcm:buffer_size = %ld,"
232 "dma_area = %p, dma_bytes = %u\n",
233 rtd->buffer_size, rtd->dma_area, rtd->dma_bytes);
234
235 switch (cmd) {
236 case SNDRV_PCM_TRIGGER_START:
237 prtd->period_ptr = prtd->dma_buffer;
238
239 ssc_writex(params->ssc->regs, params->pdc->xpr,
240 prtd->period_ptr);
241 ssc_writex(params->ssc->regs, params->pdc->xcr,
242 prtd->period_size / params->pdc_xfer_size);
243
244 prtd->period_ptr += prtd->period_size;
245 ssc_writex(params->ssc->regs, params->pdc->xnpr,
246 prtd->period_ptr);
247 ssc_writex(params->ssc->regs, params->pdc->xncr,
248 prtd->period_size / params->pdc_xfer_size);
249
250 pr_debug("atmel-pcm: trigger: "
251 "period_ptr=%lx, xpr=%u, "
252 "xcr=%u, xnpr=%u, xncr=%u\n",
253 (unsigned long)prtd->period_ptr,
254 ssc_readx(params->ssc->regs, params->pdc->xpr),
255 ssc_readx(params->ssc->regs, params->pdc->xcr),
256 ssc_readx(params->ssc->regs, params->pdc->xnpr),
257 ssc_readx(params->ssc->regs, params->pdc->xncr));
258
259 ssc_writex(params->ssc->regs, SSC_IER,
260 params->mask->ssc_endx | params->mask->ssc_endbuf);
261 ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
262 params->mask->pdc_enable);
263
264 pr_debug("sr=%u imr=%u\n",
265 ssc_readx(params->ssc->regs, SSC_SR),
266 ssc_readx(params->ssc->regs, SSC_IER));
267 break; /* SNDRV_PCM_TRIGGER_START */
268
269 case SNDRV_PCM_TRIGGER_STOP:
270 case SNDRV_PCM_TRIGGER_SUSPEND:
271 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
272 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
273 params->mask->pdc_disable);
274 break;
275
276 case SNDRV_PCM_TRIGGER_RESUME:
277 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
278 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
279 params->mask->pdc_enable);
280 break;
281
282 default:
283 ret = -EINVAL;
284 }
285
286 return ret;
287}
288
289static snd_pcm_uframes_t atmel_pcm_pointer(
290 struct snd_pcm_substream *substream)
291{
292 struct snd_pcm_runtime *runtime = substream->runtime;
293 struct atmel_runtime_data *prtd = runtime->private_data;
294 struct atmel_pcm_dma_params *params = prtd->params;
295 dma_addr_t ptr;
296 snd_pcm_uframes_t x;
297
298 ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr);
299 x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
300
301 if (x == runtime->buffer_size)
302 x = 0;
303
304 return x;
305}
306
307static int atmel_pcm_open(struct snd_pcm_substream *substream)
308{
309 struct snd_pcm_runtime *runtime = substream->runtime;
310 struct atmel_runtime_data *prtd;
311 int ret = 0;
312
313 snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware);
314
315 /* ensure that buffer size is a multiple of period size */
316 ret = snd_pcm_hw_constraint_integer(runtime,
317 SNDRV_PCM_HW_PARAM_PERIODS);
318 if (ret < 0)
319 goto out;
320
321 prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL);
322 if (prtd == NULL) {
323 ret = -ENOMEM;
324 goto out;
325 }
326 runtime->private_data = prtd;
327
328 out:
329 return ret;
330}
331
332static int atmel_pcm_close(struct snd_pcm_substream *substream)
333{
334 struct atmel_runtime_data *prtd = substream->runtime->private_data;
335
336 kfree(prtd);
337 return 0;
338}
339
340static int atmel_pcm_mmap(struct snd_pcm_substream *substream,
341 struct vm_area_struct *vma)
342{
343 return remap_pfn_range(vma, vma->vm_start,
344 substream->dma_buffer.addr >> PAGE_SHIFT,
345 vma->vm_end - vma->vm_start, vma->vm_page_prot);
346}
347
Mark Brownb2a19d02009-01-17 19:14:26 +0000348static struct snd_pcm_ops atmel_pcm_ops = {
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200349 .open = atmel_pcm_open,
350 .close = atmel_pcm_close,
351 .ioctl = snd_pcm_lib_ioctl,
352 .hw_params = atmel_pcm_hw_params,
353 .hw_free = atmel_pcm_hw_free,
354 .prepare = atmel_pcm_prepare,
355 .trigger = atmel_pcm_trigger,
356 .pointer = atmel_pcm_pointer,
357 .mmap = atmel_pcm_mmap,
358};
359
360
361/*--------------------------------------------------------------------------*\
362 * ASoC platform driver
363\*--------------------------------------------------------------------------*/
364static u64 atmel_pcm_dmamask = 0xffffffff;
365
Liam Girdwoodc6ac9842011-02-04 22:12:30 +0000366static int atmel_pcm_new(struct snd_soc_pcm_runtime *rtd)
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200367{
Liam Girdwoodc6ac9842011-02-04 22:12:30 +0000368 struct snd_card *card = rtd->card->snd_card;
369 struct snd_soc_dai *dai = rtd->cpu_dai;
370 struct snd_pcm *pcm = rtd->pcm;
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200371 int ret = 0;
372
373 if (!card->dev->dma_mask)
374 card->dev->dma_mask = &atmel_pcm_dmamask;
375 if (!card->dev->coherent_dma_mask)
376 card->dev->coherent_dma_mask = 0xffffffff;
377
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000378 if (dai->driver->playback.channels_min) {
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200379 ret = atmel_pcm_preallocate_dma_buffer(pcm,
380 SNDRV_PCM_STREAM_PLAYBACK);
381 if (ret)
382 goto out;
383 }
384
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000385 if (dai->driver->capture.channels_min) {
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200386 pr_debug("at32-pcm:"
387 "Allocating PCM capture DMA buffer\n");
388 ret = atmel_pcm_preallocate_dma_buffer(pcm,
389 SNDRV_PCM_STREAM_CAPTURE);
390 if (ret)
391 goto out;
392 }
393 out:
394 return ret;
395}
396
397static void atmel_pcm_free_dma_buffers(struct snd_pcm *pcm)
398{
399 struct snd_pcm_substream *substream;
400 struct snd_dma_buffer *buf;
401 int stream;
402
403 for (stream = 0; stream < 2; stream++) {
404 substream = pcm->streams[stream].substream;
405 if (!substream)
406 continue;
407
408 buf = &substream->dma_buffer;
409 if (!buf->area)
410 continue;
411 dma_free_coherent(pcm->card->dev, buf->bytes,
412 buf->area, buf->addr);
413 buf->area = NULL;
414 }
415}
416
417#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000418static int atmel_pcm_suspend(struct snd_soc_dai *dai)
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200419{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000420 struct snd_pcm_runtime *runtime = dai->runtime;
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200421 struct atmel_runtime_data *prtd;
422 struct atmel_pcm_dma_params *params;
423
424 if (!runtime)
425 return 0;
426
427 prtd = runtime->private_data;
428 params = prtd->params;
429
430 /* disable the PDC and save the PDC registers */
431
432 ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_disable);
433
434 prtd->pdc_xpr_save = ssc_readx(params->ssc->regs, params->pdc->xpr);
435 prtd->pdc_xcr_save = ssc_readx(params->ssc->regs, params->pdc->xcr);
436 prtd->pdc_xnpr_save = ssc_readx(params->ssc->regs, params->pdc->xnpr);
437 prtd->pdc_xncr_save = ssc_readx(params->ssc->regs, params->pdc->xncr);
438
439 return 0;
440}
441
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000442static int atmel_pcm_resume(struct snd_soc_dai *dai)
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200443{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000444 struct snd_pcm_runtime *runtime = dai->runtime;
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200445 struct atmel_runtime_data *prtd;
446 struct atmel_pcm_dma_params *params;
447
448 if (!runtime)
449 return 0;
450
451 prtd = runtime->private_data;
452 params = prtd->params;
453
454 /* restore the PDC registers and enable the PDC */
455 ssc_writex(params->ssc->regs, params->pdc->xpr, prtd->pdc_xpr_save);
456 ssc_writex(params->ssc->regs, params->pdc->xcr, prtd->pdc_xcr_save);
457 ssc_writex(params->ssc->regs, params->pdc->xnpr, prtd->pdc_xnpr_save);
458 ssc_writex(params->ssc->regs, params->pdc->xncr, prtd->pdc_xncr_save);
459
460 ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_enable);
461 return 0;
462}
463#else
464#define atmel_pcm_suspend NULL
465#define atmel_pcm_resume NULL
466#endif
467
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000468static struct snd_soc_platform_driver atmel_soc_platform = {
469 .ops = &atmel_pcm_ops,
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200470 .pcm_new = atmel_pcm_new,
471 .pcm_free = atmel_pcm_free_dma_buffers,
472 .suspend = atmel_pcm_suspend,
473 .resume = atmel_pcm_resume,
474};
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200475
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000476static int __devinit atmel_soc_platform_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000477{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000478 return snd_soc_register_platform(&pdev->dev, &atmel_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000479}
Mark Brown958e7922008-12-03 19:58:17 +0000480
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000481static int __devexit atmel_soc_platform_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000482{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000483 snd_soc_unregister_platform(&pdev->dev);
484 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000485}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000486
487static struct platform_driver atmel_pcm_driver = {
488 .driver = {
489 .name = "atmel-pcm-audio",
490 .owner = THIS_MODULE,
491 },
492
493 .probe = atmel_soc_platform_probe,
494 .remove = __devexit_p(atmel_soc_platform_remove),
495};
496
497static int __init snd_atmel_pcm_init(void)
498{
499 return platform_driver_register(&atmel_pcm_driver);
500}
501module_init(snd_atmel_pcm_init);
502
503static void __exit snd_atmel_pcm_exit(void)
504{
505 platform_driver_unregister(&atmel_pcm_driver);
506}
507module_exit(snd_atmel_pcm_exit);
Mark Brown958e7922008-12-03 19:58:17 +0000508
Sedji Gaouaou6c742502008-10-03 16:57:50 +0200509MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
510MODULE_DESCRIPTION("Atmel PCM module");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511MODULE_LICENSE("GPL v2");