ep93xx-pcm.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * linux/sound/arm/ep93xx-pcm.c - EP93xx ALSA PCM interface
  3. *
  4. * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
  5. * Copyright (C) 2006 Applied Data Systems
  6. *
  7. * Rewritten for the SoC audio subsystem (Based on PXA2xx code):
  8. * Copyright (c) 2008 Ryan Mallon
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/device.h>
  17. #include <linux/slab.h>
  18. #include <linux/dmaengine.h>
  19. #include <linux/dma-mapping.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/dmaengine_pcm.h>
  25. #include <linux/platform_data/dma-ep93xx.h>
  26. #include <mach/hardware.h>
  27. #include <mach/ep93xx-regs.h>
  28. static const struct snd_pcm_hardware ep93xx_pcm_hardware = {
  29. .info = (SNDRV_PCM_INFO_MMAP |
  30. SNDRV_PCM_INFO_MMAP_VALID |
  31. SNDRV_PCM_INFO_INTERLEAVED |
  32. SNDRV_PCM_INFO_BLOCK_TRANSFER),
  33. .rates = SNDRV_PCM_RATE_8000_192000,
  34. .rate_min = SNDRV_PCM_RATE_8000,
  35. .rate_max = SNDRV_PCM_RATE_192000,
  36. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  37. SNDRV_PCM_FMTBIT_S24_LE |
  38. SNDRV_PCM_FMTBIT_S32_LE),
  39. .buffer_bytes_max = 131072,
  40. .period_bytes_min = 32,
  41. .period_bytes_max = 32768,
  42. .periods_min = 1,
  43. .periods_max = 32,
  44. .fifo_size = 32,
  45. };
  46. static bool ep93xx_pcm_dma_filter(struct dma_chan *chan, void *filter_param)
  47. {
  48. struct ep93xx_dma_data *data = filter_param;
  49. if (data->direction == ep93xx_dma_chan_direction(chan)) {
  50. chan->private = data;
  51. return true;
  52. }
  53. return false;
  54. }
  55. static int ep93xx_pcm_open(struct snd_pcm_substream *substream)
  56. {
  57. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  58. snd_soc_set_runtime_hwparams(substream, &ep93xx_pcm_hardware);
  59. return snd_dmaengine_pcm_open(substream, ep93xx_pcm_dma_filter,
  60. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream));
  61. }
  62. static int ep93xx_pcm_hw_params(struct snd_pcm_substream *substream,
  63. struct snd_pcm_hw_params *params)
  64. {
  65. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  66. return 0;
  67. }
  68. static int ep93xx_pcm_hw_free(struct snd_pcm_substream *substream)
  69. {
  70. snd_pcm_set_runtime_buffer(substream, NULL);
  71. return 0;
  72. }
  73. static int ep93xx_pcm_mmap(struct snd_pcm_substream *substream,
  74. struct vm_area_struct *vma)
  75. {
  76. struct snd_pcm_runtime *runtime = substream->runtime;
  77. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  78. runtime->dma_area,
  79. runtime->dma_addr,
  80. runtime->dma_bytes);
  81. }
  82. static struct snd_pcm_ops ep93xx_pcm_ops = {
  83. .open = ep93xx_pcm_open,
  84. .close = snd_dmaengine_pcm_close,
  85. .ioctl = snd_pcm_lib_ioctl,
  86. .hw_params = ep93xx_pcm_hw_params,
  87. .hw_free = ep93xx_pcm_hw_free,
  88. .trigger = snd_dmaengine_pcm_trigger,
  89. .pointer = snd_dmaengine_pcm_pointer_no_residue,
  90. .mmap = ep93xx_pcm_mmap,
  91. };
  92. static int ep93xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  93. {
  94. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  95. struct snd_dma_buffer *buf = &substream->dma_buffer;
  96. size_t size = ep93xx_pcm_hardware.buffer_bytes_max;
  97. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  98. buf->dev.dev = pcm->card->dev;
  99. buf->private_data = NULL;
  100. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  101. &buf->addr, GFP_KERNEL);
  102. buf->bytes = size;
  103. return (buf->area == NULL) ? -ENOMEM : 0;
  104. }
  105. static void ep93xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  106. {
  107. struct snd_pcm_substream *substream;
  108. struct snd_dma_buffer *buf;
  109. int stream;
  110. for (stream = 0; stream < 2; stream++) {
  111. substream = pcm->streams[stream].substream;
  112. if (!substream)
  113. continue;
  114. buf = &substream->dma_buffer;
  115. if (!buf->area)
  116. continue;
  117. dma_free_writecombine(pcm->card->dev, buf->bytes, buf->area,
  118. buf->addr);
  119. buf->area = NULL;
  120. }
  121. }
  122. static u64 ep93xx_pcm_dmamask = DMA_BIT_MASK(32);
  123. static int ep93xx_pcm_new(struct snd_soc_pcm_runtime *rtd)
  124. {
  125. struct snd_card *card = rtd->card->snd_card;
  126. struct snd_pcm *pcm = rtd->pcm;
  127. int ret = 0;
  128. if (!card->dev->dma_mask)
  129. card->dev->dma_mask = &ep93xx_pcm_dmamask;
  130. if (!card->dev->coherent_dma_mask)
  131. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  132. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  133. ret = ep93xx_pcm_preallocate_dma_buffer(pcm,
  134. SNDRV_PCM_STREAM_PLAYBACK);
  135. if (ret)
  136. return ret;
  137. }
  138. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  139. ret = ep93xx_pcm_preallocate_dma_buffer(pcm,
  140. SNDRV_PCM_STREAM_CAPTURE);
  141. if (ret)
  142. return ret;
  143. }
  144. return 0;
  145. }
  146. static struct snd_soc_platform_driver ep93xx_soc_platform = {
  147. .ops = &ep93xx_pcm_ops,
  148. .pcm_new = &ep93xx_pcm_new,
  149. .pcm_free = &ep93xx_pcm_free_dma_buffers,
  150. };
  151. static int ep93xx_soc_platform_probe(struct platform_device *pdev)
  152. {
  153. return snd_soc_register_platform(&pdev->dev, &ep93xx_soc_platform);
  154. }
  155. static int ep93xx_soc_platform_remove(struct platform_device *pdev)
  156. {
  157. snd_soc_unregister_platform(&pdev->dev);
  158. return 0;
  159. }
  160. static struct platform_driver ep93xx_pcm_driver = {
  161. .driver = {
  162. .name = "ep93xx-pcm-audio",
  163. .owner = THIS_MODULE,
  164. },
  165. .probe = ep93xx_soc_platform_probe,
  166. .remove = ep93xx_soc_platform_remove,
  167. };
  168. module_platform_driver(ep93xx_pcm_driver);
  169. MODULE_AUTHOR("Ryan Mallon");
  170. MODULE_DESCRIPTION("EP93xx ALSA PCM interface");
  171. MODULE_LICENSE("GPL");
  172. MODULE_ALIAS("platform:ep93xx-pcm-audio");