dma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * dma.c -- ALSA Soc Audio Layer
  3. *
  4. * (c) 2006 Wolfson Microelectronics PLC.
  5. * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
  6. *
  7. * Copyright 2004-2005 Simtec Electronics
  8. * http://armlinux.simtec.co.uk/
  9. * Ben Dooks <ben@simtec.co.uk>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/dma-mapping.h>
  18. #include <sound/soc.h>
  19. #include <sound/pcm_params.h>
  20. #include <asm/dma.h>
  21. #include <mach/hardware.h>
  22. #include <mach/dma.h>
  23. #include "dma.h"
  24. #define ST_RUNNING (1<<0)
  25. #define ST_OPENED (1<<1)
  26. static const struct snd_pcm_hardware dma_hardware = {
  27. .info = SNDRV_PCM_INFO_INTERLEAVED |
  28. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  29. SNDRV_PCM_INFO_MMAP |
  30. SNDRV_PCM_INFO_MMAP_VALID |
  31. SNDRV_PCM_INFO_PAUSE |
  32. SNDRV_PCM_INFO_RESUME,
  33. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  34. SNDRV_PCM_FMTBIT_U16_LE |
  35. SNDRV_PCM_FMTBIT_U8 |
  36. SNDRV_PCM_FMTBIT_S8,
  37. .channels_min = 2,
  38. .channels_max = 2,
  39. .buffer_bytes_max = 128*1024,
  40. .period_bytes_min = PAGE_SIZE,
  41. .period_bytes_max = PAGE_SIZE*2,
  42. .periods_min = 2,
  43. .periods_max = 128,
  44. .fifo_size = 32,
  45. };
  46. struct runtime_data {
  47. spinlock_t lock;
  48. int state;
  49. unsigned int dma_loaded;
  50. unsigned int dma_limit;
  51. unsigned int dma_period;
  52. dma_addr_t dma_start;
  53. dma_addr_t dma_pos;
  54. dma_addr_t dma_end;
  55. struct s3c_dma_params *params;
  56. };
  57. /* dma_enqueue
  58. *
  59. * place a dma buffer onto the queue for the dma system
  60. * to handle.
  61. */
  62. static void dma_enqueue(struct snd_pcm_substream *substream)
  63. {
  64. struct runtime_data *prtd = substream->runtime->private_data;
  65. dma_addr_t pos = prtd->dma_pos;
  66. unsigned int limit;
  67. int ret;
  68. pr_debug("Entered %s\n", __func__);
  69. if (s3c_dma_has_circular())
  70. limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period;
  71. else
  72. limit = prtd->dma_limit;
  73. pr_debug("%s: loaded %d, limit %d\n",
  74. __func__, prtd->dma_loaded, limit);
  75. while (prtd->dma_loaded < limit) {
  76. unsigned long len = prtd->dma_period;
  77. pr_debug("dma_loaded: %d\n", prtd->dma_loaded);
  78. if ((pos + len) > prtd->dma_end) {
  79. len = prtd->dma_end - pos;
  80. pr_debug("%s: corrected dma len %ld\n", __func__, len);
  81. }
  82. ret = s3c2410_dma_enqueue(prtd->params->channel,
  83. substream, pos, len);
  84. if (ret == 0) {
  85. prtd->dma_loaded++;
  86. pos += prtd->dma_period;
  87. if (pos >= prtd->dma_end)
  88. pos = prtd->dma_start;
  89. } else
  90. break;
  91. }
  92. prtd->dma_pos = pos;
  93. }
  94. static void audio_buffdone(struct s3c2410_dma_chan *channel,
  95. void *dev_id, int size,
  96. enum s3c2410_dma_buffresult result)
  97. {
  98. struct snd_pcm_substream *substream = dev_id;
  99. struct runtime_data *prtd;
  100. pr_debug("Entered %s\n", __func__);
  101. if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
  102. return;
  103. prtd = substream->runtime->private_data;
  104. if (substream)
  105. snd_pcm_period_elapsed(substream);
  106. spin_lock(&prtd->lock);
  107. if (prtd->state & ST_RUNNING && !s3c_dma_has_circular()) {
  108. prtd->dma_loaded--;
  109. dma_enqueue(substream);
  110. }
  111. spin_unlock(&prtd->lock);
  112. }
  113. static int dma_hw_params(struct snd_pcm_substream *substream,
  114. struct snd_pcm_hw_params *params)
  115. {
  116. struct snd_pcm_runtime *runtime = substream->runtime;
  117. struct runtime_data *prtd = runtime->private_data;
  118. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  119. unsigned long totbytes = params_buffer_bytes(params);
  120. struct s3c_dma_params *dma =
  121. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  122. int ret = 0;
  123. pr_debug("Entered %s\n", __func__);
  124. /* return if this is a bufferless transfer e.g.
  125. * codec <--> BT codec or GSM modem -- lg FIXME */
  126. if (!dma)
  127. return 0;
  128. /* this may get called several times by oss emulation
  129. * with different params -HW */
  130. if (prtd->params == NULL) {
  131. /* prepare DMA */
  132. prtd->params = dma;
  133. pr_debug("params %p, client %p, channel %d\n", prtd->params,
  134. prtd->params->client, prtd->params->channel);
  135. ret = s3c2410_dma_request(prtd->params->channel,
  136. prtd->params->client, NULL);
  137. if (ret < 0) {
  138. printk(KERN_ERR "failed to get dma channel\n");
  139. return ret;
  140. }
  141. /* use the circular buffering if we have it available. */
  142. if (s3c_dma_has_circular())
  143. s3c2410_dma_setflags(prtd->params->channel,
  144. S3C2410_DMAF_CIRCULAR);
  145. }
  146. s3c2410_dma_set_buffdone_fn(prtd->params->channel,
  147. audio_buffdone);
  148. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  149. runtime->dma_bytes = totbytes;
  150. spin_lock_irq(&prtd->lock);
  151. prtd->dma_loaded = 0;
  152. prtd->dma_limit = runtime->hw.periods_min;
  153. prtd->dma_period = params_period_bytes(params);
  154. prtd->dma_start = runtime->dma_addr;
  155. prtd->dma_pos = prtd->dma_start;
  156. prtd->dma_end = prtd->dma_start + totbytes;
  157. spin_unlock_irq(&prtd->lock);
  158. return 0;
  159. }
  160. static int dma_hw_free(struct snd_pcm_substream *substream)
  161. {
  162. struct runtime_data *prtd = substream->runtime->private_data;
  163. pr_debug("Entered %s\n", __func__);
  164. /* TODO - do we need to ensure DMA flushed */
  165. snd_pcm_set_runtime_buffer(substream, NULL);
  166. if (prtd->params) {
  167. s3c2410_dma_free(prtd->params->channel, prtd->params->client);
  168. prtd->params = NULL;
  169. }
  170. return 0;
  171. }
  172. static int dma_prepare(struct snd_pcm_substream *substream)
  173. {
  174. struct runtime_data *prtd = substream->runtime->private_data;
  175. int ret = 0;
  176. pr_debug("Entered %s\n", __func__);
  177. /* return if this is a bufferless transfer e.g.
  178. * codec <--> BT codec or GSM modem -- lg FIXME */
  179. if (!prtd->params)
  180. return 0;
  181. /* channel needs configuring for mem=>device, increment memory addr,
  182. * sync to pclk, half-word transfers to the IIS-FIFO. */
  183. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  184. s3c2410_dma_devconfig(prtd->params->channel,
  185. S3C2410_DMASRC_MEM,
  186. prtd->params->dma_addr);
  187. } else {
  188. s3c2410_dma_devconfig(prtd->params->channel,
  189. S3C2410_DMASRC_HW,
  190. prtd->params->dma_addr);
  191. }
  192. s3c2410_dma_config(prtd->params->channel,
  193. prtd->params->dma_size);
  194. /* flush the DMA channel */
  195. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH);
  196. prtd->dma_loaded = 0;
  197. prtd->dma_pos = prtd->dma_start;
  198. /* enqueue dma buffers */
  199. dma_enqueue(substream);
  200. return ret;
  201. }
  202. static int dma_trigger(struct snd_pcm_substream *substream, int cmd)
  203. {
  204. struct runtime_data *prtd = substream->runtime->private_data;
  205. int ret = 0;
  206. pr_debug("Entered %s\n", __func__);
  207. spin_lock(&prtd->lock);
  208. switch (cmd) {
  209. case SNDRV_PCM_TRIGGER_START:
  210. case SNDRV_PCM_TRIGGER_RESUME:
  211. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  212. prtd->state |= ST_RUNNING;
  213. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START);
  214. break;
  215. case SNDRV_PCM_TRIGGER_STOP:
  216. case SNDRV_PCM_TRIGGER_SUSPEND:
  217. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  218. prtd->state &= ~ST_RUNNING;
  219. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP);
  220. break;
  221. default:
  222. ret = -EINVAL;
  223. break;
  224. }
  225. spin_unlock(&prtd->lock);
  226. return ret;
  227. }
  228. static snd_pcm_uframes_t
  229. dma_pointer(struct snd_pcm_substream *substream)
  230. {
  231. struct snd_pcm_runtime *runtime = substream->runtime;
  232. struct runtime_data *prtd = runtime->private_data;
  233. unsigned long res;
  234. dma_addr_t src, dst;
  235. pr_debug("Entered %s\n", __func__);
  236. spin_lock(&prtd->lock);
  237. s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
  238. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  239. res = dst - prtd->dma_start;
  240. else
  241. res = src - prtd->dma_start;
  242. spin_unlock(&prtd->lock);
  243. pr_debug("Pointer %x %x\n", src, dst);
  244. /* we seem to be getting the odd error from the pcm library due
  245. * to out-of-bounds pointers. this is maybe due to the dma engine
  246. * not having loaded the new values for the channel before being
  247. * called... (todo - fix )
  248. */
  249. if (res >= snd_pcm_lib_buffer_bytes(substream)) {
  250. if (res == snd_pcm_lib_buffer_bytes(substream))
  251. res = 0;
  252. }
  253. return bytes_to_frames(substream->runtime, res);
  254. }
  255. static int dma_open(struct snd_pcm_substream *substream)
  256. {
  257. struct snd_pcm_runtime *runtime = substream->runtime;
  258. struct runtime_data *prtd;
  259. pr_debug("Entered %s\n", __func__);
  260. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  261. snd_soc_set_runtime_hwparams(substream, &dma_hardware);
  262. prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL);
  263. if (prtd == NULL)
  264. return -ENOMEM;
  265. spin_lock_init(&prtd->lock);
  266. runtime->private_data = prtd;
  267. return 0;
  268. }
  269. static int dma_close(struct snd_pcm_substream *substream)
  270. {
  271. struct snd_pcm_runtime *runtime = substream->runtime;
  272. struct runtime_data *prtd = runtime->private_data;
  273. pr_debug("Entered %s\n", __func__);
  274. if (!prtd)
  275. pr_debug("dma_close called with prtd == NULL\n");
  276. kfree(prtd);
  277. return 0;
  278. }
  279. static int dma_mmap(struct snd_pcm_substream *substream,
  280. struct vm_area_struct *vma)
  281. {
  282. struct snd_pcm_runtime *runtime = substream->runtime;
  283. pr_debug("Entered %s\n", __func__);
  284. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  285. runtime->dma_area,
  286. runtime->dma_addr,
  287. runtime->dma_bytes);
  288. }
  289. static struct snd_pcm_ops dma_ops = {
  290. .open = dma_open,
  291. .close = dma_close,
  292. .ioctl = snd_pcm_lib_ioctl,
  293. .hw_params = dma_hw_params,
  294. .hw_free = dma_hw_free,
  295. .prepare = dma_prepare,
  296. .trigger = dma_trigger,
  297. .pointer = dma_pointer,
  298. .mmap = dma_mmap,
  299. };
  300. static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  301. {
  302. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  303. struct snd_dma_buffer *buf = &substream->dma_buffer;
  304. size_t size = dma_hardware.buffer_bytes_max;
  305. pr_debug("Entered %s\n", __func__);
  306. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  307. buf->dev.dev = pcm->card->dev;
  308. buf->private_data = NULL;
  309. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  310. &buf->addr, GFP_KERNEL);
  311. if (!buf->area)
  312. return -ENOMEM;
  313. buf->bytes = size;
  314. return 0;
  315. }
  316. static void dma_free_dma_buffers(struct snd_pcm *pcm)
  317. {
  318. struct snd_pcm_substream *substream;
  319. struct snd_dma_buffer *buf;
  320. int stream;
  321. pr_debug("Entered %s\n", __func__);
  322. for (stream = 0; stream < 2; stream++) {
  323. substream = pcm->streams[stream].substream;
  324. if (!substream)
  325. continue;
  326. buf = &substream->dma_buffer;
  327. if (!buf->area)
  328. continue;
  329. dma_free_writecombine(pcm->card->dev, buf->bytes,
  330. buf->area, buf->addr);
  331. buf->area = NULL;
  332. }
  333. }
  334. static u64 dma_mask = DMA_BIT_MASK(32);
  335. static int dma_new(struct snd_card *card,
  336. struct snd_soc_dai *dai, struct snd_pcm *pcm)
  337. {
  338. int ret = 0;
  339. pr_debug("Entered %s\n", __func__);
  340. if (!card->dev->dma_mask)
  341. card->dev->dma_mask = &dma_mask;
  342. if (!card->dev->coherent_dma_mask)
  343. card->dev->coherent_dma_mask = 0xffffffff;
  344. if (dai->driver->playback.channels_min) {
  345. ret = preallocate_dma_buffer(pcm,
  346. SNDRV_PCM_STREAM_PLAYBACK);
  347. if (ret)
  348. goto out;
  349. }
  350. if (dai->driver->capture.channels_min) {
  351. ret = preallocate_dma_buffer(pcm,
  352. SNDRV_PCM_STREAM_CAPTURE);
  353. if (ret)
  354. goto out;
  355. }
  356. out:
  357. return ret;
  358. }
  359. static struct snd_soc_platform_driver samsung_asoc_platform = {
  360. .ops = &dma_ops,
  361. .pcm_new = dma_new,
  362. .pcm_free = dma_free_dma_buffers,
  363. };
  364. static int __devinit samsung_asoc_platform_probe(struct platform_device *pdev)
  365. {
  366. return snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform);
  367. }
  368. static int __devexit samsung_asoc_platform_remove(struct platform_device *pdev)
  369. {
  370. snd_soc_unregister_platform(&pdev->dev);
  371. return 0;
  372. }
  373. static struct platform_driver asoc_dma_driver = {
  374. .driver = {
  375. .name = "samsung-audio",
  376. .owner = THIS_MODULE,
  377. },
  378. .probe = samsung_asoc_platform_probe,
  379. .remove = __devexit_p(samsung_asoc_platform_remove),
  380. };
  381. static int __init samsung_asoc_init(void)
  382. {
  383. return platform_driver_register(&asoc_dma_driver);
  384. }
  385. module_init(samsung_asoc_init);
  386. static void __exit samsung_asoc_exit(void)
  387. {
  388. platform_driver_unregister(&asoc_dma_driver);
  389. }
  390. module_exit(samsung_asoc_exit);
  391. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  392. MODULE_DESCRIPTION("Samsung ASoC DMA Driver");
  393. MODULE_LICENSE("GPL");
  394. MODULE_ALIAS("platform:samsung-audio");