dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 <linux/module.h>
  19. #include <sound/soc.h>
  20. #include <sound/pcm_params.h>
  21. #include <asm/dma.h>
  22. #include <mach/hardware.h>
  23. #include <mach/dma.h>
  24. #include "dma.h"
  25. #define ST_RUNNING (1<<0)
  26. #define ST_OPENED (1<<1)
  27. static const struct snd_pcm_hardware dma_hardware = {
  28. .info = SNDRV_PCM_INFO_INTERLEAVED |
  29. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  30. SNDRV_PCM_INFO_MMAP |
  31. SNDRV_PCM_INFO_MMAP_VALID,
  32. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  33. SNDRV_PCM_FMTBIT_U16_LE |
  34. SNDRV_PCM_FMTBIT_U8 |
  35. SNDRV_PCM_FMTBIT_S8,
  36. .channels_min = 2,
  37. .channels_max = 2,
  38. .buffer_bytes_max = 128*1024,
  39. .period_bytes_min = PAGE_SIZE,
  40. .period_bytes_max = PAGE_SIZE*2,
  41. .periods_min = 2,
  42. .periods_max = 128,
  43. .fifo_size = 32,
  44. };
  45. struct runtime_data {
  46. spinlock_t lock;
  47. int state;
  48. unsigned int dma_loaded;
  49. unsigned int dma_period;
  50. dma_addr_t dma_start;
  51. dma_addr_t dma_pos;
  52. dma_addr_t dma_end;
  53. struct s3c_dma_params *params;
  54. };
  55. static void audio_buffdone(void *data);
  56. /* dma_enqueue
  57. *
  58. * place a dma buffer onto the queue for the dma system
  59. * to handle.
  60. */
  61. static void dma_enqueue(struct snd_pcm_substream *substream)
  62. {
  63. struct runtime_data *prtd = substream->runtime->private_data;
  64. dma_addr_t pos = prtd->dma_pos;
  65. unsigned int limit;
  66. struct samsung_dma_prep_info dma_info;
  67. pr_debug("Entered %s\n", __func__);
  68. limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period;
  69. pr_debug("%s: loaded %d, limit %d\n",
  70. __func__, prtd->dma_loaded, limit);
  71. dma_info.cap = (samsung_dma_has_circular() ? DMA_CYCLIC : DMA_SLAVE);
  72. dma_info.direction =
  73. (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  74. ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
  75. dma_info.fp = audio_buffdone;
  76. dma_info.fp_param = substream;
  77. dma_info.period = prtd->dma_period;
  78. dma_info.len = prtd->dma_period*limit;
  79. while (prtd->dma_loaded < limit) {
  80. pr_debug("dma_loaded: %d\n", prtd->dma_loaded);
  81. if ((pos + dma_info.period) > prtd->dma_end) {
  82. dma_info.period = prtd->dma_end - pos;
  83. pr_debug("%s: corrected dma len %ld\n",
  84. __func__, dma_info.period);
  85. }
  86. dma_info.buf = pos;
  87. prtd->params->ops->prepare(prtd->params->ch, &dma_info);
  88. prtd->dma_loaded++;
  89. pos += prtd->dma_period;
  90. if (pos >= prtd->dma_end)
  91. pos = prtd->dma_start;
  92. }
  93. prtd->dma_pos = pos;
  94. }
  95. static void audio_buffdone(void *data)
  96. {
  97. struct snd_pcm_substream *substream = data;
  98. struct runtime_data *prtd = substream->runtime->private_data;
  99. pr_debug("Entered %s\n", __func__);
  100. if (prtd->state & ST_RUNNING) {
  101. prtd->dma_pos += prtd->dma_period;
  102. if (prtd->dma_pos >= prtd->dma_end)
  103. prtd->dma_pos = prtd->dma_start;
  104. if (substream)
  105. snd_pcm_period_elapsed(substream);
  106. spin_lock(&prtd->lock);
  107. if (!samsung_dma_has_circular()) {
  108. prtd->dma_loaded--;
  109. dma_enqueue(substream);
  110. }
  111. spin_unlock(&prtd->lock);
  112. }
  113. }
  114. static int dma_hw_params(struct snd_pcm_substream *substream,
  115. struct snd_pcm_hw_params *params)
  116. {
  117. struct snd_pcm_runtime *runtime = substream->runtime;
  118. struct runtime_data *prtd = runtime->private_data;
  119. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  120. unsigned long totbytes = params_buffer_bytes(params);
  121. struct s3c_dma_params *dma =
  122. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  123. struct samsung_dma_info dma_info;
  124. pr_debug("Entered %s\n", __func__);
  125. /* return if this is a bufferless transfer e.g.
  126. * codec <--> BT codec or GSM modem -- lg FIXME */
  127. if (!dma)
  128. return 0;
  129. /* this may get called several times by oss emulation
  130. * with different params -HW */
  131. if (prtd->params == NULL) {
  132. /* prepare DMA */
  133. prtd->params = dma;
  134. pr_debug("params %p, client %p, channel %d\n", prtd->params,
  135. prtd->params->client, prtd->params->channel);
  136. prtd->params->ops = samsung_dma_get_ops();
  137. dma_info.cap = (samsung_dma_has_circular() ?
  138. DMA_CYCLIC : DMA_SLAVE);
  139. dma_info.client = prtd->params->client;
  140. dma_info.direction =
  141. (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  142. ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
  143. dma_info.width = prtd->params->dma_size;
  144. dma_info.fifo = prtd->params->dma_addr;
  145. prtd->params->ch = prtd->params->ops->request(
  146. prtd->params->channel, &dma_info);
  147. }
  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_period = params_period_bytes(params);
  153. prtd->dma_start = runtime->dma_addr;
  154. prtd->dma_pos = prtd->dma_start;
  155. prtd->dma_end = prtd->dma_start + totbytes;
  156. spin_unlock_irq(&prtd->lock);
  157. return 0;
  158. }
  159. static int dma_hw_free(struct snd_pcm_substream *substream)
  160. {
  161. struct runtime_data *prtd = substream->runtime->private_data;
  162. pr_debug("Entered %s\n", __func__);
  163. snd_pcm_set_runtime_buffer(substream, NULL);
  164. if (prtd->params) {
  165. prtd->params->ops->flush(prtd->params->ch);
  166. prtd->params->ops->release(prtd->params->ch,
  167. 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. /* flush the DMA channel */
  182. prtd->params->ops->flush(prtd->params->ch);
  183. prtd->dma_loaded = 0;
  184. prtd->dma_pos = prtd->dma_start;
  185. /* enqueue dma buffers */
  186. dma_enqueue(substream);
  187. return ret;
  188. }
  189. static int dma_trigger(struct snd_pcm_substream *substream, int cmd)
  190. {
  191. struct runtime_data *prtd = substream->runtime->private_data;
  192. int ret = 0;
  193. pr_debug("Entered %s\n", __func__);
  194. spin_lock(&prtd->lock);
  195. switch (cmd) {
  196. case SNDRV_PCM_TRIGGER_START:
  197. prtd->state |= ST_RUNNING;
  198. prtd->params->ops->trigger(prtd->params->ch);
  199. break;
  200. case SNDRV_PCM_TRIGGER_STOP:
  201. prtd->state &= ~ST_RUNNING;
  202. prtd->params->ops->stop(prtd->params->ch);
  203. break;
  204. default:
  205. ret = -EINVAL;
  206. break;
  207. }
  208. spin_unlock(&prtd->lock);
  209. return ret;
  210. }
  211. static snd_pcm_uframes_t
  212. dma_pointer(struct snd_pcm_substream *substream)
  213. {
  214. struct snd_pcm_runtime *runtime = substream->runtime;
  215. struct runtime_data *prtd = runtime->private_data;
  216. unsigned long res;
  217. pr_debug("Entered %s\n", __func__);
  218. res = prtd->dma_pos - prtd->dma_start;
  219. pr_debug("Pointer offset: %lu\n", res);
  220. /* we seem to be getting the odd error from the pcm library due
  221. * to out-of-bounds pointers. this is maybe due to the dma engine
  222. * not having loaded the new values for the channel before being
  223. * called... (todo - fix )
  224. */
  225. if (res >= snd_pcm_lib_buffer_bytes(substream)) {
  226. if (res == snd_pcm_lib_buffer_bytes(substream))
  227. res = 0;
  228. }
  229. return bytes_to_frames(substream->runtime, res);
  230. }
  231. static int dma_open(struct snd_pcm_substream *substream)
  232. {
  233. struct snd_pcm_runtime *runtime = substream->runtime;
  234. struct runtime_data *prtd;
  235. pr_debug("Entered %s\n", __func__);
  236. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  237. snd_soc_set_runtime_hwparams(substream, &dma_hardware);
  238. prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL);
  239. if (prtd == NULL)
  240. return -ENOMEM;
  241. spin_lock_init(&prtd->lock);
  242. runtime->private_data = prtd;
  243. return 0;
  244. }
  245. static int dma_close(struct snd_pcm_substream *substream)
  246. {
  247. struct snd_pcm_runtime *runtime = substream->runtime;
  248. struct runtime_data *prtd = runtime->private_data;
  249. pr_debug("Entered %s\n", __func__);
  250. if (!prtd)
  251. pr_debug("dma_close called with prtd == NULL\n");
  252. kfree(prtd);
  253. return 0;
  254. }
  255. static int dma_mmap(struct snd_pcm_substream *substream,
  256. struct vm_area_struct *vma)
  257. {
  258. struct snd_pcm_runtime *runtime = substream->runtime;
  259. pr_debug("Entered %s\n", __func__);
  260. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  261. runtime->dma_area,
  262. runtime->dma_addr,
  263. runtime->dma_bytes);
  264. }
  265. static struct snd_pcm_ops dma_ops = {
  266. .open = dma_open,
  267. .close = dma_close,
  268. .ioctl = snd_pcm_lib_ioctl,
  269. .hw_params = dma_hw_params,
  270. .hw_free = dma_hw_free,
  271. .prepare = dma_prepare,
  272. .trigger = dma_trigger,
  273. .pointer = dma_pointer,
  274. .mmap = dma_mmap,
  275. };
  276. static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  277. {
  278. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  279. struct snd_dma_buffer *buf = &substream->dma_buffer;
  280. size_t size = dma_hardware.buffer_bytes_max;
  281. pr_debug("Entered %s\n", __func__);
  282. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  283. buf->dev.dev = pcm->card->dev;
  284. buf->private_data = NULL;
  285. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  286. &buf->addr, GFP_KERNEL);
  287. if (!buf->area)
  288. return -ENOMEM;
  289. buf->bytes = size;
  290. return 0;
  291. }
  292. static void dma_free_dma_buffers(struct snd_pcm *pcm)
  293. {
  294. struct snd_pcm_substream *substream;
  295. struct snd_dma_buffer *buf;
  296. int stream;
  297. pr_debug("Entered %s\n", __func__);
  298. for (stream = 0; stream < 2; stream++) {
  299. substream = pcm->streams[stream].substream;
  300. if (!substream)
  301. continue;
  302. buf = &substream->dma_buffer;
  303. if (!buf->area)
  304. continue;
  305. dma_free_writecombine(pcm->card->dev, buf->bytes,
  306. buf->area, buf->addr);
  307. buf->area = NULL;
  308. }
  309. }
  310. static u64 dma_mask = DMA_BIT_MASK(32);
  311. static int dma_new(struct snd_soc_pcm_runtime *rtd)
  312. {
  313. struct snd_card *card = rtd->card->snd_card;
  314. struct snd_pcm *pcm = rtd->pcm;
  315. int ret = 0;
  316. pr_debug("Entered %s\n", __func__);
  317. if (!card->dev->dma_mask)
  318. card->dev->dma_mask = &dma_mask;
  319. if (!card->dev->coherent_dma_mask)
  320. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  321. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  322. ret = preallocate_dma_buffer(pcm,
  323. SNDRV_PCM_STREAM_PLAYBACK);
  324. if (ret)
  325. goto out;
  326. }
  327. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  328. ret = preallocate_dma_buffer(pcm,
  329. SNDRV_PCM_STREAM_CAPTURE);
  330. if (ret)
  331. goto out;
  332. }
  333. out:
  334. return ret;
  335. }
  336. static struct snd_soc_platform_driver samsung_asoc_platform = {
  337. .ops = &dma_ops,
  338. .pcm_new = dma_new,
  339. .pcm_free = dma_free_dma_buffers,
  340. };
  341. static int __devinit samsung_asoc_platform_probe(struct platform_device *pdev)
  342. {
  343. return snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform);
  344. }
  345. static int __devexit samsung_asoc_platform_remove(struct platform_device *pdev)
  346. {
  347. snd_soc_unregister_platform(&pdev->dev);
  348. return 0;
  349. }
  350. static struct platform_driver asoc_dma_driver = {
  351. .driver = {
  352. .name = "samsung-audio",
  353. .owner = THIS_MODULE,
  354. },
  355. .probe = samsung_asoc_platform_probe,
  356. .remove = __devexit_p(samsung_asoc_platform_remove),
  357. };
  358. module_platform_driver(asoc_dma_driver);
  359. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  360. MODULE_DESCRIPTION("Samsung ASoC DMA Driver");
  361. MODULE_LICENSE("GPL");
  362. MODULE_ALIAS("platform:samsung-audio");