1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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>
40#include <linux/atmel-ssc.h>
41
42#include <sound/core.h>
43#include <sound/pcm.h>
44#include <sound/pcm_params.h>
45#include <sound/soc.h>
46
47#include "atmel-pcm.h"
48
49
50
51
52
53
54
55
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 .period_bytes_min = 32,
62 .period_bytes_max = 8192,
63 .periods_min = 2,
64 .periods_max = 1024,
65 .buffer_bytes_max = ATMEL_SSC_DMABUF_SIZE,
66};
67
68
69
70
71
72struct atmel_runtime_data {
73 struct atmel_pcm_dma_params *params;
74 dma_addr_t dma_buffer;
75 dma_addr_t dma_buffer_end;
76 size_t period_size;
77
78 dma_addr_t period_ptr;
79
80
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
90static void atmel_pcm_dma_irq(u32 ssc_sr,
91 struct snd_pcm_substream *substream)
92{
93 struct atmel_runtime_data *prtd = substream->runtime->private_data;
94 struct atmel_pcm_dma_params *params = prtd->params;
95 static int count;
96
97 count++;
98
99 if (ssc_sr & params->mask->ssc_endbuf) {
100 pr_warn("atmel-pcm: buffer %s on %s (SSC_SR=%#x, count=%d)\n",
101 substream->stream == SNDRV_PCM_STREAM_PLAYBACK
102 ? "underrun" : "overrun",
103 params->name, ssc_sr, count);
104
105
106 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
107 params->mask->pdc_disable);
108 prtd->period_ptr += prtd->period_size;
109 if (prtd->period_ptr >= prtd->dma_buffer_end)
110 prtd->period_ptr = prtd->dma_buffer;
111
112 ssc_writex(params->ssc->regs, params->pdc->xpr,
113 prtd->period_ptr);
114 ssc_writex(params->ssc->regs, params->pdc->xcr,
115 prtd->period_size / params->pdc_xfer_size);
116 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
117 params->mask->pdc_enable);
118 }
119
120 if (ssc_sr & params->mask->ssc_endx) {
121
122 prtd->period_ptr += prtd->period_size;
123 if (prtd->period_ptr >= prtd->dma_buffer_end)
124 prtd->period_ptr = prtd->dma_buffer;
125
126 ssc_writex(params->ssc->regs, params->pdc->xnpr,
127 prtd->period_ptr);
128 ssc_writex(params->ssc->regs, params->pdc->xncr,
129 prtd->period_size / params->pdc_xfer_size);
130 }
131
132 snd_pcm_period_elapsed(substream);
133}
134
135
136
137
138
139static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
140 struct snd_pcm_hw_params *params)
141{
142 struct snd_pcm_runtime *runtime = substream->runtime;
143 struct atmel_runtime_data *prtd = runtime->private_data;
144 struct snd_soc_pcm_runtime *rtd = substream->private_data;
145
146
147
148
149 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
150 runtime->dma_bytes = params_buffer_bytes(params);
151
152 prtd->params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
153 prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
154
155 prtd->dma_buffer = runtime->dma_addr;
156 prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
157 prtd->period_size = params_period_bytes(params);
158
159 pr_debug("atmel-pcm: "
160 "hw_params: DMA for %s initialized "
161 "(dma_bytes=%zu, period_size=%zu)\n",
162 prtd->params->name,
163 runtime->dma_bytes,
164 prtd->period_size);
165 return 0;
166}
167
168static int atmel_pcm_hw_free(struct snd_pcm_substream *substream)
169{
170 struct atmel_runtime_data *prtd = substream->runtime->private_data;
171 struct atmel_pcm_dma_params *params = prtd->params;
172
173 if (params != NULL) {
174 ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
175 params->mask->pdc_disable);
176 prtd->params->dma_intr_handler = NULL;
177 }
178
179 return 0;
180}
181
182static int atmel_pcm_prepare(struct snd_pcm_substream *substream)
183{
184 struct atmel_runtime_data *prtd = substream->runtime->private_data;
185 struct atmel_pcm_dma_params *params = prtd->params;
186
187 ssc_writex(params->ssc->regs, SSC_IDR,
188 params->mask->ssc_endx | params->mask->ssc_endbuf);
189 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
190 params->mask->pdc_disable);
191 return 0;
192}
193
194static int atmel_pcm_trigger(struct snd_pcm_substream *substream,
195 int cmd)
196{
197 struct snd_pcm_runtime *rtd = substream->runtime;
198 struct atmel_runtime_data *prtd = rtd->private_data;
199 struct atmel_pcm_dma_params *params = prtd->params;
200 int ret = 0;
201
202 pr_debug("atmel-pcm:buffer_size = %ld,"
203 "dma_area = %p, dma_bytes = %zu\n",
204 rtd->buffer_size, rtd->dma_area, rtd->dma_bytes);
205
206 switch (cmd) {
207 case SNDRV_PCM_TRIGGER_START:
208 prtd->period_ptr = prtd->dma_buffer;
209
210 ssc_writex(params->ssc->regs, params->pdc->xpr,
211 prtd->period_ptr);
212 ssc_writex(params->ssc->regs, params->pdc->xcr,
213 prtd->period_size / params->pdc_xfer_size);
214
215 prtd->period_ptr += prtd->period_size;
216 ssc_writex(params->ssc->regs, params->pdc->xnpr,
217 prtd->period_ptr);
218 ssc_writex(params->ssc->regs, params->pdc->xncr,
219 prtd->period_size / params->pdc_xfer_size);
220
221 pr_debug("atmel-pcm: trigger: "
222 "period_ptr=%lx, xpr=%u, "
223 "xcr=%u, xnpr=%u, xncr=%u\n",
224 (unsigned long)prtd->period_ptr,
225 ssc_readx(params->ssc->regs, params->pdc->xpr),
226 ssc_readx(params->ssc->regs, params->pdc->xcr),
227 ssc_readx(params->ssc->regs, params->pdc->xnpr),
228 ssc_readx(params->ssc->regs, params->pdc->xncr));
229
230 ssc_writex(params->ssc->regs, SSC_IER,
231 params->mask->ssc_endx | params->mask->ssc_endbuf);
232 ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
233 params->mask->pdc_enable);
234
235 pr_debug("sr=%u imr=%u\n",
236 ssc_readx(params->ssc->regs, SSC_SR),
237 ssc_readx(params->ssc->regs, SSC_IER));
238 break;
239
240 case SNDRV_PCM_TRIGGER_STOP:
241 case SNDRV_PCM_TRIGGER_SUSPEND:
242 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
243 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
244 params->mask->pdc_disable);
245 break;
246
247 case SNDRV_PCM_TRIGGER_RESUME:
248 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
249 ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
250 params->mask->pdc_enable);
251 break;
252
253 default:
254 ret = -EINVAL;
255 }
256
257 return ret;
258}
259
260static snd_pcm_uframes_t atmel_pcm_pointer(
261 struct snd_pcm_substream *substream)
262{
263 struct snd_pcm_runtime *runtime = substream->runtime;
264 struct atmel_runtime_data *prtd = runtime->private_data;
265 struct atmel_pcm_dma_params *params = prtd->params;
266 dma_addr_t ptr;
267 snd_pcm_uframes_t x;
268
269 ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr);
270 x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
271
272 if (x == runtime->buffer_size)
273 x = 0;
274
275 return x;
276}
277
278static int atmel_pcm_open(struct snd_pcm_substream *substream)
279{
280 struct snd_pcm_runtime *runtime = substream->runtime;
281 struct atmel_runtime_data *prtd;
282 int ret = 0;
283
284 snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware);
285
286
287 ret = snd_pcm_hw_constraint_integer(runtime,
288 SNDRV_PCM_HW_PARAM_PERIODS);
289 if (ret < 0)
290 goto out;
291
292 prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL);
293 if (prtd == NULL) {
294 ret = -ENOMEM;
295 goto out;
296 }
297 runtime->private_data = prtd;
298
299 out:
300 return ret;
301}
302
303static int atmel_pcm_close(struct snd_pcm_substream *substream)
304{
305 struct atmel_runtime_data *prtd = substream->runtime->private_data;
306
307 kfree(prtd);
308 return 0;
309}
310
311static struct snd_pcm_ops atmel_pcm_ops = {
312 .open = atmel_pcm_open,
313 .close = atmel_pcm_close,
314 .ioctl = snd_pcm_lib_ioctl,
315 .hw_params = atmel_pcm_hw_params,
316 .hw_free = atmel_pcm_hw_free,
317 .prepare = atmel_pcm_prepare,
318 .trigger = atmel_pcm_trigger,
319 .pointer = atmel_pcm_pointer,
320 .mmap = atmel_pcm_mmap,
321};
322
323
324
325
326
327#ifdef CONFIG_PM
328static int atmel_pcm_suspend(struct snd_soc_dai *dai)
329{
330 struct snd_pcm_runtime *runtime = dai->runtime;
331 struct atmel_runtime_data *prtd;
332 struct atmel_pcm_dma_params *params;
333
334 if (!runtime)
335 return 0;
336
337 prtd = runtime->private_data;
338 params = prtd->params;
339
340
341
342 ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_disable);
343
344 prtd->pdc_xpr_save = ssc_readx(params->ssc->regs, params->pdc->xpr);
345 prtd->pdc_xcr_save = ssc_readx(params->ssc->regs, params->pdc->xcr);
346 prtd->pdc_xnpr_save = ssc_readx(params->ssc->regs, params->pdc->xnpr);
347 prtd->pdc_xncr_save = ssc_readx(params->ssc->regs, params->pdc->xncr);
348
349 return 0;
350}
351
352static int atmel_pcm_resume(struct snd_soc_dai *dai)
353{
354 struct snd_pcm_runtime *runtime = dai->runtime;
355 struct atmel_runtime_data *prtd;
356 struct atmel_pcm_dma_params *params;
357
358 if (!runtime)
359 return 0;
360
361 prtd = runtime->private_data;
362 params = prtd->params;
363
364
365 ssc_writex(params->ssc->regs, params->pdc->xpr, prtd->pdc_xpr_save);
366 ssc_writex(params->ssc->regs, params->pdc->xcr, prtd->pdc_xcr_save);
367 ssc_writex(params->ssc->regs, params->pdc->xnpr, prtd->pdc_xnpr_save);
368 ssc_writex(params->ssc->regs, params->pdc->xncr, prtd->pdc_xncr_save);
369
370 ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_enable);
371 return 0;
372}
373#else
374#define atmel_pcm_suspend NULL
375#define atmel_pcm_resume NULL
376#endif
377
378static struct snd_soc_platform_driver atmel_soc_platform = {
379 .ops = &atmel_pcm_ops,
380 .pcm_new = atmel_pcm_new,
381 .pcm_free = atmel_pcm_free,
382 .suspend = atmel_pcm_suspend,
383 .resume = atmel_pcm_resume,
384};
385
386int atmel_pcm_pdc_platform_register(struct device *dev)
387{
388 return snd_soc_register_platform(dev, &atmel_soc_platform);
389}
390EXPORT_SYMBOL(atmel_pcm_pdc_platform_register);
391
392void atmel_pcm_pdc_platform_unregister(struct device *dev)
393{
394 snd_soc_unregister_platform(dev);
395}
396EXPORT_SYMBOL(atmel_pcm_pdc_platform_unregister);
397
398MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
399MODULE_DESCRIPTION("Atmel PCM module");
400MODULE_LICENSE("GPL");
401