dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. 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_req req;
  124. struct samsung_dma_config config;
  125. pr_debug("Entered %s\n", __func__);
  126. /* return if this is a bufferless transfer e.g.
  127. * codec <--> BT codec or GSM modem -- lg FIXME */
  128. if (!dma)
  129. return 0;
  130. /* this may get called several times by oss emulation
  131. * with different params -HW */
  132. if (prtd->params == NULL) {
  133. /* prepare DMA */
  134. prtd->params = dma;
  135. pr_debug("params %p, client %p, channel %d\n", prtd->params,
  136. prtd->params->client, prtd->params->channel);
  137. prtd->params->ops = samsung_dma_get_ops();
  138. req.cap = (samsung_dma_has_circular() ?
  139. DMA_CYCLIC : DMA_SLAVE);
  140. req.client = prtd->params->client;
  141. config.direction =
  142. (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  143. ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
  144. config.width = prtd->params->dma_size;
  145. config.fifo = prtd->params->dma_addr;
  146. prtd->params->ch = prtd->params->ops->request(
  147. prtd->params->channel, &req);
  148. prtd->params->ops->config(prtd->params->ch, &config);
  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. prtd->state |= ST_RUNNING;
  200. prtd->params->ops->trigger(prtd->params->ch);
  201. break;
  202. case SNDRV_PCM_TRIGGER_STOP:
  203. prtd->state &= ~ST_RUNNING;
  204. prtd->params->ops->stop(prtd->params->ch);
  205. break;
  206. default:
  207. ret = -EINVAL;
  208. break;
  209. }
  210. spin_unlock(&prtd->lock);
  211. return ret;
  212. }
  213. static snd_pcm_uframes_t
  214. dma_pointer(struct snd_pcm_substream *substream)
  215. {
  216. struct snd_pcm_runtime *runtime = substream->runtime;
  217. struct runtime_data *prtd = runtime->private_data;
  218. unsigned long res;
  219. pr_debug("Entered %s\n", __func__);
  220. res = prtd->dma_pos - prtd->dma_start;
  221. pr_debug("Pointer offset: %lu\n", res);
  222. /* we seem to be getting the odd error from the pcm library due
  223. * to out-of-bounds pointers. this is maybe due to the dma engine
  224. * not having loaded the new values for the channel before being
  225. * called... (todo - fix )
  226. */
  227. if (res >= snd_pcm_lib_buffer_bytes(substream)) {
  228. if (res == snd_pcm_lib_buffer_bytes(substream))
  229. res = 0;
  230. }
  231. return bytes_to_frames(substream->runtime, res);
  232. }
  233. static int dma_open(struct snd_pcm_substream *substream)
  234. {
  235. struct snd_pcm_runtime *runtime = substream->runtime;
  236. struct runtime_data *prtd;
  237. pr_debug("Entered %s\n", __func__);
  238. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  239. snd_soc_set_runtime_hwparams(substream, &dma_hardware);
  240. prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL);
  241. if (prtd == NULL)
  242. return -ENOMEM;
  243. spin_lock_init(&prtd->lock);
  244. runtime->private_data = prtd;
  245. return 0;
  246. }
  247. static int dma_close(struct snd_pcm_substream *substream)
  248. {
  249. struct snd_pcm_runtime *runtime = substream->runtime;
  250. struct runtime_data *prtd = runtime->private_data;
  251. pr_debug("Entered %s\n", __func__);
  252. if (!prtd)
  253. pr_debug("dma_close called with prtd == NULL\n");
  254. kfree(prtd);
  255. return 0;
  256. }
  257. static int dma_mmap(struct snd_pcm_substream *substream,
  258. struct vm_area_struct *vma)
  259. {
  260. struct snd_pcm_runtime *runtime = substream->runtime;
  261. pr_debug("Entered %s\n", __func__);
  262. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  263. runtime->dma_area,
  264. runtime->dma_addr,
  265. runtime->dma_bytes);
  266. }
  267. static struct snd_pcm_ops dma_ops = {
  268. .open = dma_open,
  269. .close = dma_close,
  270. .ioctl = snd_pcm_lib_ioctl,
  271. .hw_params = dma_hw_params,
  272. .hw_free = dma_hw_free,
  273. .prepare = dma_prepare,
  274. .trigger = dma_trigger,
  275. .pointer = dma_pointer,
  276. .mmap = dma_mmap,
  277. };
  278. static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  279. {
  280. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  281. struct snd_dma_buffer *buf = &substream->dma_buffer;
  282. size_t size = dma_hardware.buffer_bytes_max;
  283. pr_debug("Entered %s\n", __func__);
  284. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  285. buf->dev.dev = pcm->card->dev;
  286. buf->private_data = NULL;
  287. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  288. &buf->addr, GFP_KERNEL);
  289. if (!buf->area)
  290. return -ENOMEM;
  291. buf->bytes = size;
  292. return 0;
  293. }
  294. static void dma_free_dma_buffers(struct snd_pcm *pcm)
  295. {
  296. struct snd_pcm_substream *substream;
  297. struct snd_dma_buffer *buf;
  298. int stream;
  299. pr_debug("Entered %s\n", __func__);
  300. for (stream = 0; stream < 2; stream++) {
  301. substream = pcm->streams[stream].substream;
  302. if (!substream)
  303. continue;
  304. buf = &substream->dma_buffer;
  305. if (!buf->area)
  306. continue;
  307. dma_free_writecombine(pcm->card->dev, buf->bytes,
  308. buf->area, buf->addr);
  309. buf->area = NULL;
  310. }
  311. }
  312. static u64 dma_mask = DMA_BIT_MASK(32);
  313. static int dma_new(struct snd_soc_pcm_runtime *rtd)
  314. {
  315. struct snd_card *card = rtd->card->snd_card;
  316. struct snd_pcm *pcm = rtd->pcm;
  317. int ret = 0;
  318. pr_debug("Entered %s\n", __func__);
  319. if (!card->dev->dma_mask)
  320. card->dev->dma_mask = &dma_mask;
  321. if (!card->dev->coherent_dma_mask)
  322. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  323. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  324. ret = preallocate_dma_buffer(pcm,
  325. SNDRV_PCM_STREAM_PLAYBACK);
  326. if (ret)
  327. goto out;
  328. }
  329. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  330. ret = preallocate_dma_buffer(pcm,
  331. SNDRV_PCM_STREAM_CAPTURE);
  332. if (ret)
  333. goto out;
  334. }
  335. out:
  336. return ret;
  337. }
  338. static struct snd_soc_platform_driver samsung_asoc_platform = {
  339. .ops = &dma_ops,
  340. .pcm_new = dma_new,
  341. .pcm_free = dma_free_dma_buffers,
  342. };
  343. int asoc_dma_platform_register(struct device *dev)
  344. {
  345. return snd_soc_register_platform(dev, &samsung_asoc_platform);
  346. }
  347. EXPORT_SYMBOL_GPL(asoc_dma_platform_register);
  348. void asoc_dma_platform_unregister(struct device *dev)
  349. {
  350. snd_soc_unregister_platform(dev);
  351. }
  352. EXPORT_SYMBOL_GPL(asoc_dma_platform_unregister);
  353. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  354. MODULE_DESCRIPTION("Samsung ASoC DMA Driver");
  355. MODULE_LICENSE("GPL");