4 * (c) 2010 Arnaud Patard <apatard@mandriva.com>
5 * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/mbus.h>
21 #include <sound/soc.h>
24 #define KIRKWOOD_RATES \
25 (SNDRV_PCM_RATE_44100 | \
26 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
27 #define KIRKWOOD_FORMATS \
28 (SNDRV_PCM_FMTBIT_S16_LE | \
29 SNDRV_PCM_FMTBIT_S24_LE | \
30 SNDRV_PCM_FMTBIT_S32_LE)
32 struct kirkwood_dma_priv
{
33 struct snd_pcm_substream
*play_stream
;
34 struct snd_pcm_substream
*rec_stream
;
35 struct kirkwood_dma_data
*data
;
38 static struct snd_pcm_hardware kirkwood_dma_snd_hw
= {
39 .info
= (SNDRV_PCM_INFO_INTERLEAVED
|
41 SNDRV_PCM_INFO_MMAP_VALID
|
42 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
43 SNDRV_PCM_INFO_PAUSE
),
44 .formats
= KIRKWOOD_FORMATS
,
45 .rates
= KIRKWOOD_RATES
,
50 .buffer_bytes_max
= KIRKWOOD_SND_MAX_PERIOD_BYTES
* KIRKWOOD_SND_MAX_PERIODS
,
51 .period_bytes_min
= KIRKWOOD_SND_MIN_PERIOD_BYTES
,
52 .period_bytes_max
= KIRKWOOD_SND_MAX_PERIOD_BYTES
,
53 .periods_min
= KIRKWOOD_SND_MIN_PERIODS
,
54 .periods_max
= KIRKWOOD_SND_MAX_PERIODS
,
58 static u64 kirkwood_dma_dmamask
= 0xFFFFFFFFUL
;
60 static irqreturn_t
kirkwood_dma_irq(int irq
, void *dev_id
)
62 struct kirkwood_dma_priv
*prdata
= dev_id
;
63 struct kirkwood_dma_data
*priv
= prdata
->data
;
64 unsigned long mask
, status
, cause
;
66 mask
= readl(priv
->io
+ KIRKWOOD_INT_MASK
);
67 status
= readl(priv
->io
+ KIRKWOOD_INT_CAUSE
) & mask
;
69 cause
= readl(priv
->io
+ KIRKWOOD_ERR_CAUSE
);
70 if (unlikely(cause
)) {
71 printk(KERN_WARNING
"%s: got err interrupt 0x%lx\n",
73 writel(cause
, priv
->io
+ KIRKWOOD_ERR_CAUSE
);
77 /* we've enabled only bytes interrupts ... */
78 if (status
& ~(KIRKWOOD_INT_CAUSE_PLAY_BYTES
| \
79 KIRKWOOD_INT_CAUSE_REC_BYTES
)) {
80 printk(KERN_WARNING
"%s: unexpected interrupt %lx\n",
86 writel(status
, priv
->io
+ KIRKWOOD_INT_CAUSE
);
88 if (status
& KIRKWOOD_INT_CAUSE_PLAY_BYTES
)
89 snd_pcm_period_elapsed(prdata
->play_stream
);
91 if (status
& KIRKWOOD_INT_CAUSE_REC_BYTES
)
92 snd_pcm_period_elapsed(prdata
->rec_stream
);
98 kirkwood_dma_conf_mbus_windows(void __iomem
*base
, int win
,
100 const struct mbus_dram_target_info
*dram
)
104 /* First disable and clear windows */
105 writel(0, base
+ KIRKWOOD_AUDIO_WIN_CTRL_REG(win
));
106 writel(0, base
+ KIRKWOOD_AUDIO_WIN_BASE_REG(win
));
108 /* try to find matching cs for current dma address */
109 for (i
= 0; i
< dram
->num_cs
; i
++) {
110 const struct mbus_dram_window
*cs
= dram
->cs
+ i
;
111 if ((cs
->base
& 0xffff0000) < (dma
& 0xffff0000)) {
112 writel(cs
->base
& 0xffff0000,
113 base
+ KIRKWOOD_AUDIO_WIN_BASE_REG(win
));
114 writel(((cs
->size
- 1) & 0xffff0000) |
115 (cs
->mbus_attr
<< 8) |
116 (dram
->mbus_dram_target_id
<< 4) | 1,
117 base
+ KIRKWOOD_AUDIO_WIN_CTRL_REG(win
));
122 static int kirkwood_dma_open(struct snd_pcm_substream
*substream
)
125 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
126 struct snd_soc_pcm_runtime
*soc_runtime
= substream
->private_data
;
127 struct snd_soc_platform
*platform
= soc_runtime
->platform
;
128 struct snd_soc_dai
*cpu_dai
= soc_runtime
->cpu_dai
;
129 struct kirkwood_dma_data
*priv
;
130 struct kirkwood_dma_priv
*prdata
= snd_soc_platform_get_drvdata(platform
);
131 const struct mbus_dram_target_info
*dram
;
134 priv
= snd_soc_dai_get_dma_data(cpu_dai
, substream
);
135 snd_soc_set_runtime_hwparams(substream
, &kirkwood_dma_snd_hw
);
137 /* Ensure that all constraints linked to dma burst are fulfilled */
138 err
= snd_pcm_hw_constraint_minmax(runtime
,
139 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
141 KIRKWOOD_AUDIO_BUF_MAX
-1);
145 err
= snd_pcm_hw_constraint_step(runtime
, 0,
146 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
151 err
= snd_pcm_hw_constraint_step(substream
->runtime
, 0,
152 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
157 if (prdata
== NULL
) {
158 prdata
= kzalloc(sizeof(struct kirkwood_dma_priv
), GFP_KERNEL
);
164 err
= request_irq(priv
->irq
, kirkwood_dma_irq
, IRQF_SHARED
,
165 "kirkwood-i2s", prdata
);
171 snd_soc_platform_set_drvdata(platform
, prdata
);
174 * Enable Error interrupts. We're only ack'ing them but
175 * it's useful for diagnostics
177 writel((unsigned long)-1, priv
->io
+ KIRKWOOD_ERR_MASK
);
180 dram
= mv_mbus_dram_info();
181 addr
= virt_to_phys(substream
->dma_buffer
.area
);
182 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
183 prdata
->play_stream
= substream
;
184 kirkwood_dma_conf_mbus_windows(priv
->io
,
185 KIRKWOOD_PLAYBACK_WIN
, addr
, dram
);
187 prdata
->rec_stream
= substream
;
188 kirkwood_dma_conf_mbus_windows(priv
->io
,
189 KIRKWOOD_RECORD_WIN
, addr
, dram
);
195 static int kirkwood_dma_close(struct snd_pcm_substream
*substream
)
197 struct snd_soc_pcm_runtime
*soc_runtime
= substream
->private_data
;
198 struct snd_soc_dai
*cpu_dai
= soc_runtime
->cpu_dai
;
199 struct snd_soc_platform
*platform
= soc_runtime
->platform
;
200 struct kirkwood_dma_priv
*prdata
= snd_soc_platform_get_drvdata(platform
);
201 struct kirkwood_dma_data
*priv
;
203 priv
= snd_soc_dai_get_dma_data(cpu_dai
, substream
);
205 if (!prdata
|| !priv
)
208 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
209 prdata
->play_stream
= NULL
;
211 prdata
->rec_stream
= NULL
;
213 if (!prdata
->play_stream
&& !prdata
->rec_stream
) {
214 writel(0, priv
->io
+ KIRKWOOD_ERR_MASK
);
215 free_irq(priv
->irq
, prdata
);
217 snd_soc_platform_set_drvdata(platform
, NULL
);
223 static int kirkwood_dma_hw_params(struct snd_pcm_substream
*substream
,
224 struct snd_pcm_hw_params
*params
)
226 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
228 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
229 runtime
->dma_bytes
= params_buffer_bytes(params
);
234 static int kirkwood_dma_hw_free(struct snd_pcm_substream
*substream
)
236 snd_pcm_set_runtime_buffer(substream
, NULL
);
240 static int kirkwood_dma_prepare(struct snd_pcm_substream
*substream
)
242 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
243 struct snd_soc_pcm_runtime
*soc_runtime
= substream
->private_data
;
244 struct snd_soc_dai
*cpu_dai
= soc_runtime
->cpu_dai
;
245 struct kirkwood_dma_data
*priv
;
246 unsigned long size
, count
;
248 priv
= snd_soc_dai_get_dma_data(cpu_dai
, substream
);
250 /* compute buffer size in term of "words" as requested in specs */
251 size
= frames_to_bytes(runtime
, runtime
->buffer_size
);
253 count
= snd_pcm_lib_period_bytes(substream
);
255 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
256 writel(count
, priv
->io
+ KIRKWOOD_PLAY_BYTE_INT_COUNT
);
257 writel(runtime
->dma_addr
, priv
->io
+ KIRKWOOD_PLAY_BUF_ADDR
);
258 writel(size
, priv
->io
+ KIRKWOOD_PLAY_BUF_SIZE
);
260 writel(count
, priv
->io
+ KIRKWOOD_REC_BYTE_INT_COUNT
);
261 writel(runtime
->dma_addr
, priv
->io
+ KIRKWOOD_REC_BUF_ADDR
);
262 writel(size
, priv
->io
+ KIRKWOOD_REC_BUF_SIZE
);
269 static snd_pcm_uframes_t
kirkwood_dma_pointer(struct snd_pcm_substream
272 struct snd_soc_pcm_runtime
*soc_runtime
= substream
->private_data
;
273 struct snd_soc_dai
*cpu_dai
= soc_runtime
->cpu_dai
;
274 struct kirkwood_dma_data
*priv
;
275 snd_pcm_uframes_t count
;
277 priv
= snd_soc_dai_get_dma_data(cpu_dai
, substream
);
279 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
280 count
= bytes_to_frames(substream
->runtime
,
281 readl(priv
->io
+ KIRKWOOD_PLAY_BYTE_COUNT
));
283 count
= bytes_to_frames(substream
->runtime
,
284 readl(priv
->io
+ KIRKWOOD_REC_BYTE_COUNT
));
289 struct snd_pcm_ops kirkwood_dma_ops
= {
290 .open
= kirkwood_dma_open
,
291 .close
= kirkwood_dma_close
,
292 .ioctl
= snd_pcm_lib_ioctl
,
293 .hw_params
= kirkwood_dma_hw_params
,
294 .hw_free
= kirkwood_dma_hw_free
,
295 .prepare
= kirkwood_dma_prepare
,
296 .pointer
= kirkwood_dma_pointer
,
299 static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm
*pcm
,
302 struct snd_pcm_substream
*substream
= pcm
->streams
[stream
].substream
;
303 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
304 size_t size
= kirkwood_dma_snd_hw
.buffer_bytes_max
;
306 buf
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
307 buf
->dev
.dev
= pcm
->card
->dev
;
308 buf
->area
= dma_alloc_coherent(pcm
->card
->dev
, size
,
309 &buf
->addr
, GFP_KERNEL
);
313 buf
->private_data
= NULL
;
318 static int kirkwood_dma_new(struct snd_soc_pcm_runtime
*rtd
)
320 struct snd_card
*card
= rtd
->card
->snd_card
;
321 struct snd_pcm
*pcm
= rtd
->pcm
;
324 if (!card
->dev
->dma_mask
)
325 card
->dev
->dma_mask
= &kirkwood_dma_dmamask
;
326 if (!card
->dev
->coherent_dma_mask
)
327 card
->dev
->coherent_dma_mask
= 0xffffffff;
329 if (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
) {
330 ret
= kirkwood_dma_preallocate_dma_buffer(pcm
,
331 SNDRV_PCM_STREAM_PLAYBACK
);
336 if (pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
) {
337 ret
= kirkwood_dma_preallocate_dma_buffer(pcm
,
338 SNDRV_PCM_STREAM_CAPTURE
);
346 static void kirkwood_dma_free_dma_buffers(struct snd_pcm
*pcm
)
348 struct snd_pcm_substream
*substream
;
349 struct snd_dma_buffer
*buf
;
352 for (stream
= 0; stream
< 2; stream
++) {
353 substream
= pcm
->streams
[stream
].substream
;
356 buf
= &substream
->dma_buffer
;
360 dma_free_coherent(pcm
->card
->dev
, buf
->bytes
,
361 buf
->area
, buf
->addr
);
366 static struct snd_soc_platform_driver kirkwood_soc_platform
= {
367 .ops
= &kirkwood_dma_ops
,
368 .pcm_new
= kirkwood_dma_new
,
369 .pcm_free
= kirkwood_dma_free_dma_buffers
,
372 static int __devinit
kirkwood_soc_platform_probe(struct platform_device
*pdev
)
374 return snd_soc_register_platform(&pdev
->dev
, &kirkwood_soc_platform
);
377 static int __devexit
kirkwood_soc_platform_remove(struct platform_device
*pdev
)
379 snd_soc_unregister_platform(&pdev
->dev
);
383 static struct platform_driver kirkwood_pcm_driver
= {
385 .name
= "kirkwood-pcm-audio",
386 .owner
= THIS_MODULE
,
389 .probe
= kirkwood_soc_platform_probe
,
390 .remove
= __devexit_p(kirkwood_soc_platform_remove
),
393 module_platform_driver(kirkwood_pcm_driver
);
395 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
396 MODULE_DESCRIPTION("Marvell Kirkwood Audio DMA module");
397 MODULE_LICENSE("GPL");
398 MODULE_ALIAS("platform:kirkwood-pcm-audio");