omap-pcm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. /* 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. if (prtd->dma_data)
  88. return 0;
  89. prtd->dma_data = dma_data;
  90. err = omap_request_dma(dma_data->dma_req, dma_data->name,
  91. omap_pcm_dma_irq, substream, &prtd->dma_ch);
  92. if (!err && !cpu_is_omap1510()) {
  93. /*
  94. * Link channel with itself so DMA doesn't need any
  95. * reprogramming while looping the buffer
  96. */
  97. omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
  98. }
  99. return err;
  100. }
  101. static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
  102. {
  103. struct snd_pcm_runtime *runtime = substream->runtime;
  104. struct omap_runtime_data *prtd = runtime->private_data;
  105. if (prtd->dma_data == NULL)
  106. return 0;
  107. if (!cpu_is_omap1510())
  108. omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
  109. omap_free_dma(prtd->dma_ch);
  110. prtd->dma_data = NULL;
  111. snd_pcm_set_runtime_buffer(substream, NULL);
  112. return 0;
  113. }
  114. static int omap_pcm_prepare(struct snd_pcm_substream *substream)
  115. {
  116. struct snd_pcm_runtime *runtime = substream->runtime;
  117. struct omap_runtime_data *prtd = runtime->private_data;
  118. struct omap_pcm_dma_data *dma_data = prtd->dma_data;
  119. struct omap_dma_channel_params dma_params;
  120. /* return if this is a bufferless transfer e.g.
  121. * codec <--> BT codec or GSM modem -- lg FIXME */
  122. if (!prtd->dma_data)
  123. return 0;
  124. memset(&dma_params, 0, sizeof(dma_params));
  125. /*
  126. * Note: Regardless of interface data formats supported by OMAP McBSP
  127. * or EAC blocks, internal representation is always fixed 16-bit/sample
  128. */
  129. dma_params.data_type = OMAP_DMA_DATA_TYPE_S16;
  130. dma_params.trigger = dma_data->dma_req;
  131. dma_params.sync_mode = OMAP_DMA_SYNC_ELEMENT;
  132. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  133. dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
  134. dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT;
  135. dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC;
  136. dma_params.src_start = runtime->dma_addr;
  137. dma_params.dst_start = dma_data->port_addr;
  138. dma_params.dst_port = OMAP_DMA_PORT_MPUI;
  139. } else {
  140. dma_params.src_amode = OMAP_DMA_AMODE_CONSTANT;
  141. dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC;
  142. dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC;
  143. dma_params.src_start = dma_data->port_addr;
  144. dma_params.dst_start = runtime->dma_addr;
  145. dma_params.src_port = OMAP_DMA_PORT_MPUI;
  146. }
  147. /*
  148. * Set DMA transfer frame size equal to ALSA period size and frame
  149. * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
  150. * we can transfer the whole ALSA buffer with single DMA transfer but
  151. * still can get an interrupt at each period bounary
  152. */
  153. dma_params.elem_count = snd_pcm_lib_period_bytes(substream) / 2;
  154. dma_params.frame_count = runtime->periods;
  155. omap_set_dma_params(prtd->dma_ch, &dma_params);
  156. omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
  157. return 0;
  158. }
  159. static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  160. {
  161. struct snd_pcm_runtime *runtime = substream->runtime;
  162. struct omap_runtime_data *prtd = runtime->private_data;
  163. unsigned long flags;
  164. int ret = 0;
  165. spin_lock_irqsave(&prtd->lock, flags);
  166. switch (cmd) {
  167. case SNDRV_PCM_TRIGGER_START:
  168. case SNDRV_PCM_TRIGGER_RESUME:
  169. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  170. prtd->period_index = 0;
  171. omap_start_dma(prtd->dma_ch);
  172. break;
  173. case SNDRV_PCM_TRIGGER_STOP:
  174. case SNDRV_PCM_TRIGGER_SUSPEND:
  175. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  176. prtd->period_index = -1;
  177. omap_stop_dma(prtd->dma_ch);
  178. break;
  179. default:
  180. ret = -EINVAL;
  181. }
  182. spin_unlock_irqrestore(&prtd->lock, flags);
  183. return ret;
  184. }
  185. static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
  186. {
  187. struct snd_pcm_runtime *runtime = substream->runtime;
  188. struct omap_runtime_data *prtd = runtime->private_data;
  189. dma_addr_t ptr;
  190. snd_pcm_uframes_t offset;
  191. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  192. ptr = omap_get_dma_src_pos(prtd->dma_ch);
  193. else
  194. ptr = omap_get_dma_dst_pos(prtd->dma_ch);
  195. offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
  196. if (offset >= runtime->buffer_size)
  197. offset = 0;
  198. return offset;
  199. }
  200. static int omap_pcm_open(struct snd_pcm_substream *substream)
  201. {
  202. struct snd_pcm_runtime *runtime = substream->runtime;
  203. struct omap_runtime_data *prtd;
  204. int ret;
  205. snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
  206. /* Ensure that buffer size is a multiple of period size */
  207. ret = snd_pcm_hw_constraint_integer(runtime,
  208. SNDRV_PCM_HW_PARAM_PERIODS);
  209. if (ret < 0)
  210. goto out;
  211. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  212. if (prtd == NULL) {
  213. ret = -ENOMEM;
  214. goto out;
  215. }
  216. spin_lock_init(&prtd->lock);
  217. runtime->private_data = prtd;
  218. out:
  219. return ret;
  220. }
  221. static int omap_pcm_close(struct snd_pcm_substream *substream)
  222. {
  223. struct snd_pcm_runtime *runtime = substream->runtime;
  224. kfree(runtime->private_data);
  225. return 0;
  226. }
  227. static int omap_pcm_mmap(struct snd_pcm_substream *substream,
  228. struct vm_area_struct *vma)
  229. {
  230. struct snd_pcm_runtime *runtime = substream->runtime;
  231. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  232. runtime->dma_area,
  233. runtime->dma_addr,
  234. runtime->dma_bytes);
  235. }
  236. static struct snd_pcm_ops omap_pcm_ops = {
  237. .open = omap_pcm_open,
  238. .close = omap_pcm_close,
  239. .ioctl = snd_pcm_lib_ioctl,
  240. .hw_params = omap_pcm_hw_params,
  241. .hw_free = omap_pcm_hw_free,
  242. .prepare = omap_pcm_prepare,
  243. .trigger = omap_pcm_trigger,
  244. .pointer = omap_pcm_pointer,
  245. .mmap = omap_pcm_mmap,
  246. };
  247. static u64 omap_pcm_dmamask = DMA_BIT_MASK(32);
  248. static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  249. int stream)
  250. {
  251. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  252. struct snd_dma_buffer *buf = &substream->dma_buffer;
  253. size_t size = omap_pcm_hardware.buffer_bytes_max;
  254. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  255. buf->dev.dev = pcm->card->dev;
  256. buf->private_data = NULL;
  257. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  258. &buf->addr, GFP_KERNEL);
  259. if (!buf->area)
  260. return -ENOMEM;
  261. buf->bytes = size;
  262. return 0;
  263. }
  264. static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
  265. {
  266. struct snd_pcm_substream *substream;
  267. struct snd_dma_buffer *buf;
  268. int stream;
  269. for (stream = 0; stream < 2; stream++) {
  270. substream = pcm->streams[stream].substream;
  271. if (!substream)
  272. continue;
  273. buf = &substream->dma_buffer;
  274. if (!buf->area)
  275. continue;
  276. dma_free_writecombine(pcm->card->dev, buf->bytes,
  277. buf->area, buf->addr);
  278. buf->area = NULL;
  279. }
  280. }
  281. int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
  282. struct snd_pcm *pcm)
  283. {
  284. int ret = 0;
  285. if (!card->dev->dma_mask)
  286. card->dev->dma_mask = &omap_pcm_dmamask;
  287. if (!card->dev->coherent_dma_mask)
  288. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  289. if (dai->playback.channels_min) {
  290. ret = omap_pcm_preallocate_dma_buffer(pcm,
  291. SNDRV_PCM_STREAM_PLAYBACK);
  292. if (ret)
  293. goto out;
  294. }
  295. if (dai->capture.channels_min) {
  296. ret = omap_pcm_preallocate_dma_buffer(pcm,
  297. SNDRV_PCM_STREAM_CAPTURE);
  298. if (ret)
  299. goto out;
  300. }
  301. out:
  302. return ret;
  303. }
  304. struct snd_soc_platform omap_soc_platform = {
  305. .name = "omap-pcm-audio",
  306. .pcm_ops = &omap_pcm_ops,
  307. .pcm_new = omap_pcm_new,
  308. .pcm_free = omap_pcm_free_dma_buffers,
  309. };
  310. EXPORT_SYMBOL_GPL(omap_soc_platform);
  311. static int __init omap_soc_platform_init(void)
  312. {
  313. return snd_soc_register_platform(&omap_soc_platform);
  314. }
  315. module_init(omap_soc_platform_init);
  316. static void __exit omap_soc_platform_exit(void)
  317. {
  318. snd_soc_unregister_platform(&omap_soc_platform);
  319. }
  320. module_exit(omap_soc_platform_exit);
  321. MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
  322. MODULE_DESCRIPTION("OMAP PCM DMA module");
  323. MODULE_LICENSE("GPL");