apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * kirkwood-dma.c |
| 3 | * |
| 4 | * (c) 2010 Arnaud Patard <apatard@mandriva.com> |
Arnaud Patard (Rtp) | 6973789 | 2010-09-09 14:10:33 +0200 | [diff] [blame] | 5 | * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org> |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 6 | * |
| 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.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 22 | #include "kirkwood.h" |
| 23 | |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 24 | static struct kirkwood_dma_data *kirkwood_priv(struct snd_pcm_substream *subs) |
| 25 | { |
| 26 | struct snd_soc_pcm_runtime *soc_runtime = subs->private_data; |
| 27 | return snd_soc_dai_get_drvdata(soc_runtime->cpu_dai); |
| 28 | } |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 29 | |
| 30 | static struct snd_pcm_hardware kirkwood_dma_snd_hw = { |
| 31 | .info = (SNDRV_PCM_INFO_INTERLEAVED | |
| 32 | SNDRV_PCM_INFO_MMAP | |
| 33 | SNDRV_PCM_INFO_MMAP_VALID | |
| 34 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 35 | SNDRV_PCM_INFO_PAUSE), |
Russell King | e4065f3 | 2013-08-04 20:28:04 +0100 | [diff] [blame] | 36 | .buffer_bytes_max = KIRKWOOD_SND_MAX_BUFFER_BYTES, |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 37 | .period_bytes_min = KIRKWOOD_SND_MIN_PERIOD_BYTES, |
| 38 | .period_bytes_max = KIRKWOOD_SND_MAX_PERIOD_BYTES, |
| 39 | .periods_min = KIRKWOOD_SND_MIN_PERIODS, |
| 40 | .periods_max = KIRKWOOD_SND_MAX_PERIODS, |
| 41 | .fifo_size = 0, |
| 42 | }; |
| 43 | |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 44 | static irqreturn_t kirkwood_dma_irq(int irq, void *dev_id) |
| 45 | { |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 46 | struct kirkwood_dma_data *priv = dev_id; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 47 | unsigned long mask, status, cause; |
| 48 | |
| 49 | mask = readl(priv->io + KIRKWOOD_INT_MASK); |
| 50 | status = readl(priv->io + KIRKWOOD_INT_CAUSE) & mask; |
| 51 | |
| 52 | cause = readl(priv->io + KIRKWOOD_ERR_CAUSE); |
| 53 | if (unlikely(cause)) { |
| 54 | printk(KERN_WARNING "%s: got err interrupt 0x%lx\n", |
| 55 | __func__, cause); |
| 56 | writel(cause, priv->io + KIRKWOOD_ERR_CAUSE); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* we've enabled only bytes interrupts ... */ |
| 60 | if (status & ~(KIRKWOOD_INT_CAUSE_PLAY_BYTES | \ |
| 61 | KIRKWOOD_INT_CAUSE_REC_BYTES)) { |
| 62 | printk(KERN_WARNING "%s: unexpected interrupt %lx\n", |
| 63 | __func__, status); |
| 64 | return IRQ_NONE; |
| 65 | } |
| 66 | |
| 67 | /* ack int */ |
| 68 | writel(status, priv->io + KIRKWOOD_INT_CAUSE); |
| 69 | |
| 70 | if (status & KIRKWOOD_INT_CAUSE_PLAY_BYTES) |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 71 | snd_pcm_period_elapsed(priv->substream_play); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 72 | |
| 73 | if (status & KIRKWOOD_INT_CAUSE_REC_BYTES) |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 74 | snd_pcm_period_elapsed(priv->substream_rec); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 75 | |
| 76 | return IRQ_HANDLED; |
| 77 | } |
| 78 | |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 79 | static void |
| 80 | kirkwood_dma_conf_mbus_windows(void __iomem *base, int win, |
| 81 | unsigned long dma, |
| 82 | const struct mbus_dram_target_info *dram) |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 83 | { |
| 84 | int i; |
| 85 | |
| 86 | /* First disable and clear windows */ |
| 87 | writel(0, base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win)); |
| 88 | writel(0, base + KIRKWOOD_AUDIO_WIN_BASE_REG(win)); |
| 89 | |
| 90 | /* try to find matching cs for current dma address */ |
| 91 | for (i = 0; i < dram->num_cs; i++) { |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 92 | const struct mbus_dram_window *cs = dram->cs + i; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 93 | if ((cs->base & 0xffff0000) < (dma & 0xffff0000)) { |
| 94 | writel(cs->base & 0xffff0000, |
| 95 | base + KIRKWOOD_AUDIO_WIN_BASE_REG(win)); |
| 96 | writel(((cs->size - 1) & 0xffff0000) | |
| 97 | (cs->mbus_attr << 8) | |
| 98 | (dram->mbus_dram_target_id << 4) | 1, |
| 99 | base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win)); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static int kirkwood_dma_open(struct snd_pcm_substream *substream) |
| 105 | { |
| 106 | int err; |
| 107 | struct snd_pcm_runtime *runtime = substream->runtime; |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 108 | struct kirkwood_dma_data *priv = kirkwood_priv(substream); |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 109 | const struct mbus_dram_target_info *dram; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 110 | unsigned long addr; |
| 111 | |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 112 | snd_soc_set_runtime_hwparams(substream, &kirkwood_dma_snd_hw); |
| 113 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 114 | /* Ensure that all constraints linked to dma burst are fulfilled */ |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 115 | err = snd_pcm_hw_constraint_minmax(runtime, |
| 116 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 117 | priv->burst * 2, |
| 118 | KIRKWOOD_AUDIO_BUF_MAX-1); |
| 119 | if (err < 0) |
| 120 | return err; |
| 121 | |
| 122 | err = snd_pcm_hw_constraint_step(runtime, 0, |
| 123 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 124 | priv->burst); |
| 125 | if (err < 0) |
| 126 | return err; |
| 127 | |
| 128 | err = snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 129 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, |
| 130 | priv->burst); |
| 131 | if (err < 0) |
| 132 | return err; |
| 133 | |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 134 | if (!priv->substream_play && !priv->substream_rec) { |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 135 | err = request_irq(priv->irq, kirkwood_dma_irq, IRQF_SHARED, |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 136 | "kirkwood-i2s", priv); |
| 137 | if (err) |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 138 | return -EBUSY; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Enable Error interrupts. We're only ack'ing them but |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 142 | * it's useful for diagnostics |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 143 | */ |
Vladimir Murzin | e2c9917 | 2013-09-29 16:00:13 +0200 | [diff] [blame] | 144 | writel((unsigned int)-1, priv->io + KIRKWOOD_ERR_MASK); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 147 | dram = mv_mbus_dram_info(); |
Russell King | ae6a5d3 | 2012-11-20 12:17:51 +0000 | [diff] [blame] | 148 | addr = substream->dma_buffer.addr; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 149 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 150 | priv->substream_play = substream; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 151 | kirkwood_dma_conf_mbus_windows(priv->io, |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 152 | KIRKWOOD_PLAYBACK_WIN, addr, dram); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 153 | } else { |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 154 | priv->substream_rec = substream; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 155 | kirkwood_dma_conf_mbus_windows(priv->io, |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 156 | KIRKWOOD_RECORD_WIN, addr, dram); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int kirkwood_dma_close(struct snd_pcm_substream *substream) |
| 163 | { |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 164 | struct kirkwood_dma_data *priv = kirkwood_priv(substream); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 165 | |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 166 | if (!priv) |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 167 | return 0; |
| 168 | |
| 169 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 170 | priv->substream_play = NULL; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 171 | else |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 172 | priv->substream_rec = NULL; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 173 | |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 174 | if (!priv->substream_play && !priv->substream_rec) { |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 175 | writel(0, priv->io + KIRKWOOD_ERR_MASK); |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 176 | free_irq(priv->irq, priv); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int kirkwood_dma_hw_params(struct snd_pcm_substream *substream, |
| 183 | struct snd_pcm_hw_params *params) |
| 184 | { |
| 185 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 186 | |
| 187 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 188 | runtime->dma_bytes = params_buffer_bytes(params); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int kirkwood_dma_hw_free(struct snd_pcm_substream *substream) |
| 194 | { |
| 195 | snd_pcm_set_runtime_buffer(substream, NULL); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int kirkwood_dma_prepare(struct snd_pcm_substream *substream) |
| 200 | { |
| 201 | struct snd_pcm_runtime *runtime = substream->runtime; |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 202 | struct kirkwood_dma_data *priv = kirkwood_priv(substream); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 203 | unsigned long size, count; |
| 204 | |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 205 | /* compute buffer size in term of "words" as requested in specs */ |
| 206 | size = frames_to_bytes(runtime, runtime->buffer_size); |
| 207 | size = (size>>2)-1; |
| 208 | count = snd_pcm_lib_period_bytes(substream); |
| 209 | |
| 210 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 211 | writel(count, priv->io + KIRKWOOD_PLAY_BYTE_INT_COUNT); |
| 212 | writel(runtime->dma_addr, priv->io + KIRKWOOD_PLAY_BUF_ADDR); |
| 213 | writel(size, priv->io + KIRKWOOD_PLAY_BUF_SIZE); |
| 214 | } else { |
| 215 | writel(count, priv->io + KIRKWOOD_REC_BYTE_INT_COUNT); |
| 216 | writel(runtime->dma_addr, priv->io + KIRKWOOD_REC_BUF_ADDR); |
| 217 | writel(size, priv->io + KIRKWOOD_REC_BUF_SIZE); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static snd_pcm_uframes_t kirkwood_dma_pointer(struct snd_pcm_substream |
| 225 | *substream) |
| 226 | { |
Russell King | a7d0942 | 2013-08-04 20:22:03 +0100 | [diff] [blame] | 227 | struct kirkwood_dma_data *priv = kirkwood_priv(substream); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 228 | snd_pcm_uframes_t count; |
| 229 | |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 230 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 231 | count = bytes_to_frames(substream->runtime, |
| 232 | readl(priv->io + KIRKWOOD_PLAY_BYTE_COUNT)); |
| 233 | else |
| 234 | count = bytes_to_frames(substream->runtime, |
| 235 | readl(priv->io + KIRKWOOD_REC_BYTE_COUNT)); |
| 236 | |
| 237 | return count; |
| 238 | } |
| 239 | |
Lars-Peter Clausen | 5d0c8a5 | 2013-05-14 22:19:48 +0200 | [diff] [blame] | 240 | static struct snd_pcm_ops kirkwood_dma_ops = { |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 241 | .open = kirkwood_dma_open, |
| 242 | .close = kirkwood_dma_close, |
| 243 | .ioctl = snd_pcm_lib_ioctl, |
| 244 | .hw_params = kirkwood_dma_hw_params, |
| 245 | .hw_free = kirkwood_dma_hw_free, |
| 246 | .prepare = kirkwood_dma_prepare, |
| 247 | .pointer = kirkwood_dma_pointer, |
| 248 | }; |
| 249 | |
| 250 | static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm *pcm, |
| 251 | int stream) |
| 252 | { |
| 253 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 254 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 255 | size_t size = kirkwood_dma_snd_hw.buffer_bytes_max; |
| 256 | |
| 257 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 258 | buf->dev.dev = pcm->card->dev; |
| 259 | buf->area = dma_alloc_coherent(pcm->card->dev, size, |
| 260 | &buf->addr, GFP_KERNEL); |
| 261 | if (!buf->area) |
| 262 | return -ENOMEM; |
| 263 | buf->bytes = size; |
| 264 | buf->private_data = NULL; |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 269 | static int kirkwood_dma_new(struct snd_soc_pcm_runtime *rtd) |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 270 | { |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 271 | struct snd_card *card = rtd->card->snd_card; |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 272 | struct snd_pcm *pcm = rtd->pcm; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 273 | int ret; |
| 274 | |
Russell King | c9bd5e6 | 2013-06-27 12:53:37 +0100 | [diff] [blame] | 275 | ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32)); |
| 276 | if (ret) |
| 277 | return ret; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 278 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 279 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 280 | ret = kirkwood_dma_preallocate_dma_buffer(pcm, |
| 281 | SNDRV_PCM_STREAM_PLAYBACK); |
| 282 | if (ret) |
| 283 | return ret; |
| 284 | } |
| 285 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 286 | if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 287 | ret = kirkwood_dma_preallocate_dma_buffer(pcm, |
| 288 | SNDRV_PCM_STREAM_CAPTURE); |
| 289 | if (ret) |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static void kirkwood_dma_free_dma_buffers(struct snd_pcm *pcm) |
| 297 | { |
| 298 | struct snd_pcm_substream *substream; |
| 299 | struct snd_dma_buffer *buf; |
| 300 | int stream; |
| 301 | |
| 302 | for (stream = 0; stream < 2; stream++) { |
| 303 | substream = pcm->streams[stream].substream; |
| 304 | if (!substream) |
| 305 | continue; |
| 306 | buf = &substream->dma_buffer; |
| 307 | if (!buf->area) |
| 308 | continue; |
| 309 | |
| 310 | dma_free_coherent(pcm->card->dev, buf->bytes, |
| 311 | buf->area, buf->addr); |
| 312 | buf->area = NULL; |
| 313 | } |
| 314 | } |
| 315 | |
Russell King | 64ddf1f | 2013-08-04 20:27:03 +0100 | [diff] [blame] | 316 | struct snd_soc_platform_driver kirkwood_soc_platform = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 317 | .ops = &kirkwood_dma_ops, |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 318 | .pcm_new = kirkwood_dma_new, |
| 319 | .pcm_free = kirkwood_dma_free_dma_buffers, |
| 320 | }; |