dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 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_req req;
  126. struct samsung_dma_config config;
  127. pr_debug("Entered %s\n", __func__);
  128. /* return if this is a bufferless transfer e.g.
  129. * codec <--> BT codec or GSM modem -- lg FIXME */
  130. if (!dma)
  131. return 0;
  132. /* this may get called several times by oss emulation
  133. * with different params -HW */
  134. if (prtd->params == NULL) {
  135. /* prepare DMA */
  136. prtd->params = dma;
  137. pr_debug("params %p, client %p, channel %d\n", prtd->params,
  138. prtd->params->client, prtd->params->channel);
  139. prtd->params->ops = samsung_dma_get_ops();
  140. req.cap = (samsung_dma_has_circular() ?
  141. DMA_CYCLIC : DMA_SLAVE);
  142. req.client = prtd->params->client;
  143. config.direction =
  144. (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  145. ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
  146. config.width = prtd->params->dma_size;
  147. config.fifo = prtd->params->dma_addr;
  148. prtd->params->ch = prtd->params->ops->request(
  149. prtd->params->channel, &req);
  150. prtd->params->ops->config(prtd->params->ch, &config);
  151. }
  152. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  153. runtime->dma_bytes = totbytes;
  154. spin_lock_irq(&prtd->lock);
  155. prtd->dma_loaded = 0;
  156. prtd->dma_period = params_period_bytes(params);
  157. prtd->dma_start = runtime->dma_addr;
  158. prtd->dma_pos = prtd->dma_start;
  159. prtd->dma_end = prtd->dma_start + totbytes;
  160. spin_unlock_irq(&prtd->lock);
  161. return 0;
  162. }
  163. static int dma_hw_free(struct snd_pcm_substream *substream)
  164. {
  165. struct runtime_data *prtd = substream->runtime->private_data;
  166. pr_debug("Entered %s\n", __func__);
  167. snd_pcm_set_runtime_buffer(substream, NULL);
  168. if (prtd->params) {
  169. prtd->params->ops->flush(prtd->params->ch);
  170. prtd->params->ops->release(prtd->params->ch,
  171. prtd->params->client);
  172. prtd->params = NULL;
  173. }
  174. return 0;
  175. }
  176. static int dma_prepare(struct snd_pcm_substream *substream)
  177. {
  178. struct runtime_data *prtd = substream->runtime->private_data;
  179. int ret = 0;
  180. pr_debug("Entered %s\n", __func__);
  181. /* return if this is a bufferless transfer e.g.
  182. * codec <--> BT codec or GSM modem -- lg FIXME */
  183. if (!prtd->params)
  184. return 0;
  185. /* flush the DMA channel */
  186. prtd->params->ops->flush(prtd->params->ch);
  187. prtd->dma_loaded = 0;
  188. prtd->dma_pos = prtd->dma_start;
  189. /* enqueue dma buffers */
  190. dma_enqueue(substream);
  191. return ret;
  192. }
  193. static int dma_trigger(struct snd_pcm_substream *substream, int cmd)
  194. {
  195. struct runtime_data *prtd = substream->runtime->private_data;
  196. int ret = 0;
  197. pr_debug("Entered %s\n", __func__);
  198. spin_lock(&prtd->lock);
  199. switch (cmd) {
  200. case SNDRV_PCM_TRIGGER_START:
  201. case SNDRV_PCM_TRIGGER_RESUME:
  202. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  203. prtd->state |= ST_RUNNING;
  204. prtd->params->ops->trigger(prtd->params->ch);
  205. break;
  206. case SNDRV_PCM_TRIGGER_STOP:
  207. case SNDRV_PCM_TRIGGER_SUSPEND:
  208. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  209. prtd->state &= ~ST_RUNNING;
  210. prtd->params->ops->stop(prtd->params->ch);
  211. break;
  212. default:
  213. ret = -EINVAL;
  214. break;
  215. }
  216. spin_unlock(&prtd->lock);
  217. return ret;
  218. }
  219. static snd_pcm_uframes_t
  220. dma_pointer(struct snd_pcm_substream *substream)
  221. {
  222. struct snd_pcm_runtime *runtime = substream->runtime;
  223. struct runtime_data *prtd = runtime->private_data;
  224. unsigned long res;
  225. pr_debug("Entered %s\n", __func__);
  226. res = prtd->dma_pos - prtd->dma_start;
  227. pr_debug("Pointer offset: %lu\n", res);
  228. /* we seem to be getting the odd error from the pcm library due
  229. * to out-of-bounds pointers. this is maybe due to the dma engine
  230. * not having loaded the new values for the channel before being
  231. * called... (todo - fix )
  232. */
  233. if (res >= snd_pcm_lib_buffer_bytes(substream)) {
  234. if (res == snd_pcm_lib_buffer_bytes(substream))
  235. res = 0;
  236. }
  237. return bytes_to_frames(substream->runtime, res);
  238. }
  239. static int dma_open(struct snd_pcm_substream *substream)
  240. {
  241. struct snd_pcm_runtime *runtime = substream->runtime;
  242. struct runtime_data *prtd;
  243. pr_debug("Entered %s\n", __func__);
  244. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  245. snd_soc_set_runtime_hwparams(substream, &dma_hardware);
  246. prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL);
  247. if (prtd == NULL)
  248. return -ENOMEM;
  249. spin_lock_init(&prtd->lock);
  250. runtime->private_data = prtd;
  251. return 0;
  252. }
  253. static int dma_close(struct snd_pcm_substream *substream)
  254. {
  255. struct snd_pcm_runtime *runtime = substream->runtime;
  256. struct runtime_data *prtd = runtime->private_data;
  257. pr_debug("Entered %s\n", __func__);
  258. if (!prtd)
  259. pr_debug("dma_close called with prtd == NULL\n");
  260. kfree(prtd);
  261. return 0;
  262. }
  263. static int dma_mmap(struct snd_pcm_substream *substream,
  264. struct vm_area_struct *vma)
  265. {
  266. struct snd_pcm_runtime *runtime = substream->runtime;
  267. pr_debug("Entered %s\n", __func__);
  268. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  269. runtime->dma_area,
  270. runtime->dma_addr,
  271. runtime->dma_bytes);
  272. }
  273. static struct snd_pcm_ops dma_ops = {
  274. .open = dma_open,
  275. .close = dma_close,
  276. .ioctl = snd_pcm_lib_ioctl,
  277. .hw_params = dma_hw_params,
  278. .hw_free = dma_hw_free,
  279. .prepare = dma_prepare,
  280. .trigger = dma_trigger,
  281. .pointer = dma_pointer,
  282. .mmap = dma_mmap,
  283. };
  284. static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  285. {
  286. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  287. struct snd_dma_buffer *buf = &substream->dma_buffer;
  288. size_t size = dma_hardware.buffer_bytes_max;
  289. pr_debug("Entered %s\n", __func__);
  290. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  291. buf->dev.dev = pcm->card->dev;
  292. buf->private_data = NULL;
  293. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  294. &buf->addr, GFP_KERNEL);
  295. if (!buf->area)
  296. return -ENOMEM;
  297. buf->bytes = size;
  298. return 0;
  299. }
  300. static void dma_free_dma_buffers(struct snd_pcm *pcm)
  301. {
  302. struct snd_pcm_substream *substream;
  303. struct snd_dma_buffer *buf;
  304. int stream;
  305. pr_debug("Entered %s\n", __func__);
  306. for (stream = 0; stream < 2; stream++) {
  307. substream = pcm->streams[stream].substream;
  308. if (!substream)
  309. continue;
  310. buf = &substream->dma_buffer;
  311. if (!buf->area)
  312. continue;
  313. dma_free_writecombine(pcm->card->dev, buf->bytes,
  314. buf->area, buf->addr);
  315. buf->area = NULL;
  316. }
  317. }
  318. static u64 dma_mask = DMA_BIT_MASK(32);
  319. static int dma_new(struct snd_soc_pcm_runtime *rtd)
  320. {
  321. struct snd_card *card = rtd->card->snd_card;
  322. struct snd_pcm *pcm = rtd->pcm;
  323. int ret = 0;
  324. pr_debug("Entered %s\n", __func__);
  325. if (!card->dev->dma_mask)
  326. card->dev->dma_mask = &dma_mask;
  327. if (!card->dev->coherent_dma_mask)
  328. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  329. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  330. ret = preallocate_dma_buffer(pcm,
  331. SNDRV_PCM_STREAM_PLAYBACK);
  332. if (ret)
  333. goto out;
  334. }
  335. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  336. ret = preallocate_dma_buffer(pcm,
  337. SNDRV_PCM_STREAM_CAPTURE);
  338. if (ret)
  339. goto out;
  340. }
  341. out:
  342. return ret;
  343. }
  344. static struct snd_soc_platform_driver samsung_asoc_platform = {
  345. .ops = &dma_ops,
  346. .pcm_new = dma_new,
  347. .pcm_free = dma_free_dma_buffers,
  348. };
  349. static int __devinit samsung_asoc_platform_probe(struct platform_device *pdev)
  350. {
  351. return snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform);
  352. }
  353. static int __devexit samsung_asoc_platform_remove(struct platform_device *pdev)
  354. {
  355. snd_soc_unregister_platform(&pdev->dev);
  356. return 0;
  357. }
  358. static struct platform_driver asoc_dma_driver = {
  359. .driver = {
  360. .name = "samsung-audio",
  361. .owner = THIS_MODULE,
  362. },
  363. .probe = samsung_asoc_platform_probe,
  364. .remove = __devexit_p(samsung_asoc_platform_remove),
  365. };
  366. module_platform_driver(asoc_dma_driver);
  367. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  368. MODULE_DESCRIPTION("Samsung ASoC DMA Driver");
  369. MODULE_LICENSE("GPL");
  370. MODULE_ALIAS("platform:samsung-audio");