Blame view

sound/soc/atmel/atmel-pcm.c 14 KB
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
1
2
  /*
   * atmel-pcm.c  --  ALSA PCM interface for the Atmel atmel SoC.
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
3
   *
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   *  Copyright (C) 2005 SAN People
   *  Copyright (C) 2008 Atmel
   *
   * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
   *
   * Based on at91-pcm. by:
   * Frank Mandarino <fmandarino@endrelia.com>
   * Copyright 2006 Endrelia Technologies Inc.
   *
   * Based on pxa2xx-pcm.c by:
   *
   * Author:	Nicolas Pitre
   * Created:	Nov 30, 2004
   * Copyright:	(C) 2004 MontaVista Software, Inc.
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
18
19
   *
   * This program is free software; you can redistribute it and/or modify
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
20
21
22
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
23
   *
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
24
25
26
27
28
29
30
31
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
32
33
34
35
36
37
38
39
   */
  
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/platform_device.h>
  #include <linux/slab.h>
  #include <linux/dma-mapping.h>
  #include <linux/atmel_pdc.h>
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
40
  #include <linux/atmel-ssc.h>
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
41
42
43
44
45
  
  #include <sound/core.h>
  #include <sound/pcm.h>
  #include <sound/pcm_params.h>
  #include <sound/soc.h>
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
46
  #include "atmel-pcm.h"
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
47
48
49
50
51
52
53
54
  
  
  /*--------------------------------------------------------------------------*\
   * Hardware definition
  \*--------------------------------------------------------------------------*/
  /* TODO: These values were taken from the AT91 platform driver, check
   *	 them against real values for AT32
   */
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
55
56
57
58
59
60
61
62
63
64
65
  static const struct snd_pcm_hardware atmel_pcm_hardware = {
  	.info			= SNDRV_PCM_INFO_MMAP |
  				  SNDRV_PCM_INFO_MMAP_VALID |
  				  SNDRV_PCM_INFO_INTERLEAVED |
  				  SNDRV_PCM_INFO_PAUSE,
  	.formats		= SNDRV_PCM_FMTBIT_S16_LE,
  	.period_bytes_min	= 32,
  	.period_bytes_max	= 8192,
  	.periods_min		= 2,
  	.periods_max		= 1024,
  	.buffer_bytes_max	= 32 * 1024,
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
66
  };
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
67
68
69
  /*--------------------------------------------------------------------------*\
   * Data types
  \*--------------------------------------------------------------------------*/
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
70
71
72
73
  struct atmel_runtime_data {
  	struct atmel_pcm_dma_params *params;
  	dma_addr_t dma_buffer;		/* physical address of dma buffer */
  	dma_addr_t dma_buffer_end;	/* first address beyond DMA buffer */
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
74
  	size_t period_size;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
75
  	dma_addr_t period_ptr;		/* physical address of next period */
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
76

6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
77
  	/* PDC register save */
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
78
79
80
81
82
  	u32 pdc_xpr_save;
  	u32 pdc_xcr_save;
  	u32 pdc_xnpr_save;
  	u32 pdc_xncr_save;
  };
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
83
84
85
  /*--------------------------------------------------------------------------*\
   * Helper functions
  \*--------------------------------------------------------------------------*/
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
86
87
  static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  	int stream)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
88
89
  {
  	struct snd_pcm_substream *substream = pcm->streams[stream].substream;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  	struct snd_dma_buffer *buf = &substream->dma_buffer;
  	size_t size = atmel_pcm_hardware.buffer_bytes_max;
  
  	buf->dev.type = SNDRV_DMA_TYPE_DEV;
  	buf->dev.dev = pcm->card->dev;
  	buf->private_data = NULL;
  	buf->area = dma_alloc_coherent(pcm->card->dev, size,
  					  &buf->addr, GFP_KERNEL);
  	pr_debug("atmel-pcm:"
  		"preallocate_dma_buffer: area=%p, addr=%p, size=%d
  ",
  		(void *) buf->area,
  		(void *) buf->addr,
  		size);
  
  	if (!buf->area)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
106
  		return -ENOMEM;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
107
  	buf->bytes = size;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
108
109
  	return 0;
  }
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
110
111
112
  /*--------------------------------------------------------------------------*\
   * ISR
  \*--------------------------------------------------------------------------*/
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
113
114
  static void atmel_pcm_dma_irq(u32 ssc_sr,
  	struct snd_pcm_substream *substream)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
115
  {
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
116
117
  	struct atmel_runtime_data *prtd = substream->runtime->private_data;
  	struct atmel_pcm_dma_params *params = prtd->params;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
118
119
120
  	static int count;
  
  	count++;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
121

9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
122
  	if (ssc_sr & params->mask->ssc_endbuf) {
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
123
124
125
126
127
128
  		pr_warning("atmel-pcm: buffer %s on %s"
  				" (SSC_SR=%#x, count=%d)
  ",
  				substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  				? "underrun" : "overrun",
  				params->name, ssc_sr, count);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
129
130
131
132
133
134
135
  
  		/* re-start the PDC */
  		ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  			   params->mask->pdc_disable);
  		prtd->period_ptr += prtd->period_size;
  		if (prtd->period_ptr >= prtd->dma_buffer_end)
  			prtd->period_ptr = prtd->dma_buffer;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
136
137
138
139
140
141
142
  		ssc_writex(params->ssc->regs, params->pdc->xpr,
  			   prtd->period_ptr);
  		ssc_writex(params->ssc->regs, params->pdc->xcr,
  			   prtd->period_size / params->pdc_xfer_size);
  		ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  			   params->mask->pdc_enable);
  	}
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
143
144
145
146
147
  	if (ssc_sr & params->mask->ssc_endx) {
  		/* Load the PDC next pointer and counter registers */
  		prtd->period_ptr += prtd->period_size;
  		if (prtd->period_ptr >= prtd->dma_buffer_end)
  			prtd->period_ptr = prtd->dma_buffer;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
148

9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
149
150
151
152
153
  		ssc_writex(params->ssc->regs, params->pdc->xnpr,
  			   prtd->period_ptr);
  		ssc_writex(params->ssc->regs, params->pdc->xncr,
  			   prtd->period_size / params->pdc_xfer_size);
  	}
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
154
155
  	snd_pcm_period_elapsed(substream);
  }
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
156
157
158
  /*--------------------------------------------------------------------------*\
   * PCM operations
  \*--------------------------------------------------------------------------*/
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
159
160
  static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
  	struct snd_pcm_hw_params *params)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
161
162
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
163
  	struct atmel_runtime_data *prtd = runtime->private_data;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
164
165
166
  	struct snd_soc_pcm_runtime *rtd = substream->private_data;
  
  	/* this may get called several times by oss emulation
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
167
  	 * with different params */
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
168
169
  	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  	runtime->dma_bytes = params_buffer_bytes(params);
f0fba2ad1   Liam Girdwood   ASoC: multi-compo...
170
  	prtd->params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
171
  	prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
172
173
174
175
  
  	prtd->dma_buffer = runtime->dma_addr;
  	prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
  	prtd->period_size = params_period_bytes(params);
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
176
177
178
179
180
181
182
  	pr_debug("atmel-pcm: "
  		"hw_params: DMA for %s initialized "
  		"(dma_bytes=%u, period_size=%u)
  ",
  		prtd->params->name,
  		runtime->dma_bytes,
  		prtd->period_size);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
183
184
  	return 0;
  }
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
185
  static int atmel_pcm_hw_free(struct snd_pcm_substream *substream)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
186
  {
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
187
188
  	struct atmel_runtime_data *prtd = substream->runtime->private_data;
  	struct atmel_pcm_dma_params *params = prtd->params;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
189
190
191
192
193
194
195
196
197
  
  	if (params != NULL) {
  		ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
  			   params->mask->pdc_disable);
  		prtd->params->dma_intr_handler = NULL;
  	}
  
  	return 0;
  }
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
198
  static int atmel_pcm_prepare(struct snd_pcm_substream *substream)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
199
  {
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
200
201
  	struct atmel_runtime_data *prtd = substream->runtime->private_data;
  	struct atmel_pcm_dma_params *params = prtd->params;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
202
203
204
205
206
  
  	ssc_writex(params->ssc->regs, SSC_IDR,
  		   params->mask->ssc_endx | params->mask->ssc_endbuf);
  	ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  		   params->mask->pdc_disable);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
207
208
  	return 0;
  }
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
209
210
  static int atmel_pcm_trigger(struct snd_pcm_substream *substream,
  	int cmd)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
211
212
  {
  	struct snd_pcm_runtime *rtd = substream->runtime;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
213
214
  	struct atmel_runtime_data *prtd = rtd->private_data;
  	struct atmel_pcm_dma_params *params = prtd->params;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
215
  	int ret = 0;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
216
217
218
219
  	pr_debug("atmel-pcm:buffer_size = %ld,"
  		"dma_area = %p, dma_bytes = %u
  ",
  		rtd->buffer_size, rtd->dma_area, rtd->dma_bytes);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
  		prtd->period_ptr = prtd->dma_buffer;
  
  		ssc_writex(params->ssc->regs, params->pdc->xpr,
  			   prtd->period_ptr);
  		ssc_writex(params->ssc->regs, params->pdc->xcr,
  			   prtd->period_size / params->pdc_xfer_size);
  
  		prtd->period_ptr += prtd->period_size;
  		ssc_writex(params->ssc->regs, params->pdc->xnpr,
  			   prtd->period_ptr);
  		ssc_writex(params->ssc->regs, params->pdc->xncr,
  			   prtd->period_size / params->pdc_xfer_size);
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
235
236
237
238
239
240
241
242
243
  		pr_debug("atmel-pcm: trigger: "
  			"period_ptr=%lx, xpr=%u, "
  			"xcr=%u, xnpr=%u, xncr=%u
  ",
  			(unsigned long)prtd->period_ptr,
  			ssc_readx(params->ssc->regs, params->pdc->xpr),
  			ssc_readx(params->ssc->regs, params->pdc->xcr),
  			ssc_readx(params->ssc->regs, params->pdc->xnpr),
  			ssc_readx(params->ssc->regs, params->pdc->xncr));
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
244
245
246
247
248
  
  		ssc_writex(params->ssc->regs, SSC_IER,
  			   params->mask->ssc_endx | params->mask->ssc_endbuf);
  		ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
  			   params->mask->pdc_enable);
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
249
250
251
252
  		pr_debug("sr=%u imr=%u
  ",
  			ssc_readx(params->ssc->regs, SSC_SR),
  			ssc_readx(params->ssc->regs, SSC_IER));
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
253
  		break;		/* SNDRV_PCM_TRIGGER_START */
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
254
255
256
257
258
259
  	case SNDRV_PCM_TRIGGER_STOP:
  	case SNDRV_PCM_TRIGGER_SUSPEND:
  	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  		ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  			   params->mask->pdc_disable);
  		break;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
260
261
262
263
264
265
266
267
268
269
270
271
  	case SNDRV_PCM_TRIGGER_RESUME:
  	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  		ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  			   params->mask->pdc_enable);
  		break;
  
  	default:
  		ret = -EINVAL;
  	}
  
  	return ret;
  }
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
272
273
  static snd_pcm_uframes_t atmel_pcm_pointer(
  	struct snd_pcm_substream *substream)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
274
275
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
276
277
  	struct atmel_runtime_data *prtd = runtime->private_data;
  	struct atmel_pcm_dma_params *params = prtd->params;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
278
279
280
281
282
283
284
285
286
287
288
  	dma_addr_t ptr;
  	snd_pcm_uframes_t x;
  
  	ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr);
  	x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
  
  	if (x == runtime->buffer_size)
  		x = 0;
  
  	return x;
  }
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
289
  static int atmel_pcm_open(struct snd_pcm_substream *substream)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
290
291
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
292
  	struct atmel_runtime_data *prtd;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
293
  	int ret = 0;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
294
  	snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
295
296
297
  
  	/* ensure that buffer size is a multiple of period size */
  	ret = snd_pcm_hw_constraint_integer(runtime,
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
298
  						SNDRV_PCM_HW_PARAM_PERIODS);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
299
300
  	if (ret < 0)
  		goto out;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
301
  	prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
302
303
304
305
306
  	if (prtd == NULL) {
  		ret = -ENOMEM;
  		goto out;
  	}
  	runtime->private_data = prtd;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
307
   out:
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
308
309
  	return ret;
  }
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
310
  static int atmel_pcm_close(struct snd_pcm_substream *substream)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
311
  {
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
312
  	struct atmel_runtime_data *prtd = substream->runtime->private_data;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
313
314
315
316
  
  	kfree(prtd);
  	return 0;
  }
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
317
318
  static int atmel_pcm_mmap(struct snd_pcm_substream *substream,
  	struct vm_area_struct *vma)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
319
320
  {
  	return remap_pfn_range(vma, vma->vm_start,
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
321
322
  		       substream->dma_buffer.addr >> PAGE_SHIFT,
  		       vma->vm_end - vma->vm_start, vma->vm_page_prot);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
323
  }
b2a19d023   Mark Brown   ASoC: Staticise P...
324
  static struct snd_pcm_ops atmel_pcm_ops = {
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
325
326
327
328
329
330
331
332
333
  	.open		= atmel_pcm_open,
  	.close		= atmel_pcm_close,
  	.ioctl		= snd_pcm_lib_ioctl,
  	.hw_params	= atmel_pcm_hw_params,
  	.hw_free	= atmel_pcm_hw_free,
  	.prepare	= atmel_pcm_prepare,
  	.trigger	= atmel_pcm_trigger,
  	.pointer	= atmel_pcm_pointer,
  	.mmap		= atmel_pcm_mmap,
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
334
  };
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
335
336
337
  /*--------------------------------------------------------------------------*\
   * ASoC platform driver
  \*--------------------------------------------------------------------------*/
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
338
  static u64 atmel_pcm_dmamask = 0xffffffff;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
339

552d1ef6b   Liam Girdwood   ASoC: core - Opti...
340
  static int atmel_pcm_new(struct snd_soc_pcm_runtime *rtd)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
341
  {
552d1ef6b   Liam Girdwood   ASoC: core - Opti...
342
  	struct snd_card *card = rtd->card->snd_card;
552d1ef6b   Liam Girdwood   ASoC: core - Opti...
343
  	struct snd_pcm *pcm = rtd->pcm;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
344
345
346
  	int ret = 0;
  
  	if (!card->dev->dma_mask)
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
347
  		card->dev->dma_mask = &atmel_pcm_dmamask;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
348
349
  	if (!card->dev->coherent_dma_mask)
  		card->dev->coherent_dma_mask = 0xffffffff;
25e9e7565   Joachim Eastwood   ASoC: check for s...
350
  	if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
351
352
  		ret = atmel_pcm_preallocate_dma_buffer(pcm,
  			SNDRV_PCM_STREAM_PLAYBACK);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
353
354
355
  		if (ret)
  			goto out;
  	}
25e9e7565   Joachim Eastwood   ASoC: check for s...
356
  	if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
7309d2e28   Nicolas Ferre   ASoC: trivial: ty...
357
  		pr_debug("atmel-pcm:"
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
358
359
360
361
  				"Allocating PCM capture DMA buffer
  ");
  		ret = atmel_pcm_preallocate_dma_buffer(pcm,
  			SNDRV_PCM_STREAM_CAPTURE);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
362
363
364
  		if (ret)
  			goto out;
  	}
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
365
   out:
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
366
367
  	return ret;
  }
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
368
  static void atmel_pcm_free_dma_buffers(struct snd_pcm *pcm)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
369
370
371
372
373
374
375
  {
  	struct snd_pcm_substream *substream;
  	struct snd_dma_buffer *buf;
  	int stream;
  
  	for (stream = 0; stream < 2; stream++) {
  		substream = pcm->streams[stream].substream;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
376
  		if (!substream)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
377
378
379
380
381
382
383
384
385
386
  			continue;
  
  		buf = &substream->dma_buffer;
  		if (!buf->area)
  			continue;
  		dma_free_coherent(pcm->card->dev, buf->bytes,
  				  buf->area, buf->addr);
  		buf->area = NULL;
  	}
  }
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
387
  #ifdef CONFIG_PM
f0fba2ad1   Liam Girdwood   ASoC: multi-compo...
388
  static int atmel_pcm_suspend(struct snd_soc_dai *dai)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
389
  {
f0fba2ad1   Liam Girdwood   ASoC: multi-compo...
390
  	struct snd_pcm_runtime *runtime = dai->runtime;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
391
392
  	struct atmel_runtime_data *prtd;
  	struct atmel_pcm_dma_params *params;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
393

6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
394
  	if (!runtime)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
395
  		return 0;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
396

9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
397
398
  	prtd = runtime->private_data;
  	params = prtd->params;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
399
400
401
  	/* disable the PDC and save the PDC registers */
  
  	ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_disable);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
402
403
404
405
406
407
408
409
  
  	prtd->pdc_xpr_save = ssc_readx(params->ssc->regs, params->pdc->xpr);
  	prtd->pdc_xcr_save = ssc_readx(params->ssc->regs, params->pdc->xcr);
  	prtd->pdc_xnpr_save = ssc_readx(params->ssc->regs, params->pdc->xnpr);
  	prtd->pdc_xncr_save = ssc_readx(params->ssc->regs, params->pdc->xncr);
  
  	return 0;
  }
f0fba2ad1   Liam Girdwood   ASoC: multi-compo...
410
  static int atmel_pcm_resume(struct snd_soc_dai *dai)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
411
  {
f0fba2ad1   Liam Girdwood   ASoC: multi-compo...
412
  	struct snd_pcm_runtime *runtime = dai->runtime;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
413
414
  	struct atmel_runtime_data *prtd;
  	struct atmel_pcm_dma_params *params;
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
415

6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
416
  	if (!runtime)
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
417
  		return 0;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
418

9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
419
420
  	prtd = runtime->private_data;
  	params = prtd->params;
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
421
  	/* restore the PDC registers and enable the PDC */
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
422
423
424
425
  	ssc_writex(params->ssc->regs, params->pdc->xpr, prtd->pdc_xpr_save);
  	ssc_writex(params->ssc->regs, params->pdc->xcr, prtd->pdc_xcr_save);
  	ssc_writex(params->ssc->regs, params->pdc->xnpr, prtd->pdc_xnpr_save);
  	ssc_writex(params->ssc->regs, params->pdc->xncr, prtd->pdc_xncr_save);
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
426
  	ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_enable);
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
427
428
  	return 0;
  }
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
429
430
431
432
  #else
  #define atmel_pcm_suspend	NULL
  #define atmel_pcm_resume	NULL
  #endif
f0fba2ad1   Liam Girdwood   ASoC: multi-compo...
433
434
  static struct snd_soc_platform_driver atmel_soc_platform = {
  	.ops		= &atmel_pcm_ops,
6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
435
436
437
438
  	.pcm_new	= atmel_pcm_new,
  	.pcm_free	= atmel_pcm_free_dma_buffers,
  	.suspend	= atmel_pcm_suspend,
  	.resume		= atmel_pcm_resume,
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
439
  };
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
440

f0fba2ad1   Liam Girdwood   ASoC: multi-compo...
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
  static int __devinit atmel_soc_platform_probe(struct platform_device *pdev)
  {
  	return snd_soc_register_platform(&pdev->dev, &atmel_soc_platform);
  }
  
  static int __devexit atmel_soc_platform_remove(struct platform_device *pdev)
  {
  	snd_soc_unregister_platform(&pdev->dev);
  	return 0;
  }
  
  static struct platform_driver atmel_pcm_driver = {
  	.driver = {
  			.name = "atmel-pcm-audio",
  			.owner = THIS_MODULE,
  	},
  
  	.probe = atmel_soc_platform_probe,
  	.remove = __devexit_p(atmel_soc_platform_remove),
  };
b31c9056e   Axel Lin   ASoC: Convert atm...
461
  module_platform_driver(atmel_pcm_driver);
958e792c7   Mark Brown   ASoC: Register pl...
462

6c7425095   Sedji Gaouaou   ASoC: Merge AT91 ...
463
464
  MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
  MODULE_DESCRIPTION("Atmel PCM module");
9aaca9683   Geoffrey Wossum   [ALSA] Revised AT...
465
  MODULE_LICENSE("GPL");