omap-pcm.c 9.4 KB

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