omap-pcm.c 11 KB

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