soc-dmaengine-pcm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. prtd->pos = 0;
  125. desc = dmaengine_prep_dma_cyclic(chan,
  126. substream->runtime->dma_addr,
  127. snd_pcm_lib_buffer_bytes(substream),
  128. snd_pcm_lib_period_bytes(substream), direction);
  129. if (!desc)
  130. return -ENOMEM;
  131. desc->callback = dmaengine_pcm_dma_complete;
  132. desc->callback_param = substream;
  133. dmaengine_submit(desc);
  134. return 0;
  135. }
  136. /**
  137. * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
  138. * @substream: PCM substream
  139. * @cmd: Trigger command
  140. *
  141. * Returns 0 on success, a negative error code otherwise.
  142. *
  143. * This function can be used as the PCM trigger callback for dmaengine based PCM
  144. * driver implementations.
  145. */
  146. int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  147. {
  148. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  149. int ret;
  150. switch (cmd) {
  151. case SNDRV_PCM_TRIGGER_START:
  152. ret = dmaengine_pcm_prepare_and_submit(substream);
  153. if (ret)
  154. return ret;
  155. dma_async_issue_pending(prtd->dma_chan);
  156. break;
  157. case SNDRV_PCM_TRIGGER_RESUME:
  158. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  159. dmaengine_resume(prtd->dma_chan);
  160. break;
  161. case SNDRV_PCM_TRIGGER_SUSPEND:
  162. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  163. dmaengine_pause(prtd->dma_chan);
  164. break;
  165. case SNDRV_PCM_TRIGGER_STOP:
  166. dmaengine_terminate_all(prtd->dma_chan);
  167. break;
  168. default:
  169. return -EINVAL;
  170. }
  171. return 0;
  172. }
  173. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
  174. /**
  175. * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
  176. * @substream: PCM substream
  177. *
  178. * This function can be used as the PCM pointer callback for dmaengine based PCM
  179. * driver implementations.
  180. */
  181. snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
  182. {
  183. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  184. return bytes_to_frames(substream->runtime, prtd->pos);
  185. }
  186. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
  187. static int dmaengine_pcm_request_channel(struct dmaengine_pcm_runtime_data *prtd,
  188. dma_filter_fn filter_fn, void *filter_data)
  189. {
  190. dma_cap_mask_t mask;
  191. dma_cap_zero(mask);
  192. dma_cap_set(DMA_SLAVE, mask);
  193. dma_cap_set(DMA_CYCLIC, mask);
  194. prtd->dma_chan = dma_request_channel(mask, filter_fn, filter_data);
  195. if (!prtd->dma_chan)
  196. return -ENXIO;
  197. return 0;
  198. }
  199. /**
  200. * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
  201. * @substream: PCM substream
  202. * @filter_fn: Filter function used to request the DMA channel
  203. * @filter_data: Data passed to the DMA filter function
  204. *
  205. * Returns 0 on success, a negative error code otherwise.
  206. *
  207. * This function will request a DMA channel using the passed filter function and
  208. * data. The function should usually be called from the pcm open callback.
  209. *
  210. * Note that this function will use private_data field of the substream's
  211. * runtime. So it is not availabe to your pcm driver implementation. If you need
  212. * to keep additional data attached to a substream use
  213. * snd_dmaeinge_pcm_{set,get}_data.
  214. */
  215. int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
  216. dma_filter_fn filter_fn, void *filter_data)
  217. {
  218. struct dmaengine_pcm_runtime_data *prtd;
  219. int ret;
  220. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  221. SNDRV_PCM_HW_PARAM_PERIODS);
  222. if (ret < 0)
  223. return ret;
  224. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  225. if (!prtd)
  226. return -ENOMEM;
  227. ret = dmaengine_pcm_request_channel(prtd, filter_fn, filter_data);
  228. if (ret < 0) {
  229. kfree(prtd);
  230. return ret;
  231. }
  232. substream->runtime->private_data = prtd;
  233. return 0;
  234. }
  235. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
  236. /**
  237. * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
  238. * @substream: PCM substream
  239. */
  240. int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
  241. {
  242. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  243. dma_release_channel(prtd->dma_chan);
  244. kfree(prtd);
  245. return 0;
  246. }
  247. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);