pcm_dmaengine.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. slave_config->peripheral_config = dma_data->peripheral_config;
  143. slave_config->peripheral_size = dma_data->peripheral_size;
  144. }
  145. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data);
  146. static void dmaengine_pcm_dma_complete(void *arg)
  147. {
  148. struct snd_pcm_substream *substream = arg;
  149. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  150. prtd->pos += snd_pcm_lib_period_bytes(substream);
  151. if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
  152. prtd->pos = 0;
  153. snd_pcm_period_elapsed(substream);
  154. }
  155. static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
  156. {
  157. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  158. struct dma_chan *chan = prtd->dma_chan;
  159. struct dw_cyclic_desc *cdesc;
  160. enum dma_transfer_direction direction;
  161. direction = snd_pcm_substream_to_dma_direction(substream);
  162. prtd->pos = 0;
  163. cdesc = dw_dma_cyclic_prep(chan,
  164. substream->runtime->dma_addr,
  165. snd_pcm_lib_buffer_bytes(substream),
  166. snd_pcm_lib_period_bytes(substream), direction);
  167. if (IS_ERR(cdesc))
  168. return PTR_ERR(cdesc);
  169. cdesc->period_callback = dmaengine_pcm_dma_complete;
  170. cdesc->period_callback_param = substream;
  171. return 0;
  172. }
  173. /**
  174. * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
  175. * @substream: PCM substream
  176. * @cmd: Trigger command
  177. *
  178. * Returns 0 on success, a negative error code otherwise.
  179. *
  180. * This function can be used as the PCM trigger callback for dmaengine based PCM
  181. * driver implementations.
  182. */
  183. int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  184. {
  185. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  186. int ret;
  187. switch (cmd) {
  188. case SNDRV_PCM_TRIGGER_RESUME:
  189. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  190. case SNDRV_PCM_TRIGGER_START:
  191. ret = dmaengine_pcm_prepare_and_submit(substream);
  192. if (ret)
  193. return ret;
  194. ret = dw_dma_cyclic_start(prtd->dma_chan);
  195. if (ret)
  196. return ret;
  197. break;
  198. case SNDRV_PCM_TRIGGER_SUSPEND:
  199. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  200. case SNDRV_PCM_TRIGGER_STOP:
  201. dw_dma_cyclic_stop(prtd->dma_chan);
  202. break;
  203. default:
  204. return -EINVAL;
  205. }
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
  209. /**
  210. * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
  211. * @substream: PCM substream
  212. *
  213. * This function is deprecated and should not be used by new drivers, as its
  214. * results may be unreliable.
  215. */
  216. snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
  217. {
  218. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  219. return bytes_to_frames(substream->runtime, prtd->pos);
  220. }
  221. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
  222. /**
  223. * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
  224. * @substream: PCM substream
  225. *
  226. * This function can be used as the PCM pointer callback for dmaengine based PCM
  227. * driver implementations.
  228. */
  229. snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
  230. {
  231. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  232. struct snd_pcm_runtime *runtime = substream->runtime;
  233. snd_pcm_uframes_t frames;
  234. unsigned long bytes = 0;
  235. if (substream->stream== SNDRV_PCM_STREAM_PLAYBACK)
  236. bytes = dw_dma_get_src_addr(prtd->dma_chan);
  237. else if (substream->stream== SNDRV_PCM_STREAM_CAPTURE)
  238. bytes = dw_dma_get_dst_addr(prtd->dma_chan);
  239. bytes -= runtime->dma_addr;
  240. frames = bytes_to_frames(runtime, bytes);
  241. if (frames >= runtime->buffer_size)
  242. frames -= runtime->buffer_size;
  243. return frames;
  244. }
  245. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
  246. /**
  247. * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM
  248. * @filter_fn: Filter function used to request the DMA channel
  249. * @filter_data: Data passed to the DMA filter function
  250. *
  251. * Returns NULL or the requested DMA channel.
  252. *
  253. * This function request a DMA channel for usage with dmaengine PCM.
  254. */
  255. struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn,
  256. void *filter_data)
  257. {
  258. dma_cap_mask_t mask;
  259. dma_cap_zero(mask);
  260. dma_cap_set(DMA_SLAVE, mask);
  261. dma_cap_set(DMA_CYCLIC, mask);
  262. return dma_request_channel(mask, filter_fn, filter_data);
  263. }
  264. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel);
  265. /**
  266. * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
  267. * @substream: PCM substream
  268. * @chan: DMA channel to use for data transfers
  269. *
  270. * Returns 0 on success, a negative error code otherwise.
  271. *
  272. * The function should usually be called from the pcm open callback. Note that
  273. * this function will use private_data field of the substream's runtime. So it
  274. * is not available to your pcm driver implementation.
  275. */
  276. int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
  277. struct dma_chan *chan)
  278. {
  279. struct dmaengine_pcm_runtime_data *prtd;
  280. int ret;
  281. if (!chan)
  282. return -ENXIO;
  283. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  284. SNDRV_PCM_HW_PARAM_PERIODS);
  285. if (ret < 0)
  286. return ret;
  287. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  288. if (!prtd)
  289. return -ENOMEM;
  290. prtd->dma_chan = chan;
  291. substream->runtime->private_data = prtd;
  292. return 0;
  293. }
  294. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
  295. /**
  296. * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel
  297. * @substream: PCM substream
  298. * @filter_fn: Filter function used to request the DMA channel
  299. * @filter_data: Data passed to the DMA filter function
  300. *
  301. * Returns 0 on success, a negative error code otherwise.
  302. *
  303. * This function will request a DMA channel using the passed filter function and
  304. * data. The function should usually be called from the pcm open callback. Note
  305. * that this function will use private_data field of the substream's runtime. So
  306. * it is not available to your pcm driver implementation.
  307. */
  308. //int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream, struct dma_chan *chan)
  309. //{
  310. // struct dmaengine_pcm_runtime_data *prtd;
  311. // int ret;
  312. // if (!chan)
  313. // return -ENXIO;
  314. // ret = snd_pcm_hw_constraint_integer(substream->runtime,
  315. // SNDRV_PCM_HW_PARAM_PERIODS);
  316. // if (ret < 0)
  317. // return ret;
  318. // prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  319. // if (!prtd)
  320. // return -ENOMEM;
  321. // prtd->dma_chan = chan; substream->runtime->private_data = prtd;
  322. // return 0;
  323. //}
  324. //EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
  325. int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream,
  326. dma_filter_fn filter_fn, void *filter_data)
  327. {
  328. return snd_dmaengine_pcm_open(substream,
  329. snd_dmaengine_pcm_request_channel(filter_fn, filter_data));
  330. }
  331. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan);
  332. int snd_dmaengine_pcm_sync_stop(struct snd_pcm_substream *substream)
  333. {
  334. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  335. struct dma_tx_state state;
  336. enum dma_status status;
  337. status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
  338. if (status != DMA_PAUSED)
  339. dmaengine_synchronize(prtd->dma_chan);
  340. return 0;
  341. }
  342. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_sync_stop);
  343. /**
  344. * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
  345. * @substream: PCM substream
  346. */
  347. int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
  348. {
  349. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  350. dw_dma_cyclic_free(prtd->dma_chan);
  351. kfree(prtd);
  352. return 0;
  353. }
  354. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
  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. /**
  420. * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel
  421. * @substream: PCM substream
  422. *
  423. * Releases the DMA channel associated with the PCM substream.
  424. */
  425. int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream)
  426. {
  427. struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  428. dw_dma_cyclic_free(prtd->dma_chan);
  429. dma_release_channel(prtd->dma_chan);
  430. kfree(prtd);
  431. return 0;
  432. }
  433. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan);
  434. MODULE_LICENSE("GPL");