pcm_dw_dmaengine.c 12 KB

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