omap-pcm.c 11 KB

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