ep93xx-pcm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_request_chan(substream,
  60. ep93xx_pcm_dma_filter,
  61. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream));
  62. }
  63. static int ep93xx_pcm_hw_params(struct snd_pcm_substream *substream,
  64. struct snd_pcm_hw_params *params)
  65. {
  66. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  67. return 0;
  68. }
  69. static int ep93xx_pcm_hw_free(struct snd_pcm_substream *substream)
  70. {
  71. snd_pcm_set_runtime_buffer(substream, NULL);
  72. return 0;
  73. }
  74. static int ep93xx_pcm_mmap(struct snd_pcm_substream *substream,
  75. struct vm_area_struct *vma)
  76. {
  77. struct snd_pcm_runtime *runtime = substream->runtime;
  78. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  79. runtime->dma_area,
  80. runtime->dma_addr,
  81. runtime->dma_bytes);
  82. }
  83. static struct snd_pcm_ops ep93xx_pcm_ops = {
  84. .open = ep93xx_pcm_open,
  85. .close = snd_dmaengine_pcm_close_release_chan,
  86. .ioctl = snd_pcm_lib_ioctl,
  87. .hw_params = ep93xx_pcm_hw_params,
  88. .hw_free = ep93xx_pcm_hw_free,
  89. .trigger = snd_dmaengine_pcm_trigger,
  90. .pointer = snd_dmaengine_pcm_pointer_no_residue,
  91. .mmap = ep93xx_pcm_mmap,
  92. };
  93. static int ep93xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  94. {
  95. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  96. struct snd_dma_buffer *buf = &substream->dma_buffer;
  97. size_t size = ep93xx_pcm_hardware.buffer_bytes_max;
  98. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  99. buf->dev.dev = pcm->card->dev;
  100. buf->private_data = NULL;
  101. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  102. &buf->addr, GFP_KERNEL);
  103. buf->bytes = size;
  104. return (buf->area == NULL) ? -ENOMEM : 0;
  105. }
  106. static void ep93xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  107. {
  108. struct snd_pcm_substream *substream;
  109. struct snd_dma_buffer *buf;
  110. int stream;
  111. for (stream = 0; stream < 2; stream++) {
  112. substream = pcm->streams[stream].substream;
  113. if (!substream)
  114. continue;
  115. buf = &substream->dma_buffer;
  116. if (!buf->area)
  117. continue;
  118. dma_free_writecombine(pcm->card->dev, buf->bytes, buf->area,
  119. buf->addr);
  120. buf->area = NULL;
  121. }
  122. }
  123. static u64 ep93xx_pcm_dmamask = DMA_BIT_MASK(32);
  124. static int ep93xx_pcm_new(struct snd_soc_pcm_runtime *rtd)
  125. {
  126. struct snd_card *card = rtd->card->snd_card;
  127. struct snd_pcm *pcm = rtd->pcm;
  128. int ret = 0;
  129. if (!card->dev->dma_mask)
  130. card->dev->dma_mask = &ep93xx_pcm_dmamask;
  131. if (!card->dev->coherent_dma_mask)
  132. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  133. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  134. ret = ep93xx_pcm_preallocate_dma_buffer(pcm,
  135. SNDRV_PCM_STREAM_PLAYBACK);
  136. if (ret)
  137. return ret;
  138. }
  139. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  140. ret = ep93xx_pcm_preallocate_dma_buffer(pcm,
  141. SNDRV_PCM_STREAM_CAPTURE);
  142. if (ret)
  143. return ret;
  144. }
  145. return 0;
  146. }
  147. static struct snd_soc_platform_driver ep93xx_soc_platform = {
  148. .ops = &ep93xx_pcm_ops,
  149. .pcm_new = &ep93xx_pcm_new,
  150. .pcm_free = &ep93xx_pcm_free_dma_buffers,
  151. };
  152. static int ep93xx_soc_platform_probe(struct platform_device *pdev)
  153. {
  154. return snd_soc_register_platform(&pdev->dev, &ep93xx_soc_platform);
  155. }
  156. static int ep93xx_soc_platform_remove(struct platform_device *pdev)
  157. {
  158. snd_soc_unregister_platform(&pdev->dev);
  159. return 0;
  160. }
  161. static struct platform_driver ep93xx_pcm_driver = {
  162. .driver = {
  163. .name = "ep93xx-pcm-audio",
  164. .owner = THIS_MODULE,
  165. },
  166. .probe = ep93xx_soc_platform_probe,
  167. .remove = ep93xx_soc_platform_remove,
  168. };
  169. module_platform_driver(ep93xx_pcm_driver);
  170. MODULE_AUTHOR("Ryan Mallon");
  171. MODULE_DESCRIPTION("EP93xx ALSA PCM interface");
  172. MODULE_LICENSE("GPL");
  173. MODULE_ALIAS("platform:ep93xx-pcm-audio");