soc-dmaengine-pcm.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. dma_cookie_t cookie;
  32. unsigned int pos;
  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. struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
  40. {
  41. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  42. return prtd->dma_chan;
  43. }
  44. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
  45. /**
  46. * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
  47. * @substream: PCM substream
  48. * @params: hw_params
  49. * @slave_config: DMA slave config
  50. *
  51. * This function can be used to initialize a dma_slave_config from a substream
  52. * and hw_params in a dmaengine based PCM driver implementation.
  53. */
  54. int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
  55. const struct snd_pcm_hw_params *params,
  56. struct dma_slave_config *slave_config)
  57. {
  58. enum dma_slave_buswidth buswidth;
  59. switch (params_format(params)) {
  60. case SNDRV_PCM_FORMAT_S8:
  61. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  62. break;
  63. case SNDRV_PCM_FORMAT_S16_LE:
  64. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  65. break;
  66. case SNDRV_PCM_FORMAT_S18_3LE:
  67. case SNDRV_PCM_FORMAT_S20_3LE:
  68. case SNDRV_PCM_FORMAT_S24_LE:
  69. case SNDRV_PCM_FORMAT_S32_LE:
  70. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  71. break;
  72. default:
  73. return -EINVAL;
  74. }
  75. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  76. slave_config->direction = DMA_MEM_TO_DEV;
  77. slave_config->dst_addr_width = buswidth;
  78. } else {
  79. slave_config->direction = DMA_DEV_TO_MEM;
  80. slave_config->src_addr_width = buswidth;
  81. }
  82. return 0;
  83. }
  84. EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
  85. static void dmaengine_pcm_dma_complete(void *arg)
  86. {
  87. struct snd_pcm_substream *substream = arg;
  88. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  89. prtd->pos += snd_pcm_lib_period_bytes(substream);
  90. if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
  91. prtd->pos = 0;
  92. snd_pcm_period_elapsed(substream);
  93. }
  94. static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
  95. {
  96. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  97. struct dma_chan *chan = prtd->dma_chan;
  98. struct dma_async_tx_descriptor *desc;
  99. enum dma_transfer_direction direction;
  100. unsigned long flags = DMA_CTRL_ACK;
  101. direction = snd_pcm_substream_to_dma_direction(substream);
  102. if (!substream->runtime->no_period_wakeup)
  103. flags |= DMA_PREP_INTERRUPT;
  104. prtd->pos = 0;
  105. desc = dmaengine_prep_dma_cyclic(chan,
  106. substream->runtime->dma_addr,
  107. snd_pcm_lib_buffer_bytes(substream),
  108. snd_pcm_lib_period_bytes(substream), direction, flags);
  109. if (!desc)
  110. return -ENOMEM;
  111. desc->callback = dmaengine_pcm_dma_complete;
  112. desc->callback_param = substream;
  113. prtd->cookie = dmaengine_submit(desc);
  114. return 0;
  115. }
  116. /**
  117. * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
  118. * @substream: PCM substream
  119. * @cmd: Trigger command
  120. *
  121. * Returns 0 on success, a negative error code otherwise.
  122. *
  123. * This function can be used as the PCM trigger callback for dmaengine based PCM
  124. * driver implementations.
  125. */
  126. int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  127. {
  128. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  129. int ret;
  130. switch (cmd) {
  131. case SNDRV_PCM_TRIGGER_START:
  132. ret = dmaengine_pcm_prepare_and_submit(substream);
  133. if (ret)
  134. return ret;
  135. dma_async_issue_pending(prtd->dma_chan);
  136. break;
  137. case SNDRV_PCM_TRIGGER_RESUME:
  138. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  139. dmaengine_resume(prtd->dma_chan);
  140. break;
  141. case SNDRV_PCM_TRIGGER_SUSPEND:
  142. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  143. dmaengine_pause(prtd->dma_chan);
  144. break;
  145. case SNDRV_PCM_TRIGGER_STOP:
  146. dmaengine_terminate_all(prtd->dma_chan);
  147. break;
  148. default:
  149. return -EINVAL;
  150. }
  151. return 0;
  152. }
  153. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
  154. /**
  155. * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
  156. * @substream: PCM substream
  157. *
  158. * This function is deprecated and should not be used by new drivers, as its
  159. * results may be unreliable.
  160. */
  161. snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
  162. {
  163. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  164. return bytes_to_frames(substream->runtime, prtd->pos);
  165. }
  166. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
  167. /**
  168. * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
  169. * @substream: PCM substream
  170. *
  171. * This function can be used as the PCM pointer callback for dmaengine based PCM
  172. * driver implementations.
  173. */
  174. snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
  175. {
  176. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  177. struct dma_tx_state state;
  178. enum dma_status status;
  179. unsigned int buf_size;
  180. unsigned int pos = 0;
  181. status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
  182. if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
  183. buf_size = snd_pcm_lib_buffer_bytes(substream);
  184. if (state.residue > 0 && state.residue <= buf_size)
  185. pos = buf_size - state.residue;
  186. }
  187. return bytes_to_frames(substream->runtime, pos);
  188. }
  189. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
  190. static int dmaengine_pcm_request_channel(struct dmaengine_pcm_runtime_data *prtd,
  191. dma_filter_fn filter_fn, void *filter_data)
  192. {
  193. dma_cap_mask_t mask;
  194. dma_cap_zero(mask);
  195. dma_cap_set(DMA_SLAVE, mask);
  196. dma_cap_set(DMA_CYCLIC, mask);
  197. prtd->dma_chan = dma_request_channel(mask, filter_fn, filter_data);
  198. if (!prtd->dma_chan)
  199. return -ENXIO;
  200. return 0;
  201. }
  202. /**
  203. * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
  204. * @substream: PCM substream
  205. * @filter_fn: Filter function used to request the DMA channel
  206. * @filter_data: Data passed to the DMA filter function
  207. *
  208. * Returns 0 on success, a negative error code otherwise.
  209. *
  210. * This function will request a DMA channel using the passed filter function and
  211. * data. The function should usually be called from the pcm open callback.
  212. *
  213. * Note that this function will use private_data field of the substream's
  214. * runtime. So it is not availabe to your pcm driver implementation. If you need
  215. * to keep additional data attached to a substream use
  216. * snd_dmaengine_pcm_{set,get}_data.
  217. */
  218. int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
  219. dma_filter_fn filter_fn, void *filter_data)
  220. {
  221. struct dmaengine_pcm_runtime_data *prtd;
  222. int ret;
  223. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  224. SNDRV_PCM_HW_PARAM_PERIODS);
  225. if (ret < 0)
  226. return ret;
  227. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  228. if (!prtd)
  229. return -ENOMEM;
  230. ret = dmaengine_pcm_request_channel(prtd, filter_fn, filter_data);
  231. if (ret < 0) {
  232. kfree(prtd);
  233. return ret;
  234. }
  235. substream->runtime->private_data = prtd;
  236. return 0;
  237. }
  238. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
  239. /**
  240. * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
  241. * @substream: PCM substream
  242. */
  243. int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
  244. {
  245. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  246. dma_release_channel(prtd->dma_chan);
  247. kfree(prtd);
  248. return 0;
  249. }
  250. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
  251. MODULE_LICENSE("GPL");