omap-pcm.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * omap-pcm.c -- ALSA PCM interface for the OMAP SoC
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * Contact: Jarkko Nikula <jhnikula@gmail.com>
  7. * Peter Ujfalusi <peter.ujfalusi@nokia.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 <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <mach/dma.h>
  30. #include "omap-pcm.h"
  31. static const struct snd_pcm_hardware omap_pcm_hardware = {
  32. .info = SNDRV_PCM_INFO_MMAP |
  33. SNDRV_PCM_INFO_MMAP_VALID |
  34. SNDRV_PCM_INFO_INTERLEAVED |
  35. SNDRV_PCM_INFO_PAUSE |
  36. SNDRV_PCM_INFO_RESUME,
  37. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  38. .period_bytes_min = 32,
  39. .period_bytes_max = 64 * 1024,
  40. .periods_min = 2,
  41. .periods_max = 255,
  42. .buffer_bytes_max = 128 * 1024,
  43. };
  44. struct omap_runtime_data {
  45. spinlock_t lock;
  46. struct omap_pcm_dma_data *dma_data;
  47. int dma_ch;
  48. int period_index;
  49. };
  50. static void omap_pcm_dma_irq(int ch, u16 stat, void *data)
  51. {
  52. struct snd_pcm_substream *substream = data;
  53. struct snd_pcm_runtime *runtime = substream->runtime;
  54. struct omap_runtime_data *prtd = runtime->private_data;
  55. unsigned long flags;
  56. if (cpu_is_omap1510()) {
  57. /*
  58. * OMAP1510 doesn't support DMA chaining so have to restart
  59. * the transfer after all periods are transferred
  60. */
  61. spin_lock_irqsave(&prtd->lock, flags);
  62. if (prtd->period_index >= 0) {
  63. if (++prtd->period_index == runtime->periods) {
  64. prtd->period_index = 0;
  65. omap_start_dma(prtd->dma_ch);
  66. }
  67. }
  68. spin_unlock_irqrestore(&prtd->lock, flags);
  69. }
  70. snd_pcm_period_elapsed(substream);
  71. }
  72. /* this may get called several times by oss emulation */
  73. static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
  74. struct snd_pcm_hw_params *params)
  75. {
  76. struct snd_pcm_runtime *runtime = substream->runtime;
  77. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  78. struct omap_runtime_data *prtd = runtime->private_data;
  79. struct omap_pcm_dma_data *dma_data = rtd->dai->cpu_dai->dma_data;
  80. int err = 0;
  81. if (!dma_data)
  82. return -ENODEV;
  83. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  84. runtime->dma_bytes = params_buffer_bytes(params);
  85. if (prtd->dma_data)
  86. return 0;
  87. prtd->dma_data = dma_data;
  88. err = omap_request_dma(dma_data->dma_req, dma_data->name,
  89. omap_pcm_dma_irq, substream, &prtd->dma_ch);
  90. if (!err && !cpu_is_omap1510()) {
  91. /*
  92. * Link channel with itself so DMA doesn't need any
  93. * reprogramming while looping the buffer
  94. */
  95. omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
  96. }
  97. return err;
  98. }
  99. static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
  100. {
  101. struct snd_pcm_runtime *runtime = substream->runtime;
  102. struct omap_runtime_data *prtd = runtime->private_data;
  103. if (prtd->dma_data == NULL)
  104. return 0;
  105. if (!cpu_is_omap1510())
  106. omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
  107. omap_free_dma(prtd->dma_ch);
  108. prtd->dma_data = NULL;
  109. snd_pcm_set_runtime_buffer(substream, NULL);
  110. return 0;
  111. }
  112. static int omap_pcm_prepare(struct snd_pcm_substream *substream)
  113. {
  114. struct snd_pcm_runtime *runtime = substream->runtime;
  115. struct omap_runtime_data *prtd = runtime->private_data;
  116. struct omap_pcm_dma_data *dma_data = prtd->dma_data;
  117. struct omap_dma_channel_params dma_params;
  118. memset(&dma_params, 0, sizeof(dma_params));
  119. /*
  120. * Note: Regardless of interface data formats supported by OMAP McBSP
  121. * or EAC blocks, internal representation is always fixed 16-bit/sample
  122. */
  123. dma_params.data_type = OMAP_DMA_DATA_TYPE_S16;
  124. dma_params.trigger = dma_data->dma_req;
  125. dma_params.sync_mode = OMAP_DMA_SYNC_ELEMENT;
  126. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  127. dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
  128. dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT;
  129. dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC;
  130. dma_params.src_start = runtime->dma_addr;
  131. dma_params.dst_start = dma_data->port_addr;
  132. dma_params.dst_port = OMAP_DMA_PORT_MPUI;
  133. } else {
  134. dma_params.src_amode = OMAP_DMA_AMODE_CONSTANT;
  135. dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC;
  136. dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC;
  137. dma_params.src_start = dma_data->port_addr;
  138. dma_params.dst_start = runtime->dma_addr;
  139. dma_params.src_port = OMAP_DMA_PORT_MPUI;
  140. }
  141. /*
  142. * Set DMA transfer frame size equal to ALSA period size and frame
  143. * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
  144. * we can transfer the whole ALSA buffer with single DMA transfer but
  145. * still can get an interrupt at each period bounary
  146. */
  147. dma_params.elem_count = snd_pcm_lib_period_bytes(substream) / 2;
  148. dma_params.frame_count = runtime->periods;
  149. omap_set_dma_params(prtd->dma_ch, &dma_params);
  150. omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
  151. return 0;
  152. }
  153. static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  154. {
  155. struct snd_pcm_runtime *runtime = substream->runtime;
  156. struct omap_runtime_data *prtd = runtime->private_data;
  157. unsigned long flags;
  158. int ret = 0;
  159. spin_lock_irqsave(&prtd->lock, flags);
  160. switch (cmd) {
  161. case SNDRV_PCM_TRIGGER_START:
  162. case SNDRV_PCM_TRIGGER_RESUME:
  163. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  164. prtd->period_index = 0;
  165. omap_start_dma(prtd->dma_ch);
  166. break;
  167. case SNDRV_PCM_TRIGGER_STOP:
  168. case SNDRV_PCM_TRIGGER_SUSPEND:
  169. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  170. prtd->period_index = -1;
  171. omap_stop_dma(prtd->dma_ch);
  172. break;
  173. default:
  174. ret = -EINVAL;
  175. }
  176. spin_unlock_irqrestore(&prtd->lock, flags);
  177. return ret;
  178. }
  179. static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
  180. {
  181. struct snd_pcm_runtime *runtime = substream->runtime;
  182. struct omap_runtime_data *prtd = runtime->private_data;
  183. dma_addr_t ptr;
  184. snd_pcm_uframes_t offset;
  185. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  186. ptr = omap_get_dma_src_pos(prtd->dma_ch);
  187. else
  188. ptr = omap_get_dma_dst_pos(prtd->dma_ch);
  189. offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
  190. if (offset >= runtime->buffer_size)
  191. offset = 0;
  192. return offset;
  193. }
  194. static int omap_pcm_open(struct snd_pcm_substream *substream)
  195. {
  196. struct snd_pcm_runtime *runtime = substream->runtime;
  197. struct omap_runtime_data *prtd;
  198. int ret;
  199. snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
  200. /* Ensure that buffer size is a multiple of period size */
  201. ret = snd_pcm_hw_constraint_integer(runtime,
  202. SNDRV_PCM_HW_PARAM_PERIODS);
  203. if (ret < 0)
  204. goto out;
  205. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  206. if (prtd == NULL) {
  207. ret = -ENOMEM;
  208. goto out;
  209. }
  210. spin_lock_init(&prtd->lock);
  211. runtime->private_data = prtd;
  212. out:
  213. return ret;
  214. }
  215. static int omap_pcm_close(struct snd_pcm_substream *substream)
  216. {
  217. struct snd_pcm_runtime *runtime = substream->runtime;
  218. kfree(runtime->private_data);
  219. return 0;
  220. }
  221. static int omap_pcm_mmap(struct snd_pcm_substream *substream,
  222. struct vm_area_struct *vma)
  223. {
  224. struct snd_pcm_runtime *runtime = substream->runtime;
  225. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  226. runtime->dma_area,
  227. runtime->dma_addr,
  228. runtime->dma_bytes);
  229. }
  230. static struct snd_pcm_ops omap_pcm_ops = {
  231. .open = omap_pcm_open,
  232. .close = omap_pcm_close,
  233. .ioctl = snd_pcm_lib_ioctl,
  234. .hw_params = omap_pcm_hw_params,
  235. .hw_free = omap_pcm_hw_free,
  236. .prepare = omap_pcm_prepare,
  237. .trigger = omap_pcm_trigger,
  238. .pointer = omap_pcm_pointer,
  239. .mmap = omap_pcm_mmap,
  240. };
  241. static u64 omap_pcm_dmamask = DMA_BIT_MASK(32);
  242. static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  243. int stream)
  244. {
  245. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  246. struct snd_dma_buffer *buf = &substream->dma_buffer;
  247. size_t size = omap_pcm_hardware.buffer_bytes_max;
  248. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  249. buf->dev.dev = pcm->card->dev;
  250. buf->private_data = NULL;
  251. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  252. &buf->addr, GFP_KERNEL);
  253. if (!buf->area)
  254. return -ENOMEM;
  255. buf->bytes = size;
  256. return 0;
  257. }
  258. static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
  259. {
  260. struct snd_pcm_substream *substream;
  261. struct snd_dma_buffer *buf;
  262. int stream;
  263. for (stream = 0; stream < 2; stream++) {
  264. substream = pcm->streams[stream].substream;
  265. if (!substream)
  266. continue;
  267. buf = &substream->dma_buffer;
  268. if (!buf->area)
  269. continue;
  270. dma_free_writecombine(pcm->card->dev, buf->bytes,
  271. buf->area, buf->addr);
  272. buf->area = NULL;
  273. }
  274. }
  275. int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
  276. struct snd_pcm *pcm)
  277. {
  278. int ret = 0;
  279. if (!card->dev->dma_mask)
  280. card->dev->dma_mask = &omap_pcm_dmamask;
  281. if (!card->dev->coherent_dma_mask)
  282. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  283. if (dai->playback.channels_min) {
  284. ret = omap_pcm_preallocate_dma_buffer(pcm,
  285. SNDRV_PCM_STREAM_PLAYBACK);
  286. if (ret)
  287. goto out;
  288. }
  289. if (dai->capture.channels_min) {
  290. ret = omap_pcm_preallocate_dma_buffer(pcm,
  291. SNDRV_PCM_STREAM_CAPTURE);
  292. if (ret)
  293. goto out;
  294. }
  295. out:
  296. return ret;
  297. }
  298. struct snd_soc_platform omap_soc_platform = {
  299. .name = "omap-pcm-audio",
  300. .pcm_ops = &omap_pcm_ops,
  301. .pcm_new = omap_pcm_new,
  302. .pcm_free = omap_pcm_free_dma_buffers,
  303. };
  304. EXPORT_SYMBOL_GPL(omap_soc_platform);
  305. static int __init omap_soc_platform_init(void)
  306. {
  307. return snd_soc_register_platform(&omap_soc_platform);
  308. }
  309. module_init(omap_soc_platform_init);
  310. static void __exit omap_soc_platform_exit(void)
  311. {
  312. snd_soc_unregister_platform(&omap_soc_platform);
  313. }
  314. module_exit(omap_soc_platform_exit);
  315. MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
  316. MODULE_DESCRIPTION("OMAP PCM DMA module");
  317. MODULE_LICENSE("GPL");