omap-pcm.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #include <sound/omap-pcm.h>
  34. #ifdef CONFIG_ARCH_OMAP1
  35. #define pcm_omap1510() cpu_is_omap1510()
  36. #else
  37. #define pcm_omap1510() 0
  38. #endif
  39. static struct snd_pcm_hardware omap_pcm_hardware = {
  40. .info = SNDRV_PCM_INFO_MMAP |
  41. SNDRV_PCM_INFO_MMAP_VALID |
  42. SNDRV_PCM_INFO_INTERLEAVED |
  43. SNDRV_PCM_INFO_PAUSE |
  44. SNDRV_PCM_INFO_RESUME |
  45. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
  46. .period_bytes_min = 32,
  47. .period_bytes_max = 64 * 1024,
  48. .periods_min = 2,
  49. .periods_max = 255,
  50. .buffer_bytes_max = 128 * 1024,
  51. };
  52. /* sDMA supports only 1, 2, and 4 byte transfer elements. */
  53. static void omap_pcm_limit_supported_formats(void)
  54. {
  55. int i;
  56. for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
  57. switch (snd_pcm_format_physical_width(i)) {
  58. case 8:
  59. case 16:
  60. case 32:
  61. omap_pcm_hardware.formats |= (1LL << i);
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. }
  68. /* this may get called several times by oss emulation */
  69. static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
  70. struct snd_pcm_hw_params *params)
  71. {
  72. struct snd_pcm_runtime *runtime = substream->runtime;
  73. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  74. struct omap_pcm_dma_data *dma_data;
  75. struct dma_slave_config config;
  76. struct dma_chan *chan;
  77. int err = 0;
  78. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  79. /* return if this is a bufferless transfer e.g.
  80. * codec <--> BT codec or GSM modem -- lg FIXME */
  81. if (!dma_data)
  82. return 0;
  83. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  84. runtime->dma_bytes = params_buffer_bytes(params);
  85. chan = snd_dmaengine_pcm_get_chan(substream);
  86. if (!chan)
  87. return -EINVAL;
  88. /* fills in addr_width and direction */
  89. err = snd_hwparams_to_dma_slave_config(substream, params, &config);
  90. if (err)
  91. return err;
  92. snd_dmaengine_pcm_set_config_from_dai_data(substream,
  93. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream),
  94. &config);
  95. return dmaengine_slave_config(chan, &config);
  96. }
  97. static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
  98. {
  99. snd_pcm_set_runtime_buffer(substream, NULL);
  100. return 0;
  101. }
  102. static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
  103. {
  104. snd_pcm_uframes_t offset;
  105. if (pcm_omap1510())
  106. offset = snd_dmaengine_pcm_pointer_no_residue(substream);
  107. else
  108. offset = snd_dmaengine_pcm_pointer(substream);
  109. return offset;
  110. }
  111. static int omap_pcm_open(struct snd_pcm_substream *substream)
  112. {
  113. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  114. struct snd_dmaengine_dai_dma_data *dma_data;
  115. int ret;
  116. snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
  117. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  118. /* DT boot: filter_data is the DMA name */
  119. if (rtd->cpu_dai->dev->of_node) {
  120. struct dma_chan *chan;
  121. chan = dma_request_slave_channel(rtd->cpu_dai->dev,
  122. dma_data->filter_data);
  123. ret = snd_dmaengine_pcm_open(substream, chan);
  124. } else {
  125. ret = snd_dmaengine_pcm_open_request_chan(substream,
  126. omap_dma_filter_fn,
  127. dma_data->filter_data);
  128. }
  129. return ret;
  130. }
  131. static int omap_pcm_mmap(struct snd_pcm_substream *substream,
  132. struct vm_area_struct *vma)
  133. {
  134. struct snd_pcm_runtime *runtime = substream->runtime;
  135. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  136. runtime->dma_area,
  137. runtime->dma_addr,
  138. runtime->dma_bytes);
  139. }
  140. static struct snd_pcm_ops omap_pcm_ops = {
  141. .open = omap_pcm_open,
  142. .close = snd_dmaengine_pcm_close_release_chan,
  143. .ioctl = snd_pcm_lib_ioctl,
  144. .hw_params = omap_pcm_hw_params,
  145. .hw_free = omap_pcm_hw_free,
  146. .trigger = snd_dmaengine_pcm_trigger,
  147. .pointer = omap_pcm_pointer,
  148. .mmap = omap_pcm_mmap,
  149. };
  150. static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  151. 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 = omap_pcm_hardware.buffer_bytes_max;
  156. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  157. buf->dev.dev = pcm->card->dev;
  158. buf->private_data = NULL;
  159. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  160. &buf->addr, GFP_KERNEL);
  161. if (!buf->area)
  162. return -ENOMEM;
  163. buf->bytes = size;
  164. return 0;
  165. }
  166. static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
  167. {
  168. struct snd_pcm_substream *substream;
  169. struct snd_dma_buffer *buf;
  170. int stream;
  171. for (stream = 0; stream < 2; stream++) {
  172. substream = pcm->streams[stream].substream;
  173. if (!substream)
  174. continue;
  175. buf = &substream->dma_buffer;
  176. if (!buf->area)
  177. continue;
  178. dma_free_writecombine(pcm->card->dev, buf->bytes,
  179. buf->area, buf->addr);
  180. buf->area = NULL;
  181. }
  182. }
  183. static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
  184. {
  185. struct snd_card *card = rtd->card->snd_card;
  186. struct snd_pcm *pcm = rtd->pcm;
  187. int ret;
  188. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  189. if (ret)
  190. return ret;
  191. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  192. ret = omap_pcm_preallocate_dma_buffer(pcm,
  193. SNDRV_PCM_STREAM_PLAYBACK);
  194. if (ret)
  195. goto out;
  196. }
  197. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  198. ret = omap_pcm_preallocate_dma_buffer(pcm,
  199. SNDRV_PCM_STREAM_CAPTURE);
  200. if (ret)
  201. goto out;
  202. }
  203. out:
  204. /* free preallocated buffers in case of error */
  205. if (ret)
  206. omap_pcm_free_dma_buffers(pcm);
  207. return ret;
  208. }
  209. static struct snd_soc_platform_driver omap_soc_platform = {
  210. .ops = &omap_pcm_ops,
  211. .pcm_new = omap_pcm_new,
  212. .pcm_free = omap_pcm_free_dma_buffers,
  213. };
  214. int omap_pcm_platform_register(struct device *dev)
  215. {
  216. omap_pcm_limit_supported_formats();
  217. return devm_snd_soc_register_platform(dev, &omap_soc_platform);
  218. }
  219. EXPORT_SYMBOL_GPL(omap_pcm_platform_register);
  220. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
  221. MODULE_DESCRIPTION("OMAP PCM DMA module");
  222. MODULE_LICENSE("GPL");