omap-pcm.c 12 KB

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