1 /* 2 * kirkwood-dma.c 3 * 4 * (c) 2010 Arnaud Patard <apatard@mandriva.com> 5 * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org> 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> 22 #include "kirkwood.h" 23 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) 31 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; 36 }; 37 38 static struct snd_pcm_hardware kirkwood_dma_snd_hw = { 39 .info = (SNDRV_PCM_INFO_INTERLEAVED | 40 SNDRV_PCM_INFO_MMAP | 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, 46 .rate_min = 44100, 47 .rate_max = 96000, 48 .channels_min = 1, 49 .channels_max = 2, 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, 55 .fifo_size = 0, 56 }; 57 58 static u64 kirkwood_dma_dmamask = DMA_BIT_MASK(32); 59 60 static irqreturn_t kirkwood_dma_irq(int irq, void *dev_id) 61 { 62 struct kirkwood_dma_priv *prdata = dev_id; 63 struct kirkwood_dma_data *priv = prdata->data; 64 unsigned long mask, status, cause; 65 66 mask = readl(priv->io + KIRKWOOD_INT_MASK); 67 status = readl(priv->io + KIRKWOOD_INT_CAUSE) & mask; 68 69 cause = readl(priv->io + KIRKWOOD_ERR_CAUSE); 70 if (unlikely(cause)) { 71 printk(KERN_WARNING "%s: got err interrupt 0x%lx\n", 72 __func__, cause); 73 writel(cause, priv->io + KIRKWOOD_ERR_CAUSE); 74 return IRQ_HANDLED; 75 } 76 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", 81 __func__, status); 82 return IRQ_NONE; 83 } 84 85 /* ack int */ 86 writel(status, priv->io + KIRKWOOD_INT_CAUSE); 87 88 if (status & KIRKWOOD_INT_CAUSE_PLAY_BYTES) 89 snd_pcm_period_elapsed(prdata->play_stream); 90 91 if (status & KIRKWOOD_INT_CAUSE_REC_BYTES) 92 snd_pcm_period_elapsed(prdata->rec_stream); 93 94 return IRQ_HANDLED; 95 } 96 97 static void 98 kirkwood_dma_conf_mbus_windows(void __iomem *base, int win, 99 unsigned long dma, 100 const struct mbus_dram_target_info *dram) 101 { 102 int i; 103 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)); 107 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)); 118 } 119 } 120 } 121 122 static int kirkwood_dma_open(struct snd_pcm_substream *substream) 123 { 124 int err; 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; 132 unsigned long addr; 133 134 priv = snd_soc_dai_get_dma_data(cpu_dai, substream); 135 snd_soc_set_runtime_hwparams(substream, &kirkwood_dma_snd_hw); 136 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, 140 priv->burst * 2, 141 KIRKWOOD_AUDIO_BUF_MAX-1); 142 if (err < 0) 143 return err; 144 145 err = snd_pcm_hw_constraint_step(runtime, 0, 146 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 147 priv->burst); 148 if (err < 0) 149 return err; 150 151 err = snd_pcm_hw_constraint_step(substream->runtime, 0, 152 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 153 priv->burst); 154 if (err < 0) 155 return err; 156 157 if (prdata == NULL) { 158 prdata = kzalloc(sizeof(struct kirkwood_dma_priv), GFP_KERNEL); 159 if (prdata == NULL) 160 return -ENOMEM; 161 162 prdata->data = priv; 163 164 err = request_irq(priv->irq, kirkwood_dma_irq, IRQF_SHARED, 165 "kirkwood-i2s", prdata); 166 if (err) { 167 kfree(prdata); 168 return -EBUSY; 169 } 170 171 snd_soc_platform_set_drvdata(platform, prdata); 172 173 /* 174 * Enable Error interrupts. We're only ack'ing them but 175 * it's useful for diagnostics 176 */ 177 writel((unsigned long)-1, priv->io + KIRKWOOD_ERR_MASK); 178 } 179 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); 186 } else { 187 prdata->rec_stream = substream; 188 kirkwood_dma_conf_mbus_windows(priv->io, 189 KIRKWOOD_RECORD_WIN, addr, dram); 190 } 191 192 return 0; 193 } 194 195 static int kirkwood_dma_close(struct snd_pcm_substream *substream) 196 { 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; 202 203 priv = snd_soc_dai_get_dma_data(cpu_dai, substream); 204 205 if (!prdata || !priv) 206 return 0; 207 208 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 209 prdata->play_stream = NULL; 210 else 211 prdata->rec_stream = NULL; 212 213 if (!prdata->play_stream && !prdata->rec_stream) { 214 writel(0, priv->io + KIRKWOOD_ERR_MASK); 215 free_irq(priv->irq, prdata); 216 kfree(prdata); 217 snd_soc_platform_set_drvdata(platform, NULL); 218 } 219 220 return 0; 221 } 222 223 static int kirkwood_dma_hw_params(struct snd_pcm_substream *substream, 224 struct snd_pcm_hw_params *params) 225 { 226 struct snd_pcm_runtime *runtime = substream->runtime; 227 228 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); 229 runtime->dma_bytes = params_buffer_bytes(params); 230 231 return 0; 232 } 233 234 static int kirkwood_dma_hw_free(struct snd_pcm_substream *substream) 235 { 236 snd_pcm_set_runtime_buffer(substream, NULL); 237 return 0; 238 } 239 240 static int kirkwood_dma_prepare(struct snd_pcm_substream *substream) 241 { 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; 247 248 priv = snd_soc_dai_get_dma_data(cpu_dai, substream); 249 250 /* compute buffer size in term of "words" as requested in specs */ 251 size = frames_to_bytes(runtime, runtime->buffer_size); 252 size = (size>>2)-1; 253 count = snd_pcm_lib_period_bytes(substream); 254 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); 259 } else { 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); 263 } 264 265 266 return 0; 267 } 268 269 static snd_pcm_uframes_t kirkwood_dma_pointer(struct snd_pcm_substream 270 *substream) 271 { 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; 276 277 priv = snd_soc_dai_get_dma_data(cpu_dai, substream); 278 279 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 280 count = bytes_to_frames(substream->runtime, 281 readl(priv->io + KIRKWOOD_PLAY_BYTE_COUNT)); 282 else 283 count = bytes_to_frames(substream->runtime, 284 readl(priv->io + KIRKWOOD_REC_BYTE_COUNT)); 285 286 return count; 287 } 288 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, 297 }; 298 299 static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm *pcm, 300 int stream) 301 { 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; 305 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); 310 if (!buf->area) 311 return -ENOMEM; 312 buf->bytes = size; 313 buf->private_data = NULL; 314 315 return 0; 316 } 317 318 static int kirkwood_dma_new(struct snd_soc_pcm_runtime *rtd) 319 { 320 struct snd_card *card = rtd->card->snd_card; 321 struct snd_pcm *pcm = rtd->pcm; 322 int ret; 323 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 = DMA_BIT_MASK(32); 328 329 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { 330 ret = kirkwood_dma_preallocate_dma_buffer(pcm, 331 SNDRV_PCM_STREAM_PLAYBACK); 332 if (ret) 333 return ret; 334 } 335 336 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { 337 ret = kirkwood_dma_preallocate_dma_buffer(pcm, 338 SNDRV_PCM_STREAM_CAPTURE); 339 if (ret) 340 return ret; 341 } 342 343 return 0; 344 } 345 346 static void kirkwood_dma_free_dma_buffers(struct snd_pcm *pcm) 347 { 348 struct snd_pcm_substream *substream; 349 struct snd_dma_buffer *buf; 350 int stream; 351 352 for (stream = 0; stream < 2; stream++) { 353 substream = pcm->streams[stream].substream; 354 if (!substream) 355 continue; 356 buf = &substream->dma_buffer; 357 if (!buf->area) 358 continue; 359 360 dma_free_coherent(pcm->card->dev, buf->bytes, 361 buf->area, buf->addr); 362 buf->area = NULL; 363 } 364 } 365 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, 370 }; 371 372 static int __devinit kirkwood_soc_platform_probe(struct platform_device *pdev) 373 { 374 return snd_soc_register_platform(&pdev->dev, &kirkwood_soc_platform); 375 } 376 377 static int __devexit kirkwood_soc_platform_remove(struct platform_device *pdev) 378 { 379 snd_soc_unregister_platform(&pdev->dev); 380 return 0; 381 } 382 383 static struct platform_driver kirkwood_pcm_driver = { 384 .driver = { 385 .name = "kirkwood-pcm-audio", 386 .owner = THIS_MODULE, 387 }, 388 389 .probe = kirkwood_soc_platform_probe, 390 .remove = __devexit_p(kirkwood_soc_platform_remove), 391 }; 392 393 module_platform_driver(kirkwood_pcm_driver); 394 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"); 399