atmel-pcm-dma.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * atmel-pcm-dma.c -- ALSA PCM DMA support for the Atmel SoC.
  3. *
  4. * Copyright (C) 2012 Atmel
  5. *
  6. * Author: Bo Shen <voice.shen@atmel.com>
  7. *
  8. * Based on atmel-pcm by:
  9. * Sedji Gaouaou <sedji.gaouaou@atmel.com>
  10. * Copyright 2008 Atmel
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/slab.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/dmaengine.h>
  32. #include <linux/atmel-ssc.h>
  33. #include <linux/platform_data/dma-atmel.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/soc.h>
  38. #include <sound/dmaengine_pcm.h>
  39. #include "atmel-pcm.h"
  40. /*--------------------------------------------------------------------------*\
  41. * Hardware definition
  42. \*--------------------------------------------------------------------------*/
  43. static const struct snd_pcm_hardware atmel_pcm_dma_hardware = {
  44. .info = SNDRV_PCM_INFO_MMAP |
  45. SNDRV_PCM_INFO_MMAP_VALID |
  46. SNDRV_PCM_INFO_INTERLEAVED |
  47. SNDRV_PCM_INFO_RESUME |
  48. SNDRV_PCM_INFO_PAUSE,
  49. .period_bytes_min = 256, /* lighting DMA overhead */
  50. .period_bytes_max = 2 * 0xffff, /* if 2 bytes format */
  51. .periods_min = 8,
  52. .periods_max = 1024, /* no limit */
  53. .buffer_bytes_max = 512 * 1024,
  54. };
  55. /**
  56. * atmel_pcm_dma_irq: SSC interrupt handler for DMAENGINE enabled SSC
  57. *
  58. * We use DMAENGINE to send/receive data to/from SSC so this ISR is only to
  59. * check if any overrun occured.
  60. */
  61. static void atmel_pcm_dma_irq(u32 ssc_sr,
  62. struct snd_pcm_substream *substream)
  63. {
  64. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  65. struct atmel_pcm_dma_params *prtd;
  66. prtd = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  67. if (ssc_sr & prtd->mask->ssc_error) {
  68. if (snd_pcm_running(substream))
  69. pr_warn("atmel-pcm: buffer %s on %s (SSC_SR=%#x)\n",
  70. substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  71. ? "underrun" : "overrun", prtd->name,
  72. ssc_sr);
  73. /* stop RX and capture: will be enabled again at restart */
  74. ssc_writex(prtd->ssc->regs, SSC_CR, prtd->mask->ssc_disable);
  75. snd_pcm_stop_xrun(substream);
  76. /* now drain RHR and read status to remove xrun condition */
  77. ssc_readx(prtd->ssc->regs, SSC_RHR);
  78. ssc_readx(prtd->ssc->regs, SSC_SR);
  79. }
  80. }
  81. static int atmel_pcm_configure_dma(struct snd_pcm_substream *substream,
  82. struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
  83. {
  84. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  85. struct atmel_pcm_dma_params *prtd;
  86. struct ssc_device *ssc;
  87. int ret;
  88. prtd = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  89. ssc = prtd->ssc;
  90. ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
  91. if (ret) {
  92. pr_err("atmel-pcm: hwparams to dma slave configure failed\n");
  93. return ret;
  94. }
  95. slave_config->dst_addr = ssc->phybase + SSC_THR;
  96. slave_config->dst_maxburst = 1;
  97. slave_config->src_addr = ssc->phybase + SSC_RHR;
  98. slave_config->src_maxburst = 1;
  99. prtd->dma_intr_handler = atmel_pcm_dma_irq;
  100. return 0;
  101. }
  102. static const struct snd_dmaengine_pcm_config atmel_dmaengine_pcm_config = {
  103. .prepare_slave_config = atmel_pcm_configure_dma,
  104. .pcm_hardware = &atmel_pcm_dma_hardware,
  105. .prealloc_buffer_size = 64 * 1024,
  106. };
  107. int atmel_pcm_dma_platform_register(struct device *dev)
  108. {
  109. return snd_dmaengine_pcm_register(dev, &atmel_dmaengine_pcm_config, 0);
  110. }
  111. EXPORT_SYMBOL(atmel_pcm_dma_platform_register);
  112. void atmel_pcm_dma_platform_unregister(struct device *dev)
  113. {
  114. snd_dmaengine_pcm_unregister(dev);
  115. }
  116. EXPORT_SYMBOL(atmel_pcm_dma_platform_unregister);
  117. MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
  118. MODULE_DESCRIPTION("Atmel DMA based PCM module");
  119. MODULE_LICENSE("GPL");