mxs-pcm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
  3. *
  4. * Based on sound/soc/imx/imx-pcm-dma-mx2.c
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/dmaengine.h>
  30. #include <sound/core.h>
  31. #include <sound/initval.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/soc.h>
  35. #include <sound/dmaengine_pcm.h>
  36. #include "mxs-pcm.h"
  37. static struct snd_pcm_hardware snd_mxs_hardware = {
  38. .info = SNDRV_PCM_INFO_MMAP |
  39. SNDRV_PCM_INFO_MMAP_VALID |
  40. SNDRV_PCM_INFO_PAUSE |
  41. SNDRV_PCM_INFO_RESUME |
  42. SNDRV_PCM_INFO_INTERLEAVED,
  43. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  44. SNDRV_PCM_FMTBIT_S20_3LE |
  45. SNDRV_PCM_FMTBIT_S24_LE,
  46. .channels_min = 2,
  47. .channels_max = 2,
  48. .period_bytes_min = 32,
  49. .period_bytes_max = 8192,
  50. .periods_min = 1,
  51. .periods_max = 52,
  52. .buffer_bytes_max = 64 * 1024,
  53. .fifo_size = 32,
  54. };
  55. static bool filter(struct dma_chan *chan, void *param)
  56. {
  57. struct mxs_pcm_dma_params *dma_params = param;
  58. if (!mxs_dma_is_apbx(chan))
  59. return false;
  60. if (chan->chan_id != dma_params->chan_num)
  61. return false;
  62. chan->private = &dma_params->dma_data;
  63. return true;
  64. }
  65. static int snd_mxs_pcm_hw_params(struct snd_pcm_substream *substream,
  66. struct snd_pcm_hw_params *params)
  67. {
  68. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  69. return 0;
  70. }
  71. static int snd_mxs_open(struct snd_pcm_substream *substream)
  72. {
  73. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  74. snd_soc_set_runtime_hwparams(substream, &snd_mxs_hardware);
  75. return snd_dmaengine_pcm_open(substream, filter,
  76. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream));
  77. }
  78. static int snd_mxs_pcm_mmap(struct snd_pcm_substream *substream,
  79. struct vm_area_struct *vma)
  80. {
  81. struct snd_pcm_runtime *runtime = substream->runtime;
  82. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  83. runtime->dma_area,
  84. runtime->dma_addr,
  85. runtime->dma_bytes);
  86. }
  87. static struct snd_pcm_ops mxs_pcm_ops = {
  88. .open = snd_mxs_open,
  89. .close = snd_dmaengine_pcm_close,
  90. .ioctl = snd_pcm_lib_ioctl,
  91. .hw_params = snd_mxs_pcm_hw_params,
  92. .trigger = snd_dmaengine_pcm_trigger,
  93. .pointer = snd_dmaengine_pcm_pointer_no_residue,
  94. .mmap = snd_mxs_pcm_mmap,
  95. };
  96. static int mxs_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  97. {
  98. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  99. struct snd_dma_buffer *buf = &substream->dma_buffer;
  100. size_t size = snd_mxs_hardware.buffer_bytes_max;
  101. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  102. buf->dev.dev = pcm->card->dev;
  103. buf->private_data = NULL;
  104. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  105. &buf->addr, GFP_KERNEL);
  106. if (!buf->area)
  107. return -ENOMEM;
  108. buf->bytes = size;
  109. return 0;
  110. }
  111. static u64 mxs_pcm_dmamask = DMA_BIT_MASK(32);
  112. static int mxs_pcm_new(struct snd_soc_pcm_runtime *rtd)
  113. {
  114. struct snd_card *card = rtd->card->snd_card;
  115. struct snd_pcm *pcm = rtd->pcm;
  116. int ret = 0;
  117. if (!card->dev->dma_mask)
  118. card->dev->dma_mask = &mxs_pcm_dmamask;
  119. if (!card->dev->coherent_dma_mask)
  120. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  121. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  122. ret = mxs_pcm_preallocate_dma_buffer(pcm,
  123. SNDRV_PCM_STREAM_PLAYBACK);
  124. if (ret)
  125. goto out;
  126. }
  127. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  128. ret = mxs_pcm_preallocate_dma_buffer(pcm,
  129. SNDRV_PCM_STREAM_CAPTURE);
  130. if (ret)
  131. goto out;
  132. }
  133. out:
  134. return ret;
  135. }
  136. static void mxs_pcm_free(struct snd_pcm *pcm)
  137. {
  138. struct snd_pcm_substream *substream;
  139. struct snd_dma_buffer *buf;
  140. int stream;
  141. for (stream = 0; stream < 2; stream++) {
  142. substream = pcm->streams[stream].substream;
  143. if (!substream)
  144. continue;
  145. buf = &substream->dma_buffer;
  146. if (!buf->area)
  147. continue;
  148. dma_free_writecombine(pcm->card->dev, buf->bytes,
  149. buf->area, buf->addr);
  150. buf->area = NULL;
  151. }
  152. }
  153. static struct snd_soc_platform_driver mxs_soc_platform = {
  154. .ops = &mxs_pcm_ops,
  155. .pcm_new = mxs_pcm_new,
  156. .pcm_free = mxs_pcm_free,
  157. };
  158. int mxs_pcm_platform_register(struct device *dev)
  159. {
  160. return snd_soc_register_platform(dev, &mxs_soc_platform);
  161. }
  162. EXPORT_SYMBOL_GPL(mxs_pcm_platform_register);
  163. void mxs_pcm_platform_unregister(struct device *dev)
  164. {
  165. snd_soc_unregister_platform(dev);
  166. }
  167. EXPORT_SYMBOL_GPL(mxs_pcm_platform_unregister);
  168. MODULE_LICENSE("GPL");