s3c-dma.c 12 KB

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