soc-dmaengine-pcm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (C) 2012, Analog Devices Inc.
  3. * Author: Lars-Peter Clausen <lars@metafoo.de>
  4. *
  5. * Based on:
  6. * imx-pcm-dma-mx2.c, Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
  7. * mxs-pcm.c, Copyright (C) 2011 Freescale Semiconductor, Inc.
  8. * ep93xx-pcm.c, Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
  9. * Copyright (C) 2006 Applied Data Systems
  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. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/slab.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <sound/dmaengine_pcm.h>
  29. struct dmaengine_pcm_runtime_data {
  30. struct dma_chan *dma_chan;
  31. unsigned int pos;
  32. void *data;
  33. };
  34. static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
  35. const struct snd_pcm_substream *substream)
  36. {
  37. return substream->runtime->private_data;
  38. }
  39. /**
  40. * snd_dmaengine_pcm_set_data - Set dmaengine substream private data
  41. * @substream: PCM substream
  42. * @data: Data to set
  43. */
  44. void snd_dmaengine_pcm_set_data(struct snd_pcm_substream *substream, void *data)
  45. {
  46. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  47. prtd->data = data;
  48. }
  49. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_data);
  50. /**
  51. * snd_dmaengine_pcm_get_data - Get dmaeinge substream private data
  52. * @substream: PCM substream
  53. *
  54. * Returns the data previously set with snd_dmaengine_pcm_set_data
  55. */
  56. void *snd_dmaengine_pcm_get_data(struct snd_pcm_substream *substream)
  57. {
  58. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  59. return prtd->data;
  60. }
  61. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_data);
  62. struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
  63. {
  64. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  65. return prtd->dma_chan;
  66. }
  67. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
  68. /**
  69. * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
  70. * @substream: PCM substream
  71. * @params: hw_params
  72. * @slave_config: DMA slave config
  73. *
  74. * This function can be used to initialize a dma_slave_config from a substream
  75. * and hw_params in a dmaengine based PCM driver implementation.
  76. */
  77. int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
  78. const struct snd_pcm_hw_params *params,
  79. struct dma_slave_config *slave_config)
  80. {
  81. enum dma_slave_buswidth buswidth;
  82. switch (params_format(params)) {
  83. case SNDRV_PCM_FORMAT_S8:
  84. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  85. break;
  86. case SNDRV_PCM_FORMAT_S16_LE:
  87. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  88. break;
  89. case SNDRV_PCM_FORMAT_S18_3LE:
  90. case SNDRV_PCM_FORMAT_S20_3LE:
  91. case SNDRV_PCM_FORMAT_S24_LE:
  92. case SNDRV_PCM_FORMAT_S32_LE:
  93. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  94. break;
  95. default:
  96. return -EINVAL;
  97. }
  98. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  99. slave_config->direction = DMA_MEM_TO_DEV;
  100. slave_config->dst_addr_width = buswidth;
  101. } else {
  102. slave_config->direction = DMA_DEV_TO_MEM;
  103. slave_config->src_addr_width = buswidth;
  104. }
  105. return 0;
  106. }
  107. EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
  108. static void dmaengine_pcm_dma_complete(void *arg)
  109. {
  110. struct snd_pcm_substream *substream = arg;
  111. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  112. prtd->pos += snd_pcm_lib_period_bytes(substream);
  113. if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
  114. prtd->pos = 0;
  115. snd_pcm_period_elapsed(substream);
  116. }
  117. static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
  118. {
  119. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  120. struct dma_chan *chan = prtd->dma_chan;
  121. struct dma_async_tx_descriptor *desc;
  122. enum dma_transfer_direction direction;
  123. direction = snd_pcm_substream_to_dma_direction(substream);
  124. desc = chan->device->device_prep_dma_cyclic(chan,
  125. substream->runtime->dma_addr,
  126. snd_pcm_lib_buffer_bytes(substream),
  127. snd_pcm_lib_period_bytes(substream), direction);
  128. if (!desc)
  129. return -ENOMEM;
  130. desc->callback = dmaengine_pcm_dma_complete;
  131. desc->callback_param = substream;
  132. dmaengine_submit(desc);
  133. return 0;
  134. }
  135. /**
  136. * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
  137. * @substream: PCM substream
  138. * @cmd: Trigger command
  139. *
  140. * Returns 0 on success, a negative error code otherwise.
  141. *
  142. * This function can be used as the PCM trigger callback for dmaengine based PCM
  143. * driver implementations.
  144. */
  145. int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  146. {
  147. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  148. int ret;
  149. switch (cmd) {
  150. case SNDRV_PCM_TRIGGER_START:
  151. ret = dmaengine_pcm_prepare_and_submit(substream);
  152. if (ret)
  153. return ret;
  154. dma_async_issue_pending(prtd->dma_chan);
  155. break;
  156. case SNDRV_PCM_TRIGGER_RESUME:
  157. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  158. dmaengine_resume(prtd->dma_chan);
  159. break;
  160. case SNDRV_PCM_TRIGGER_SUSPEND:
  161. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  162. dmaengine_pause(prtd->dma_chan);
  163. break;
  164. case SNDRV_PCM_TRIGGER_STOP:
  165. dmaengine_terminate_all(prtd->dma_chan);
  166. break;
  167. default:
  168. return -EINVAL;
  169. }
  170. return 0;
  171. }
  172. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
  173. /**
  174. * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
  175. * @substream: PCM substream
  176. *
  177. * This function can be used as the PCM pointer callback for dmaengine based PCM
  178. * driver implementations.
  179. */
  180. snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
  181. {
  182. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  183. return bytes_to_frames(substream->runtime, prtd->pos);
  184. }
  185. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
  186. static int dmaengine_pcm_request_channel(struct dmaengine_pcm_runtime_data *prtd,
  187. dma_filter_fn filter_fn, void *filter_data)
  188. {
  189. dma_cap_mask_t mask;
  190. dma_cap_zero(mask);
  191. dma_cap_set(DMA_SLAVE, mask);
  192. dma_cap_set(DMA_CYCLIC, mask);
  193. prtd->dma_chan = dma_request_channel(mask, filter_fn, filter_data);
  194. if (!prtd->dma_chan)
  195. return -ENXIO;
  196. return 0;
  197. }
  198. /**
  199. * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
  200. * @substream: PCM substream
  201. * @filter_fn: Filter function used to request the DMA channel
  202. * @filter_data: Data passed to the DMA filter function
  203. *
  204. * Returns 0 on success, a negative error code otherwise.
  205. *
  206. * This function will request a DMA channel using the passed filter function and
  207. * data. The function should usually be called from the pcm open callback.
  208. *
  209. * Note that this function will use private_data field of the substream's
  210. * runtime. So it is not availabe to your pcm driver implementation. If you need
  211. * to keep additional data attached to a substream use
  212. * snd_dmaeinge_pcm_{set,get}_data.
  213. */
  214. int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
  215. dma_filter_fn filter_fn, void *filter_data)
  216. {
  217. struct dmaengine_pcm_runtime_data *prtd;
  218. int ret;
  219. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  220. SNDRV_PCM_HW_PARAM_PERIODS);
  221. if (ret < 0)
  222. return ret;
  223. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  224. if (!prtd)
  225. return -ENOMEM;
  226. ret = dmaengine_pcm_request_channel(prtd, filter_fn, filter_data);
  227. if (ret < 0) {
  228. kfree(prtd);
  229. return ret;
  230. }
  231. substream->runtime->private_data = prtd;
  232. return 0;
  233. }
  234. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
  235. /**
  236. * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
  237. * @substream: PCM substream
  238. */
  239. int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
  240. {
  241. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  242. dma_release_channel(prtd->dma_chan);
  243. kfree(prtd);
  244. return 0;
  245. }
  246. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);