dma.c 11 KB

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