spear_pcm.c 5.1 KB

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