dma.c 12 KB

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