1/* 2 * ALSA PCM interface for the Stetch s6000 family 3 * 4 * Author: Daniel Gloeckner, <dg@emlix.com> 5 * Copyright: (C) 2009 emlix GmbH <info@emlix.com> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 12#include <linux/module.h> 13#include <linux/init.h> 14#include <linux/platform_device.h> 15#include <linux/slab.h> 16#include <linux/dma-mapping.h> 17#include <linux/interrupt.h> 18 19#include <sound/core.h> 20#include <sound/pcm.h> 21#include <sound/pcm_params.h> 22#include <sound/soc.h> 23 24#include <asm/dma.h> 25#include <variant/dmac.h> 26 27#include "s6000-pcm.h" 28 29#define S6_PCM_PREALLOCATE_SIZE (96 * 1024) 30#define S6_PCM_PREALLOCATE_MAX (2048 * 1024) 31 32static struct snd_pcm_hardware s6000_pcm_hardware = { 33 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | 34 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | 35 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_JOINT_DUPLEX), 36 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE), 37 .rates = (SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_5512 | \ 38 SNDRV_PCM_RATE_8000_192000), 39 .rate_min = 0, 40 .rate_max = 1562500, 41 .channels_min = 2, 42 .channels_max = 8, 43 .buffer_bytes_max = 0x7ffffff0, 44 .period_bytes_min = 16, 45 .period_bytes_max = 0xfffff0, 46 .periods_min = 2, 47 .periods_max = 1024, /* no limit */ 48 .fifo_size = 0, 49}; 50 51struct s6000_runtime_data { 52 spinlock_t lock; 53 int period; /* current DMA period */ 54}; 55 56static void s6000_pcm_enqueue_dma(struct snd_pcm_substream *substream) 57{ 58 struct snd_pcm_runtime *runtime = substream->runtime; 59 struct s6000_runtime_data *prtd = runtime->private_data; 60 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; 61 struct s6000_pcm_dma_params *par; 62 int channel; 63 unsigned int period_size; 64 unsigned int dma_offset; 65 dma_addr_t dma_pos; 66 dma_addr_t src, dst; 67 68 par = snd_soc_dai_get_dma_data(soc_runtime->dai->cpu_dai, substream); 69 70 period_size = snd_pcm_lib_period_bytes(substream); 71 dma_offset = prtd->period * period_size; 72 dma_pos = runtime->dma_addr + dma_offset; 73 74 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 75 src = dma_pos; 76 dst = par->sif_out; 77 channel = par->dma_out; 78 } else { 79 src = par->sif_in; 80 dst = dma_pos; 81 channel = par->dma_in; 82 } 83 84 if (!s6dmac_channel_enabled(DMA_MASK_DMAC(channel), 85 DMA_INDEX_CHNL(channel))) 86 return; 87 88 if (s6dmac_fifo_full(DMA_MASK_DMAC(channel), DMA_INDEX_CHNL(channel))) { 89 printk(KERN_ERR "s6000-pcm: fifo full\n"); 90 return; 91 } 92 93 BUG_ON(period_size & 15); 94 s6dmac_put_fifo(DMA_MASK_DMAC(channel), DMA_INDEX_CHNL(channel), 95 src, dst, period_size); 96 97 prtd->period++; 98 if (unlikely(prtd->period >= runtime->periods)) 99 prtd->period = 0; 100} 101 102static irqreturn_t s6000_pcm_irq(int irq, void *data) 103{ 104 struct snd_pcm *pcm = data; 105 struct snd_soc_pcm_runtime *runtime = pcm->private_data; 106 struct s6000_pcm_dma_params *params = 107 snd_soc_dai_get_dma_data(soc_runtime->dai->cpu_dai, substream); 108 struct s6000_runtime_data *prtd; 109 unsigned int has_xrun; 110 int i, ret = IRQ_NONE; 111 u32 channel[2] = { 112 [SNDRV_PCM_STREAM_PLAYBACK] = params->dma_out, 113 [SNDRV_PCM_STREAM_CAPTURE] = params->dma_in 114 }; 115 116 has_xrun = params->check_xrun(runtime->dai->cpu_dai); 117 118 for (i = 0; i < ARRAY_SIZE(channel); ++i) { 119 struct snd_pcm_substream *substream = pcm->streams[i].substream; 120 unsigned int pending; 121 122 if (!channel[i]) 123 continue; 124 125 if (unlikely(has_xrun & (1 << i)) && 126 substream->runtime && 127 snd_pcm_running(substream)) { 128 dev_dbg(pcm->dev, "xrun\n"); 129 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); 130 ret = IRQ_HANDLED; 131 } 132 133 pending = s6dmac_int_sources(DMA_MASK_DMAC(channel[i]), 134 DMA_INDEX_CHNL(channel[i])); 135 136 if (pending & 1) { 137 ret = IRQ_HANDLED; 138 if (likely(substream->runtime && 139 snd_pcm_running(substream))) { 140 snd_pcm_period_elapsed(substream); 141 dev_dbg(pcm->dev, "period elapsed %x %x\n", 142 s6dmac_cur_src(DMA_MASK_DMAC(channel[i]), 143 DMA_INDEX_CHNL(channel[i])), 144 s6dmac_cur_dst(DMA_MASK_DMAC(channel[i]), 145 DMA_INDEX_CHNL(channel[i]))); 146 prtd = substream->runtime->private_data; 147 spin_lock(&prtd->lock); 148 s6000_pcm_enqueue_dma(substream); 149 spin_unlock(&prtd->lock); 150 } 151 } 152 153 if (unlikely(pending & ~7)) { 154 if (pending & (1 << 3)) 155 printk(KERN_WARNING 156 "s6000-pcm: DMA %x Underflow\n", 157 channel[i]); 158 if (pending & (1 << 4)) 159 printk(KERN_WARNING 160 "s6000-pcm: DMA %x Overflow\n", 161 channel[i]); 162 if (pending & 0x1e0) 163 printk(KERN_WARNING 164 "s6000-pcm: DMA %x Master Error " 165 "(mask %x)\n", 166 channel[i], pending >> 5); 167 168 } 169 } 170 171 return ret; 172} 173 174static int s6000_pcm_start(struct snd_pcm_substream *substream) 175{ 176 struct s6000_runtime_data *prtd = substream->runtime->private_data; 177 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; 178 struct s6000_pcm_dma_params *par; 179 unsigned long flags; 180 int srcinc; 181 u32 dma; 182 183 par = snd_soc_dai_get_dma_data(soc_runtime->dai->cpu_dai, substream); 184 185 spin_lock_irqsave(&prtd->lock, flags); 186 187 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 188 srcinc = 1; 189 dma = par->dma_out; 190 } else { 191 srcinc = 0; 192 dma = par->dma_in; 193 } 194 s6dmac_enable_chan(DMA_MASK_DMAC(dma), DMA_INDEX_CHNL(dma), 195 1 /* priority 1 (0 is max) */, 196 0 /* peripheral requests w/o xfer length mode */, 197 srcinc /* source address increment */, 198 srcinc^1 /* destination address increment */, 199 0 /* chunksize 0 (skip impossible on this dma) */, 200 0 /* source skip after chunk (impossible) */, 201 0 /* destination skip after chunk (impossible) */, 202 4 /* 16 byte burst size */, 203 -1 /* don't conserve bandwidth */, 204 0 /* low watermark irq descriptor threshold */, 205 0 /* disable hardware timestamps */, 206 1 /* enable channel */); 207 208 s6000_pcm_enqueue_dma(substream); 209 s6000_pcm_enqueue_dma(substream); 210 211 spin_unlock_irqrestore(&prtd->lock, flags); 212 213 return 0; 214} 215 216static int s6000_pcm_stop(struct snd_pcm_substream *substream) 217{ 218 struct s6000_runtime_data *prtd = substream->runtime->private_data; 219 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; 220 struct s6000_pcm_dma_params *par; 221 unsigned long flags; 222 u32 channel; 223 224 par = snd_soc_dai_get_dma_data(soc_runtime->dai->cpu_dai, substream); 225 226 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 227 channel = par->dma_out; 228 else 229 channel = par->dma_in; 230 231 s6dmac_set_terminal_count(DMA_MASK_DMAC(channel), 232 DMA_INDEX_CHNL(channel), 0); 233 234 spin_lock_irqsave(&prtd->lock, flags); 235 236 s6dmac_disable_chan(DMA_MASK_DMAC(channel), DMA_INDEX_CHNL(channel)); 237 238 spin_unlock_irqrestore(&prtd->lock, flags); 239 240 return 0; 241} 242 243static int s6000_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 244{ 245 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; 246 struct s6000_pcm_dma_params *par; 247 int ret; 248 249 par = snd_soc_dai_get_dma_data(soc_runtime->dai->cpu_dai, substream); 250 251 ret = par->trigger(substream, cmd, 0); 252 if (ret < 0) 253 return ret; 254 255 switch (cmd) { 256 case SNDRV_PCM_TRIGGER_START: 257 case SNDRV_PCM_TRIGGER_RESUME: 258 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 259 ret = s6000_pcm_start(substream); 260 break; 261 case SNDRV_PCM_TRIGGER_STOP: 262 case SNDRV_PCM_TRIGGER_SUSPEND: 263 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 264 ret = s6000_pcm_stop(substream); 265 break; 266 default: 267 ret = -EINVAL; 268 } 269 if (ret < 0) 270 return ret; 271 272 return par->trigger(substream, cmd, 1); 273} 274 275static int s6000_pcm_prepare(struct snd_pcm_substream *substream) 276{ 277 struct s6000_runtime_data *prtd = substream->runtime->private_data; 278 279 prtd->period = 0; 280 281 return 0; 282} 283 284static snd_pcm_uframes_t s6000_pcm_pointer(struct snd_pcm_substream *substream) 285{ 286 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; 287 struct s6000_pcm_dma_params *par; 288 struct snd_pcm_runtime *runtime = substream->runtime; 289 struct s6000_runtime_data *prtd = runtime->private_data; 290 unsigned long flags; 291 unsigned int offset; 292 dma_addr_t count; 293 294 par = snd_soc_dai_get_dma_data(soc_runtime->dai->cpu_dai, substream); 295 296 spin_lock_irqsave(&prtd->lock, flags); 297 298 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 299 count = s6dmac_cur_src(DMA_MASK_DMAC(par->dma_out), 300 DMA_INDEX_CHNL(par->dma_out)); 301 else 302 count = s6dmac_cur_dst(DMA_MASK_DMAC(par->dma_in), 303 DMA_INDEX_CHNL(par->dma_in)); 304 305 count -= runtime->dma_addr; 306 307 spin_unlock_irqrestore(&prtd->lock, flags); 308 309 offset = bytes_to_frames(runtime, count); 310 if (unlikely(offset >= runtime->buffer_size)) 311 offset = 0; 312 313 return offset; 314} 315 316static int s6000_pcm_open(struct snd_pcm_substream *substream) 317{ 318 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; 319 struct s6000_pcm_dma_params *par; 320 struct snd_pcm_runtime *runtime = substream->runtime; 321 struct s6000_runtime_data *prtd; 322 int ret; 323 324 par = snd_soc_dai_get_dma_data(soc_runtime->dai->cpu_dai, substream); 325 snd_soc_set_runtime_hwparams(substream, &s6000_pcm_hardware); 326 327 ret = snd_pcm_hw_constraint_step(runtime, 0, 328 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 16); 329 if (ret < 0) 330 return ret; 331 ret = snd_pcm_hw_constraint_step(runtime, 0, 332 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 16); 333 if (ret < 0) 334 return ret; 335 ret = snd_pcm_hw_constraint_integer(runtime, 336 SNDRV_PCM_HW_PARAM_PERIODS); 337 if (ret < 0) 338 return ret; 339 340 if (par->same_rate) { 341 int rate; 342 spin_lock(&par->lock); /* needed? */ 343 rate = par->rate; 344 spin_unlock(&par->lock); 345 if (rate != -1) { 346 ret = snd_pcm_hw_constraint_minmax(runtime, 347 SNDRV_PCM_HW_PARAM_RATE, 348 rate, rate); 349 if (ret < 0) 350 return ret; 351 } 352 } 353 354 prtd = kzalloc(sizeof(struct s6000_runtime_data), GFP_KERNEL); 355 if (prtd == NULL) 356 return -ENOMEM; 357 358 spin_lock_init(&prtd->lock); 359 360 runtime->private_data = prtd; 361 362 return 0; 363} 364 365static int s6000_pcm_close(struct snd_pcm_substream *substream) 366{ 367 struct snd_pcm_runtime *runtime = substream->runtime; 368 struct s6000_runtime_data *prtd = runtime->private_data; 369 370 kfree(prtd); 371 372 return 0; 373} 374 375static int s6000_pcm_hw_params(struct snd_pcm_substream *substream, 376 struct snd_pcm_hw_params *hw_params) 377{ 378 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; 379 struct s6000_pcm_dma_params *par; 380 int ret; 381 ret = snd_pcm_lib_malloc_pages(substream, 382 params_buffer_bytes(hw_params)); 383 if (ret < 0) { 384 printk(KERN_WARNING "s6000-pcm: allocation of memory failed\n"); 385 return ret; 386 } 387 388 par = snd_soc_dai_get_dma_data(soc_runtime->dai->cpu_dai, substream); 389 390 if (par->same_rate) { 391 spin_lock(&par->lock); 392 if (par->rate == -1 || 393 !(par->in_use & ~(1 << substream->stream))) { 394 par->rate = params_rate(hw_params); 395 par->in_use |= 1 << substream->stream; 396 } else if (params_rate(hw_params) != par->rate) { 397 snd_pcm_lib_free_pages(substream); 398 par->in_use &= ~(1 << substream->stream); 399 ret = -EBUSY; 400 } 401 spin_unlock(&par->lock); 402 } 403 return ret; 404} 405 406static int s6000_pcm_hw_free(struct snd_pcm_substream *substream) 407{ 408 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data; 409 struct s6000_pcm_dma_params *par = 410 snd_soc_dai_get_dma_data(soc_runtime->dai->cpu_dai, substream); 411 412 spin_lock(&par->lock); 413 par->in_use &= ~(1 << substream->stream); 414 if (!par->in_use) 415 par->rate = -1; 416 spin_unlock(&par->lock); 417 418 return snd_pcm_lib_free_pages(substream); 419} 420 421static struct snd_pcm_ops s6000_pcm_ops = { 422 .open = s6000_pcm_open, 423 .close = s6000_pcm_close, 424 .ioctl = snd_pcm_lib_ioctl, 425 .hw_params = s6000_pcm_hw_params, 426 .hw_free = s6000_pcm_hw_free, 427 .trigger = s6000_pcm_trigger, 428 .prepare = s6000_pcm_prepare, 429 .pointer = s6000_pcm_pointer, 430}; 431 432static void s6000_pcm_free(struct snd_pcm *pcm) 433{ 434 struct snd_soc_pcm_runtime *runtime = pcm->private_data; 435 struct s6000_pcm_dma_params *params = 436 snd_soc_dai_get_dma_data(soc_runtime->dai->cpu_dai, substream); 437 438 free_irq(params->irq, pcm); 439 snd_pcm_lib_preallocate_free_for_all(pcm); 440} 441 442static u64 s6000_pcm_dmamask = DMA_BIT_MASK(32); 443 444static int s6000_pcm_new(struct snd_card *card, 445 struct snd_soc_dai *dai, struct snd_pcm *pcm) 446{ 447 struct snd_soc_pcm_runtime *runtime = pcm->private_data; 448 struct s6000_pcm_dma_params *params; 449 int res; 450 451 params = snd_soc_dai_get_dma_data(soc_runtime->dai->cpu_dai, substream); 452 453 if (!card->dev->dma_mask) 454 card->dev->dma_mask = &s6000_pcm_dmamask; 455 if (!card->dev->coherent_dma_mask) 456 card->dev->coherent_dma_mask = DMA_BIT_MASK(32); 457 458 if (params->dma_in) { 459 s6dmac_disable_chan(DMA_MASK_DMAC(params->dma_in), 460 DMA_INDEX_CHNL(params->dma_in)); 461 s6dmac_int_sources(DMA_MASK_DMAC(params->dma_in), 462 DMA_INDEX_CHNL(params->dma_in)); 463 } 464 465 if (params->dma_out) { 466 s6dmac_disable_chan(DMA_MASK_DMAC(params->dma_out), 467 DMA_INDEX_CHNL(params->dma_out)); 468 s6dmac_int_sources(DMA_MASK_DMAC(params->dma_out), 469 DMA_INDEX_CHNL(params->dma_out)); 470 } 471 472 res = request_irq(params->irq, s6000_pcm_irq, IRQF_SHARED, 473 s6000_soc_platform.name, pcm); 474 if (res) { 475 printk(KERN_ERR "s6000-pcm couldn't get IRQ\n"); 476 return res; 477 } 478 479 res = snd_pcm_lib_preallocate_pages_for_all(pcm, 480 SNDRV_DMA_TYPE_DEV, 481 card->dev, 482 S6_PCM_PREALLOCATE_SIZE, 483 S6_PCM_PREALLOCATE_MAX); 484 if (res) 485 printk(KERN_WARNING "s6000-pcm: preallocation failed\n"); 486 487 spin_lock_init(¶ms->lock); 488 params->in_use = 0; 489 params->rate = -1; 490 return 0; 491} 492 493struct snd_soc_platform s6000_soc_platform = { 494 .name = "s6000-audio", 495 .pcm_ops = &s6000_pcm_ops, 496 .pcm_new = s6000_pcm_new, 497 .pcm_free = s6000_pcm_free, 498}; 499EXPORT_SYMBOL_GPL(s6000_soc_platform); 500 501static int __init s6000_pcm_init(void) 502{ 503 return snd_soc_register_platform(&s6000_soc_platform); 504} 505module_init(s6000_pcm_init); 506 507static void __exit s6000_pcm_exit(void) 508{ 509 snd_soc_unregister_platform(&s6000_soc_platform); 510} 511module_exit(s6000_pcm_exit); 512 513MODULE_AUTHOR("Daniel Gloeckner"); 514MODULE_DESCRIPTION("Stretch s6000 family PCM DMA module"); 515MODULE_LICENSE("GPL"); 516