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
);
97 static void kirkwood_dma_conf_mbus_windows(void __iomem
*base
, int win
,
99 struct mbus_dram_target_info
*dram
)
103 /* First disable and clear windows */
104 writel(0, base
+ KIRKWOOD_AUDIO_WIN_CTRL_REG(win
));
105 writel(0, base
+ KIRKWOOD_AUDIO_WIN_BASE_REG(win
));
107 /* try to find matching cs for current dma address */
108 for (i
= 0; i
< dram
->num_cs
; i
++) {
109 struct mbus_dram_window
*cs
= dram
->cs
+ i
;
110 if ((cs
->base
& 0xffff0000) < (dma
& 0xffff0000)) {
111 writel(cs
->base
& 0xffff0000,
112 base
+ KIRKWOOD_AUDIO_WIN_BASE_REG(win
));
113 writel(((cs
->size
- 1) & 0xffff0000) |
114 (cs
->mbus_attr
<< 8) |
115 (dram
->mbus_dram_target_id
<< 4) | 1,
116 base
+ KIRKWOOD_AUDIO_WIN_CTRL_REG(win
));
121 static int kirkwood_dma_open(struct snd_pcm_substream
*substream
)
124 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
125 struct snd_soc_pcm_runtime
*soc_runtime
= substream
->private_data
;
126 struct snd_soc_platform
*platform
= soc_runtime
->platform
;
127 struct snd_soc_dai
*cpu_dai
= soc_runtime
->cpu_dai
;
128 struct kirkwood_dma_data
*priv
;
129 struct kirkwood_dma_priv
*prdata
= snd_soc_platform_get_drvdata(platform
);
132 priv
= snd_soc_dai_get_dma_data(cpu_dai
, substream
);
133 snd_soc_set_runtime_hwparams(substream
, &kirkwood_dma_snd_hw
);
135 /* Ensure that all constraints linked to dma burst are fullfilled */
136 err
= snd_pcm_hw_constraint_minmax(runtime
,
137 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
139 KIRKWOOD_AUDIO_BUF_MAX
-1);
143 err
= snd_pcm_hw_constraint_step(runtime
, 0,
144 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
149 err
= snd_pcm_hw_constraint_step(substream
->runtime
, 0,
150 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
155 if (prdata
== NULL
) {
156 prdata
= kzalloc(sizeof(struct kirkwood_dma_priv
), GFP_KERNEL
);
162 err
= request_irq(priv
->irq
, kirkwood_dma_irq
, IRQF_SHARED
,
163 "kirkwood-i2s", prdata
);
169 snd_soc_platform_set_drvdata(platform
, prdata
);
172 * Enable Error interrupts. We're only ack'ing them but
173 * it's usefull for diagnostics
175 writel((unsigned long)-1, priv
->io
+ KIRKWOOD_ERR_MASK
);
178 addr
= virt_to_phys(substream
->dma_buffer
.area
);
179 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
180 prdata
->play_stream
= substream
;
181 kirkwood_dma_conf_mbus_windows(priv
->io
,
182 KIRKWOOD_PLAYBACK_WIN
, addr
, priv
->dram
);
184 prdata
->rec_stream
= substream
;
185 kirkwood_dma_conf_mbus_windows(priv
->io
,
186 KIRKWOOD_RECORD_WIN
, addr
, priv
->dram
);
192 static int kirkwood_dma_close(struct snd_pcm_substream
*substream
)
194 struct snd_soc_pcm_runtime
*soc_runtime
= substream
->private_data
;
195 struct snd_soc_dai
*cpu_dai
= soc_runtime
->cpu_dai
;
196 struct snd_soc_platform
*platform
= soc_runtime
->platform
;
197 struct kirkwood_dma_priv
*prdata
= snd_soc_platform_get_drvdata(platform
);
198 struct kirkwood_dma_data
*priv
;
200 priv
= snd_soc_dai_get_dma_data(cpu_dai
, substream
);
202 if (!prdata
|| !priv
)
205 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
206 prdata
->play_stream
= NULL
;
208 prdata
->rec_stream
= NULL
;
210 if (!prdata
->play_stream
&& !prdata
->rec_stream
) {
211 writel(0, priv
->io
+ KIRKWOOD_ERR_MASK
);
212 free_irq(priv
->irq
, prdata
);
214 snd_soc_platform_set_drvdata(platform
, NULL
);
220 static int kirkwood_dma_hw_params(struct snd_pcm_substream
*substream
,
221 struct snd_pcm_hw_params
*params
)
223 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
225 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
226 runtime
->dma_bytes
= params_buffer_bytes(params
);
231 static int kirkwood_dma_hw_free(struct snd_pcm_substream
*substream
)
233 snd_pcm_set_runtime_buffer(substream
, NULL
);
237 static int kirkwood_dma_prepare(struct snd_pcm_substream
*substream
)
239 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
240 struct snd_soc_pcm_runtime
*soc_runtime
= substream
->private_data
;
241 struct snd_soc_dai
*cpu_dai
= soc_runtime
->cpu_dai
;
242 struct kirkwood_dma_data
*priv
;
243 unsigned long size
, count
;
245 priv
= snd_soc_dai_get_dma_data(cpu_dai
, substream
);
247 /* compute buffer size in term of "words" as requested in specs */
248 size
= frames_to_bytes(runtime
, runtime
->buffer_size
);
250 count
= snd_pcm_lib_period_bytes(substream
);
252 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
253 writel(count
, priv
->io
+ KIRKWOOD_PLAY_BYTE_INT_COUNT
);
254 writel(runtime
->dma_addr
, priv
->io
+ KIRKWOOD_PLAY_BUF_ADDR
);
255 writel(size
, priv
->io
+ KIRKWOOD_PLAY_BUF_SIZE
);
257 writel(count
, priv
->io
+ KIRKWOOD_REC_BYTE_INT_COUNT
);
258 writel(runtime
->dma_addr
, priv
->io
+ KIRKWOOD_REC_BUF_ADDR
);
259 writel(size
, priv
->io
+ KIRKWOOD_REC_BUF_SIZE
);
266 static snd_pcm_uframes_t
kirkwood_dma_pointer(struct snd_pcm_substream
269 struct snd_soc_pcm_runtime
*soc_runtime
= substream
->private_data
;
270 struct snd_soc_dai
*cpu_dai
= soc_runtime
->cpu_dai
;
271 struct kirkwood_dma_data
*priv
;
272 snd_pcm_uframes_t count
;
274 priv
= snd_soc_dai_get_dma_data(cpu_dai
, substream
);
276 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
277 count
= bytes_to_frames(substream
->runtime
,
278 readl(priv
->io
+ KIRKWOOD_PLAY_BYTE_COUNT
));
280 count
= bytes_to_frames(substream
->runtime
,
281 readl(priv
->io
+ KIRKWOOD_REC_BYTE_COUNT
));
286 struct snd_pcm_ops kirkwood_dma_ops
= {
287 .open
= kirkwood_dma_open
,
288 .close
= kirkwood_dma_close
,
289 .ioctl
= snd_pcm_lib_ioctl
,
290 .hw_params
= kirkwood_dma_hw_params
,
291 .hw_free
= kirkwood_dma_hw_free
,
292 .prepare
= kirkwood_dma_prepare
,
293 .pointer
= kirkwood_dma_pointer
,
296 static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm
*pcm
,
299 struct snd_pcm_substream
*substream
= pcm
->streams
[stream
].substream
;
300 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
301 size_t size
= kirkwood_dma_snd_hw
.buffer_bytes_max
;
303 buf
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
304 buf
->dev
.dev
= pcm
->card
->dev
;
305 buf
->area
= dma_alloc_coherent(pcm
->card
->dev
, size
,
306 &buf
->addr
, GFP_KERNEL
);
310 buf
->private_data
= NULL
;
315 static int kirkwood_dma_new(struct snd_card
*card
,
316 struct snd_soc_dai
*dai
, struct snd_pcm
*pcm
)
320 if (!card
->dev
->dma_mask
)
321 card
->dev
->dma_mask
= &kirkwood_dma_dmamask
;
322 if (!card
->dev
->coherent_dma_mask
)
323 card
->dev
->coherent_dma_mask
= 0xffffffff;
325 if (dai
->driver
->playback
.channels_min
) {
326 ret
= kirkwood_dma_preallocate_dma_buffer(pcm
,
327 SNDRV_PCM_STREAM_PLAYBACK
);
332 if (dai
->driver
->capture
.channels_min
) {
333 ret
= kirkwood_dma_preallocate_dma_buffer(pcm
,
334 SNDRV_PCM_STREAM_CAPTURE
);
342 static void kirkwood_dma_free_dma_buffers(struct snd_pcm
*pcm
)
344 struct snd_pcm_substream
*substream
;
345 struct snd_dma_buffer
*buf
;
348 for (stream
= 0; stream
< 2; stream
++) {
349 substream
= pcm
->streams
[stream
].substream
;
352 buf
= &substream
->dma_buffer
;
356 dma_free_coherent(pcm
->card
->dev
, buf
->bytes
,
357 buf
->area
, buf
->addr
);
362 static struct snd_soc_platform_driver kirkwood_soc_platform
= {
363 .ops
= &kirkwood_dma_ops
,
364 .pcm_new
= kirkwood_dma_new
,
365 .pcm_free
= kirkwood_dma_free_dma_buffers
,
368 static int __devinit
kirkwood_soc_platform_probe(struct platform_device
*pdev
)
370 return snd_soc_register_platform(&pdev
->dev
, &kirkwood_soc_platform
);
373 static int __devexit
kirkwood_soc_platform_remove(struct platform_device
*pdev
)
375 snd_soc_unregister_platform(&pdev
->dev
);
379 static struct platform_driver kirkwood_pcm_driver
= {
381 .name
= "kirkwood-pcm-audio",
382 .owner
= THIS_MODULE
,
385 .probe
= kirkwood_soc_platform_probe
,
386 .remove
= __devexit_p(kirkwood_soc_platform_remove
),
389 static int __init
kirkwood_pcm_init(void)
391 return platform_driver_register(&kirkwood_pcm_driver
);
393 module_init(kirkwood_pcm_init
);
395 static void __exit
kirkwood_pcm_exit(void)
397 platform_driver_unregister(&kirkwood_pcm_driver
);
399 module_exit(kirkwood_pcm_exit
);
401 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
402 MODULE_DESCRIPTION("Marvell Kirkwood Audio DMA module");
403 MODULE_LICENSE("GPL");
404 MODULE_ALIAS("platform:kirkwood-pcm-audio");