soc-generic-dmaengine-pcm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright (C) 2013, Analog Devices Inc.
  3. * Author: Lars-Peter Clausen <lars@metafoo.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/dmaengine.h>
  18. #include <linux/slab.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/of.h>
  24. #include <sound/dmaengine_pcm.h>
  25. /*
  26. * The platforms dmaengine driver does not support reporting the amount of
  27. * bytes that are still left to transfer.
  28. */
  29. #define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
  30. struct dmaengine_pcm {
  31. struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];
  32. const struct snd_dmaengine_pcm_config *config;
  33. struct snd_soc_platform platform;
  34. unsigned int flags;
  35. };
  36. static struct dmaengine_pcm *soc_platform_to_pcm(struct snd_soc_platform *p)
  37. {
  38. return container_of(p, struct dmaengine_pcm, platform);
  39. }
  40. static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
  41. struct snd_pcm_substream *substream)
  42. {
  43. if (!pcm->chan[substream->stream])
  44. return NULL;
  45. return pcm->chan[substream->stream]->device->dev;
  46. }
  47. /**
  48. * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback
  49. * @substream: PCM substream
  50. * @params: hw_params
  51. * @slave_config: DMA slave config to prepare
  52. *
  53. * This function can be used as a generic prepare_slave_config callback for
  54. * platforms which make use of the snd_dmaengine_dai_dma_data struct for their
  55. * DAI DMA data. Internally the function will first call
  56. * snd_hwparams_to_dma_slave_config to fill in the slave config based on the
  57. * hw_params, followed by snd_dmaengine_set_config_from_dai_data to fill in the
  58. * remaining fields based on the DAI DMA data.
  59. */
  60. int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
  61. struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
  62. {
  63. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  64. struct snd_dmaengine_dai_dma_data *dma_data;
  65. int ret;
  66. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  67. ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
  68. if (ret)
  69. return ret;
  70. snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
  71. slave_config);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
  75. static int dmaengine_pcm_hw_params(struct snd_pcm_substream *substream,
  76. struct snd_pcm_hw_params *params)
  77. {
  78. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  79. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  80. struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
  81. int (*prepare_slave_config)(struct snd_pcm_substream *substream,
  82. struct snd_pcm_hw_params *params,
  83. struct dma_slave_config *slave_config);
  84. struct dma_slave_config slave_config;
  85. int ret;
  86. memset(&slave_config, 0, sizeof(slave_config));
  87. if (!pcm->config)
  88. prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
  89. else
  90. prepare_slave_config = pcm->config->prepare_slave_config;
  91. if (prepare_slave_config) {
  92. ret = prepare_slave_config(substream, params, &slave_config);
  93. if (ret)
  94. return ret;
  95. ret = dmaengine_slave_config(chan, &slave_config);
  96. if (ret)
  97. return ret;
  98. }
  99. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  100. }
  101. static int dmaengine_pcm_set_runtime_hwparams(struct snd_pcm_substream *substream)
  102. {
  103. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  104. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  105. struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
  106. struct dma_chan *chan = pcm->chan[substream->stream];
  107. struct snd_dmaengine_dai_dma_data *dma_data;
  108. struct dma_slave_caps dma_caps;
  109. struct snd_pcm_hardware hw;
  110. u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  111. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  112. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  113. int i, ret;
  114. if (pcm->config && pcm->config->pcm_hardware)
  115. return snd_soc_set_runtime_hwparams(substream,
  116. pcm->config->pcm_hardware);
  117. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  118. memset(&hw, 0, sizeof(hw));
  119. hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  120. SNDRV_PCM_INFO_INTERLEAVED;
  121. hw.periods_min = 2;
  122. hw.periods_max = UINT_MAX;
  123. hw.period_bytes_min = 256;
  124. hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
  125. hw.buffer_bytes_max = SIZE_MAX;
  126. hw.fifo_size = dma_data->fifo_size;
  127. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
  128. hw.info |= SNDRV_PCM_INFO_BATCH;
  129. ret = dma_get_slave_caps(chan, &dma_caps);
  130. if (ret == 0) {
  131. if (dma_caps.cmd_pause)
  132. hw.info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
  133. if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
  134. hw.info |= SNDRV_PCM_INFO_BATCH;
  135. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  136. addr_widths = dma_caps.dst_addr_widths;
  137. else
  138. addr_widths = dma_caps.src_addr_widths;
  139. }
  140. /*
  141. * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep
  142. * hw.formats set to 0, meaning no restrictions are in place.
  143. * In this case it's the responsibility of the DAI driver to
  144. * provide the supported format information.
  145. */
  146. if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
  147. /*
  148. * Prepare formats mask for valid/allowed sample types. If the
  149. * dma does not have support for the given physical word size,
  150. * it needs to be masked out so user space can not use the
  151. * format which produces corrupted audio.
  152. * In case the dma driver does not implement the slave_caps the
  153. * default assumption is that it supports 1, 2 and 4 bytes
  154. * widths.
  155. */
  156. for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
  157. int bits = snd_pcm_format_physical_width(i);
  158. /*
  159. * Enable only samples with DMA supported physical
  160. * widths
  161. */
  162. switch (bits) {
  163. case 8:
  164. case 16:
  165. case 24:
  166. case 32:
  167. case 64:
  168. if (addr_widths & (1 << (bits / 8)))
  169. hw.formats |= (1LL << i);
  170. break;
  171. default:
  172. /* Unsupported types */
  173. break;
  174. }
  175. }
  176. return snd_soc_set_runtime_hwparams(substream, &hw);
  177. }
  178. static int dmaengine_pcm_open(struct snd_pcm_substream *substream)
  179. {
  180. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  181. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  182. struct dma_chan *chan = pcm->chan[substream->stream];
  183. int ret;
  184. ret = dmaengine_pcm_set_runtime_hwparams(substream);
  185. if (ret)
  186. return ret;
  187. return snd_dmaengine_pcm_open(substream, chan);
  188. }
  189. static struct dma_chan *dmaengine_pcm_compat_request_channel(
  190. struct snd_soc_pcm_runtime *rtd,
  191. struct snd_pcm_substream *substream)
  192. {
  193. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  194. struct snd_dmaengine_dai_dma_data *dma_data;
  195. dma_filter_fn fn = NULL;
  196. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  197. if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
  198. return pcm->chan[0];
  199. if (pcm->config && pcm->config->compat_request_channel)
  200. return pcm->config->compat_request_channel(rtd, substream);
  201. if (pcm->config)
  202. fn = pcm->config->compat_filter_fn;
  203. return snd_dmaengine_pcm_request_channel(fn, dma_data->filter_data);
  204. }
  205. static bool dmaengine_pcm_can_report_residue(struct device *dev,
  206. struct dma_chan *chan)
  207. {
  208. struct dma_slave_caps dma_caps;
  209. int ret;
  210. ret = dma_get_slave_caps(chan, &dma_caps);
  211. if (ret != 0) {
  212. dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
  213. ret);
  214. return false;
  215. }
  216. if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
  217. return false;
  218. return true;
  219. }
  220. static int dmaengine_pcm_new(struct snd_soc_pcm_runtime *rtd)
  221. {
  222. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  223. const struct snd_dmaengine_pcm_config *config = pcm->config;
  224. struct device *dev = rtd->platform->dev;
  225. struct snd_dmaengine_dai_dma_data *dma_data;
  226. struct snd_pcm_substream *substream;
  227. size_t prealloc_buffer_size;
  228. size_t max_buffer_size;
  229. unsigned int i;
  230. int ret;
  231. if (config && config->prealloc_buffer_size) {
  232. prealloc_buffer_size = config->prealloc_buffer_size;
  233. max_buffer_size = config->pcm_hardware->buffer_bytes_max;
  234. } else {
  235. prealloc_buffer_size = 512 * 1024;
  236. max_buffer_size = SIZE_MAX;
  237. }
  238. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
  239. substream = rtd->pcm->streams[i].substream;
  240. if (!substream)
  241. continue;
  242. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  243. if (!pcm->chan[i] &&
  244. (pcm->flags & SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME))
  245. pcm->chan[i] = dma_request_slave_channel(dev,
  246. dma_data->chan_name);
  247. if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
  248. pcm->chan[i] = dmaengine_pcm_compat_request_channel(rtd,
  249. substream);
  250. }
  251. if (!pcm->chan[i]) {
  252. dev_err(rtd->platform->dev,
  253. "Missing dma channel for stream: %d\n", i);
  254. return -EINVAL;
  255. }
  256. ret = snd_pcm_lib_preallocate_pages(substream,
  257. SNDRV_DMA_TYPE_DEV_IRAM,
  258. dmaengine_dma_dev(pcm, substream),
  259. prealloc_buffer_size,
  260. max_buffer_size);
  261. if (ret)
  262. return ret;
  263. if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
  264. pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
  265. }
  266. return 0;
  267. }
  268. static snd_pcm_uframes_t dmaengine_pcm_pointer(
  269. struct snd_pcm_substream *substream)
  270. {
  271. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  272. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  273. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
  274. return snd_dmaengine_pcm_pointer_no_residue(substream);
  275. else
  276. return snd_dmaengine_pcm_pointer(substream);
  277. }
  278. static const struct snd_pcm_ops dmaengine_pcm_ops = {
  279. .open = dmaengine_pcm_open,
  280. .close = snd_dmaengine_pcm_close,
  281. .ioctl = snd_pcm_lib_ioctl,
  282. .hw_params = dmaengine_pcm_hw_params,
  283. .hw_free = snd_pcm_lib_free_pages,
  284. .trigger = snd_dmaengine_pcm_trigger,
  285. .pointer = dmaengine_pcm_pointer,
  286. };
  287. static const struct snd_soc_platform_driver dmaengine_pcm_platform = {
  288. .component_driver = {
  289. .probe_order = SND_SOC_COMP_ORDER_LATE,
  290. },
  291. .ops = &dmaengine_pcm_ops,
  292. .pcm_new = dmaengine_pcm_new,
  293. };
  294. static const char * const dmaengine_pcm_dma_channel_names[] = {
  295. [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
  296. [SNDRV_PCM_STREAM_CAPTURE] = "rx",
  297. };
  298. static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
  299. struct device *dev, const struct snd_dmaengine_pcm_config *config)
  300. {
  301. unsigned int i;
  302. const char *name;
  303. struct dma_chan *chan;
  304. if ((pcm->flags & (SND_DMAENGINE_PCM_FLAG_NO_DT |
  305. SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME)) ||
  306. !dev->of_node)
  307. return 0;
  308. if (config && config->dma_dev) {
  309. /*
  310. * If this warning is seen, it probably means that your Linux
  311. * device structure does not match your HW device structure.
  312. * It would be best to refactor the Linux device structure to
  313. * correctly match the HW structure.
  314. */
  315. dev_warn(dev, "DMA channels sourced from device %s",
  316. dev_name(config->dma_dev));
  317. dev = config->dma_dev;
  318. }
  319. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
  320. i++) {
  321. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  322. name = "rx-tx";
  323. else
  324. name = dmaengine_pcm_dma_channel_names[i];
  325. if (config && config->chan_names[i])
  326. name = config->chan_names[i];
  327. chan = dma_request_slave_channel_reason(dev, name);
  328. if (IS_ERR(chan)) {
  329. if (PTR_ERR(chan) == -EPROBE_DEFER)
  330. return -EPROBE_DEFER;
  331. pcm->chan[i] = NULL;
  332. } else {
  333. pcm->chan[i] = chan;
  334. }
  335. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  336. break;
  337. }
  338. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  339. pcm->chan[1] = pcm->chan[0];
  340. return 0;
  341. }
  342. static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
  343. {
  344. unsigned int i;
  345. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
  346. i++) {
  347. if (!pcm->chan[i])
  348. continue;
  349. dma_release_channel(pcm->chan[i]);
  350. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  351. break;
  352. }
  353. }
  354. /**
  355. * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
  356. * @dev: The parent device for the PCM device
  357. * @config: Platform specific PCM configuration
  358. * @flags: Platform specific quirks
  359. */
  360. int snd_dmaengine_pcm_register(struct device *dev,
  361. const struct snd_dmaengine_pcm_config *config, unsigned int flags)
  362. {
  363. struct dmaengine_pcm *pcm;
  364. int ret;
  365. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  366. if (!pcm)
  367. return -ENOMEM;
  368. pcm->config = config;
  369. pcm->flags = flags;
  370. ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
  371. if (ret)
  372. goto err_free_dma;
  373. ret = snd_soc_add_platform(dev, &pcm->platform,
  374. &dmaengine_pcm_platform);
  375. if (ret)
  376. goto err_free_dma;
  377. return 0;
  378. err_free_dma:
  379. dmaengine_pcm_release_chan(pcm);
  380. kfree(pcm);
  381. return ret;
  382. }
  383. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
  384. /**
  385. * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
  386. * @dev: Parent device the PCM was register with
  387. *
  388. * Removes a dmaengine based PCM device previously registered with
  389. * snd_dmaengine_pcm_register.
  390. */
  391. void snd_dmaengine_pcm_unregister(struct device *dev)
  392. {
  393. struct snd_soc_platform *platform;
  394. struct dmaengine_pcm *pcm;
  395. platform = snd_soc_lookup_platform(dev);
  396. if (!platform)
  397. return;
  398. pcm = soc_platform_to_pcm(platform);
  399. snd_soc_remove_platform(platform);
  400. dmaengine_pcm_release_chan(pcm);
  401. kfree(pcm);
  402. }
  403. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
  404. MODULE_LICENSE("GPL");