omap-pcm.c 8.2 KB

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