pcm_dmaengine.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. * Return: zero if successful, or a negative error code
  46. */
  47. int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
  48. const struct snd_pcm_hw_params *params,
  49. struct dma_slave_config *slave_config)
  50. {
  51. enum dma_slave_buswidth buswidth;
  52. int bits;
  53. bits = params_physical_width(params);
  54. if (bits < 8 || bits > 64)
  55. return -EINVAL;
  56. else if (bits == 8)
  57. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  58. else if (bits == 16)
  59. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  60. else if (bits == 24)
  61. buswidth = DMA_SLAVE_BUSWIDTH_3_BYTES;
  62. else if (bits <= 32)
  63. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  64. else
  65. buswidth = DMA_SLAVE_BUSWIDTH_8_BYTES;
  66. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  67. slave_config->direction = DMA_MEM_TO_DEV;
  68. slave_config->dst_addr_width = buswidth;
  69. } else {
  70. slave_config->direction = DMA_DEV_TO_MEM;
  71. slave_config->src_addr_width = buswidth;
  72. }
  73. slave_config->device_fc = false;
  74. return 0;
  75. }
  76. EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
  77. /**
  78. * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config
  79. * using DAI DMA data.
  80. * @substream: PCM substream
  81. * @dma_data: DAI DMA data
  82. * @slave_config: DMA slave configuration
  83. *
  84. * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width
  85. * fields of the DMA slave config from the same fields of the DAI DMA
  86. * data struct. The src and dst fields will be initialized depending on the
  87. * direction of the substream. If the substream is a playback stream the dst
  88. * fields will be initialized, if it is a capture stream the src fields will be
  89. * initialized. The {dst,src}_addr_width field will only be initialized if the
  90. * SND_DMAENGINE_PCM_DAI_FLAG_PACK flag is set or if the addr_width field of
  91. * the DAI DMA data struct is not equal to DMA_SLAVE_BUSWIDTH_UNDEFINED. If
  92. * both conditions are met the latter takes priority.
  93. */
  94. void snd_dmaengine_pcm_set_config_from_dai_data(
  95. const struct snd_pcm_substream *substream,
  96. const struct snd_dmaengine_dai_dma_data *dma_data,
  97. struct dma_slave_config *slave_config)
  98. {
  99. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  100. slave_config->dst_addr = dma_data->addr;
  101. slave_config->dst_maxburst = dma_data->maxburst;
  102. slave_config->dst_port_window_size = dma_data->port_window_size;
  103. if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
  104. slave_config->dst_addr_width =
  105. DMA_SLAVE_BUSWIDTH_UNDEFINED;
  106. if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
  107. slave_config->dst_addr_width = dma_data->addr_width;
  108. } else {
  109. slave_config->src_addr = dma_data->addr;
  110. slave_config->src_maxburst = dma_data->maxburst;
  111. slave_config->src_port_window_size = dma_data->port_window_size;
  112. if (dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK)
  113. slave_config->src_addr_width =
  114. DMA_SLAVE_BUSWIDTH_UNDEFINED;
  115. if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED)
  116. slave_config->src_addr_width = dma_data->addr_width;
  117. }
  118. slave_config->peripheral_config = dma_data->peripheral_config;
  119. slave_config->peripheral_size = dma_data->peripheral_size;
  120. }
  121. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
  122. static void dmaengine_pcm_dma_complete(void *arg)
  123. {
  124. unsigned int new_pos;
  125. struct snd_pcm_substream *substream = arg;
  126. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  127. new_pos = prtd->pos + snd_pcm_lib_period_bytes(substream);
  128. if (new_pos >= snd_pcm_lib_buffer_bytes(substream))
  129. new_pos = 0;
  130. prtd->pos = new_pos;
  131. snd_pcm_period_elapsed(substream);
  132. }
  133. static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
  134. {
  135. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  136. struct dma_chan *chan = prtd->dma_chan;
  137. struct dma_async_tx_descriptor *desc;
  138. enum dma_transfer_direction direction;
  139. unsigned long flags = DMA_CTRL_ACK;
  140. direction = snd_pcm_substream_to_dma_direction(substream);
  141. if (!substream->runtime->no_period_wakeup)
  142. flags |= DMA_PREP_INTERRUPT;
  143. prtd->pos = 0;
  144. desc = dmaengine_prep_dma_cyclic(chan,
  145. substream->runtime->dma_addr,
  146. snd_pcm_lib_buffer_bytes(substream),
  147. snd_pcm_lib_period_bytes(substream), direction, flags);
  148. if (!desc)
  149. return -ENOMEM;
  150. desc->callback = dmaengine_pcm_dma_complete;
  151. desc->callback_param = substream;
  152. prtd->cookie = dmaengine_submit(desc);
  153. return 0;
  154. }
  155. /**
  156. * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
  157. * @substream: PCM substream
  158. * @cmd: Trigger command
  159. *
  160. * This function can be used as the PCM trigger callback for dmaengine based PCM
  161. * driver implementations.
  162. *
  163. * Return: 0 on success, a negative error code otherwise
  164. */
  165. int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  166. {
  167. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  168. struct snd_pcm_runtime *runtime = substream->runtime;
  169. int ret;
  170. switch (cmd) {
  171. case SNDRV_PCM_TRIGGER_START:
  172. ret = dmaengine_pcm_prepare_and_submit(substream);
  173. if (ret)
  174. return ret;
  175. dma_async_issue_pending(prtd->dma_chan);
  176. break;
  177. case SNDRV_PCM_TRIGGER_RESUME:
  178. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  179. dmaengine_resume(prtd->dma_chan);
  180. break;
  181. case SNDRV_PCM_TRIGGER_SUSPEND:
  182. if (runtime->info & SNDRV_PCM_INFO_PAUSE)
  183. dmaengine_pause(prtd->dma_chan);
  184. else
  185. dmaengine_terminate_async(prtd->dma_chan);
  186. break;
  187. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  188. dmaengine_pause(prtd->dma_chan);
  189. break;
  190. case SNDRV_PCM_TRIGGER_STOP:
  191. dmaengine_terminate_async(prtd->dma_chan);
  192. break;
  193. default:
  194. return -EINVAL;
  195. }
  196. return 0;
  197. }
  198. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
  199. /**
  200. * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
  201. * @substream: PCM substream
  202. *
  203. * This function is deprecated and should not be used by new drivers, as its
  204. * results may be unreliable.
  205. *
  206. * Return: PCM position in frames
  207. */
  208. snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
  209. {
  210. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  211. return bytes_to_frames(substream->runtime, prtd->pos);
  212. }
  213. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
  214. /**
  215. * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
  216. * @substream: PCM substream
  217. *
  218. * This function can be used as the PCM pointer callback for dmaengine based PCM
  219. * driver implementations.
  220. *
  221. * Return: PCM position in frames
  222. */
  223. snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
  224. {
  225. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  226. struct snd_pcm_runtime *runtime = substream->runtime;
  227. struct dma_tx_state state;
  228. enum dma_status status;
  229. unsigned int buf_size;
  230. unsigned int pos = 0;
  231. status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
  232. if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
  233. buf_size = snd_pcm_lib_buffer_bytes(substream);
  234. if (state.residue > 0 && state.residue <= buf_size)
  235. pos = buf_size - state.residue;
  236. runtime->delay = bytes_to_frames(runtime,
  237. state.in_flight_bytes);
  238. }
  239. return bytes_to_frames(runtime, pos);
  240. }
  241. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
  242. /**
  243. * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
  244. * @filter_fn: Filter function used to request the DMA channel
  245. * @filter_data: Data passed to the DMA filter function
  246. *
  247. * This function request a DMA channel for usage with dmaengine PCM.
  248. *
  249. * Return: NULL or the requested DMA channel
  250. */
  251. struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
  252. void *filter_data)
  253. {
  254. dma_cap_mask_t mask;
  255. dma_cap_zero(mask);
  256. dma_cap_set(DMA_SLAVE, mask);
  257. dma_cap_set(DMA_CYCLIC, mask);
  258. return dma_request_channel(mask, filter_fn, filter_data);
  259. }
  260. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
  261. /**
  262. * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
  263. * @substream: PCM substream
  264. * @chan: DMA channel to use for data transfers
  265. *
  266. * The function should usually be called from the pcm open callback. Note that
  267. * this function will use private_data field of the substream's runtime. So it
  268. * is not available to your pcm driver implementation.
  269. *
  270. * Return: 0 on success, a negative error code otherwise
  271. */
  272. int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
  273. struct dma_chan *chan)
  274. {
  275. struct dmaengine_pcm_runtime_data *prtd;
  276. int ret;
  277. if (!chan)
  278. return -ENXIO;
  279. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  280. SNDRV_PCM_HW_PARAM_PERIODS);
  281. if (ret < 0)
  282. return ret;
  283. prtd = kzalloc_obj(*prtd);
  284. if (!prtd)
  285. return -ENOMEM;
  286. prtd->dma_chan = chan;
  287. substream->runtime->private_data = prtd;
  288. return 0;
  289. }
  290. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
  291. int snd_dmaengine_pcm_sync_stop(struct snd_pcm_substream *substream)
  292. {
  293. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  294. struct dma_tx_state state;
  295. enum dma_status status;
  296. status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
  297. if (status != DMA_PAUSED)
  298. dmaengine_synchronize(prtd->dma_chan);
  299. return 0;
  300. }
  301. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_sync_stop);
  302. static void __snd_dmaengine_pcm_close(struct snd_pcm_substream *substream,
  303. bool release_channel)
  304. {
  305. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  306. struct dma_tx_state state;
  307. enum dma_status status;
  308. status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
  309. if (status == DMA_PAUSED)
  310. dmaengine_terminate_async(prtd->dma_chan);
  311. dmaengine_synchronize(prtd->dma_chan);
  312. if (release_channel)
  313. dma_release_channel(prtd->dma_chan);
  314. kfree(prtd);
  315. }
  316. /**
  317. * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
  318. * @substream: PCM substream
  319. *
  320. * Return: 0 on success, a negative error code otherwise
  321. */
  322. int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
  323. {
  324. __snd_dmaengine_pcm_close(substream, false);
  325. return 0;
  326. }
  327. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
  328. /**
  329. * snd_dmaengine_pcm_close_release_chan - Close a dmaengine based PCM
  330. * substream and release channel
  331. * @substream: PCM substream
  332. *
  333. * Releases the DMA channel associated with the PCM substream.
  334. *
  335. * Return: zero if successful, or a negative error code
  336. */
  337. int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
  338. {
  339. __snd_dmaengine_pcm_close(substream, true);
  340. return 0;
  341. }
  342. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
  343. /**
  344. * snd_dmaengine_pcm_refine_runtime_hwparams - Refine runtime hw params
  345. * @substream: PCM substream
  346. * @dma_data: DAI DMA data
  347. * @hw: PCM hw params
  348. * @chan: DMA channel to use for data transfers
  349. *
  350. * This function will query DMA capability, then refine the pcm hardware
  351. * parameters.
  352. *
  353. * Return: 0 on success, a negative error code otherwise
  354. */
  355. int snd_dmaengine_pcm_refine_runtime_hwparams(
  356. struct snd_pcm_substream *substream,
  357. struct snd_dmaengine_dai_dma_data *dma_data,
  358. struct snd_pcm_hardware *hw,
  359. struct dma_chan *chan)
  360. {
  361. struct dma_slave_caps dma_caps;
  362. u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  363. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  364. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  365. snd_pcm_format_t i;
  366. int ret = 0;
  367. if (!hw || !chan || !dma_data)
  368. return -EINVAL;
  369. ret = dma_get_slave_caps(chan, &dma_caps);
  370. if (ret == 0) {
  371. if (dma_caps.cmd_pause && dma_caps.cmd_resume)
  372. hw->info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
  373. if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
  374. hw->info |= SNDRV_PCM_INFO_BATCH;
  375. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  376. addr_widths = dma_caps.dst_addr_widths;
  377. else
  378. addr_widths = dma_caps.src_addr_widths;
  379. }
  380. /*
  381. * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep
  382. * hw.formats set to 0, meaning no restrictions are in place.
  383. * In this case it's the responsibility of the DAI driver to
  384. * provide the supported format information.
  385. */
  386. if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
  387. /*
  388. * Prepare formats mask for valid/allowed sample types. If the
  389. * dma does not have support for the given physical word size,
  390. * it needs to be masked out so user space can not use the
  391. * format which produces corrupted audio.
  392. * In case the dma driver does not implement the slave_caps the
  393. * default assumption is that it supports 1, 2 and 4 bytes
  394. * widths.
  395. */
  396. pcm_for_each_format(i) {
  397. int bits = snd_pcm_format_physical_width(i);
  398. /*
  399. * Enable only samples with DMA supported physical
  400. * widths
  401. */
  402. switch (bits) {
  403. case 8:
  404. case 16:
  405. case 24:
  406. case 32:
  407. case 64:
  408. if (addr_widths & (1 << (bits / 8)))
  409. hw->formats |= pcm_format_to_bits(i);
  410. break;
  411. default:
  412. /* Unsupported types */
  413. break;
  414. }
  415. }
  416. return ret;
  417. }
  418. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_refine_runtime_hwparams);
  419. MODULE_DESCRIPTION("PCM dmaengine helper APIs");
  420. MODULE_LICENSE("GPL");