imx-pcm.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
  3. *
  4. * This code is based on code copyrighted by Freescale,
  5. * Liam Girdwood, Javier Martin and probably others.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/dma-mapping.h>
  13. #include <linux/module.h>
  14. #include <sound/pcm.h>
  15. #include <sound/soc.h>
  16. #include "imx-pcm.h"
  17. int snd_imx_pcm_mmap(struct snd_pcm_substream *substream,
  18. struct vm_area_struct *vma)
  19. {
  20. struct snd_pcm_runtime *runtime = substream->runtime;
  21. int ret;
  22. ret = dma_mmap_writecombine(substream->pcm->card->dev, vma,
  23. runtime->dma_area, runtime->dma_addr, runtime->dma_bytes);
  24. pr_debug("%s: ret: %d %p 0x%08x 0x%08x\n", __func__, ret,
  25. runtime->dma_area,
  26. runtime->dma_addr,
  27. runtime->dma_bytes);
  28. return ret;
  29. }
  30. EXPORT_SYMBOL_GPL(snd_imx_pcm_mmap);
  31. static int imx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  32. {
  33. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  34. struct snd_dma_buffer *buf = &substream->dma_buffer;
  35. size_t size = IMX_SSI_DMABUF_SIZE;
  36. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  37. buf->dev.dev = pcm->card->dev;
  38. buf->private_data = NULL;
  39. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  40. &buf->addr, GFP_KERNEL);
  41. if (!buf->area)
  42. return -ENOMEM;
  43. buf->bytes = size;
  44. return 0;
  45. }
  46. static u64 imx_pcm_dmamask = DMA_BIT_MASK(32);
  47. int imx_pcm_new(struct snd_soc_pcm_runtime *rtd)
  48. {
  49. struct snd_card *card = rtd->card->snd_card;
  50. struct snd_pcm *pcm = rtd->pcm;
  51. int ret = 0;
  52. if (!card->dev->dma_mask)
  53. card->dev->dma_mask = &imx_pcm_dmamask;
  54. if (!card->dev->coherent_dma_mask)
  55. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  56. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  57. ret = imx_pcm_preallocate_dma_buffer(pcm,
  58. SNDRV_PCM_STREAM_PLAYBACK);
  59. if (ret)
  60. goto out;
  61. }
  62. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  63. ret = imx_pcm_preallocate_dma_buffer(pcm,
  64. SNDRV_PCM_STREAM_CAPTURE);
  65. if (ret)
  66. goto out;
  67. }
  68. out:
  69. return ret;
  70. }
  71. EXPORT_SYMBOL_GPL(imx_pcm_new);
  72. void imx_pcm_free(struct snd_pcm *pcm)
  73. {
  74. struct snd_pcm_substream *substream;
  75. struct snd_dma_buffer *buf;
  76. int stream;
  77. for (stream = 0; stream < 2; stream++) {
  78. substream = pcm->streams[stream].substream;
  79. if (!substream)
  80. continue;
  81. buf = &substream->dma_buffer;
  82. if (!buf->area)
  83. continue;
  84. dma_free_writecombine(pcm->card->dev, buf->bytes,
  85. buf->area, buf->addr);
  86. buf->area = NULL;
  87. }
  88. }
  89. EXPORT_SYMBOL_GPL(imx_pcm_free);