pcm_dmaengine.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. int bits;
  60. bits = params_physical_width(params);
  61. if (bits < 8 || bits > 64)
  62. return -EINVAL;
  63. else if (bits == 8)
  64. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  65. else if (bits == 16)
  66. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  67. else if (bits == 24)
  68. buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
  69. else if (bits <= 32)
  70. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  71. else
  72. buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
  73. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  74. slave_config->direction = DMA_MEM_TO_DEV;
  75. slave_config->dst_addr_width = buswidth;
  76. } else {
  77. slave_config->direction = DMA_DEV_TO_MEM;
  78. slave_config->src_addr_width = buswidth;
  79. }
  80. slave_config->device_fc = false;
  81. return 0;
  82. }
  83. EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
  84. /**
  85. * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
  86. * using DAI DMA data.
  87. * @substream: PCM substream
  88. * @dma_data: DAI DMA data
  89. * @slave_config: DMA slave configuration
  90. *
  91. * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
  92. * slave_id fields of the DMA slave config from the same fields of the DAI DMA
  93. * data struct. The src and dst fields will be initialized depending on the
  94. * direction of the substream. If the substream is a playback stream the dst
  95. * fields will be initialized, if it is a capture stream the src fields will be
  96. * initialized. The {dst,src}_addr_width field will only be initialized if the
  97. * addr_width field of the DAI DMA data struct is not equal to
  98. * DMA_SLAVE_BUSWIDTH_UNDEFINED.
  99. */
  100. void snd_dmaengine_pcm_set_config_from_dai_data(
  101. const struct snd_pcm_substream *substream,
  102. const struct snd_dmaengine_dai_dma_data *dma_data,
  103. struct dma_slave_config *slave_config)
  104. {
  105. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  106. slave_config->dst_addr = dma_data->addr;
  107. slave_config->dst_maxburst = dma_data->maxburst;
  108. if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
  109. slave_config->dst_addr_width = dma_data->addr_width;
  110. } else {
  111. slave_config->src_addr = dma_data->addr;
  112. slave_config->src_maxburst = dma_data->maxburst;
  113. if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
  114. slave_config->src_addr_width = dma_data->addr_width;
  115. }
  116. slave_config->slave_id = dma_data->slave_id;
  117. }
  118. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
  119. static void dmaengine_pcm_dma_complete(void *arg)
  120. {
  121. struct snd_pcm_substream *substream = arg;
  122. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  123. prtd->pos += snd_pcm_lib_period_bytes(substream);
  124. if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
  125. prtd->pos = 0;
  126. snd_pcm_period_elapsed(substream);
  127. }
  128. static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
  129. {
  130. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  131. struct dma_chan *chan = prtd->dma_chan;
  132. struct dma_async_tx_descriptor *desc;
  133. enum dma_transfer_direction direction;
  134. unsigned long flags = DMA_CTRL_ACK;
  135. direction = snd_pcm_substream_to_dma_direction(substream);
  136. if (!substream->runtime->no_period_wakeup)
  137. flags |= DMA_PREP_INTERRUPT;
  138. prtd->pos = 0;
  139. desc = dmaengine_prep_dma_cyclic(chan,
  140. substream->runtime->dma_addr,
  141. snd_pcm_lib_buffer_bytes(substream),
  142. snd_pcm_lib_period_bytes(substream), direction, flags);
  143. if (!desc)
  144. return -ENOMEM;
  145. desc->callback = dmaengine_pcm_dma_complete;
  146. desc->callback_param = substream;
  147. prtd->cookie = dmaengine_submit(desc);
  148. return 0;
  149. }
  150. /**
  151. * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
  152. * @substream: PCM substream
  153. * @cmd: Trigger command
  154. *
  155. * Returns 0 on success, a negative error code otherwise.
  156. *
  157. * This function can be used as the PCM trigger callback for dmaengine based PCM
  158. * driver implementations.
  159. */
  160. int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  161. {
  162. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  163. struct snd_pcm_runtime *runtime = substream->runtime;
  164. int ret;
  165. switch (cmd) {
  166. case SNDRV_PCM_TRIGGER_START:
  167. ret = dmaengine_pcm_prepare_and_submit(substream);
  168. if (ret)
  169. return ret;
  170. dma_async_issue_pending(prtd->dma_chan);
  171. break;
  172. case SNDRV_PCM_TRIGGER_RESUME:
  173. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  174. dmaengine_resume(prtd->dma_chan);
  175. break;
  176. case SNDRV_PCM_TRIGGER_SUSPEND:
  177. if (runtime->info & SNDRV_PCM_INFO_PAUSE)
  178. dmaengine_pause(prtd->dma_chan);
  179. else
  180. dmaengine_terminate_all(prtd->dma_chan);
  181. break;
  182. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  183. dmaengine_pause(prtd->dma_chan);
  184. break;
  185. case SNDRV_PCM_TRIGGER_STOP:
  186. dmaengine_terminate_all(prtd->dma_chan);
  187. break;
  188. default:
  189. return -EINVAL;
  190. }
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
  194. /**
  195. * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
  196. * @substream: PCM substream
  197. *
  198. * This function is deprecated and should not be used by new drivers, as its
  199. * results may be unreliable.
  200. */
  201. snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
  202. {
  203. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  204. return bytes_to_frames(substream->runtime, prtd->pos);
  205. }
  206. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
  207. /**
  208. * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
  209. * @substream: PCM substream
  210. *
  211. * This function can be used as the PCM pointer callback for dmaengine based PCM
  212. * driver implementations.
  213. */
  214. snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
  215. {
  216. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  217. struct dma_tx_state state;
  218. enum dma_status status;
  219. unsigned int buf_size;
  220. unsigned int pos = 0;
  221. status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
  222. if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
  223. buf_size = snd_pcm_lib_buffer_bytes(substream);
  224. if (state.residue > 0 && state.residue <= buf_size)
  225. pos = buf_size - state.residue;
  226. }
  227. return bytes_to_frames(substream->runtime, pos);
  228. }
  229. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
  230. /**
  231. * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
  232. * @filter_fn: Filter function used to request the DMA channel
  233. * @filter_data: Data passed to the DMA filter function
  234. *
  235. * Returns NULL or the requested DMA channel.
  236. *
  237. * This function request a DMA channel for usage with dmaengine PCM.
  238. */
  239. struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
  240. void *filter_data)
  241. {
  242. dma_cap_mask_t mask;
  243. dma_cap_zero(mask);
  244. dma_cap_set(DMA_SLAVE, mask);
  245. dma_cap_set(DMA_CYCLIC, mask);
  246. return dma_request_channel(mask, filter_fn, filter_data);
  247. }
  248. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
  249. /**
  250. * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
  251. * @substream: PCM substream
  252. * @chan: DMA channel to use for data transfers
  253. *
  254. * Returns 0 on success, a negative error code otherwise.
  255. *
  256. * The function should usually be called from the pcm open callback. Note that
  257. * this function will use private_data field of the substream's runtime. So it
  258. * is not available to your pcm driver implementation.
  259. */
  260. int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
  261. struct dma_chan *chan)
  262. {
  263. struct dmaengine_pcm_runtime_data *prtd;
  264. int ret;
  265. if (!chan)
  266. return -ENXIO;
  267. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  268. SNDRV_PCM_HW_PARAM_PERIODS);
  269. if (ret < 0)
  270. return ret;
  271. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  272. if (!prtd)
  273. return -ENOMEM;
  274. prtd->dma_chan = chan;
  275. substream->runtime->private_data = prtd;
  276. return 0;
  277. }
  278. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
  279. /**
  280. * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
  281. * @substream: PCM substream
  282. * @filter_fn: Filter function used to request the DMA channel
  283. * @filter_data: Data passed to the DMA filter function
  284. *
  285. * Returns 0 on success, a negative error code otherwise.
  286. *
  287. * This function will request a DMA channel using the passed filter function and
  288. * data. The function should usually be called from the pcm open callback. Note
  289. * that this function will use private_data field of the substream's runtime. So
  290. * it is not available to your pcm driver implementation.
  291. */
  292. int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
  293. dma_filter_fn filter_fn, void *filter_data)
  294. {
  295. return snd_dmaengine_pcm_open(substream,
  296. snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
  297. }
  298. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
  299. /**
  300. * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
  301. * @substream: PCM substream
  302. */
  303. int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
  304. {
  305. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  306. kfree(prtd);
  307. return 0;
  308. }
  309. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
  310. /**
  311. * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel
  312. * @substream: PCM substream
  313. *
  314. * Releases the DMA channel associated with the PCM substream.
  315. */
  316. int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
  317. {
  318. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  319. dma_release_channel(prtd->dma_chan);
  320. return snd_dmaengine_pcm_close(substream);
  321. }
  322. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
  323. MODULE_LICENSE("GPL");