ep93xx-pcm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 <mach/dma.h>
  26. #include <mach/hardware.h>
  27. #include <mach/ep93xx-regs.h>
  28. #include "ep93xx-pcm.h"
  29. static const struct snd_pcm_hardware ep93xx_pcm_hardware = {
  30. .info = (SNDRV_PCM_INFO_MMAP |
  31. SNDRV_PCM_INFO_MMAP_VALID |
  32. SNDRV_PCM_INFO_INTERLEAVED |
  33. SNDRV_PCM_INFO_BLOCK_TRANSFER),
  34. .rates = SNDRV_PCM_RATE_8000_192000,
  35. .rate_min = SNDRV_PCM_RATE_8000,
  36. .rate_max = SNDRV_PCM_RATE_192000,
  37. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  38. SNDRV_PCM_FMTBIT_S24_LE |
  39. SNDRV_PCM_FMTBIT_S32_LE),
  40. .buffer_bytes_max = 131072,
  41. .period_bytes_min = 32,
  42. .period_bytes_max = 32768,
  43. .periods_min = 1,
  44. .periods_max = 32,
  45. .fifo_size = 32,
  46. };
  47. static bool ep93xx_pcm_dma_filter(struct dma_chan *chan, void *filter_param)
  48. {
  49. struct ep93xx_dma_data *data = filter_param;
  50. if (data->direction == ep93xx_dma_chan_direction(chan)) {
  51. chan->private = data;
  52. return true;
  53. }
  54. return false;
  55. }
  56. static int ep93xx_pcm_open(struct snd_pcm_substream *substream)
  57. {
  58. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  59. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  60. struct ep93xx_pcm_dma_params *dma_params;
  61. struct ep93xx_dma_data *dma_data;
  62. int ret;
  63. snd_soc_set_runtime_hwparams(substream, &ep93xx_pcm_hardware);
  64. dma_data = kmalloc(sizeof(*dma_data), GFP_KERNEL);
  65. if (!dma_data)
  66. return -ENOMEM;
  67. dma_params = snd_soc_dai_get_dma_data(cpu_dai, substream);
  68. dma_data->port = dma_params->dma_port;
  69. dma_data->name = dma_params->name;
  70. dma_data->direction = snd_pcm_substream_to_dma_direction(substream);
  71. ret = snd_dmaengine_pcm_open(substream, ep93xx_pcm_dma_filter, dma_data);
  72. if (ret) {
  73. kfree(dma_data);
  74. return ret;
  75. }
  76. snd_dmaengine_pcm_set_data(substream, dma_data);
  77. return 0;
  78. }
  79. static int ep93xx_pcm_close(struct snd_pcm_substream *substream)
  80. {
  81. struct dma_data *dma_data = snd_dmaengine_pcm_get_data(substream);
  82. snd_dmaengine_pcm_close(substream);
  83. kfree(dma_data);
  84. return 0;
  85. }
  86. static int ep93xx_pcm_hw_params(struct snd_pcm_substream *substream,
  87. struct snd_pcm_hw_params *params)
  88. {
  89. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  90. return 0;
  91. }
  92. static int ep93xx_pcm_hw_free(struct snd_pcm_substream *substream)
  93. {
  94. snd_pcm_set_runtime_buffer(substream, NULL);
  95. return 0;
  96. }
  97. static int ep93xx_pcm_mmap(struct snd_pcm_substream *substream,
  98. struct vm_area_struct *vma)
  99. {
  100. struct snd_pcm_runtime *runtime = substream->runtime;
  101. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  102. runtime->dma_area,
  103. runtime->dma_addr,
  104. runtime->dma_bytes);
  105. }
  106. static struct snd_pcm_ops ep93xx_pcm_ops = {
  107. .open = ep93xx_pcm_open,
  108. .close = ep93xx_pcm_close,
  109. .ioctl = snd_pcm_lib_ioctl,
  110. .hw_params = ep93xx_pcm_hw_params,
  111. .hw_free = ep93xx_pcm_hw_free,
  112. .trigger = snd_dmaengine_pcm_trigger,
  113. .pointer = snd_dmaengine_pcm_pointer,
  114. .mmap = ep93xx_pcm_mmap,
  115. };
  116. static int ep93xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  117. {
  118. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  119. struct snd_dma_buffer *buf = &substream->dma_buffer;
  120. size_t size = ep93xx_pcm_hardware.buffer_bytes_max;
  121. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  122. buf->dev.dev = pcm->card->dev;
  123. buf->private_data = NULL;
  124. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  125. &buf->addr, GFP_KERNEL);
  126. buf->bytes = size;
  127. return (buf->area == NULL) ? -ENOMEM : 0;
  128. }
  129. static void ep93xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  130. {
  131. struct snd_pcm_substream *substream;
  132. struct snd_dma_buffer *buf;
  133. int stream;
  134. for (stream = 0; stream < 2; stream++) {
  135. substream = pcm->streams[stream].substream;
  136. if (!substream)
  137. continue;
  138. buf = &substream->dma_buffer;
  139. if (!buf->area)
  140. continue;
  141. dma_free_writecombine(pcm->card->dev, buf->bytes, buf->area,
  142. buf->addr);
  143. buf->area = NULL;
  144. }
  145. }
  146. static u64 ep93xx_pcm_dmamask = DMA_BIT_MASK(32);
  147. static int ep93xx_pcm_new(struct snd_soc_pcm_runtime *rtd)
  148. {
  149. struct snd_card *card = rtd->card->snd_card;
  150. struct snd_pcm *pcm = rtd->pcm;
  151. int ret = 0;
  152. if (!card->dev->dma_mask)
  153. card->dev->dma_mask = &ep93xx_pcm_dmamask;
  154. if (!card->dev->coherent_dma_mask)
  155. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  156. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  157. ret = ep93xx_pcm_preallocate_dma_buffer(pcm,
  158. SNDRV_PCM_STREAM_PLAYBACK);
  159. if (ret)
  160. return ret;
  161. }
  162. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  163. ret = ep93xx_pcm_preallocate_dma_buffer(pcm,
  164. SNDRV_PCM_STREAM_CAPTURE);
  165. if (ret)
  166. return ret;
  167. }
  168. return 0;
  169. }
  170. static struct snd_soc_platform_driver ep93xx_soc_platform = {
  171. .ops = &ep93xx_pcm_ops,
  172. .pcm_new = &ep93xx_pcm_new,
  173. .pcm_free = &ep93xx_pcm_free_dma_buffers,
  174. };
  175. static int __devinit ep93xx_soc_platform_probe(struct platform_device *pdev)
  176. {
  177. return snd_soc_register_platform(&pdev->dev, &ep93xx_soc_platform);
  178. }
  179. static int __devexit ep93xx_soc_platform_remove(struct platform_device *pdev)
  180. {
  181. snd_soc_unregister_platform(&pdev->dev);
  182. return 0;
  183. }
  184. static struct platform_driver ep93xx_pcm_driver = {
  185. .driver = {
  186. .name = "ep93xx-pcm-audio",
  187. .owner = THIS_MODULE,
  188. },
  189. .probe = ep93xx_soc_platform_probe,
  190. .remove = __devexit_p(ep93xx_soc_platform_remove),
  191. };
  192. module_platform_driver(ep93xx_pcm_driver);
  193. MODULE_AUTHOR("Ryan Mallon");
  194. MODULE_DESCRIPTION("EP93xx ALSA PCM interface");
  195. MODULE_LICENSE("GPL");
  196. MODULE_ALIAS("platform:ep93xx-pcm-audio");