dma.c 11 KB

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