soc-generic-dmaengine-pcm.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_CAPTURE + 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. struct dma_slave_config slave_config;
  77. int ret;
  78. if (pcm->config->prepare_slave_config) {
  79. ret = pcm->config->prepare_slave_config(substream, params,
  80. &slave_config);
  81. if (ret)
  82. return ret;
  83. ret = dmaengine_slave_config(chan, &slave_config);
  84. if (ret)
  85. return ret;
  86. }
  87. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  88. }
  89. static int dmaengine_pcm_set_runtime_hwparams(struct snd_pcm_substream *substream)
  90. {
  91. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  92. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  93. struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
  94. struct dma_chan *chan = pcm->chan[substream->stream];
  95. struct snd_dmaengine_dai_dma_data *dma_data;
  96. struct dma_slave_caps dma_caps;
  97. struct snd_pcm_hardware hw;
  98. int ret;
  99. if (pcm->config->pcm_hardware)
  100. return snd_soc_set_runtime_hwparams(substream,
  101. pcm->config->pcm_hardware);
  102. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  103. memset(&hw, 0, sizeof(hw));
  104. hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  105. SNDRV_PCM_INFO_INTERLEAVED;
  106. hw.periods_min = 2;
  107. hw.periods_max = UINT_MAX;
  108. hw.period_bytes_min = 256;
  109. hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
  110. hw.buffer_bytes_max = SIZE_MAX;
  111. hw.fifo_size = dma_data->fifo_size;
  112. ret = dma_get_slave_caps(chan, &dma_caps);
  113. if (ret == 0) {
  114. if (dma_caps.cmd_pause)
  115. hw.info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
  116. }
  117. return snd_soc_set_runtime_hwparams(substream, &hw);
  118. }
  119. static int dmaengine_pcm_open(struct snd_pcm_substream *substream)
  120. {
  121. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  122. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  123. struct dma_chan *chan = pcm->chan[substream->stream];
  124. int ret;
  125. ret = dmaengine_pcm_set_runtime_hwparams(substream);
  126. if (ret)
  127. return ret;
  128. return snd_dmaengine_pcm_open(substream, chan);
  129. }
  130. static void dmaengine_pcm_free(struct snd_pcm *pcm)
  131. {
  132. snd_pcm_lib_preallocate_free_for_all(pcm);
  133. }
  134. static struct dma_chan *dmaengine_pcm_compat_request_channel(
  135. struct snd_soc_pcm_runtime *rtd,
  136. struct snd_pcm_substream *substream)
  137. {
  138. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  139. if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
  140. return pcm->chan[0];
  141. if (pcm->config->compat_request_channel)
  142. return pcm->config->compat_request_channel(rtd, substream);
  143. return snd_dmaengine_pcm_request_channel(pcm->config->compat_filter_fn,
  144. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream));
  145. }
  146. static int dmaengine_pcm_new(struct snd_soc_pcm_runtime *rtd)
  147. {
  148. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  149. const struct snd_dmaengine_pcm_config *config = pcm->config;
  150. struct snd_pcm_substream *substream;
  151. unsigned int i;
  152. int ret;
  153. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
  154. substream = rtd->pcm->streams[i].substream;
  155. if (!substream)
  156. continue;
  157. if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
  158. pcm->chan[i] = dmaengine_pcm_compat_request_channel(rtd,
  159. substream);
  160. }
  161. if (!pcm->chan[i]) {
  162. dev_err(rtd->platform->dev,
  163. "Missing dma channel for stream: %d\n", i);
  164. ret = -EINVAL;
  165. goto err_free;
  166. }
  167. ret = snd_pcm_lib_preallocate_pages(substream,
  168. SNDRV_DMA_TYPE_DEV,
  169. dmaengine_dma_dev(pcm, substream),
  170. config->prealloc_buffer_size,
  171. config->pcm_hardware->buffer_bytes_max);
  172. if (ret)
  173. goto err_free;
  174. }
  175. return 0;
  176. err_free:
  177. dmaengine_pcm_free(rtd->pcm);
  178. return ret;
  179. }
  180. static const struct snd_pcm_ops dmaengine_pcm_ops = {
  181. .open = dmaengine_pcm_open,
  182. .close = snd_dmaengine_pcm_close,
  183. .ioctl = snd_pcm_lib_ioctl,
  184. .hw_params = dmaengine_pcm_hw_params,
  185. .hw_free = snd_pcm_lib_free_pages,
  186. .trigger = snd_dmaengine_pcm_trigger,
  187. .pointer = snd_dmaengine_pcm_pointer,
  188. };
  189. static const struct snd_soc_platform_driver dmaengine_pcm_platform = {
  190. .ops = &dmaengine_pcm_ops,
  191. .pcm_new = dmaengine_pcm_new,
  192. .pcm_free = dmaengine_pcm_free,
  193. .probe_order = SND_SOC_COMP_ORDER_LATE,
  194. };
  195. static const struct snd_pcm_ops dmaengine_no_residue_pcm_ops = {
  196. .open = dmaengine_pcm_open,
  197. .close = snd_dmaengine_pcm_close,
  198. .ioctl = snd_pcm_lib_ioctl,
  199. .hw_params = dmaengine_pcm_hw_params,
  200. .hw_free = snd_pcm_lib_free_pages,
  201. .trigger = snd_dmaengine_pcm_trigger,
  202. .pointer = snd_dmaengine_pcm_pointer_no_residue,
  203. };
  204. static const struct snd_soc_platform_driver dmaengine_no_residue_pcm_platform = {
  205. .ops = &dmaengine_no_residue_pcm_ops,
  206. .pcm_new = dmaengine_pcm_new,
  207. .pcm_free = dmaengine_pcm_free,
  208. .probe_order = SND_SOC_COMP_ORDER_LATE,
  209. };
  210. static const char * const dmaengine_pcm_dma_channel_names[] = {
  211. [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
  212. [SNDRV_PCM_STREAM_CAPTURE] = "rx",
  213. };
  214. static void dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
  215. struct device *dev)
  216. {
  217. unsigned int i;
  218. if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_DT) || !dev->of_node)
  219. return;
  220. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) {
  221. pcm->chan[0] = dma_request_slave_channel(dev, "rx-tx");
  222. pcm->chan[1] = pcm->chan[0];
  223. } else {
  224. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
  225. pcm->chan[i] = dma_request_slave_channel(dev,
  226. dmaengine_pcm_dma_channel_names[i]);
  227. }
  228. }
  229. }
  230. /**
  231. * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
  232. * @dev: The parent device for the PCM device
  233. * @config: Platform specific PCM configuration
  234. * @flags: Platform specific quirks
  235. */
  236. int snd_dmaengine_pcm_register(struct device *dev,
  237. const struct snd_dmaengine_pcm_config *config, unsigned int flags)
  238. {
  239. struct dmaengine_pcm *pcm;
  240. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  241. if (!pcm)
  242. return -ENOMEM;
  243. pcm->config = config;
  244. pcm->flags = flags;
  245. dmaengine_pcm_request_chan_of(pcm, dev);
  246. if (flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
  247. return snd_soc_add_platform(dev, &pcm->platform,
  248. &dmaengine_no_residue_pcm_platform);
  249. else
  250. return snd_soc_add_platform(dev, &pcm->platform,
  251. &dmaengine_pcm_platform);
  252. }
  253. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
  254. /**
  255. * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
  256. * @dev: Parent device the PCM was register with
  257. *
  258. * Removes a dmaengine based PCM device previously registered with
  259. * snd_dmaengine_pcm_register.
  260. */
  261. void snd_dmaengine_pcm_unregister(struct device *dev)
  262. {
  263. struct snd_soc_platform *platform;
  264. struct dmaengine_pcm *pcm;
  265. unsigned int i;
  266. platform = snd_soc_lookup_platform(dev);
  267. if (!platform)
  268. return;
  269. pcm = soc_platform_to_pcm(platform);
  270. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
  271. if (pcm->chan[i]) {
  272. dma_release_channel(pcm->chan[i]);
  273. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  274. break;
  275. }
  276. }
  277. snd_soc_remove_platform(platform);
  278. kfree(pcm);
  279. }
  280. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
  281. MODULE_LICENSE("GPL");