omap-pcm.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "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 const 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. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  47. SNDRV_PCM_FMTBIT_S32_LE,
  48. .period_bytes_min = 32,
  49. .period_bytes_max = 64 * 1024,
  50. .periods_min = 2,
  51. .periods_max = 255,
  52. .buffer_bytes_max = 128 * 1024,
  53. };
  54. static int omap_pcm_get_dma_buswidth(int num_bits)
  55. {
  56. int buswidth;
  57. switch (num_bits) {
  58. case 16:
  59. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  60. break;
  61. case 32:
  62. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  63. break;
  64. default:
  65. buswidth = -EINVAL;
  66. break;
  67. }
  68. return buswidth;
  69. }
  70. /* this may get called several times by oss emulation */
  71. static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
  72. struct snd_pcm_hw_params *params)
  73. {
  74. struct snd_pcm_runtime *runtime = substream->runtime;
  75. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  76. struct omap_pcm_dma_data *dma_data;
  77. struct dma_slave_config config;
  78. struct dma_chan *chan;
  79. int err = 0;
  80. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  81. /* return if this is a bufferless transfer e.g.
  82. * codec <--> BT codec or GSM modem -- lg FIXME */
  83. if (!dma_data)
  84. return 0;
  85. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  86. runtime->dma_bytes = params_buffer_bytes(params);
  87. chan = snd_dmaengine_pcm_get_chan(substream);
  88. if (!chan)
  89. return -EINVAL;
  90. /* fills in addr_width and direction */
  91. err = snd_hwparams_to_dma_slave_config(substream, params, &config);
  92. if (err)
  93. return err;
  94. /* Override the *_dma addr_width if requested by the DAI driver */
  95. if (dma_data->data_type) {
  96. int buswidth = omap_pcm_get_dma_buswidth(dma_data->data_type);
  97. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  98. config.dst_addr_width = buswidth;
  99. else
  100. config.src_addr_width = buswidth;
  101. }
  102. config.src_addr = dma_data->port_addr;
  103. config.dst_addr = dma_data->port_addr;
  104. config.src_maxburst = dma_data->packet_size;
  105. config.dst_maxburst = dma_data->packet_size;
  106. return dmaengine_slave_config(chan, &config);
  107. }
  108. static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
  109. {
  110. snd_pcm_set_runtime_buffer(substream, NULL);
  111. return 0;
  112. }
  113. static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  114. {
  115. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  116. struct omap_pcm_dma_data *dma_data;
  117. int ret = 0;
  118. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  119. switch (cmd) {
  120. case SNDRV_PCM_TRIGGER_START:
  121. case SNDRV_PCM_TRIGGER_RESUME:
  122. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  123. /* Configure McBSP internal buffer usage */
  124. if (dma_data->set_threshold)
  125. dma_data->set_threshold(substream);
  126. break;
  127. case SNDRV_PCM_TRIGGER_STOP:
  128. case SNDRV_PCM_TRIGGER_SUSPEND:
  129. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  130. break;
  131. default:
  132. ret = -EINVAL;
  133. }
  134. if (ret == 0)
  135. ret = snd_dmaengine_pcm_trigger(substream, cmd);
  136. return ret;
  137. }
  138. static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
  139. {
  140. snd_pcm_uframes_t offset;
  141. if (pcm_omap1510())
  142. offset = snd_dmaengine_pcm_pointer_no_residue(substream);
  143. else
  144. offset = snd_dmaengine_pcm_pointer(substream);
  145. return offset;
  146. }
  147. static int omap_pcm_open(struct snd_pcm_substream *substream)
  148. {
  149. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  150. struct omap_pcm_dma_data *dma_data;
  151. snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
  152. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  153. return snd_dmaengine_pcm_open(substream, omap_dma_filter_fn,
  154. &dma_data->dma_req);
  155. }
  156. static int omap_pcm_close(struct snd_pcm_substream *substream)
  157. {
  158. snd_dmaengine_pcm_close(substream);
  159. return 0;
  160. }
  161. static int omap_pcm_mmap(struct snd_pcm_substream *substream,
  162. struct vm_area_struct *vma)
  163. {
  164. struct snd_pcm_runtime *runtime = substream->runtime;
  165. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  166. runtime->dma_area,
  167. runtime->dma_addr,
  168. runtime->dma_bytes);
  169. }
  170. static struct snd_pcm_ops omap_pcm_ops = {
  171. .open = omap_pcm_open,
  172. .close = omap_pcm_close,
  173. .ioctl = snd_pcm_lib_ioctl,
  174. .hw_params = omap_pcm_hw_params,
  175. .hw_free = omap_pcm_hw_free,
  176. .trigger = omap_pcm_trigger,
  177. .pointer = omap_pcm_pointer,
  178. .mmap = omap_pcm_mmap,
  179. };
  180. static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
  181. static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  182. int stream)
  183. {
  184. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  185. struct snd_dma_buffer *buf = &substream->dma_buffer;
  186. size_t size = omap_pcm_hardware.buffer_bytes_max;
  187. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  188. buf->dev.dev = pcm->card->dev;
  189. buf->private_data = NULL;
  190. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  191. &buf->addr, GFP_KERNEL);
  192. if (!buf->area)
  193. return -ENOMEM;
  194. buf->bytes = size;
  195. return 0;
  196. }
  197. static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
  198. {
  199. struct snd_pcm_substream *substream;
  200. struct snd_dma_buffer *buf;
  201. int stream;
  202. for (stream = 0; stream < 2; stream++) {
  203. substream = pcm->streams[stream].substream;
  204. if (!substream)
  205. continue;
  206. buf = &substream->dma_buffer;
  207. if (!buf->area)
  208. continue;
  209. dma_free_writecombine(pcm->card->dev, buf->bytes,
  210. buf->area, buf->addr);
  211. buf->area = NULL;
  212. }
  213. }
  214. static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
  215. {
  216. struct snd_card *card = rtd->card->snd_card;
  217. struct snd_pcm *pcm = rtd->pcm;
  218. int ret = 0;
  219. if (!card->dev->dma_mask)
  220. card->dev->dma_mask = &omap_pcm_dmamask;
  221. if (!card->dev->coherent_dma_mask)
  222. card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
  223. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  224. ret = omap_pcm_preallocate_dma_buffer(pcm,
  225. SNDRV_PCM_STREAM_PLAYBACK);
  226. if (ret)
  227. goto out;
  228. }
  229. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  230. ret = omap_pcm_preallocate_dma_buffer(pcm,
  231. SNDRV_PCM_STREAM_CAPTURE);
  232. if (ret)
  233. goto out;
  234. }
  235. out:
  236. /* free preallocated buffers in case of error */
  237. if (ret)
  238. omap_pcm_free_dma_buffers(pcm);
  239. return ret;
  240. }
  241. static struct snd_soc_platform_driver omap_soc_platform = {
  242. .ops = &omap_pcm_ops,
  243. .pcm_new = omap_pcm_new,
  244. .pcm_free = omap_pcm_free_dma_buffers,
  245. };
  246. static int omap_pcm_probe(struct platform_device *pdev)
  247. {
  248. return snd_soc_register_platform(&pdev->dev,
  249. &omap_soc_platform);
  250. }
  251. static int omap_pcm_remove(struct platform_device *pdev)
  252. {
  253. snd_soc_unregister_platform(&pdev->dev);
  254. return 0;
  255. }
  256. static struct platform_driver omap_pcm_driver = {
  257. .driver = {
  258. .name = "omap-pcm-audio",
  259. .owner = THIS_MODULE,
  260. },
  261. .probe = omap_pcm_probe,
  262. .remove = omap_pcm_remove,
  263. };
  264. module_platform_driver(omap_pcm_driver);
  265. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
  266. MODULE_DESCRIPTION("OMAP PCM DMA module");
  267. MODULE_LICENSE("GPL");
  268. MODULE_ALIAS("platform:omap-pcm-audio");