omap-pcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 <linux/slab.h>
  26. #include <sound/core.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/soc.h>
  30. #include <plat/dma.h>
  31. #include "omap-pcm.h"
  32. static const struct snd_pcm_hardware omap_pcm_hardware = {
  33. .info = SNDRV_PCM_INFO_MMAP |
  34. SNDRV_PCM_INFO_MMAP_VALID |
  35. SNDRV_PCM_INFO_INTERLEAVED |
  36. SNDRV_PCM_INFO_PAUSE |
  37. SNDRV_PCM_INFO_RESUME,
  38. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  39. SNDRV_PCM_FMTBIT_S32_LE,
  40. .period_bytes_min = 32,
  41. .period_bytes_max = 64 * 1024,
  42. .periods_min = 2,
  43. .periods_max = 255,
  44. .buffer_bytes_max = 128 * 1024,
  45. };
  46. struct omap_runtime_data {
  47. spinlock_t lock;
  48. struct omap_pcm_dma_data *dma_data;
  49. int dma_ch;
  50. int period_index;
  51. };
  52. static void omap_pcm_dma_irq(int ch, u16 stat, void *data)
  53. {
  54. struct snd_pcm_substream *substream = data;
  55. struct snd_pcm_runtime *runtime = substream->runtime;
  56. struct omap_runtime_data *prtd = runtime->private_data;
  57. unsigned long flags;
  58. if ((cpu_is_omap1510())) {
  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 progress counters
  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;
  96. int err = 0;
  97. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  98. /* return if this is a bufferless transfer e.g.
  99. * codec <--> BT codec or GSM modem -- lg FIXME */
  100. if (!dma_data)
  101. return 0;
  102. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  103. runtime->dma_bytes = params_buffer_bytes(params);
  104. if (prtd->dma_data)
  105. return 0;
  106. prtd->dma_data = dma_data;
  107. err = omap_request_dma(dma_data->dma_req, dma_data->name,
  108. omap_pcm_dma_irq, substream, &prtd->dma_ch);
  109. if (!err) {
  110. /*
  111. * Link channel with itself so DMA doesn't need any
  112. * reprogramming while looping the buffer
  113. */
  114. omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
  115. }
  116. return err;
  117. }
  118. static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
  119. {
  120. struct snd_pcm_runtime *runtime = substream->runtime;
  121. struct omap_runtime_data *prtd = runtime->private_data;
  122. if (prtd->dma_data == NULL)
  123. return 0;
  124. omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
  125. omap_free_dma(prtd->dma_ch);
  126. prtd->dma_data = NULL;
  127. snd_pcm_set_runtime_buffer(substream, NULL);
  128. return 0;
  129. }
  130. static int omap_pcm_prepare(struct snd_pcm_substream *substream)
  131. {
  132. struct snd_pcm_runtime *runtime = substream->runtime;
  133. struct omap_runtime_data *prtd = runtime->private_data;
  134. struct omap_pcm_dma_data *dma_data = prtd->dma_data;
  135. struct omap_dma_channel_params dma_params;
  136. int bytes;
  137. /* return if this is a bufferless transfer e.g.
  138. * codec <--> BT codec or GSM modem -- lg FIXME */
  139. if (!prtd->dma_data)
  140. return 0;
  141. memset(&dma_params, 0, sizeof(dma_params));
  142. dma_params.data_type = dma_data->data_type;
  143. dma_params.trigger = dma_data->dma_req;
  144. dma_params.sync_mode = dma_data->sync_mode;
  145. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  146. dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
  147. dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT;
  148. dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC;
  149. dma_params.src_start = runtime->dma_addr;
  150. dma_params.dst_start = dma_data->port_addr;
  151. dma_params.dst_port = OMAP_DMA_PORT_MPUI;
  152. dma_params.dst_fi = dma_data->packet_size;
  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. dma_params.src_fi = dma_data->packet_size;
  161. }
  162. /*
  163. * Set DMA transfer frame size equal to ALSA period size and frame
  164. * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
  165. * we can transfer the whole ALSA buffer with single DMA transfer but
  166. * still can get an interrupt at each period bounary
  167. */
  168. bytes = snd_pcm_lib_period_bytes(substream);
  169. dma_params.elem_count = bytes >> dma_data->data_type;
  170. dma_params.frame_count = runtime->periods;
  171. omap_set_dma_params(prtd->dma_ch, &dma_params);
  172. if ((cpu_is_omap1510()))
  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 (cpu_is_omap1510()) {
  222. offset = prtd->period_index * runtime->period_size;
  223. } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  224. ptr = omap_get_dma_dst_pos(prtd->dma_ch);
  225. offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
  226. } else {
  227. ptr = omap_get_dma_src_pos(prtd->dma_ch);
  228. offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
  229. }
  230. if (offset >= runtime->buffer_size)
  231. offset = 0;
  232. return offset;
  233. }
  234. static int omap_pcm_open(struct snd_pcm_substream *substream)
  235. {
  236. struct snd_pcm_runtime *runtime = substream->runtime;
  237. struct omap_runtime_data *prtd;
  238. int ret;
  239. snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
  240. /* Ensure that buffer size is a multiple of period size */
  241. ret = snd_pcm_hw_constraint_integer(runtime,
  242. SNDRV_PCM_HW_PARAM_PERIODS);
  243. if (ret < 0)
  244. goto out;
  245. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  246. if (prtd == NULL) {
  247. ret = -ENOMEM;
  248. goto out;
  249. }
  250. spin_lock_init(&prtd->lock);
  251. runtime->private_data = prtd;
  252. out:
  253. return ret;
  254. }
  255. static int omap_pcm_close(struct snd_pcm_substream *substream)
  256. {
  257. struct snd_pcm_runtime *runtime = substream->runtime;
  258. kfree(runtime->private_data);
  259. return 0;
  260. }
  261. static int omap_pcm_mmap(struct snd_pcm_substream *substream,
  262. struct vm_area_struct *vma)
  263. {
  264. struct snd_pcm_runtime *runtime = substream->runtime;
  265. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  266. runtime->dma_area,
  267. runtime->dma_addr,
  268. runtime->dma_bytes);
  269. }
  270. static struct snd_pcm_ops omap_pcm_ops = {
  271. .open = omap_pcm_open,
  272. .close = omap_pcm_close,
  273. .ioctl = snd_pcm_lib_ioctl,
  274. .hw_params = omap_pcm_hw_params,
  275. .hw_free = omap_pcm_hw_free,
  276. .prepare = omap_pcm_prepare,
  277. .trigger = omap_pcm_trigger,
  278. .pointer = omap_pcm_pointer,
  279. .mmap = omap_pcm_mmap,
  280. };
  281. static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
  282. static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  283. int stream)
  284. {
  285. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  286. struct snd_dma_buffer *buf = &substream->dma_buffer;
  287. size_t size = omap_pcm_hardware.buffer_bytes_max;
  288. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  289. buf->dev.dev = pcm->card->dev;
  290. buf->private_data = NULL;
  291. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  292. &buf->addr, GFP_KERNEL);
  293. if (!buf->area)
  294. return -ENOMEM;
  295. buf->bytes = size;
  296. return 0;
  297. }
  298. static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
  299. {
  300. struct snd_pcm_substream *substream;
  301. struct snd_dma_buffer *buf;
  302. int stream;
  303. for (stream = 0; stream < 2; stream++) {
  304. substream = pcm->streams[stream].substream;
  305. if (!substream)
  306. continue;
  307. buf = &substream->dma_buffer;
  308. if (!buf->area)
  309. continue;
  310. dma_free_writecombine(pcm->card->dev, buf->bytes,
  311. buf->area, buf->addr);
  312. buf->area = NULL;
  313. }
  314. }
  315. static int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
  316. struct snd_pcm *pcm)
  317. {
  318. int ret = 0;
  319. if (!card->dev->dma_mask)
  320. card->dev->dma_mask = &omap_pcm_dmamask;
  321. if (!card->dev->coherent_dma_mask)
  322. card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
  323. if (dai->driver->playback.channels_min) {
  324. ret = omap_pcm_preallocate_dma_buffer(pcm,
  325. SNDRV_PCM_STREAM_PLAYBACK);
  326. if (ret)
  327. goto out;
  328. }
  329. if (dai->driver->capture.channels_min) {
  330. ret = omap_pcm_preallocate_dma_buffer(pcm,
  331. SNDRV_PCM_STREAM_CAPTURE);
  332. if (ret)
  333. goto out;
  334. }
  335. out:
  336. return ret;
  337. }
  338. static struct snd_soc_platform_driver omap_soc_platform = {
  339. .ops = &omap_pcm_ops,
  340. .pcm_new = omap_pcm_new,
  341. .pcm_free = omap_pcm_free_dma_buffers,
  342. };
  343. static __devinit int omap_pcm_probe(struct platform_device *pdev)
  344. {
  345. return snd_soc_register_platform(&pdev->dev,
  346. &omap_soc_platform);
  347. }
  348. static int __devexit omap_pcm_remove(struct platform_device *pdev)
  349. {
  350. snd_soc_unregister_platform(&pdev->dev);
  351. return 0;
  352. }
  353. static struct platform_driver omap_pcm_driver = {
  354. .driver = {
  355. .name = "omap-pcm-audio",
  356. .owner = THIS_MODULE,
  357. },
  358. .probe = omap_pcm_probe,
  359. .remove = __devexit_p(omap_pcm_remove),
  360. };
  361. static int __init snd_omap_pcm_init(void)
  362. {
  363. return platform_driver_register(&omap_pcm_driver);
  364. }
  365. module_init(snd_omap_pcm_init);
  366. static void __exit snd_omap_pcm_exit(void)
  367. {
  368. platform_driver_unregister(&omap_pcm_driver);
  369. }
  370. module_exit(snd_omap_pcm_exit);
  371. MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
  372. MODULE_DESCRIPTION("OMAP PCM DMA module");
  373. MODULE_LICENSE("GPL");