ALSA: ASoC - Fix wrong section types
[deliverable/linux.git] / sound / soc / atmel / atmel-pcm.c
CommitLineData
6c742509
SG
1/*
2 * atmel-pcm.c -- ALSA PCM interface for the Atmel atmel SoC.
9aaca968 3 *
6c742509
SG
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.
9aaca968
GW
18 *
19 * This program is free software; you can redistribute it and/or modify
6c742509
SG
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.
9aaca968 23 *
6c742509
SG
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.
28 *
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
9aaca968
GW
32 */
33
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>
6c742509 40#include <linux/atmel-ssc.h>
9aaca968
GW
41
42#include <sound/core.h>
43#include <sound/pcm.h>
44#include <sound/pcm_params.h>
45#include <sound/soc.h>
46
6c742509 47#include <mach/hardware.h>
9aaca968 48
6c742509 49#include "atmel-pcm.h"
9aaca968
GW
50
51
52/*--------------------------------------------------------------------------*\
53 * Hardware definition
54\*--------------------------------------------------------------------------*/
55/* TODO: These values were taken from the AT91 platform driver, check
56 * them against real values for AT32
57 */
6c742509
SG
58static const struct snd_pcm_hardware atmel_pcm_hardware = {
59 .info = SNDRV_PCM_INFO_MMAP |
60 SNDRV_PCM_INFO_MMAP_VALID |
61 SNDRV_PCM_INFO_INTERLEAVED |
62 SNDRV_PCM_INFO_PAUSE,
63 .formats = SNDRV_PCM_FMTBIT_S16_LE,
64 .period_bytes_min = 32,
65 .period_bytes_max = 8192,
66 .periods_min = 2,
67 .periods_max = 1024,
68 .buffer_bytes_max = 32 * 1024,
9aaca968
GW
69};
70
71
9aaca968
GW
72/*--------------------------------------------------------------------------*\
73 * Data types
74\*--------------------------------------------------------------------------*/
6c742509
SG
75struct atmel_runtime_data {
76 struct atmel_pcm_dma_params *params;
77 dma_addr_t dma_buffer; /* physical address of dma buffer */
78 dma_addr_t dma_buffer_end; /* first address beyond DMA buffer */
9aaca968
GW
79 size_t period_size;
80
6c742509
SG
81 dma_addr_t period_ptr; /* physical address of next period */
82 int periods; /* period index of period_ptr */
9aaca968 83
6c742509 84 /* PDC register save */
9aaca968
GW
85 u32 pdc_xpr_save;
86 u32 pdc_xcr_save;
87 u32 pdc_xnpr_save;
88 u32 pdc_xncr_save;
89};
90
91
9aaca968
GW
92/*--------------------------------------------------------------------------*\
93 * Helper functions
94\*--------------------------------------------------------------------------*/
6c742509
SG
95static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
96 int stream)
9aaca968
GW
97{
98 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
6c742509
SG
99 struct snd_dma_buffer *buf = &substream->dma_buffer;
100 size_t size = atmel_pcm_hardware.buffer_bytes_max;
101
102 buf->dev.type = SNDRV_DMA_TYPE_DEV;
103 buf->dev.dev = pcm->card->dev;
104 buf->private_data = NULL;
105 buf->area = dma_alloc_coherent(pcm->card->dev, size,
106 &buf->addr, GFP_KERNEL);
107 pr_debug("atmel-pcm:"
108 "preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
109 (void *) buf->area,
110 (void *) buf->addr,
111 size);
112
113 if (!buf->area)
9aaca968
GW
114 return -ENOMEM;
115
6c742509 116 buf->bytes = size;
9aaca968
GW
117 return 0;
118}
9aaca968
GW
119/*--------------------------------------------------------------------------*\
120 * ISR
121\*--------------------------------------------------------------------------*/
6c742509
SG
122static void atmel_pcm_dma_irq(u32 ssc_sr,
123 struct snd_pcm_substream *substream)
9aaca968 124{
6c742509
SG
125 struct atmel_runtime_data *prtd = substream->runtime->private_data;
126 struct atmel_pcm_dma_params *params = prtd->params;
9aaca968
GW
127 static int count;
128
129 count++;
6c742509 130
9aaca968 131 if (ssc_sr & params->mask->ssc_endbuf) {
6c742509
SG
132 pr_warning("atmel-pcm: buffer %s on %s"
133 " (SSC_SR=%#x, count=%d)\n",
134 substream->stream == SNDRV_PCM_STREAM_PLAYBACK
135 ? "underrun" : "overrun",
136 params->name, ssc_sr, count);
9aaca968
GW
137
138 /* re-start the PDC */
139 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
140 params->mask->pdc_disable);
141 prtd->period_ptr += prtd->period_size;
142 if (prtd->period_ptr >= prtd->dma_buffer_end)
143 prtd->period_ptr = prtd->dma_buffer;
144
9aaca968
GW
145 ssc_writex(params->ssc->regs, params->pdc->xpr,
146 prtd->period_ptr);
147 ssc_writex(params->ssc->regs, params->pdc->xcr,
148 prtd->period_size / params->pdc_xfer_size);
149 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
150 params->mask->pdc_enable);
151 }
152
9aaca968
GW
153 if (ssc_sr & params->mask->ssc_endx) {
154 /* Load the PDC next pointer and counter registers */
155 prtd->period_ptr += prtd->period_size;
156 if (prtd->period_ptr >= prtd->dma_buffer_end)
157 prtd->period_ptr = prtd->dma_buffer;
6c742509 158
9aaca968
GW
159 ssc_writex(params->ssc->regs, params->pdc->xnpr,
160 prtd->period_ptr);
161 ssc_writex(params->ssc->regs, params->pdc->xncr,
162 prtd->period_size / params->pdc_xfer_size);
163 }
164
9aaca968
GW
165 snd_pcm_period_elapsed(substream);
166}
167
168
9aaca968
GW
169/*--------------------------------------------------------------------------*\
170 * PCM operations
171\*--------------------------------------------------------------------------*/
6c742509
SG
172static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
173 struct snd_pcm_hw_params *params)
9aaca968
GW
174{
175 struct snd_pcm_runtime *runtime = substream->runtime;
6c742509 176 struct atmel_runtime_data *prtd = runtime->private_data;
9aaca968
GW
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
178
179 /* this may get called several times by oss emulation
6c742509
SG
180 * with different params */
181
9aaca968
GW
182 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
183 runtime->dma_bytes = params_buffer_bytes(params);
184
185 prtd->params = rtd->dai->cpu_dai->dma_data;
6c742509 186 prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
9aaca968
GW
187
188 prtd->dma_buffer = runtime->dma_addr;
189 prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
190 prtd->period_size = params_period_bytes(params);
191
6c742509
SG
192 pr_debug("atmel-pcm: "
193 "hw_params: DMA for %s initialized "
194 "(dma_bytes=%u, period_size=%u)\n",
195 prtd->params->name,
196 runtime->dma_bytes,
197 prtd->period_size);
9aaca968
GW
198 return 0;
199}
200
6c742509 201static int atmel_pcm_hw_free(struct snd_pcm_substream *substream)
9aaca968 202{
6c742509
SG
203 struct atmel_runtime_data *prtd = substream->runtime->private_data;
204 struct atmel_pcm_dma_params *params = prtd->params;
9aaca968
GW
205
206 if (params != NULL) {
207 ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
208 params->mask->pdc_disable);
209 prtd->params->dma_intr_handler = NULL;
210 }
211
212 return 0;
213}
214
6c742509 215static int atmel_pcm_prepare(struct snd_pcm_substream *substream)
9aaca968 216{
6c742509
SG
217 struct atmel_runtime_data *prtd = substream->runtime->private_data;
218 struct atmel_pcm_dma_params *params = prtd->params;
9aaca968
GW
219
220 ssc_writex(params->ssc->regs, SSC_IDR,
221 params->mask->ssc_endx | params->mask->ssc_endbuf);
222 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
223 params->mask->pdc_disable);
9aaca968
GW
224 return 0;
225}
226
6c742509
SG
227static int atmel_pcm_trigger(struct snd_pcm_substream *substream,
228 int cmd)
9aaca968
GW
229{
230 struct snd_pcm_runtime *rtd = substream->runtime;
6c742509
SG
231 struct atmel_runtime_data *prtd = rtd->private_data;
232 struct atmel_pcm_dma_params *params = prtd->params;
9aaca968
GW
233 int ret = 0;
234
6c742509
SG
235 pr_debug("atmel-pcm:buffer_size = %ld,"
236 "dma_area = %p, dma_bytes = %u\n",
237 rtd->buffer_size, rtd->dma_area, rtd->dma_bytes);
9aaca968
GW
238
239 switch (cmd) {
240 case SNDRV_PCM_TRIGGER_START:
241 prtd->period_ptr = prtd->dma_buffer;
242
243 ssc_writex(params->ssc->regs, params->pdc->xpr,
244 prtd->period_ptr);
245 ssc_writex(params->ssc->regs, params->pdc->xcr,
246 prtd->period_size / params->pdc_xfer_size);
247
248 prtd->period_ptr += prtd->period_size;
249 ssc_writex(params->ssc->regs, params->pdc->xnpr,
250 prtd->period_ptr);
251 ssc_writex(params->ssc->regs, params->pdc->xncr,
252 prtd->period_size / params->pdc_xfer_size);
253
6c742509
SG
254 pr_debug("atmel-pcm: trigger: "
255 "period_ptr=%lx, xpr=%u, "
256 "xcr=%u, xnpr=%u, xncr=%u\n",
257 (unsigned long)prtd->period_ptr,
258 ssc_readx(params->ssc->regs, params->pdc->xpr),
259 ssc_readx(params->ssc->regs, params->pdc->xcr),
260 ssc_readx(params->ssc->regs, params->pdc->xnpr),
261 ssc_readx(params->ssc->regs, params->pdc->xncr));
9aaca968
GW
262
263 ssc_writex(params->ssc->regs, SSC_IER,
264 params->mask->ssc_endx | params->mask->ssc_endbuf);
265 ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
266 params->mask->pdc_enable);
267
6c742509
SG
268 pr_debug("sr=%u imr=%u\n",
269 ssc_readx(params->ssc->regs, SSC_SR),
270 ssc_readx(params->ssc->regs, SSC_IER));
9aaca968
GW
271 break; /* SNDRV_PCM_TRIGGER_START */
272
9aaca968
GW
273 case SNDRV_PCM_TRIGGER_STOP:
274 case SNDRV_PCM_TRIGGER_SUSPEND:
275 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
276 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
277 params->mask->pdc_disable);
278 break;
279
9aaca968
GW
280 case SNDRV_PCM_TRIGGER_RESUME:
281 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
282 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
283 params->mask->pdc_enable);
284 break;
285
286 default:
287 ret = -EINVAL;
288 }
289
290 return ret;
291}
292
6c742509
SG
293static snd_pcm_uframes_t atmel_pcm_pointer(
294 struct snd_pcm_substream *substream)
9aaca968
GW
295{
296 struct snd_pcm_runtime *runtime = substream->runtime;
6c742509
SG
297 struct atmel_runtime_data *prtd = runtime->private_data;
298 struct atmel_pcm_dma_params *params = prtd->params;
9aaca968
GW
299 dma_addr_t ptr;
300 snd_pcm_uframes_t x;
301
302 ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr);
303 x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
304
305 if (x == runtime->buffer_size)
306 x = 0;
307
308 return x;
309}
310
6c742509 311static int atmel_pcm_open(struct snd_pcm_substream *substream)
9aaca968
GW
312{
313 struct snd_pcm_runtime *runtime = substream->runtime;
6c742509 314 struct atmel_runtime_data *prtd;
9aaca968
GW
315 int ret = 0;
316
6c742509 317 snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware);
9aaca968
GW
318
319 /* ensure that buffer size is a multiple of period size */
320 ret = snd_pcm_hw_constraint_integer(runtime,
6c742509 321 SNDRV_PCM_HW_PARAM_PERIODS);
9aaca968
GW
322 if (ret < 0)
323 goto out;
324
6c742509 325 prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL);
9aaca968
GW
326 if (prtd == NULL) {
327 ret = -ENOMEM;
328 goto out;
329 }
330 runtime->private_data = prtd;
331
6c742509 332 out:
9aaca968
GW
333 return ret;
334}
335
6c742509 336static int atmel_pcm_close(struct snd_pcm_substream *substream)
9aaca968 337{
6c742509 338 struct atmel_runtime_data *prtd = substream->runtime->private_data;
9aaca968
GW
339
340 kfree(prtd);
341 return 0;
342}
343
6c742509
SG
344static int atmel_pcm_mmap(struct snd_pcm_substream *substream,
345 struct vm_area_struct *vma)
9aaca968
GW
346{
347 return remap_pfn_range(vma, vma->vm_start,
6c742509
SG
348 substream->dma_buffer.addr >> PAGE_SHIFT,
349 vma->vm_end - vma->vm_start, vma->vm_page_prot);
9aaca968
GW
350}
351
6c742509
SG
352struct snd_pcm_ops atmel_pcm_ops = {
353 .open = atmel_pcm_open,
354 .close = atmel_pcm_close,
355 .ioctl = snd_pcm_lib_ioctl,
356 .hw_params = atmel_pcm_hw_params,
357 .hw_free = atmel_pcm_hw_free,
358 .prepare = atmel_pcm_prepare,
359 .trigger = atmel_pcm_trigger,
360 .pointer = atmel_pcm_pointer,
361 .mmap = atmel_pcm_mmap,
9aaca968
GW
362};
363
364
9aaca968
GW
365/*--------------------------------------------------------------------------*\
366 * ASoC platform driver
367\*--------------------------------------------------------------------------*/
6c742509 368static u64 atmel_pcm_dmamask = 0xffffffff;
9aaca968 369
6c742509
SG
370static int atmel_pcm_new(struct snd_card *card,
371 struct snd_soc_dai *dai, struct snd_pcm *pcm)
9aaca968
GW
372{
373 int ret = 0;
374
375 if (!card->dev->dma_mask)
6c742509 376 card->dev->dma_mask = &atmel_pcm_dmamask;
9aaca968
GW
377 if (!card->dev->coherent_dma_mask)
378 card->dev->coherent_dma_mask = 0xffffffff;
379
380 if (dai->playback.channels_min) {
6c742509
SG
381 ret = atmel_pcm_preallocate_dma_buffer(pcm,
382 SNDRV_PCM_STREAM_PLAYBACK);
9aaca968
GW
383 if (ret)
384 goto out;
385 }
386
387 if (dai->capture.channels_min) {
6c742509
SG
388 pr_debug("at32-pcm:"
389 "Allocating PCM capture DMA buffer\n");
390 ret = atmel_pcm_preallocate_dma_buffer(pcm,
391 SNDRV_PCM_STREAM_CAPTURE);
9aaca968
GW
392 if (ret)
393 goto out;
394 }
6c742509 395 out:
9aaca968
GW
396 return ret;
397}
398
6c742509 399static void atmel_pcm_free_dma_buffers(struct snd_pcm *pcm)
9aaca968
GW
400{
401 struct snd_pcm_substream *substream;
402 struct snd_dma_buffer *buf;
403 int stream;
404
405 for (stream = 0; stream < 2; stream++) {
406 substream = pcm->streams[stream].substream;
6c742509 407 if (!substream)
9aaca968
GW
408 continue;
409
410 buf = &substream->dma_buffer;
411 if (!buf->area)
412 continue;
413 dma_free_coherent(pcm->card->dev, buf->bytes,
414 buf->area, buf->addr);
415 buf->area = NULL;
416 }
417}
418
9aaca968 419#ifdef CONFIG_PM
07c84d04 420static int atmel_pcm_suspend(struct snd_soc_dai *dai)
9aaca968
GW
421{
422 struct snd_pcm_runtime *runtime = dai->runtime;
6c742509
SG
423 struct atmel_runtime_data *prtd;
424 struct atmel_pcm_dma_params *params;
9aaca968 425
6c742509 426 if (!runtime)
9aaca968 427 return 0;
6c742509 428
9aaca968
GW
429 prtd = runtime->private_data;
430 params = prtd->params;
431
6c742509
SG
432 /* disable the PDC and save the PDC registers */
433
434 ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_disable);
9aaca968
GW
435
436 prtd->pdc_xpr_save = ssc_readx(params->ssc->regs, params->pdc->xpr);
437 prtd->pdc_xcr_save = ssc_readx(params->ssc->regs, params->pdc->xcr);
438 prtd->pdc_xnpr_save = ssc_readx(params->ssc->regs, params->pdc->xnpr);
439 prtd->pdc_xncr_save = ssc_readx(params->ssc->regs, params->pdc->xncr);
440
441 return 0;
442}
443
07c84d04 444static int atmel_pcm_resume(struct snd_soc_dai *dai)
9aaca968
GW
445{
446 struct snd_pcm_runtime *runtime = dai->runtime;
6c742509
SG
447 struct atmel_runtime_data *prtd;
448 struct atmel_pcm_dma_params *params;
9aaca968 449
6c742509 450 if (!runtime)
9aaca968 451 return 0;
6c742509 452
9aaca968
GW
453 prtd = runtime->private_data;
454 params = prtd->params;
455
6c742509 456 /* restore the PDC registers and enable the PDC */
9aaca968
GW
457 ssc_writex(params->ssc->regs, params->pdc->xpr, prtd->pdc_xpr_save);
458 ssc_writex(params->ssc->regs, params->pdc->xcr, prtd->pdc_xcr_save);
459 ssc_writex(params->ssc->regs, params->pdc->xnpr, prtd->pdc_xnpr_save);
460 ssc_writex(params->ssc->regs, params->pdc->xncr, prtd->pdc_xncr_save);
461
6c742509 462 ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_enable);
9aaca968
GW
463 return 0;
464}
6c742509
SG
465#else
466#define atmel_pcm_suspend NULL
467#define atmel_pcm_resume NULL
468#endif
469
470struct snd_soc_platform atmel_soc_platform = {
471 .name = "atmel-audio",
472 .pcm_ops = &atmel_pcm_ops,
473 .pcm_new = atmel_pcm_new,
474 .pcm_free = atmel_pcm_free_dma_buffers,
475 .suspend = atmel_pcm_suspend,
476 .resume = atmel_pcm_resume,
9aaca968 477};
6c742509 478EXPORT_SYMBOL_GPL(atmel_soc_platform);
9aaca968 479
c9b3a40f 480static int __init atmel_pcm_modinit(void)
958e792c
MB
481{
482 return snd_soc_register_platform(&atmel_soc_platform);
483}
484module_init(atmel_pcm_modinit);
485
471716f7 486static void __exit atmel_pcm_modexit(void)
958e792c
MB
487{
488 snd_soc_unregister_platform(&atmel_soc_platform);
489}
490module_exit(atmel_pcm_modexit);
491
6c742509
SG
492MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
493MODULE_DESCRIPTION("Atmel PCM module");
9aaca968 494MODULE_LICENSE("GPL");
This page took 0.123791 seconds and 5 git commands to generate.