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