PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/soc/samsung/dma.c

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