soc-generic-dmaengine-pcm.c 13 KB

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