pcm_dmaengine.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2012, Analog Devices Inc.
  4. * Author: Lars-Peter Clausen <lars@metafoo.de>
  5. *
  6. * Based on:
  7. * imx-pcm-dma-mx2.c, Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
  8. * mxs-pcm.c, Copyright (C) 2011 Freescale Semiconductor, Inc.
  9. * ep93xx-pcm.c, Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
  10. * Copyright (C) 2006 Applied Data Systems
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/slab.h>
  16. #include <sound/pcm.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/soc.h>
  19. #include <sound/dmaengine_pcm.h>
  20. struct dmaengine_pcm_runtime_data {
  21. struct dma_chan *dma_chan;
  22. dma_cookie_t cookie;
  23. unsigned int pos;
  24. };
  25. static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
  26. const struct snd_pcm_substream *substream)
  27. {
  28. return substream->runtime->private_data;
  29. }
  30. struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
  31. {
  32. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  33. return prtd->dma_chan;
  34. }
  35. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
  36. /**
  37. * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
  38. * @substream: PCM substream
  39. * @params: hw_params
  40. * @slave_config: DMA slave config
  41. *
  42. * This function can be used to initialize a dma_slave_config from a substream
  43. * and hw_params in a dmaengine based PCM driver implementation.
  44. */
  45. int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
  46. const struct snd_pcm_hw_params *params,
  47. struct dma_slave_config *slave_config)
  48. {
  49. enum dma_slave_buswidth buswidth;
  50. int bits;
  51. bits = params_physical_width(params);
  52. if (bits < 8 || bits > 64)
  53. return -EINVAL;
  54. else if (bits == 8)
  55. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  56. else if (bits == 16)
  57. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  58. else if (bits == 24)
  59. buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
  60. else if (bits <= 32)
  61. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  62. else
  63. buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
  64. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  65. slave_config->direction = DMA_MEM_TO_DEV;
  66. slave_config->dst_addr_width = buswidth;
  67. } else {
  68. slave_config->direction = DMA_DEV_TO_MEM;
  69. slave_config->src_addr_width = buswidth;
  70. }
  71. slave_config->device_fc = false;
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
  75. /**
  76. * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
  77. * using DAI DMA data.
  78. * @substream: PCM substream
  79. * @dma_data: DAI DMA data
  80. * @slave_config: DMA slave configuration
  81. *
  82. * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and
  83. * slave_id fields of the DMA slave config from the same fields of the DAI DMA
  84. * data struct. The src and dst fields will be initialized depending on the
  85. * direction of the substream. If the substream is a playback stream the dst
  86. * fields will be initialized, if it is a capture stream the src fields will be
  87. * initialized. The {dst,src}_addr_width field will only be initialized if the
  88. * SND_DMAENGINE_PCM_DAI_FLAG_PACK flag is set or if the addr_width field of
  89. * the DAI DMA data struct is not equal to DMA_SLAVE_BUSWIDTH_UNDEFINED. If
  90. * both conditions are met the latter takes priority.
  91. */
  92. void snd_dmaengine_pcm_set_config_from_dai_data(
  93. const struct snd_pcm_substream *substream,
  94. const struct snd_dmaengine_dai_dma_data *dma_data,
  95. struct dma_slave_config *slave_config)
  96. {
  97. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  98. slave_config->dst_addr = dma_data->addr;
  99. slave_config->dst_maxburst = dma_data->maxburst;
  100. if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
  101. slave_config->dst_addr_width =
  102. DMA_SLAVE_BUSWIDTH_UNDEFINED;
  103. if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
  104. slave_config->dst_addr_width = dma_data->addr_width;
  105. } else {
  106. slave_config->src_addr = dma_data->addr;
  107. slave_config->src_maxburst = dma_data->maxburst;
  108. if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
  109. slave_config->src_addr_width =
  110. DMA_SLAVE_BUSWIDTH_UNDEFINED;
  111. if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
  112. slave_config->src_addr_width = dma_data->addr_width;
  113. }
  114. slave_config->slave_id = dma_data->slave_id;
  115. slave_config->peripheral_config = dma_data->peripheral_config;
  116. slave_config->peripheral_size = dma_data->peripheral_size;
  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_async(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_async(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 snd_pcm_runtime *runtime = substream->runtime;
  218. struct dma_tx_state state;
  219. enum dma_status status;
  220. unsigned int buf_size;
  221. unsigned int pos = 0;
  222. status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
  223. if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
  224. buf_size = snd_pcm_lib_buffer_bytes(substream);
  225. if (state.residue > 0 && state.residue <= buf_size)
  226. pos = buf_size - state.residue;
  227. runtime->delay = bytes_to_frames(runtime,
  228. state.in_flight_bytes);
  229. }
  230. return bytes_to_frames(runtime, pos);
  231. }
  232. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
  233. /**
  234. * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
  235. * @filter_fn: Filter function used to request the DMA channel
  236. * @filter_data: Data passed to the DMA filter function
  237. *
  238. * Returns NULL or the requested DMA channel.
  239. *
  240. * This function request a DMA channel for usage with dmaengine PCM.
  241. */
  242. struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
  243. void *filter_data)
  244. {
  245. dma_cap_mask_t mask;
  246. dma_cap_zero(mask);
  247. dma_cap_set(DMA_SLAVE, mask);
  248. dma_cap_set(DMA_CYCLIC, mask);
  249. return dma_request_channel(mask, filter_fn, filter_data);
  250. }
  251. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
  252. /**
  253. * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
  254. * @substream: PCM substream
  255. * @chan: DMA channel to use for data transfers
  256. *
  257. * Returns 0 on success, a negative error code otherwise.
  258. *
  259. * The function should usually be called from the pcm open callback. Note that
  260. * this function will use private_data field of the substream's runtime. So it
  261. * is not available to your pcm driver implementation.
  262. */
  263. int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
  264. struct dma_chan *chan)
  265. {
  266. struct dmaengine_pcm_runtime_data *prtd;
  267. int ret;
  268. if (!chan)
  269. return -ENXIO;
  270. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  271. SNDRV_PCM_HW_PARAM_PERIODS);
  272. if (ret < 0)
  273. return ret;
  274. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  275. if (!prtd)
  276. return -ENOMEM;
  277. prtd->dma_chan = chan;
  278. substream->runtime->private_data = prtd;
  279. return 0;
  280. }
  281. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
  282. /**
  283. * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
  284. * @substream: PCM substream
  285. * @filter_fn: Filter function used to request the DMA channel
  286. * @filter_data: Data passed to the DMA filter function
  287. *
  288. * Returns 0 on success, a negative error code otherwise.
  289. *
  290. * This function will request a DMA channel using the passed filter function and
  291. * data. The function should usually be called from the pcm open callback. Note
  292. * that this function will use private_data field of the substream's runtime. So
  293. * it is not available to your pcm driver implementation.
  294. */
  295. int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
  296. dma_filter_fn filter_fn, void *filter_data)
  297. {
  298. return snd_dmaengine_pcm_open(substream,
  299. snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
  300. }
  301. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
  302. /**
  303. * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
  304. * @substream: PCM substream
  305. */
  306. int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
  307. {
  308. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  309. dmaengine_synchronize(prtd->dma_chan);
  310. kfree(prtd);
  311. return 0;
  312. }
  313. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
  314. /**
  315. * snd_dmaengine_pcm_close_release_chan - Close a dmaengine based PCM
  316. * substream and release channel
  317. * @substream: PCM substream
  318. *
  319. * Releases the DMA channel associated with the PCM substream.
  320. */
  321. int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
  322. {
  323. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  324. dmaengine_synchronize(prtd->dma_chan);
  325. dma_release_channel(prtd->dma_chan);
  326. kfree(prtd);
  327. return 0;
  328. }
  329. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
  330. /**
  331. * snd_dmaengine_pcm_refine_runtime_hwparams - Refine runtime hw params
  332. * @substream: PCM substream
  333. * @dma_data: DAI DMA data
  334. * @hw: PCM hw params
  335. * @chan: DMA channel to use for data transfers
  336. *
  337. * Returns 0 on success, a negative error code otherwise.
  338. *
  339. * This function will query DMA capability, then refine the pcm hardware
  340. * parameters.
  341. */
  342. int snd_dmaengine_pcm_refine_runtime_hwparams(
  343. struct snd_pcm_substream *substream,
  344. struct snd_dmaengine_dai_dma_data *dma_data,
  345. struct snd_pcm_hardware *hw,
  346. struct dma_chan *chan)
  347. {
  348. struct dma_slave_caps dma_caps;
  349. u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  350. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  351. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  352. snd_pcm_format_t i;
  353. int ret = 0;
  354. if (!hw || !chan || !dma_data)
  355. return -EINVAL;
  356. ret = dma_get_slave_caps(chan, &dma_caps);
  357. if (ret == 0) {
  358. if (dma_caps.cmd_pause && dma_caps.cmd_resume)
  359. hw->info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
  360. if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
  361. hw->info |= SNDRV_PCM_INFO_BATCH;
  362. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  363. addr_widths = dma_caps.dst_addr_widths;
  364. else
  365. addr_widths = dma_caps.src_addr_widths;
  366. }
  367. /*
  368. * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep
  369. * hw.formats set to 0, meaning no restrictions are in place.
  370. * In this case it's the responsibility of the DAI driver to
  371. * provide the supported format information.
  372. */
  373. if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
  374. /*
  375. * Prepare formats mask for valid/allowed sample types. If the
  376. * dma does not have support for the given physical word size,
  377. * it needs to be masked out so user space can not use the
  378. * format which produces corrupted audio.
  379. * In case the dma driver does not implement the slave_caps the
  380. * default assumption is that it supports 1, 2 and 4 bytes
  381. * widths.
  382. */
  383. pcm_for_each_format(i) {
  384. int bits = snd_pcm_format_physical_width(i);
  385. /*
  386. * Enable only samples with DMA supported physical
  387. * widths
  388. */
  389. switch (bits) {
  390. case 8:
  391. case 16:
  392. case 24:
  393. case 32:
  394. case 64:
  395. if (addr_widths & (1 << (bits / 8)))
  396. hw->formats |= pcm_format_to_bits(i);
  397. break;
  398. default:
  399. /* Unsupported types */
  400. break;
  401. }
  402. }
  403. return ret;
  404. }
  405. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_refine_runtime_hwparams);
  406. MODULE_LICENSE("GPL");