soc-dmaengine-pcm.c 8.9 KB

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