spear_pcm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * ALSA PCM interface for ST SPEAr Processors
  3. *
  4. * sound/soc/spear/spear_pcm.c
  5. *
  6. * Copyright (C) 2012 ST Microelectronics
  7. * Rajeev Kumar<rajeev-dlh.kumar@st.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/slab.h>
  20. #include <sound/core.h>
  21. #include <sound/dmaengine_pcm.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/spear_dma.h>
  26. struct snd_pcm_hardware spear_pcm_hardware = {
  27. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  28. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  29. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  30. .buffer_bytes_max = 16 * 1024, /* max buffer size */
  31. .period_bytes_min = 2 * 1024, /* 1 msec data minimum period size */
  32. .period_bytes_max = 2 * 1024, /* maximum period size */
  33. .periods_min = 1, /* min # periods */
  34. .periods_max = 8, /* max # of periods */
  35. .fifo_size = 0, /* fifo size in bytes */
  36. };
  37. static int spear_pcm_hw_params(struct snd_pcm_substream *substream,
  38. struct snd_pcm_hw_params *params)
  39. {
  40. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  41. return 0;
  42. }
  43. static int spear_pcm_hw_free(struct snd_pcm_substream *substream)
  44. {
  45. snd_pcm_set_runtime_buffer(substream, NULL);
  46. return 0;
  47. }
  48. static int spear_pcm_open(struct snd_pcm_substream *substream)
  49. {
  50. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  51. struct spear_dma_data *dma_data = (struct spear_dma_data *)
  52. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  53. int ret;
  54. ret = snd_soc_set_runtime_hwparams(substream, &spear_pcm_hardware);
  55. if (ret)
  56. return ret;
  57. ret = snd_dmaengine_pcm_open(substream, dma_data->filter, dma_data);
  58. if (ret)
  59. return ret;
  60. snd_dmaengine_pcm_set_data(substream, dma_data);
  61. return 0;
  62. }
  63. static int spear_pcm_close(struct snd_pcm_substream *substream)
  64. {
  65. snd_dmaengine_pcm_close(substream);
  66. return 0;
  67. }
  68. static int spear_pcm_mmap(struct snd_pcm_substream *substream,
  69. struct vm_area_struct *vma)
  70. {
  71. struct snd_pcm_runtime *runtime = substream->runtime;
  72. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  73. runtime->dma_area, runtime->dma_addr,
  74. runtime->dma_bytes);
  75. }
  76. static struct snd_pcm_ops spear_pcm_ops = {
  77. .open = spear_pcm_open,
  78. .close = spear_pcm_close,
  79. .ioctl = snd_pcm_lib_ioctl,
  80. .hw_params = spear_pcm_hw_params,
  81. .hw_free = spear_pcm_hw_free,
  82. .trigger = snd_dmaengine_pcm_trigger,
  83. .pointer = snd_dmaengine_pcm_pointer,
  84. .mmap = spear_pcm_mmap,
  85. };
  86. static int
  87. spear_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
  88. size_t size)
  89. {
  90. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  91. struct snd_dma_buffer *buf = &substream->dma_buffer;
  92. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  93. buf->dev.dev = pcm->card->dev;
  94. buf->private_data = NULL;
  95. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  96. &buf->addr, GFP_KERNEL);
  97. if (!buf->area)
  98. return -ENOMEM;
  99. dev_info(buf->dev.dev,
  100. " preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
  101. (void *)buf->area, (void *)buf->addr, size);
  102. buf->bytes = size;
  103. return 0;
  104. }
  105. static void spear_pcm_free(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 || !buf->area)
  116. continue;
  117. dma_free_writecombine(pcm->card->dev, buf->bytes,
  118. buf->area, buf->addr);
  119. buf->area = NULL;
  120. }
  121. }
  122. static u64 spear_pcm_dmamask = DMA_BIT_MASK(32);
  123. static int spear_pcm_new(struct snd_card *card,
  124. struct snd_soc_dai *dai, struct snd_pcm *pcm)
  125. {
  126. int ret;
  127. if (!card->dev->dma_mask)
  128. card->dev->dma_mask = &spear_pcm_dmamask;
  129. if (!card->dev->coherent_dma_mask)
  130. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  131. if (dai->driver->playback.channels_min) {
  132. ret = spear_pcm_preallocate_dma_buffer(pcm,
  133. SNDRV_PCM_STREAM_PLAYBACK,
  134. spear_pcm_hardware.buffer_bytes_max);
  135. if (ret)
  136. return ret;
  137. }
  138. if (dai->driver->capture.channels_min) {
  139. ret = spear_pcm_preallocate_dma_buffer(pcm,
  140. SNDRV_PCM_STREAM_CAPTURE,
  141. spear_pcm_hardware.buffer_bytes_max);
  142. if (ret)
  143. return ret;
  144. }
  145. return 0;
  146. }
  147. struct snd_soc_platform_driver spear_soc_platform = {
  148. .ops = &spear_pcm_ops,
  149. .pcm_new = spear_pcm_new,
  150. .pcm_free = spear_pcm_free,
  151. };
  152. static int spear_soc_platform_probe(struct platform_device *pdev)
  153. {
  154. return snd_soc_register_platform(&pdev->dev, &spear_soc_platform);
  155. }
  156. static int spear_soc_platform_remove(struct platform_device *pdev)
  157. {
  158. snd_soc_unregister_platform(&pdev->dev);
  159. return 0;
  160. }
  161. static struct platform_driver spear_pcm_driver = {
  162. .driver = {
  163. .name = "spear-pcm-audio",
  164. .owner = THIS_MODULE,
  165. },
  166. .probe = spear_soc_platform_probe,
  167. .remove = spear_soc_platform_remove,
  168. };
  169. module_platform_driver(spear_pcm_driver);
  170. MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
  171. MODULE_DESCRIPTION("SPEAr PCM DMA module");
  172. MODULE_LICENSE("GPL");
  173. MODULE_ALIAS("platform:spear-pcm-audio");