omap-pcm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * omap-pcm.c -- ALSA PCM interface for the OMAP SoC
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
  7. * Peter Ujfalusi <peter.ujfalusi@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. #include <linux/dma-mapping.h>
  25. #include <linux/slab.h>
  26. #include <linux/module.h>
  27. #include <linux/omap-dma.h>
  28. #include <sound/core.h>
  29. #include <sound/pcm.h>
  30. #include <sound/pcm_params.h>
  31. #include <sound/dmaengine_pcm.h>
  32. #include <sound/soc.h>
  33. #ifdef CONFIG_ARCH_OMAP1
  34. #define pcm_omap1510() cpu_is_omap1510()
  35. #else
  36. #define pcm_omap1510() 0
  37. #endif
  38. static const struct snd_pcm_hardware omap_pcm_hardware = {
  39. .info = SNDRV_PCM_INFO_MMAP |
  40. SNDRV_PCM_INFO_MMAP_VALID |
  41. SNDRV_PCM_INFO_INTERLEAVED |
  42. SNDRV_PCM_INFO_PAUSE |
  43. SNDRV_PCM_INFO_RESUME |
  44. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
  45. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  46. SNDRV_PCM_FMTBIT_S32_LE,
  47. .period_bytes_min = 32,
  48. .period_bytes_max = 64 * 1024,
  49. .periods_min = 2,
  50. .periods_max = 255,
  51. .buffer_bytes_max = 128 * 1024,
  52. };
  53. /* this may get called several times by oss emulation */
  54. static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
  55. struct snd_pcm_hw_params *params)
  56. {
  57. struct snd_pcm_runtime *runtime = substream->runtime;
  58. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  59. struct omap_pcm_dma_data *dma_data;
  60. struct dma_slave_config config;
  61. struct dma_chan *chan;
  62. int err = 0;
  63. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  64. /* return if this is a bufferless transfer e.g.
  65. * codec <--> BT codec or GSM modem -- lg FIXME */
  66. if (!dma_data)
  67. return 0;
  68. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  69. runtime->dma_bytes = params_buffer_bytes(params);
  70. chan = snd_dmaengine_pcm_get_chan(substream);
  71. if (!chan)
  72. return -EINVAL;
  73. /* fills in addr_width and direction */
  74. err = snd_hwparams_to_dma_slave_config(substream, params, &config);
  75. if (err)
  76. return err;
  77. snd_dmaengine_pcm_set_config_from_dai_data(substream,
  78. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream),
  79. &config);
  80. return dmaengine_slave_config(chan, &config);
  81. }
  82. static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
  83. {
  84. snd_pcm_set_runtime_buffer(substream, NULL);
  85. return 0;
  86. }
  87. static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
  88. {
  89. snd_pcm_uframes_t offset;
  90. if (pcm_omap1510())
  91. offset = snd_dmaengine_pcm_pointer_no_residue(substream);
  92. else
  93. offset = snd_dmaengine_pcm_pointer(substream);
  94. return offset;
  95. }
  96. static int omap_pcm_open(struct snd_pcm_substream *substream)
  97. {
  98. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  99. struct snd_dmaengine_dai_dma_data *dma_data;
  100. int ret;
  101. snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
  102. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  103. /* DT boot: filter_data is the DMA name */
  104. if (rtd->cpu_dai->dev->of_node) {
  105. struct dma_chan *chan;
  106. chan = dma_request_slave_channel(rtd->cpu_dai->dev,
  107. dma_data->filter_data);
  108. ret = snd_dmaengine_pcm_open(substream, chan);
  109. } else {
  110. ret = snd_dmaengine_pcm_open_request_chan(substream,
  111. omap_dma_filter_fn,
  112. dma_data->filter_data);
  113. }
  114. return ret;
  115. }
  116. static int omap_pcm_mmap(struct snd_pcm_substream *substream,
  117. struct vm_area_struct *vma)
  118. {
  119. struct snd_pcm_runtime *runtime = substream->runtime;
  120. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  121. runtime->dma_area,
  122. runtime->dma_addr,
  123. runtime->dma_bytes);
  124. }
  125. static struct snd_pcm_ops omap_pcm_ops = {
  126. .open = omap_pcm_open,
  127. .close = snd_dmaengine_pcm_close_release_chan,
  128. .ioctl = snd_pcm_lib_ioctl,
  129. .hw_params = omap_pcm_hw_params,
  130. .hw_free = omap_pcm_hw_free,
  131. .trigger = snd_dmaengine_pcm_trigger,
  132. .pointer = omap_pcm_pointer,
  133. .mmap = omap_pcm_mmap,
  134. };
  135. static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
  136. static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  137. int stream)
  138. {
  139. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  140. struct snd_dma_buffer *buf = &substream->dma_buffer;
  141. size_t size = omap_pcm_hardware.buffer_bytes_max;
  142. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  143. buf->dev.dev = pcm->card->dev;
  144. buf->private_data = NULL;
  145. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  146. &buf->addr, GFP_KERNEL);
  147. if (!buf->area)
  148. return -ENOMEM;
  149. buf->bytes = size;
  150. return 0;
  151. }
  152. static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
  153. {
  154. struct snd_pcm_substream *substream;
  155. struct snd_dma_buffer *buf;
  156. int stream;
  157. for (stream = 0; stream < 2; stream++) {
  158. substream = pcm->streams[stream].substream;
  159. if (!substream)
  160. continue;
  161. buf = &substream->dma_buffer;
  162. if (!buf->area)
  163. continue;
  164. dma_free_writecombine(pcm->card->dev, buf->bytes,
  165. buf->area, buf->addr);
  166. buf->area = NULL;
  167. }
  168. }
  169. static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
  170. {
  171. struct snd_card *card = rtd->card->snd_card;
  172. struct snd_pcm *pcm = rtd->pcm;
  173. int ret = 0;
  174. if (!card->dev->dma_mask)
  175. card->dev->dma_mask = &omap_pcm_dmamask;
  176. if (!card->dev->coherent_dma_mask)
  177. card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
  178. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  179. ret = omap_pcm_preallocate_dma_buffer(pcm,
  180. SNDRV_PCM_STREAM_PLAYBACK);
  181. if (ret)
  182. goto out;
  183. }
  184. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  185. ret = omap_pcm_preallocate_dma_buffer(pcm,
  186. SNDRV_PCM_STREAM_CAPTURE);
  187. if (ret)
  188. goto out;
  189. }
  190. out:
  191. /* free preallocated buffers in case of error */
  192. if (ret)
  193. omap_pcm_free_dma_buffers(pcm);
  194. return ret;
  195. }
  196. static struct snd_soc_platform_driver omap_soc_platform = {
  197. .ops = &omap_pcm_ops,
  198. .pcm_new = omap_pcm_new,
  199. .pcm_free = omap_pcm_free_dma_buffers,
  200. };
  201. static int omap_pcm_probe(struct platform_device *pdev)
  202. {
  203. return snd_soc_register_platform(&pdev->dev,
  204. &omap_soc_platform);
  205. }
  206. static int omap_pcm_remove(struct platform_device *pdev)
  207. {
  208. snd_soc_unregister_platform(&pdev->dev);
  209. return 0;
  210. }
  211. static struct platform_driver omap_pcm_driver = {
  212. .driver = {
  213. .name = "omap-pcm-audio",
  214. .owner = THIS_MODULE,
  215. },
  216. .probe = omap_pcm_probe,
  217. .remove = omap_pcm_remove,
  218. };
  219. module_platform_driver(omap_pcm_driver);
  220. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
  221. MODULE_DESCRIPTION("OMAP PCM DMA module");
  222. MODULE_LICENSE("GPL");
  223. MODULE_ALIAS("platform:omap-pcm-audio");