dma.c 11 KB

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