mxs-pcm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. SNDRV_PCM_INFO_HALF_DUPLEX,
  44. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  45. SNDRV_PCM_FMTBIT_S20_3LE |
  46. SNDRV_PCM_FMTBIT_S24_LE,
  47. .channels_min = 2,
  48. .channels_max = 2,
  49. .period_bytes_min = 32,
  50. .period_bytes_max = 8192,
  51. .periods_min = 1,
  52. .periods_max = 52,
  53. .buffer_bytes_max = 64 * 1024,
  54. .fifo_size = 32,
  55. };
  56. static bool filter(struct dma_chan *chan, void *param)
  57. {
  58. struct mxs_pcm_dma_params *dma_params = param;
  59. if (!mxs_dma_is_apbx(chan))
  60. return false;
  61. if (chan->chan_id != dma_params->chan_num)
  62. return false;
  63. chan->private = &dma_params->dma_data;
  64. return true;
  65. }
  66. static int snd_mxs_pcm_hw_params(struct snd_pcm_substream *substream,
  67. struct snd_pcm_hw_params *params)
  68. {
  69. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  70. return 0;
  71. }
  72. static int snd_mxs_open(struct snd_pcm_substream *substream)
  73. {
  74. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  75. snd_soc_set_runtime_hwparams(substream, &snd_mxs_hardware);
  76. return snd_dmaengine_pcm_open_request_chan(substream, filter,
  77. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream));
  78. }
  79. static int snd_mxs_pcm_mmap(struct snd_pcm_substream *substream,
  80. struct vm_area_struct *vma)
  81. {
  82. struct snd_pcm_runtime *runtime = substream->runtime;
  83. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  84. runtime->dma_area,
  85. runtime->dma_addr,
  86. runtime->dma_bytes);
  87. }
  88. static struct snd_pcm_ops mxs_pcm_ops = {
  89. .open = snd_mxs_open,
  90. .close = snd_dmaengine_pcm_close_release_chan,
  91. .ioctl = snd_pcm_lib_ioctl,
  92. .hw_params = snd_mxs_pcm_hw_params,
  93. .trigger = snd_dmaengine_pcm_trigger,
  94. .pointer = snd_dmaengine_pcm_pointer_no_residue,
  95. .mmap = snd_mxs_pcm_mmap,
  96. };
  97. static int mxs_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  98. {
  99. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  100. struct snd_dma_buffer *buf = &substream->dma_buffer;
  101. size_t size = snd_mxs_hardware.buffer_bytes_max;
  102. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  103. buf->dev.dev = pcm->card->dev;
  104. buf->private_data = NULL;
  105. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  106. &buf->addr, GFP_KERNEL);
  107. if (!buf->area)
  108. return -ENOMEM;
  109. buf->bytes = size;
  110. return 0;
  111. }
  112. static u64 mxs_pcm_dmamask = DMA_BIT_MASK(32);
  113. static int mxs_pcm_new(struct snd_soc_pcm_runtime *rtd)
  114. {
  115. struct snd_card *card = rtd->card->snd_card;
  116. struct snd_pcm *pcm = rtd->pcm;
  117. int ret = 0;
  118. if (!card->dev->dma_mask)
  119. card->dev->dma_mask = &mxs_pcm_dmamask;
  120. if (!card->dev->coherent_dma_mask)
  121. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  122. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  123. ret = mxs_pcm_preallocate_dma_buffer(pcm,
  124. SNDRV_PCM_STREAM_PLAYBACK);
  125. if (ret)
  126. goto out;
  127. }
  128. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  129. ret = mxs_pcm_preallocate_dma_buffer(pcm,
  130. SNDRV_PCM_STREAM_CAPTURE);
  131. if (ret)
  132. goto out;
  133. }
  134. out:
  135. return ret;
  136. }
  137. static void mxs_pcm_free(struct snd_pcm *pcm)
  138. {
  139. struct snd_pcm_substream *substream;
  140. struct snd_dma_buffer *buf;
  141. int stream;
  142. for (stream = 0; stream < 2; stream++) {
  143. substream = pcm->streams[stream].substream;
  144. if (!substream)
  145. continue;
  146. buf = &substream->dma_buffer;
  147. if (!buf->area)
  148. continue;
  149. dma_free_writecombine(pcm->card->dev, buf->bytes,
  150. buf->area, buf->addr);
  151. buf->area = NULL;
  152. }
  153. }
  154. static struct snd_soc_platform_driver mxs_soc_platform = {
  155. .ops = &mxs_pcm_ops,
  156. .pcm_new = mxs_pcm_new,
  157. .pcm_free = mxs_pcm_free,
  158. };
  159. int mxs_pcm_platform_register(struct device *dev)
  160. {
  161. return snd_soc_register_platform(dev, &mxs_soc_platform);
  162. }
  163. EXPORT_SYMBOL_GPL(mxs_pcm_platform_register);
  164. void mxs_pcm_platform_unregister(struct device *dev)
  165. {
  166. snd_soc_unregister_platform(dev);
  167. }
  168. EXPORT_SYMBOL_GPL(mxs_pcm_platform_unregister);
  169. MODULE_LICENSE("GPL");