atmel-pcm.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * atmel-pcm.c -- ALSA PCM interface for the Atmel atmel SoC.
  3. *
  4. * Copyright (C) 2005 SAN People
  5. * Copyright (C) 2008 Atmel
  6. *
  7. * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
  8. *
  9. * Based on at91-pcm. by:
  10. * Frank Mandarino <fmandarino@endrelia.com>
  11. * Copyright 2006 Endrelia Technologies Inc.
  12. *
  13. * Based on pxa2xx-pcm.c by:
  14. *
  15. * Author: Nicolas Pitre
  16. * Created: Nov 30, 2004
  17. * Copyright: (C) 2004 MontaVista Software, Inc.
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. */
  33. #include <linux/module.h>
  34. #include <linux/dma-mapping.h>
  35. #include <sound/pcm.h>
  36. #include <sound/soc.h>
  37. #include "atmel-pcm.h"
  38. static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  39. int stream)
  40. {
  41. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  42. struct snd_dma_buffer *buf = &substream->dma_buffer;
  43. size_t size = ATMEL_SSC_DMABUF_SIZE;
  44. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  45. buf->dev.dev = pcm->card->dev;
  46. buf->private_data = NULL;
  47. buf->area = dma_alloc_coherent(pcm->card->dev, size,
  48. &buf->addr, GFP_KERNEL);
  49. pr_debug("atmel-pcm: alloc dma buffer: area=%p, addr=%p, size=%zu\n",
  50. (void *)buf->area, (void *)buf->addr, size);
  51. if (!buf->area)
  52. return -ENOMEM;
  53. buf->bytes = size;
  54. return 0;
  55. }
  56. int atmel_pcm_mmap(struct snd_pcm_substream *substream,
  57. struct vm_area_struct *vma)
  58. {
  59. return remap_pfn_range(vma, vma->vm_start,
  60. substream->dma_buffer.addr >> PAGE_SHIFT,
  61. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  62. }
  63. EXPORT_SYMBOL_GPL(atmel_pcm_mmap);
  64. static u64 atmel_pcm_dmamask = DMA_BIT_MASK(32);
  65. int atmel_pcm_new(struct snd_soc_pcm_runtime *rtd)
  66. {
  67. struct snd_card *card = rtd->card->snd_card;
  68. struct snd_pcm *pcm = rtd->pcm;
  69. int ret = 0;
  70. if (!card->dev->dma_mask)
  71. card->dev->dma_mask = &atmel_pcm_dmamask;
  72. if (!card->dev->coherent_dma_mask)
  73. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  74. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  75. pr_debug("atmel-pcm: allocating PCM playback DMA buffer\n");
  76. ret = atmel_pcm_preallocate_dma_buffer(pcm,
  77. SNDRV_PCM_STREAM_PLAYBACK);
  78. if (ret)
  79. goto out;
  80. }
  81. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  82. pr_debug("atmel-pcm: allocating PCM capture DMA buffer\n");
  83. ret = atmel_pcm_preallocate_dma_buffer(pcm,
  84. SNDRV_PCM_STREAM_CAPTURE);
  85. if (ret)
  86. goto out;
  87. }
  88. out:
  89. return ret;
  90. }
  91. EXPORT_SYMBOL_GPL(atmel_pcm_new);
  92. void atmel_pcm_free(struct snd_pcm *pcm)
  93. {
  94. struct snd_pcm_substream *substream;
  95. struct snd_dma_buffer *buf;
  96. int stream;
  97. for (stream = 0; stream < 2; stream++) {
  98. substream = pcm->streams[stream].substream;
  99. if (!substream)
  100. continue;
  101. buf = &substream->dma_buffer;
  102. if (!buf->area)
  103. continue;
  104. dma_free_coherent(pcm->card->dev, buf->bytes,
  105. buf->area, buf->addr);
  106. buf->area = NULL;
  107. }
  108. }
  109. EXPORT_SYMBOL_GPL(atmel_pcm_free);