blob: d3d4bdca1cc62d0387ca558cb7b7b020209b6f63 [file] [log] [blame]
apatard@mandriva.comf9b95982010-05-31 13:49:14 +02001/*
2 * kirkwood-dma.c
3 *
4 * (c) 2010 Arnaud Patard <apatard@mandriva.com>
Arnaud Patard (Rtp)69737892010-09-09 14:10:33 +02005 * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
apatard@mandriva.comf9b95982010-05-31 13:49:14 +02006 *
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.
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/io.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>
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020022#include "kirkwood.h"
23
24#define KIRKWOOD_RATES \
Russell King0aa5e472012-11-20 12:20:14 +000025 (SNDRV_PCM_RATE_8000_192000 | \
26 SNDRV_PCM_RATE_CONTINUOUS | \
27 SNDRV_PCM_RATE_KNOT)
28
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020029#define KIRKWOOD_FORMATS \
30 (SNDRV_PCM_FMTBIT_S16_LE | \
31 SNDRV_PCM_FMTBIT_S24_LE | \
Russell King17034702012-11-20 12:20:55 +000032 SNDRV_PCM_FMTBIT_S32_LE | \
33 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | \
34 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020035
36struct kirkwood_dma_priv {
37 struct snd_pcm_substream *play_stream;
38 struct snd_pcm_substream *rec_stream;
39 struct kirkwood_dma_data *data;
40};
41
42static struct snd_pcm_hardware kirkwood_dma_snd_hw = {
43 .info = (SNDRV_PCM_INFO_INTERLEAVED |
44 SNDRV_PCM_INFO_MMAP |
45 SNDRV_PCM_INFO_MMAP_VALID |
46 SNDRV_PCM_INFO_BLOCK_TRANSFER |
47 SNDRV_PCM_INFO_PAUSE),
48 .formats = KIRKWOOD_FORMATS,
49 .rates = KIRKWOOD_RATES,
Russell King0aa5e472012-11-20 12:20:14 +000050 .rate_min = 8000,
51 .rate_max = 384000,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020052 .channels_min = 1,
Russell King17034702012-11-20 12:20:55 +000053 .channels_max = 8,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020054 .buffer_bytes_max = KIRKWOOD_SND_MAX_PERIOD_BYTES * KIRKWOOD_SND_MAX_PERIODS,
55 .period_bytes_min = KIRKWOOD_SND_MIN_PERIOD_BYTES,
56 .period_bytes_max = KIRKWOOD_SND_MAX_PERIOD_BYTES,
57 .periods_min = KIRKWOOD_SND_MIN_PERIODS,
58 .periods_max = KIRKWOOD_SND_MAX_PERIODS,
59 .fifo_size = 0,
60};
61
Joachim Eastwood350e16d2012-01-01 02:43:03 +010062static u64 kirkwood_dma_dmamask = DMA_BIT_MASK(32);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020063
64static irqreturn_t kirkwood_dma_irq(int irq, void *dev_id)
65{
66 struct kirkwood_dma_priv *prdata = dev_id;
67 struct kirkwood_dma_data *priv = prdata->data;
68 unsigned long mask, status, cause;
69
70 mask = readl(priv->io + KIRKWOOD_INT_MASK);
71 status = readl(priv->io + KIRKWOOD_INT_CAUSE) & mask;
72
73 cause = readl(priv->io + KIRKWOOD_ERR_CAUSE);
74 if (unlikely(cause)) {
75 printk(KERN_WARNING "%s: got err interrupt 0x%lx\n",
76 __func__, cause);
77 writel(cause, priv->io + KIRKWOOD_ERR_CAUSE);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020078 }
79
80 /* we've enabled only bytes interrupts ... */
81 if (status & ~(KIRKWOOD_INT_CAUSE_PLAY_BYTES | \
82 KIRKWOOD_INT_CAUSE_REC_BYTES)) {
83 printk(KERN_WARNING "%s: unexpected interrupt %lx\n",
84 __func__, status);
85 return IRQ_NONE;
86 }
87
88 /* ack int */
89 writel(status, priv->io + KIRKWOOD_INT_CAUSE);
90
91 if (status & KIRKWOOD_INT_CAUSE_PLAY_BYTES)
92 snd_pcm_period_elapsed(prdata->play_stream);
93
94 if (status & KIRKWOOD_INT_CAUSE_REC_BYTES)
95 snd_pcm_period_elapsed(prdata->rec_stream);
96
97 return IRQ_HANDLED;
98}
99
Andrew Lunn63a93322011-12-07 21:48:07 +0100100static void
101kirkwood_dma_conf_mbus_windows(void __iomem *base, int win,
102 unsigned long dma,
103 const struct mbus_dram_target_info *dram)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200104{
105 int i;
106
107 /* First disable and clear windows */
108 writel(0, base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
109 writel(0, base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
110
111 /* try to find matching cs for current dma address */
112 for (i = 0; i < dram->num_cs; i++) {
Andrew Lunn63a93322011-12-07 21:48:07 +0100113 const struct mbus_dram_window *cs = dram->cs + i;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200114 if ((cs->base & 0xffff0000) < (dma & 0xffff0000)) {
115 writel(cs->base & 0xffff0000,
116 base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
117 writel(((cs->size - 1) & 0xffff0000) |
118 (cs->mbus_attr << 8) |
119 (dram->mbus_dram_target_id << 4) | 1,
120 base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
121 }
122 }
123}
124
125static int kirkwood_dma_open(struct snd_pcm_substream *substream)
126{
127 int err;
128 struct snd_pcm_runtime *runtime = substream->runtime;
129 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000130 struct snd_soc_platform *platform = soc_runtime->platform;
131 struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200132 struct kirkwood_dma_data *priv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000133 struct kirkwood_dma_priv *prdata = snd_soc_platform_get_drvdata(platform);
Andrew Lunn63a93322011-12-07 21:48:07 +0100134 const struct mbus_dram_target_info *dram;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200135 unsigned long addr;
136
137 priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
138 snd_soc_set_runtime_hwparams(substream, &kirkwood_dma_snd_hw);
139
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300140 /* Ensure that all constraints linked to dma burst are fulfilled */
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200141 err = snd_pcm_hw_constraint_minmax(runtime,
142 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
143 priv->burst * 2,
144 KIRKWOOD_AUDIO_BUF_MAX-1);
145 if (err < 0)
146 return err;
147
148 err = snd_pcm_hw_constraint_step(runtime, 0,
149 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
150 priv->burst);
151 if (err < 0)
152 return err;
153
154 err = snd_pcm_hw_constraint_step(substream->runtime, 0,
155 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
156 priv->burst);
157 if (err < 0)
158 return err;
159
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000160 if (prdata == NULL) {
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200161 prdata = kzalloc(sizeof(struct kirkwood_dma_priv), GFP_KERNEL);
162 if (prdata == NULL)
163 return -ENOMEM;
164
165 prdata->data = priv;
166
167 err = request_irq(priv->irq, kirkwood_dma_irq, IRQF_SHARED,
168 "kirkwood-i2s", prdata);
169 if (err) {
170 kfree(prdata);
171 return -EBUSY;
172 }
173
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000174 snd_soc_platform_set_drvdata(platform, prdata);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200175
176 /*
177 * Enable Error interrupts. We're only ack'ing them but
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300178 * it's useful for diagnostics
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200179 */
180 writel((unsigned long)-1, priv->io + KIRKWOOD_ERR_MASK);
181 }
182
Andrew Lunn63a93322011-12-07 21:48:07 +0100183 dram = mv_mbus_dram_info();
Russell Kingae6a5d32012-11-20 12:17:51 +0000184 addr = substream->dma_buffer.addr;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200185 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
186 prdata->play_stream = substream;
187 kirkwood_dma_conf_mbus_windows(priv->io,
Andrew Lunn63a93322011-12-07 21:48:07 +0100188 KIRKWOOD_PLAYBACK_WIN, addr, dram);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200189 } else {
190 prdata->rec_stream = substream;
191 kirkwood_dma_conf_mbus_windows(priv->io,
Andrew Lunn63a93322011-12-07 21:48:07 +0100192 KIRKWOOD_RECORD_WIN, addr, dram);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200193 }
194
195 return 0;
196}
197
198static int kirkwood_dma_close(struct snd_pcm_substream *substream)
199{
200 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000201 struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
202 struct snd_soc_platform *platform = soc_runtime->platform;
203 struct kirkwood_dma_priv *prdata = snd_soc_platform_get_drvdata(platform);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200204 struct kirkwood_dma_data *priv;
205
206 priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
207
208 if (!prdata || !priv)
209 return 0;
210
211 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
212 prdata->play_stream = NULL;
213 else
214 prdata->rec_stream = NULL;
215
216 if (!prdata->play_stream && !prdata->rec_stream) {
217 writel(0, priv->io + KIRKWOOD_ERR_MASK);
218 free_irq(priv->irq, prdata);
219 kfree(prdata);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000220 snd_soc_platform_set_drvdata(platform, NULL);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200221 }
222
223 return 0;
224}
225
226static int kirkwood_dma_hw_params(struct snd_pcm_substream *substream,
227 struct snd_pcm_hw_params *params)
228{
229 struct snd_pcm_runtime *runtime = substream->runtime;
230
231 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
232 runtime->dma_bytes = params_buffer_bytes(params);
233
234 return 0;
235}
236
237static int kirkwood_dma_hw_free(struct snd_pcm_substream *substream)
238{
239 snd_pcm_set_runtime_buffer(substream, NULL);
240 return 0;
241}
242
243static int kirkwood_dma_prepare(struct snd_pcm_substream *substream)
244{
245 struct snd_pcm_runtime *runtime = substream->runtime;
246 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000247 struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200248 struct kirkwood_dma_data *priv;
249 unsigned long size, count;
250
251 priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
252
253 /* compute buffer size in term of "words" as requested in specs */
254 size = frames_to_bytes(runtime, runtime->buffer_size);
255 size = (size>>2)-1;
256 count = snd_pcm_lib_period_bytes(substream);
257
258 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
259 writel(count, priv->io + KIRKWOOD_PLAY_BYTE_INT_COUNT);
260 writel(runtime->dma_addr, priv->io + KIRKWOOD_PLAY_BUF_ADDR);
261 writel(size, priv->io + KIRKWOOD_PLAY_BUF_SIZE);
262 } else {
263 writel(count, priv->io + KIRKWOOD_REC_BYTE_INT_COUNT);
264 writel(runtime->dma_addr, priv->io + KIRKWOOD_REC_BUF_ADDR);
265 writel(size, priv->io + KIRKWOOD_REC_BUF_SIZE);
266 }
267
268
269 return 0;
270}
271
272static snd_pcm_uframes_t kirkwood_dma_pointer(struct snd_pcm_substream
273 *substream)
274{
275 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000276 struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200277 struct kirkwood_dma_data *priv;
278 snd_pcm_uframes_t count;
279
280 priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
281
282 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
283 count = bytes_to_frames(substream->runtime,
284 readl(priv->io + KIRKWOOD_PLAY_BYTE_COUNT));
285 else
286 count = bytes_to_frames(substream->runtime,
287 readl(priv->io + KIRKWOOD_REC_BYTE_COUNT));
288
289 return count;
290}
291
292struct snd_pcm_ops kirkwood_dma_ops = {
293 .open = kirkwood_dma_open,
294 .close = kirkwood_dma_close,
295 .ioctl = snd_pcm_lib_ioctl,
296 .hw_params = kirkwood_dma_hw_params,
297 .hw_free = kirkwood_dma_hw_free,
298 .prepare = kirkwood_dma_prepare,
299 .pointer = kirkwood_dma_pointer,
300};
301
302static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm *pcm,
303 int stream)
304{
305 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
306 struct snd_dma_buffer *buf = &substream->dma_buffer;
307 size_t size = kirkwood_dma_snd_hw.buffer_bytes_max;
308
309 buf->dev.type = SNDRV_DMA_TYPE_DEV;
310 buf->dev.dev = pcm->card->dev;
311 buf->area = dma_alloc_coherent(pcm->card->dev, size,
312 &buf->addr, GFP_KERNEL);
313 if (!buf->area)
314 return -ENOMEM;
315 buf->bytes = size;
316 buf->private_data = NULL;
317
318 return 0;
319}
320
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100321static int kirkwood_dma_new(struct snd_soc_pcm_runtime *rtd)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200322{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100323 struct snd_card *card = rtd->card->snd_card;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100324 struct snd_pcm *pcm = rtd->pcm;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200325 int ret;
326
327 if (!card->dev->dma_mask)
328 card->dev->dma_mask = &kirkwood_dma_dmamask;
329 if (!card->dev->coherent_dma_mask)
Joachim Eastwood350e16d2012-01-01 02:43:03 +0100330 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200331
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100332 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200333 ret = kirkwood_dma_preallocate_dma_buffer(pcm,
334 SNDRV_PCM_STREAM_PLAYBACK);
335 if (ret)
336 return ret;
337 }
338
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100339 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200340 ret = kirkwood_dma_preallocate_dma_buffer(pcm,
341 SNDRV_PCM_STREAM_CAPTURE);
342 if (ret)
343 return ret;
344 }
345
346 return 0;
347}
348
349static void kirkwood_dma_free_dma_buffers(struct snd_pcm *pcm)
350{
351 struct snd_pcm_substream *substream;
352 struct snd_dma_buffer *buf;
353 int stream;
354
355 for (stream = 0; stream < 2; stream++) {
356 substream = pcm->streams[stream].substream;
357 if (!substream)
358 continue;
359 buf = &substream->dma_buffer;
360 if (!buf->area)
361 continue;
362
363 dma_free_coherent(pcm->card->dev, buf->bytes,
364 buf->area, buf->addr);
365 buf->area = NULL;
366 }
367}
368
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000369static struct snd_soc_platform_driver kirkwood_soc_platform = {
370 .ops = &kirkwood_dma_ops,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200371 .pcm_new = kirkwood_dma_new,
372 .pcm_free = kirkwood_dma_free_dma_buffers,
373};
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200374
Bill Pemberton34e15fb2012-12-07 09:26:26 -0500375static int kirkwood_soc_platform_probe(struct platform_device *pdev)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200376{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000377 return snd_soc_register_platform(&pdev->dev, &kirkwood_soc_platform);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200378}
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200379
Bill Pemberton34e15fb2012-12-07 09:26:26 -0500380static int kirkwood_soc_platform_remove(struct platform_device *pdev)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200381{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000382 snd_soc_unregister_platform(&pdev->dev);
383 return 0;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200384}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000385
386static struct platform_driver kirkwood_pcm_driver = {
387 .driver = {
388 .name = "kirkwood-pcm-audio",
389 .owner = THIS_MODULE,
390 },
391
392 .probe = kirkwood_soc_platform_probe,
Bill Pemberton34e15fb2012-12-07 09:26:26 -0500393 .remove = kirkwood_soc_platform_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000394};
395
Axel Lin41b10222011-11-24 11:43:09 +0800396module_platform_driver(kirkwood_pcm_driver);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200397
Arnaud Patard (Rtp)69737892010-09-09 14:10:33 +0200398MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200399MODULE_DESCRIPTION("Marvell Kirkwood Audio DMA module");
400MODULE_LICENSE("GPL");
Arnaud Patard (Rtp)3ba31052010-09-02 10:31:47 +0200401MODULE_ALIAS("platform:kirkwood-pcm-audio");