tegra_pcm.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * tegra_pcm.c - Tegra PCM driver
  3. *
  4. * Author: Stephen Warren <swarren@nvidia.com>
  5. * Copyright (C) 2010,2012 - NVIDIA, Inc.
  6. *
  7. * Based on code copyright/by:
  8. *
  9. * Copyright (c) 2009-2010, NVIDIA Corporation.
  10. * Scott Peterson <speterson@nvidia.com>
  11. * Vijay Mali <vmali@nvidia.com>
  12. *
  13. * Copyright (C) 2010 Google, Inc.
  14. * Iliyan Malchev <malchev@google.com>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * version 2 as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  28. * 02110-1301 USA
  29. *
  30. */
  31. #include <linux/dma-mapping.h>
  32. #include <linux/module.h>
  33. #include <linux/slab.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/soc.h>
  38. #include <sound/dmaengine_pcm.h>
  39. #include "tegra_pcm.h"
  40. static const struct snd_pcm_hardware tegra_pcm_hardware = {
  41. .info = SNDRV_PCM_INFO_MMAP |
  42. SNDRV_PCM_INFO_MMAP_VALID |
  43. SNDRV_PCM_INFO_PAUSE |
  44. SNDRV_PCM_INFO_RESUME |
  45. SNDRV_PCM_INFO_INTERLEAVED,
  46. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  47. .channels_min = 2,
  48. .channels_max = 2,
  49. .period_bytes_min = 1024,
  50. .period_bytes_max = PAGE_SIZE,
  51. .periods_min = 2,
  52. .periods_max = 8,
  53. .buffer_bytes_max = PAGE_SIZE * 8,
  54. .fifo_size = 4,
  55. };
  56. static int tegra_pcm_open(struct snd_pcm_substream *substream)
  57. {
  58. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  59. struct device *dev = rtd->platform->dev;
  60. int ret;
  61. /* Set HW params now that initialization is complete */
  62. snd_soc_set_runtime_hwparams(substream, &tegra_pcm_hardware);
  63. ret = snd_dmaengine_pcm_open(substream, NULL, NULL);
  64. if (ret) {
  65. dev_err(dev, "dmaengine pcm open failed with err %d\n", ret);
  66. return ret;
  67. }
  68. return 0;
  69. }
  70. static int tegra_pcm_close(struct snd_pcm_substream *substream)
  71. {
  72. snd_dmaengine_pcm_close(substream);
  73. return 0;
  74. }
  75. static int tegra_pcm_hw_params(struct snd_pcm_substream *substream,
  76. struct snd_pcm_hw_params *params)
  77. {
  78. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  79. struct device *dev = rtd->platform->dev;
  80. struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
  81. struct tegra_pcm_dma_params *dmap;
  82. struct dma_slave_config slave_config;
  83. int ret;
  84. dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  85. ret = snd_hwparams_to_dma_slave_config(substream, params,
  86. &slave_config);
  87. if (ret) {
  88. dev_err(dev, "hw params config failed with err %d\n", ret);
  89. return ret;
  90. }
  91. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  92. slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  93. slave_config.dst_addr = dmap->addr;
  94. slave_config.dst_maxburst = 4;
  95. } else {
  96. slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  97. slave_config.src_addr = dmap->addr;
  98. slave_config.src_maxburst = 4;
  99. }
  100. slave_config.slave_id = dmap->req_sel;
  101. ret = dmaengine_slave_config(chan, &slave_config);
  102. if (ret < 0) {
  103. dev_err(dev, "dma slave config failed with err %d\n", ret);
  104. return ret;
  105. }
  106. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  107. return 0;
  108. }
  109. static int tegra_pcm_hw_free(struct snd_pcm_substream *substream)
  110. {
  111. snd_pcm_set_runtime_buffer(substream, NULL);
  112. return 0;
  113. }
  114. static int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  115. {
  116. switch (cmd) {
  117. case SNDRV_PCM_TRIGGER_START:
  118. case SNDRV_PCM_TRIGGER_RESUME:
  119. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  120. return snd_dmaengine_pcm_trigger(substream,
  121. SNDRV_PCM_TRIGGER_START);
  122. case SNDRV_PCM_TRIGGER_STOP:
  123. case SNDRV_PCM_TRIGGER_SUSPEND:
  124. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  125. return snd_dmaengine_pcm_trigger(substream,
  126. SNDRV_PCM_TRIGGER_STOP);
  127. default:
  128. return -EINVAL;
  129. }
  130. return 0;
  131. }
  132. static int tegra_pcm_mmap(struct snd_pcm_substream *substream,
  133. struct vm_area_struct *vma)
  134. {
  135. struct snd_pcm_runtime *runtime = substream->runtime;
  136. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  137. runtime->dma_area,
  138. runtime->dma_addr,
  139. runtime->dma_bytes);
  140. }
  141. static struct snd_pcm_ops tegra_pcm_ops = {
  142. .open = tegra_pcm_open,
  143. .close = tegra_pcm_close,
  144. .ioctl = snd_pcm_lib_ioctl,
  145. .hw_params = tegra_pcm_hw_params,
  146. .hw_free = tegra_pcm_hw_free,
  147. .trigger = tegra_pcm_trigger,
  148. .pointer = snd_dmaengine_pcm_pointer,
  149. .mmap = tegra_pcm_mmap,
  150. };
  151. static int tegra_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  152. {
  153. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  154. struct snd_dma_buffer *buf = &substream->dma_buffer;
  155. size_t size = tegra_pcm_hardware.buffer_bytes_max;
  156. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  157. &buf->addr, GFP_KERNEL);
  158. if (!buf->area)
  159. return -ENOMEM;
  160. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  161. buf->dev.dev = pcm->card->dev;
  162. buf->private_data = NULL;
  163. buf->bytes = size;
  164. return 0;
  165. }
  166. static void tegra_pcm_deallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  167. {
  168. struct snd_pcm_substream *substream;
  169. struct snd_dma_buffer *buf;
  170. substream = pcm->streams[stream].substream;
  171. if (!substream)
  172. return;
  173. buf = &substream->dma_buffer;
  174. if (!buf->area)
  175. return;
  176. dma_free_writecombine(pcm->card->dev, buf->bytes,
  177. buf->area, buf->addr);
  178. buf->area = NULL;
  179. }
  180. static u64 tegra_dma_mask = DMA_BIT_MASK(32);
  181. static int tegra_pcm_new(struct snd_soc_pcm_runtime *rtd)
  182. {
  183. struct snd_card *card = rtd->card->snd_card;
  184. struct snd_pcm *pcm = rtd->pcm;
  185. int ret = 0;
  186. if (!card->dev->dma_mask)
  187. card->dev->dma_mask = &tegra_dma_mask;
  188. if (!card->dev->coherent_dma_mask)
  189. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  190. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  191. ret = tegra_pcm_preallocate_dma_buffer(pcm,
  192. SNDRV_PCM_STREAM_PLAYBACK);
  193. if (ret)
  194. goto err;
  195. }
  196. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  197. ret = tegra_pcm_preallocate_dma_buffer(pcm,
  198. SNDRV_PCM_STREAM_CAPTURE);
  199. if (ret)
  200. goto err_free_play;
  201. }
  202. return 0;
  203. err_free_play:
  204. tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
  205. err:
  206. return ret;
  207. }
  208. static void tegra_pcm_free(struct snd_pcm *pcm)
  209. {
  210. tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_CAPTURE);
  211. tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
  212. }
  213. static struct snd_soc_platform_driver tegra_pcm_platform = {
  214. .ops = &tegra_pcm_ops,
  215. .pcm_new = tegra_pcm_new,
  216. .pcm_free = tegra_pcm_free,
  217. };
  218. int tegra_pcm_platform_register(struct device *dev)
  219. {
  220. return snd_soc_register_platform(dev, &tegra_pcm_platform);
  221. }
  222. EXPORT_SYMBOL_GPL(tegra_pcm_platform_register);
  223. void tegra_pcm_platform_unregister(struct device *dev)
  224. {
  225. snd_soc_unregister_platform(dev);
  226. }
  227. EXPORT_SYMBOL_GPL(tegra_pcm_platform_unregister);
  228. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  229. MODULE_DESCRIPTION("Tegra PCM ASoC driver");
  230. MODULE_LICENSE("GPL");