soc-generic-dmaengine-pcm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. * Prepare formats mask for valid/allowed sample types. If the dma does
  142. * not have support for the given physical word size, it needs to be
  143. * masked out so user space can not use the format which produces
  144. * corrupted audio.
  145. * In case the dma driver does not implement the slave_caps the default
  146. * assumption is that it supports 1, 2 and 4 bytes widths.
  147. */
  148. for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
  149. int bits = snd_pcm_format_physical_width(i);
  150. /* Enable only samples with DMA supported physical widths */
  151. switch (bits) {
  152. case 8:
  153. case 16:
  154. case 24:
  155. case 32:
  156. case 64:
  157. if (addr_widths & (1 << (bits / 8)))
  158. hw.formats |= (1LL << i);
  159. break;
  160. default:
  161. /* Unsupported types */
  162. break;
  163. }
  164. }
  165. return snd_soc_set_runtime_hwparams(substream, &hw);
  166. }
  167. static int dmaengine_pcm_open(struct snd_pcm_substream *substream)
  168. {
  169. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  170. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  171. struct dma_chan *chan = pcm->chan[substream->stream];
  172. int ret;
  173. ret = dmaengine_pcm_set_runtime_hwparams(substream);
  174. if (ret)
  175. return ret;
  176. return snd_dmaengine_pcm_open(substream, chan);
  177. }
  178. static struct dma_chan *dmaengine_pcm_compat_request_channel(
  179. struct snd_soc_pcm_runtime *rtd,
  180. struct snd_pcm_substream *substream)
  181. {
  182. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  183. struct snd_dmaengine_dai_dma_data *dma_data;
  184. dma_filter_fn fn = NULL;
  185. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  186. if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
  187. return pcm->chan[0];
  188. if (pcm->config && pcm->config->compat_request_channel)
  189. return pcm->config->compat_request_channel(rtd, substream);
  190. if (pcm->config)
  191. fn = pcm->config->compat_filter_fn;
  192. return snd_dmaengine_pcm_request_channel(fn, dma_data->filter_data);
  193. }
  194. static bool dmaengine_pcm_can_report_residue(struct device *dev,
  195. struct dma_chan *chan)
  196. {
  197. struct dma_slave_caps dma_caps;
  198. int ret;
  199. ret = dma_get_slave_caps(chan, &dma_caps);
  200. if (ret != 0) {
  201. dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
  202. ret);
  203. return false;
  204. }
  205. if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
  206. return false;
  207. return true;
  208. }
  209. static int dmaengine_pcm_new(struct snd_soc_pcm_runtime *rtd)
  210. {
  211. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  212. const struct snd_dmaengine_pcm_config *config = pcm->config;
  213. struct device *dev = rtd->platform->dev;
  214. struct snd_dmaengine_dai_dma_data *dma_data;
  215. struct snd_pcm_substream *substream;
  216. size_t prealloc_buffer_size;
  217. size_t max_buffer_size;
  218. unsigned int i;
  219. int ret;
  220. if (config && config->prealloc_buffer_size) {
  221. prealloc_buffer_size = config->prealloc_buffer_size;
  222. max_buffer_size = config->pcm_hardware->buffer_bytes_max;
  223. } else {
  224. prealloc_buffer_size = 512 * 1024;
  225. max_buffer_size = SIZE_MAX;
  226. }
  227. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
  228. substream = rtd->pcm->streams[i].substream;
  229. if (!substream)
  230. continue;
  231. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  232. if (!pcm->chan[i] &&
  233. (pcm->flags & SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME))
  234. pcm->chan[i] = dma_request_slave_channel(dev,
  235. dma_data->chan_name);
  236. if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
  237. pcm->chan[i] = dmaengine_pcm_compat_request_channel(rtd,
  238. substream);
  239. }
  240. if (!pcm->chan[i]) {
  241. dev_err(rtd->platform->dev,
  242. "Missing dma channel for stream: %d\n", i);
  243. return -EINVAL;
  244. }
  245. ret = snd_pcm_lib_preallocate_pages(substream,
  246. SNDRV_DMA_TYPE_DEV_IRAM,
  247. dmaengine_dma_dev(pcm, substream),
  248. prealloc_buffer_size,
  249. max_buffer_size);
  250. if (ret)
  251. return ret;
  252. if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
  253. pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
  254. }
  255. return 0;
  256. }
  257. static snd_pcm_uframes_t dmaengine_pcm_pointer(
  258. struct snd_pcm_substream *substream)
  259. {
  260. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  261. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  262. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
  263. return snd_dmaengine_pcm_pointer_no_residue(substream);
  264. else
  265. return snd_dmaengine_pcm_pointer(substream);
  266. }
  267. static const struct snd_pcm_ops dmaengine_pcm_ops = {
  268. .open = dmaengine_pcm_open,
  269. .close = snd_dmaengine_pcm_close,
  270. .ioctl = snd_pcm_lib_ioctl,
  271. .hw_params = dmaengine_pcm_hw_params,
  272. .hw_free = snd_pcm_lib_free_pages,
  273. .trigger = snd_dmaengine_pcm_trigger,
  274. .pointer = dmaengine_pcm_pointer,
  275. };
  276. static const struct snd_soc_platform_driver dmaengine_pcm_platform = {
  277. .component_driver = {
  278. .probe_order = SND_SOC_COMP_ORDER_LATE,
  279. },
  280. .ops = &dmaengine_pcm_ops,
  281. .pcm_new = dmaengine_pcm_new,
  282. };
  283. static const char * const dmaengine_pcm_dma_channel_names[] = {
  284. [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
  285. [SNDRV_PCM_STREAM_CAPTURE] = "rx",
  286. };
  287. static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
  288. struct device *dev, const struct snd_dmaengine_pcm_config *config)
  289. {
  290. unsigned int i;
  291. const char *name;
  292. struct dma_chan *chan;
  293. if ((pcm->flags & (SND_DMAENGINE_PCM_FLAG_NO_DT |
  294. SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME)) ||
  295. !dev->of_node)
  296. return 0;
  297. if (config && config->dma_dev) {
  298. /*
  299. * If this warning is seen, it probably means that your Linux
  300. * device structure does not match your HW device structure.
  301. * It would be best to refactor the Linux device structure to
  302. * correctly match the HW structure.
  303. */
  304. dev_warn(dev, "DMA channels sourced from device %s",
  305. dev_name(config->dma_dev));
  306. dev = config->dma_dev;
  307. }
  308. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
  309. i++) {
  310. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  311. name = "rx-tx";
  312. else
  313. name = dmaengine_pcm_dma_channel_names[i];
  314. if (config && config->chan_names[i])
  315. name = config->chan_names[i];
  316. chan = dma_request_slave_channel_reason(dev, name);
  317. if (IS_ERR(chan)) {
  318. if (PTR_ERR(chan) == -EPROBE_DEFER)
  319. return -EPROBE_DEFER;
  320. pcm->chan[i] = NULL;
  321. } else {
  322. pcm->chan[i] = chan;
  323. }
  324. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  325. break;
  326. }
  327. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  328. pcm->chan[1] = pcm->chan[0];
  329. return 0;
  330. }
  331. static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
  332. {
  333. unsigned int i;
  334. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
  335. i++) {
  336. if (!pcm->chan[i])
  337. continue;
  338. dma_release_channel(pcm->chan[i]);
  339. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  340. break;
  341. }
  342. }
  343. /**
  344. * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
  345. * @dev: The parent device for the PCM device
  346. * @config: Platform specific PCM configuration
  347. * @flags: Platform specific quirks
  348. */
  349. int snd_dmaengine_pcm_register(struct device *dev,
  350. const struct snd_dmaengine_pcm_config *config, unsigned int flags)
  351. {
  352. struct dmaengine_pcm *pcm;
  353. int ret;
  354. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  355. if (!pcm)
  356. return -ENOMEM;
  357. pcm->config = config;
  358. pcm->flags = flags;
  359. ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
  360. if (ret)
  361. goto err_free_dma;
  362. ret = snd_soc_add_platform(dev, &pcm->platform,
  363. &dmaengine_pcm_platform);
  364. if (ret)
  365. goto err_free_dma;
  366. return 0;
  367. err_free_dma:
  368. dmaengine_pcm_release_chan(pcm);
  369. kfree(pcm);
  370. return ret;
  371. }
  372. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
  373. /**
  374. * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
  375. * @dev: Parent device the PCM was register with
  376. *
  377. * Removes a dmaengine based PCM device previously registered with
  378. * snd_dmaengine_pcm_register.
  379. */
  380. void snd_dmaengine_pcm_unregister(struct device *dev)
  381. {
  382. struct snd_soc_platform *platform;
  383. struct dmaengine_pcm *pcm;
  384. platform = snd_soc_lookup_platform(dev);
  385. if (!platform)
  386. return;
  387. pcm = soc_platform_to_pcm(platform);
  388. snd_soc_remove_platform(platform);
  389. dmaengine_pcm_release_chan(pcm);
  390. kfree(pcm);
  391. }
  392. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
  393. MODULE_LICENSE("GPL");