mxs-pcm.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <linux/fsl/mxs-dma.h>
  31. #include <sound/core.h>
  32. #include <sound/initval.h>
  33. #include <sound/pcm.h>
  34. #include <sound/pcm_params.h>
  35. #include <sound/soc.h>
  36. #include <sound/dmaengine_pcm.h>
  37. #include "mxs-pcm.h"
  38. struct mxs_pcm_dma_data {
  39. struct mxs_dma_data dma_data;
  40. struct mxs_pcm_dma_params *dma_params;
  41. };
  42. static struct snd_pcm_hardware snd_mxs_hardware = {
  43. .info = SNDRV_PCM_INFO_MMAP |
  44. SNDRV_PCM_INFO_MMAP_VALID |
  45. SNDRV_PCM_INFO_PAUSE |
  46. SNDRV_PCM_INFO_RESUME |
  47. SNDRV_PCM_INFO_INTERLEAVED,
  48. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  49. SNDRV_PCM_FMTBIT_S20_3LE |
  50. SNDRV_PCM_FMTBIT_S24_LE,
  51. .channels_min = 2,
  52. .channels_max = 2,
  53. .period_bytes_min = 32,
  54. .period_bytes_max = 8192,
  55. .periods_min = 1,
  56. .periods_max = 52,
  57. .buffer_bytes_max = 64 * 1024,
  58. .fifo_size = 32,
  59. };
  60. static bool filter(struct dma_chan *chan, void *param)
  61. {
  62. struct mxs_pcm_dma_data *pcm_dma_data = param;
  63. struct mxs_pcm_dma_params *dma_params = pcm_dma_data->dma_params;
  64. if (!mxs_dma_is_apbx(chan))
  65. return false;
  66. if (chan->chan_id != dma_params->chan_num)
  67. return false;
  68. chan->private = &pcm_dma_data->dma_data;
  69. return true;
  70. }
  71. static int snd_mxs_pcm_hw_params(struct snd_pcm_substream *substream,
  72. struct snd_pcm_hw_params *params)
  73. {
  74. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  75. return 0;
  76. }
  77. static int snd_mxs_open(struct snd_pcm_substream *substream)
  78. {
  79. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  80. struct mxs_pcm_dma_data *pcm_dma_data;
  81. int ret;
  82. pcm_dma_data = kzalloc(sizeof(*pcm_dma_data), GFP_KERNEL);
  83. if (pcm_dma_data == NULL)
  84. return -ENOMEM;
  85. pcm_dma_data->dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  86. pcm_dma_data->dma_data.chan_irq = pcm_dma_data->dma_params->chan_irq;
  87. ret = snd_dmaengine_pcm_open(substream, filter, pcm_dma_data);
  88. if (ret) {
  89. kfree(pcm_dma_data);
  90. return ret;
  91. }
  92. snd_soc_set_runtime_hwparams(substream, &snd_mxs_hardware);
  93. snd_dmaengine_pcm_set_data(substream, pcm_dma_data);
  94. return 0;
  95. }
  96. static int snd_mxs_close(struct snd_pcm_substream *substream)
  97. {
  98. struct mxs_pcm_dma_data *pcm_dma_data = snd_dmaengine_pcm_get_data(substream);
  99. snd_dmaengine_pcm_close(substream);
  100. kfree(pcm_dma_data);
  101. return 0;
  102. }
  103. static int snd_mxs_pcm_mmap(struct snd_pcm_substream *substream,
  104. struct vm_area_struct *vma)
  105. {
  106. struct snd_pcm_runtime *runtime = substream->runtime;
  107. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  108. runtime->dma_area,
  109. runtime->dma_addr,
  110. runtime->dma_bytes);
  111. }
  112. static struct snd_pcm_ops mxs_pcm_ops = {
  113. .open = snd_mxs_open,
  114. .close = snd_mxs_close,
  115. .ioctl = snd_pcm_lib_ioctl,
  116. .hw_params = snd_mxs_pcm_hw_params,
  117. .trigger = snd_dmaengine_pcm_trigger,
  118. .pointer = snd_dmaengine_pcm_pointer,
  119. .mmap = snd_mxs_pcm_mmap,
  120. };
  121. static int mxs_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  122. {
  123. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  124. struct snd_dma_buffer *buf = &substream->dma_buffer;
  125. size_t size = snd_mxs_hardware.buffer_bytes_max;
  126. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  127. buf->dev.dev = pcm->card->dev;
  128. buf->private_data = NULL;
  129. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  130. &buf->addr, GFP_KERNEL);
  131. if (!buf->area)
  132. return -ENOMEM;
  133. buf->bytes = size;
  134. return 0;
  135. }
  136. static u64 mxs_pcm_dmamask = DMA_BIT_MASK(32);
  137. static int mxs_pcm_new(struct snd_soc_pcm_runtime *rtd)
  138. {
  139. struct snd_card *card = rtd->card->snd_card;
  140. struct snd_pcm *pcm = rtd->pcm;
  141. int ret = 0;
  142. if (!card->dev->dma_mask)
  143. card->dev->dma_mask = &mxs_pcm_dmamask;
  144. if (!card->dev->coherent_dma_mask)
  145. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  146. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  147. ret = mxs_pcm_preallocate_dma_buffer(pcm,
  148. SNDRV_PCM_STREAM_PLAYBACK);
  149. if (ret)
  150. goto out;
  151. }
  152. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  153. ret = mxs_pcm_preallocate_dma_buffer(pcm,
  154. SNDRV_PCM_STREAM_CAPTURE);
  155. if (ret)
  156. goto out;
  157. }
  158. out:
  159. return ret;
  160. }
  161. static void mxs_pcm_free(struct snd_pcm *pcm)
  162. {
  163. struct snd_pcm_substream *substream;
  164. struct snd_dma_buffer *buf;
  165. int stream;
  166. for (stream = 0; stream < 2; stream++) {
  167. substream = pcm->streams[stream].substream;
  168. if (!substream)
  169. continue;
  170. buf = &substream->dma_buffer;
  171. if (!buf->area)
  172. continue;
  173. dma_free_writecombine(pcm->card->dev, buf->bytes,
  174. buf->area, buf->addr);
  175. buf->area = NULL;
  176. }
  177. }
  178. static struct snd_soc_platform_driver mxs_soc_platform = {
  179. .ops = &mxs_pcm_ops,
  180. .pcm_new = mxs_pcm_new,
  181. .pcm_free = mxs_pcm_free,
  182. };
  183. static int __devinit mxs_soc_platform_probe(struct platform_device *pdev)
  184. {
  185. return snd_soc_register_platform(&pdev->dev, &mxs_soc_platform);
  186. }
  187. static int __devexit mxs_soc_platform_remove(struct platform_device *pdev)
  188. {
  189. snd_soc_unregister_platform(&pdev->dev);
  190. return 0;
  191. }
  192. static struct platform_driver mxs_pcm_driver = {
  193. .driver = {
  194. .name = "mxs-pcm-audio",
  195. .owner = THIS_MODULE,
  196. },
  197. .probe = mxs_soc_platform_probe,
  198. .remove = __devexit_p(mxs_soc_platform_remove),
  199. };
  200. module_platform_driver(mxs_pcm_driver);
  201. MODULE_LICENSE("GPL");
  202. MODULE_ALIAS("platform:mxs-pcm-audio");