atmel-pcm-dma.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  50. .period_bytes_min = 256, /* lighting DMA overhead */
  51. .period_bytes_max = 2 * 0xffff, /* if 2 bytes format */
  52. .periods_min = 8,
  53. .periods_max = 1024, /* no limit */
  54. .buffer_bytes_max = ATMEL_SSC_DMABUF_SIZE,
  55. };
  56. /**
  57. * atmel_pcm_dma_irq: SSC interrupt handler for DMAENGINE enabled SSC
  58. *
  59. * We use DMAENGINE to send/receive data to/from SSC so this ISR is only to
  60. * check if any overrun occured.
  61. */
  62. static void atmel_pcm_dma_irq(u32 ssc_sr,
  63. struct snd_pcm_substream *substream)
  64. {
  65. struct atmel_pcm_dma_params *prtd;
  66. prtd = snd_dmaengine_pcm_get_data(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(substream, SNDRV_PCM_STATE_XRUN);
  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. /*--------------------------------------------------------------------------*\
  82. * DMAENGINE operations
  83. \*--------------------------------------------------------------------------*/
  84. static bool filter(struct dma_chan *chan, void *slave)
  85. {
  86. struct at_dma_slave *sl = slave;
  87. if (sl->dma_dev == chan->device->dev) {
  88. chan->private = sl;
  89. return true;
  90. } else {
  91. return false;
  92. }
  93. }
  94. static int atmel_pcm_configure_dma(struct snd_pcm_substream *substream,
  95. struct snd_pcm_hw_params *params)
  96. {
  97. struct atmel_pcm_dma_params *prtd;
  98. struct ssc_device *ssc;
  99. struct dma_chan *dma_chan;
  100. struct dma_slave_config slave_config;
  101. int ret;
  102. prtd = snd_dmaengine_pcm_get_data(substream);
  103. ssc = prtd->ssc;
  104. ret = snd_hwparams_to_dma_slave_config(substream, params,
  105. &slave_config);
  106. if (ret) {
  107. pr_err("atmel-pcm: hwparams to dma slave configure failed\n");
  108. return ret;
  109. }
  110. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  111. slave_config.dst_addr = (dma_addr_t)ssc->phybase + SSC_THR;
  112. slave_config.dst_maxburst = 1;
  113. } else {
  114. slave_config.src_addr = (dma_addr_t)ssc->phybase + SSC_RHR;
  115. slave_config.src_maxburst = 1;
  116. }
  117. slave_config.device_fc = false;
  118. dma_chan = snd_dmaengine_pcm_get_chan(substream);
  119. if (dmaengine_slave_config(dma_chan, &slave_config)) {
  120. pr_err("atmel-pcm: failed to configure dma channel\n");
  121. ret = -EBUSY;
  122. return ret;
  123. }
  124. return 0;
  125. }
  126. static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
  127. struct snd_pcm_hw_params *params)
  128. {
  129. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  130. struct atmel_pcm_dma_params *prtd;
  131. struct ssc_device *ssc;
  132. struct at_dma_slave *sdata = NULL;
  133. int ret;
  134. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  135. prtd = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  136. ssc = prtd->ssc;
  137. if (ssc->pdev)
  138. sdata = ssc->pdev->dev.platform_data;
  139. ret = snd_dmaengine_pcm_open(substream, filter, sdata);
  140. if (ret) {
  141. pr_err("atmel-pcm: dmaengine pcm open failed\n");
  142. return -EINVAL;
  143. }
  144. snd_dmaengine_pcm_set_data(substream, prtd);
  145. ret = atmel_pcm_configure_dma(substream, params);
  146. if (ret) {
  147. pr_err("atmel-pcm: failed to configure dmai\n");
  148. goto err;
  149. }
  150. prtd->dma_intr_handler = atmel_pcm_dma_irq;
  151. return 0;
  152. err:
  153. snd_dmaengine_pcm_close(substream);
  154. return ret;
  155. }
  156. static int atmel_pcm_dma_prepare(struct snd_pcm_substream *substream)
  157. {
  158. struct atmel_pcm_dma_params *prtd;
  159. prtd = snd_dmaengine_pcm_get_data(substream);
  160. ssc_writex(prtd->ssc->regs, SSC_IER, prtd->mask->ssc_error);
  161. ssc_writex(prtd->ssc->regs, SSC_CR, prtd->mask->ssc_enable);
  162. return 0;
  163. }
  164. static int atmel_pcm_open(struct snd_pcm_substream *substream)
  165. {
  166. snd_soc_set_runtime_hwparams(substream, &atmel_pcm_dma_hardware);
  167. return 0;
  168. }
  169. static int atmel_pcm_close(struct snd_pcm_substream *substream)
  170. {
  171. snd_dmaengine_pcm_close(substream);
  172. return 0;
  173. }
  174. static struct snd_pcm_ops atmel_pcm_ops = {
  175. .open = atmel_pcm_open,
  176. .close = atmel_pcm_close,
  177. .ioctl = snd_pcm_lib_ioctl,
  178. .hw_params = atmel_pcm_hw_params,
  179. .prepare = atmel_pcm_dma_prepare,
  180. .trigger = snd_dmaengine_pcm_trigger,
  181. .pointer = snd_dmaengine_pcm_pointer_no_residue,
  182. .mmap = atmel_pcm_mmap,
  183. };
  184. static struct snd_soc_platform_driver atmel_soc_platform = {
  185. .ops = &atmel_pcm_ops,
  186. .pcm_new = atmel_pcm_new,
  187. .pcm_free = atmel_pcm_free,
  188. };
  189. int atmel_pcm_dma_platform_register(struct device *dev)
  190. {
  191. return snd_soc_register_platform(dev, &atmel_soc_platform);
  192. }
  193. EXPORT_SYMBOL(atmel_pcm_dma_platform_register);
  194. void atmel_pcm_dma_platform_unregister(struct device *dev)
  195. {
  196. snd_soc_unregister_platform(dev);
  197. }
  198. EXPORT_SYMBOL(atmel_pcm_dma_platform_unregister);
  199. MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
  200. MODULE_DESCRIPTION("Atmel DMA based PCM module");
  201. MODULE_LICENSE("GPL");