spear_pcm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. return snd_dmaengine_pcm_open(substream, dma_data->filter, dma_data)
  58. }
  59. static int spear_pcm_mmap(struct snd_pcm_substream *substream,
  60. struct vm_area_struct *vma)
  61. {
  62. struct snd_pcm_runtime *runtime = substream->runtime;
  63. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  64. runtime->dma_area, runtime->dma_addr,
  65. runtime->dma_bytes);
  66. }
  67. static struct snd_pcm_ops spear_pcm_ops = {
  68. .open = spear_pcm_open,
  69. .close = snd_dmaengine_pcm_close,
  70. .ioctl = snd_pcm_lib_ioctl,
  71. .hw_params = spear_pcm_hw_params,
  72. .hw_free = spear_pcm_hw_free,
  73. .trigger = snd_dmaengine_pcm_trigger,
  74. .pointer = snd_dmaengine_pcm_pointer,
  75. .mmap = spear_pcm_mmap,
  76. };
  77. static int
  78. spear_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
  79. size_t size)
  80. {
  81. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  82. struct snd_dma_buffer *buf = &substream->dma_buffer;
  83. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  84. buf->dev.dev = pcm->card->dev;
  85. buf->private_data = NULL;
  86. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  87. &buf->addr, GFP_KERNEL);
  88. if (!buf->area)
  89. return -ENOMEM;
  90. dev_info(buf->dev.dev,
  91. " preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
  92. (void *)buf->area, (void *)buf->addr, size);
  93. buf->bytes = size;
  94. return 0;
  95. }
  96. static void spear_pcm_free(struct snd_pcm *pcm)
  97. {
  98. struct snd_pcm_substream *substream;
  99. struct snd_dma_buffer *buf;
  100. int stream;
  101. for (stream = 0; stream < 2; stream++) {
  102. substream = pcm->streams[stream].substream;
  103. if (!substream)
  104. continue;
  105. buf = &substream->dma_buffer;
  106. if (!buf || !buf->area)
  107. continue;
  108. dma_free_writecombine(pcm->card->dev, buf->bytes,
  109. buf->area, buf->addr);
  110. buf->area = NULL;
  111. }
  112. }
  113. static u64 spear_pcm_dmamask = DMA_BIT_MASK(32);
  114. static int spear_pcm_new(struct snd_soc_pcm_runtime *rtd)
  115. {
  116. struct snd_card *card = rtd->card->snd_card;
  117. int ret;
  118. if (!card->dev->dma_mask)
  119. card->dev->dma_mask = &spear_pcm_dmamask;
  120. if (!card->dev->coherent_dma_mask)
  121. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  122. if (rtd->cpu_dai->driver->playback.channels_min) {
  123. ret = spear_pcm_preallocate_dma_buffer(rtd->pcm,
  124. SNDRV_PCM_STREAM_PLAYBACK,
  125. spear_pcm_hardware.buffer_bytes_max);
  126. if (ret)
  127. return ret;
  128. }
  129. if (rtd->cpu_dai->driver->capture.channels_min) {
  130. ret = spear_pcm_preallocate_dma_buffer(rtd->pcm,
  131. SNDRV_PCM_STREAM_CAPTURE,
  132. spear_pcm_hardware.buffer_bytes_max);
  133. if (ret)
  134. return ret;
  135. }
  136. return 0;
  137. }
  138. struct snd_soc_platform_driver spear_soc_platform = {
  139. .ops = &spear_pcm_ops,
  140. .pcm_new = spear_pcm_new,
  141. .pcm_free = spear_pcm_free,
  142. };
  143. static int spear_soc_platform_probe(struct platform_device *pdev)
  144. {
  145. return snd_soc_register_platform(&pdev->dev, &spear_soc_platform);
  146. }
  147. static int spear_soc_platform_remove(struct platform_device *pdev)
  148. {
  149. snd_soc_unregister_platform(&pdev->dev);
  150. return 0;
  151. }
  152. static struct platform_driver spear_pcm_driver = {
  153. .driver = {
  154. .name = "spear-pcm-audio",
  155. .owner = THIS_MODULE,
  156. },
  157. .probe = spear_soc_platform_probe,
  158. .remove = spear_soc_platform_remove,
  159. };
  160. module_platform_driver(spear_pcm_driver);
  161. MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
  162. MODULE_DESCRIPTION("SPEAr PCM DMA module");
  163. MODULE_LICENSE("GPL");
  164. MODULE_ALIAS("platform:spear-pcm-audio");