blob: 4af1936cf0f4fdbbaafe0bee2879872418efadd7 [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 Kingb51600c2013-08-31 13:43:36 +010032 SNDRV_PCM_FMTBIT_S32_LE)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020033
Russell Kinga7d09422013-08-04 20:22:03 +010034static struct kirkwood_dma_data *kirkwood_priv(struct snd_pcm_substream *subs)
35{
36 struct snd_soc_pcm_runtime *soc_runtime = subs->private_data;
37 return snd_soc_dai_get_drvdata(soc_runtime->cpu_dai);
38}
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020039
40static struct snd_pcm_hardware kirkwood_dma_snd_hw = {
41 .info = (SNDRV_PCM_INFO_INTERLEAVED |
42 SNDRV_PCM_INFO_MMAP |
43 SNDRV_PCM_INFO_MMAP_VALID |
44 SNDRV_PCM_INFO_BLOCK_TRANSFER |
45 SNDRV_PCM_INFO_PAUSE),
46 .formats = KIRKWOOD_FORMATS,
47 .rates = KIRKWOOD_RATES,
Russell King0aa5e472012-11-20 12:20:14 +000048 .rate_min = 8000,
49 .rate_max = 384000,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020050 .channels_min = 1,
Russell King17034702012-11-20 12:20:55 +000051 .channels_max = 8,
Russell Kinge4065f32013-08-04 20:28:04 +010052 .buffer_bytes_max = KIRKWOOD_SND_MAX_BUFFER_BYTES,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020053 .period_bytes_min = KIRKWOOD_SND_MIN_PERIOD_BYTES,
54 .period_bytes_max = KIRKWOOD_SND_MAX_PERIOD_BYTES,
55 .periods_min = KIRKWOOD_SND_MIN_PERIODS,
56 .periods_max = KIRKWOOD_SND_MAX_PERIODS,
57 .fifo_size = 0,
58};
59
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020060static irqreturn_t kirkwood_dma_irq(int irq, void *dev_id)
61{
Russell Kinga7d09422013-08-04 20:22:03 +010062 struct kirkwood_dma_data *priv = dev_id;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020063 unsigned long mask, status, cause;
64
65 mask = readl(priv->io + KIRKWOOD_INT_MASK);
66 status = readl(priv->io + KIRKWOOD_INT_CAUSE) & mask;
67
68 cause = readl(priv->io + KIRKWOOD_ERR_CAUSE);
69 if (unlikely(cause)) {
70 printk(KERN_WARNING "%s: got err interrupt 0x%lx\n",
71 __func__, cause);
72 writel(cause, priv->io + KIRKWOOD_ERR_CAUSE);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020073 }
74
75 /* we've enabled only bytes interrupts ... */
76 if (status & ~(KIRKWOOD_INT_CAUSE_PLAY_BYTES | \
77 KIRKWOOD_INT_CAUSE_REC_BYTES)) {
78 printk(KERN_WARNING "%s: unexpected interrupt %lx\n",
79 __func__, status);
80 return IRQ_NONE;
81 }
82
83 /* ack int */
84 writel(status, priv->io + KIRKWOOD_INT_CAUSE);
85
86 if (status & KIRKWOOD_INT_CAUSE_PLAY_BYTES)
Russell Kinga7d09422013-08-04 20:22:03 +010087 snd_pcm_period_elapsed(priv->substream_play);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020088
89 if (status & KIRKWOOD_INT_CAUSE_REC_BYTES)
Russell Kinga7d09422013-08-04 20:22:03 +010090 snd_pcm_period_elapsed(priv->substream_rec);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020091
92 return IRQ_HANDLED;
93}
94
Andrew Lunn63a93322011-12-07 21:48:07 +010095static void
96kirkwood_dma_conf_mbus_windows(void __iomem *base, int win,
97 unsigned long dma,
98 const struct mbus_dram_target_info *dram)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020099{
100 int i;
101
102 /* First disable and clear windows */
103 writel(0, base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
104 writel(0, base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
105
106 /* try to find matching cs for current dma address */
107 for (i = 0; i < dram->num_cs; i++) {
Andrew Lunn63a93322011-12-07 21:48:07 +0100108 const struct mbus_dram_window *cs = dram->cs + i;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200109 if ((cs->base & 0xffff0000) < (dma & 0xffff0000)) {
110 writel(cs->base & 0xffff0000,
111 base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
112 writel(((cs->size - 1) & 0xffff0000) |
113 (cs->mbus_attr << 8) |
114 (dram->mbus_dram_target_id << 4) | 1,
115 base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
116 }
117 }
118}
119
120static int kirkwood_dma_open(struct snd_pcm_substream *substream)
121{
122 int err;
123 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kinga7d09422013-08-04 20:22:03 +0100124 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
Andrew Lunn63a93322011-12-07 21:48:07 +0100125 const struct mbus_dram_target_info *dram;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200126 unsigned long addr;
127
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200128 snd_soc_set_runtime_hwparams(substream, &kirkwood_dma_snd_hw);
129
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300130 /* Ensure that all constraints linked to dma burst are fulfilled */
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200131 err = snd_pcm_hw_constraint_minmax(runtime,
132 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
133 priv->burst * 2,
134 KIRKWOOD_AUDIO_BUF_MAX-1);
135 if (err < 0)
136 return err;
137
138 err = snd_pcm_hw_constraint_step(runtime, 0,
139 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
140 priv->burst);
141 if (err < 0)
142 return err;
143
144 err = snd_pcm_hw_constraint_step(substream->runtime, 0,
145 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
146 priv->burst);
147 if (err < 0)
148 return err;
149
Russell Kinga7d09422013-08-04 20:22:03 +0100150 if (!priv->substream_play && !priv->substream_rec) {
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200151 err = request_irq(priv->irq, kirkwood_dma_irq, IRQF_SHARED,
Russell Kinga7d09422013-08-04 20:22:03 +0100152 "kirkwood-i2s", priv);
153 if (err)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200154 return -EBUSY;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200155
156 /*
157 * Enable Error interrupts. We're only ack'ing them but
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * it's useful for diagnostics
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200159 */
Vladimir Murzine2c99172013-09-29 16:00:13 +0200160 writel((unsigned int)-1, priv->io + KIRKWOOD_ERR_MASK);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200161 }
162
Andrew Lunn63a93322011-12-07 21:48:07 +0100163 dram = mv_mbus_dram_info();
Russell Kingae6a5d32012-11-20 12:17:51 +0000164 addr = substream->dma_buffer.addr;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200165 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Russell Kinga7d09422013-08-04 20:22:03 +0100166 priv->substream_play = substream;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200167 kirkwood_dma_conf_mbus_windows(priv->io,
Andrew Lunn63a93322011-12-07 21:48:07 +0100168 KIRKWOOD_PLAYBACK_WIN, addr, dram);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200169 } else {
Russell Kinga7d09422013-08-04 20:22:03 +0100170 priv->substream_rec = substream;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200171 kirkwood_dma_conf_mbus_windows(priv->io,
Andrew Lunn63a93322011-12-07 21:48:07 +0100172 KIRKWOOD_RECORD_WIN, addr, dram);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200173 }
174
175 return 0;
176}
177
178static int kirkwood_dma_close(struct snd_pcm_substream *substream)
179{
Russell Kinga7d09422013-08-04 20:22:03 +0100180 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200181
Russell Kinga7d09422013-08-04 20:22:03 +0100182 if (!priv)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200183 return 0;
184
185 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Russell Kinga7d09422013-08-04 20:22:03 +0100186 priv->substream_play = NULL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200187 else
Russell Kinga7d09422013-08-04 20:22:03 +0100188 priv->substream_rec = NULL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200189
Russell Kinga7d09422013-08-04 20:22:03 +0100190 if (!priv->substream_play && !priv->substream_rec) {
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200191 writel(0, priv->io + KIRKWOOD_ERR_MASK);
Russell Kinga7d09422013-08-04 20:22:03 +0100192 free_irq(priv->irq, priv);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200193 }
194
195 return 0;
196}
197
198static int kirkwood_dma_hw_params(struct snd_pcm_substream *substream,
199 struct snd_pcm_hw_params *params)
200{
201 struct snd_pcm_runtime *runtime = substream->runtime;
202
203 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
204 runtime->dma_bytes = params_buffer_bytes(params);
205
206 return 0;
207}
208
209static int kirkwood_dma_hw_free(struct snd_pcm_substream *substream)
210{
211 snd_pcm_set_runtime_buffer(substream, NULL);
212 return 0;
213}
214
215static int kirkwood_dma_prepare(struct snd_pcm_substream *substream)
216{
217 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kinga7d09422013-08-04 20:22:03 +0100218 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200219 unsigned long size, count;
220
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200221 /* compute buffer size in term of "words" as requested in specs */
222 size = frames_to_bytes(runtime, runtime->buffer_size);
223 size = (size>>2)-1;
224 count = snd_pcm_lib_period_bytes(substream);
225
226 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
227 writel(count, priv->io + KIRKWOOD_PLAY_BYTE_INT_COUNT);
228 writel(runtime->dma_addr, priv->io + KIRKWOOD_PLAY_BUF_ADDR);
229 writel(size, priv->io + KIRKWOOD_PLAY_BUF_SIZE);
230 } else {
231 writel(count, priv->io + KIRKWOOD_REC_BYTE_INT_COUNT);
232 writel(runtime->dma_addr, priv->io + KIRKWOOD_REC_BUF_ADDR);
233 writel(size, priv->io + KIRKWOOD_REC_BUF_SIZE);
234 }
235
236
237 return 0;
238}
239
240static snd_pcm_uframes_t kirkwood_dma_pointer(struct snd_pcm_substream
241 *substream)
242{
Russell Kinga7d09422013-08-04 20:22:03 +0100243 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200244 snd_pcm_uframes_t count;
245
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200246 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
247 count = bytes_to_frames(substream->runtime,
248 readl(priv->io + KIRKWOOD_PLAY_BYTE_COUNT));
249 else
250 count = bytes_to_frames(substream->runtime,
251 readl(priv->io + KIRKWOOD_REC_BYTE_COUNT));
252
253 return count;
254}
255
Lars-Peter Clausen5d0c8a52013-05-14 22:19:48 +0200256static struct snd_pcm_ops kirkwood_dma_ops = {
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200257 .open = kirkwood_dma_open,
258 .close = kirkwood_dma_close,
259 .ioctl = snd_pcm_lib_ioctl,
260 .hw_params = kirkwood_dma_hw_params,
261 .hw_free = kirkwood_dma_hw_free,
262 .prepare = kirkwood_dma_prepare,
263 .pointer = kirkwood_dma_pointer,
264};
265
266static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm *pcm,
267 int stream)
268{
269 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
270 struct snd_dma_buffer *buf = &substream->dma_buffer;
271 size_t size = kirkwood_dma_snd_hw.buffer_bytes_max;
272
273 buf->dev.type = SNDRV_DMA_TYPE_DEV;
274 buf->dev.dev = pcm->card->dev;
275 buf->area = dma_alloc_coherent(pcm->card->dev, size,
276 &buf->addr, GFP_KERNEL);
277 if (!buf->area)
278 return -ENOMEM;
279 buf->bytes = size;
280 buf->private_data = NULL;
281
282 return 0;
283}
284
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100285static int kirkwood_dma_new(struct snd_soc_pcm_runtime *rtd)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200286{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100287 struct snd_card *card = rtd->card->snd_card;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100288 struct snd_pcm *pcm = rtd->pcm;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200289 int ret;
290
Russell Kingc9bd5e62013-06-27 12:53:37 +0100291 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
292 if (ret)
293 return ret;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200294
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100295 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200296 ret = kirkwood_dma_preallocate_dma_buffer(pcm,
297 SNDRV_PCM_STREAM_PLAYBACK);
298 if (ret)
299 return ret;
300 }
301
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100302 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200303 ret = kirkwood_dma_preallocate_dma_buffer(pcm,
304 SNDRV_PCM_STREAM_CAPTURE);
305 if (ret)
306 return ret;
307 }
308
309 return 0;
310}
311
312static void kirkwood_dma_free_dma_buffers(struct snd_pcm *pcm)
313{
314 struct snd_pcm_substream *substream;
315 struct snd_dma_buffer *buf;
316 int stream;
317
318 for (stream = 0; stream < 2; stream++) {
319 substream = pcm->streams[stream].substream;
320 if (!substream)
321 continue;
322 buf = &substream->dma_buffer;
323 if (!buf->area)
324 continue;
325
326 dma_free_coherent(pcm->card->dev, buf->bytes,
327 buf->area, buf->addr);
328 buf->area = NULL;
329 }
330}
331
Russell King64ddf1f2013-08-04 20:27:03 +0100332struct snd_soc_platform_driver kirkwood_soc_platform = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000333 .ops = &kirkwood_dma_ops,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200334 .pcm_new = kirkwood_dma_new,
335 .pcm_free = kirkwood_dma_free_dma_buffers,
336};