soc-generic-dmaengine-pcm.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <linux/of_dma.h>
  25. #include <sound/dmaengine_pcm.h>
  26. struct dmaengine_pcm {
  27. struct dma_chan *chan[SNDRV_PCM_STREAM_CAPTURE + 1];
  28. const struct snd_dmaengine_pcm_config *config;
  29. struct snd_soc_platform platform;
  30. bool compat;
  31. };
  32. static struct dmaengine_pcm *soc_platform_to_pcm(struct snd_soc_platform *p)
  33. {
  34. return container_of(p, struct dmaengine_pcm, platform);
  35. }
  36. /**
  37. * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback
  38. * @substream: PCM substream
  39. * @params: hw_params
  40. * @slave_config: DMA slave config to prepare
  41. *
  42. * This function can be used as a generic prepare_slave_config callback for
  43. * platforms which make use of the snd_dmaengine_dai_dma_data struct for their
  44. * DAI DMA data. Internally the function will first call
  45. * snd_hwparams_to_dma_slave_config to fill in the slave config based on the
  46. * hw_params, followed by snd_dmaengine_set_config_from_dai_data to fill in the
  47. * remaining fields based on the DAI DMA data.
  48. */
  49. int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
  50. struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
  51. {
  52. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  53. struct snd_dmaengine_dai_dma_data *dma_data;
  54. int ret;
  55. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  56. ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
  57. if (ret)
  58. return ret;
  59. snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
  60. slave_config);
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
  64. static int dmaengine_pcm_hw_params(struct snd_pcm_substream *substream,
  65. struct snd_pcm_hw_params *params)
  66. {
  67. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  68. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  69. struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
  70. struct dma_slave_config slave_config;
  71. int ret;
  72. if (pcm->config->prepare_slave_config) {
  73. ret = pcm->config->prepare_slave_config(substream, params,
  74. &slave_config);
  75. if (ret)
  76. return ret;
  77. ret = dmaengine_slave_config(chan, &slave_config);
  78. if (ret)
  79. return ret;
  80. }
  81. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  82. }
  83. static int dmaengine_pcm_open(struct snd_pcm_substream *substream)
  84. {
  85. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  86. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  87. struct dma_chan *chan = pcm->chan[substream->stream];
  88. int ret;
  89. ret = snd_soc_set_runtime_hwparams(substream,
  90. pcm->config->pcm_hardware);
  91. if (ret)
  92. return ret;
  93. return snd_dmaengine_pcm_open(substream, chan);
  94. }
  95. static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
  96. struct snd_pcm_substream *substream)
  97. {
  98. if (!pcm->chan[substream->stream])
  99. return NULL;
  100. return pcm->chan[substream->stream]->device->dev;
  101. }
  102. static void dmaengine_pcm_free(struct snd_pcm *pcm)
  103. {
  104. snd_pcm_lib_preallocate_free_for_all(pcm);
  105. }
  106. static struct dma_chan *dmaengine_pcm_compat_request_channel(
  107. struct snd_soc_pcm_runtime *rtd,
  108. struct snd_pcm_substream *substream)
  109. {
  110. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  111. if (pcm->config->compat_request_channel)
  112. return pcm->config->compat_request_channel(rtd, substream);
  113. return snd_dmaengine_pcm_request_channel(pcm->config->compat_filter_fn,
  114. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream));
  115. }
  116. static int dmaengine_pcm_new(struct snd_soc_pcm_runtime *rtd)
  117. {
  118. struct dmaengine_pcm *pcm = soc_platform_to_pcm(rtd->platform);
  119. const struct snd_dmaengine_pcm_config *config = pcm->config;
  120. struct snd_pcm_substream *substream;
  121. unsigned int i;
  122. int ret;
  123. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
  124. substream = rtd->pcm->streams[i].substream;
  125. if (!substream)
  126. continue;
  127. if (!pcm->chan[i] && pcm->compat) {
  128. pcm->chan[i] = dmaengine_pcm_compat_request_channel(rtd,
  129. substream);
  130. }
  131. if (!pcm->chan[i]) {
  132. dev_err(rtd->platform->dev,
  133. "Missing dma channel for stream: %d\n", i);
  134. ret = -EINVAL;
  135. goto err_free;
  136. }
  137. ret = snd_pcm_lib_preallocate_pages(substream,
  138. SNDRV_DMA_TYPE_DEV,
  139. dmaengine_dma_dev(pcm, substream),
  140. config->prealloc_buffer_size,
  141. config->pcm_hardware->buffer_bytes_max);
  142. if (ret)
  143. goto err_free;
  144. }
  145. return 0;
  146. err_free:
  147. dmaengine_pcm_free(rtd->pcm);
  148. return ret;
  149. }
  150. static const struct snd_pcm_ops dmaengine_pcm_ops = {
  151. .open = dmaengine_pcm_open,
  152. .close = snd_dmaengine_pcm_close,
  153. .ioctl = snd_pcm_lib_ioctl,
  154. .hw_params = dmaengine_pcm_hw_params,
  155. .hw_free = snd_pcm_lib_free_pages,
  156. .trigger = snd_dmaengine_pcm_trigger,
  157. .pointer = snd_dmaengine_pcm_pointer,
  158. };
  159. static const struct snd_soc_platform_driver dmaengine_pcm_platform = {
  160. .ops = &dmaengine_pcm_ops,
  161. .pcm_new = dmaengine_pcm_new,
  162. .pcm_free = dmaengine_pcm_free,
  163. .probe_order = SND_SOC_COMP_ORDER_LATE,
  164. };
  165. static const struct snd_pcm_ops dmaengine_no_residue_pcm_ops = {
  166. .open = dmaengine_pcm_open,
  167. .close = snd_dmaengine_pcm_close,
  168. .ioctl = snd_pcm_lib_ioctl,
  169. .hw_params = dmaengine_pcm_hw_params,
  170. .hw_free = snd_pcm_lib_free_pages,
  171. .trigger = snd_dmaengine_pcm_trigger,
  172. .pointer = snd_dmaengine_pcm_pointer_no_residue,
  173. };
  174. static const struct snd_soc_platform_driver dmaengine_no_residue_pcm_platform = {
  175. .ops = &dmaengine_no_residue_pcm_ops,
  176. .pcm_new = dmaengine_pcm_new,
  177. .pcm_free = dmaengine_pcm_free,
  178. .probe_order = SND_SOC_COMP_ORDER_LATE,
  179. };
  180. static const char * const dmaengine_pcm_dma_channel_names[] = {
  181. [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
  182. [SNDRV_PCM_STREAM_CAPTURE] = "rx",
  183. };
  184. /**
  185. * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
  186. * @dev: The parent device for the PCM device
  187. * @config: Platform specific PCM configuration
  188. * @flags: Platform specific quirks
  189. */
  190. int snd_dmaengine_pcm_register(struct device *dev,
  191. const struct snd_dmaengine_pcm_config *config, unsigned int flags)
  192. {
  193. struct dmaengine_pcm *pcm;
  194. unsigned int i;
  195. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  196. if (!pcm)
  197. return -ENOMEM;
  198. pcm->config = config;
  199. if (flags & SND_DMAENGINE_PCM_FLAG_COMPAT)
  200. pcm->compat = true;
  201. if (!(flags & SND_DMAENGINE_PCM_FLAG_NO_DT) && dev->of_node) {
  202. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
  203. pcm->chan[i] = of_dma_request_slave_channel(dev->of_node,
  204. dmaengine_pcm_dma_channel_names[i]);
  205. }
  206. }
  207. if (flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
  208. return snd_soc_add_platform(dev, &pcm->platform,
  209. &dmaengine_no_residue_pcm_platform);
  210. else
  211. return snd_soc_add_platform(dev, &pcm->platform,
  212. &dmaengine_pcm_platform);
  213. }
  214. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
  215. /**
  216. * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
  217. * @dev: Parent device the PCM was register with
  218. *
  219. * Removes a dmaengine based PCM device previously registered with
  220. * snd_dmaengine_pcm_register.
  221. */
  222. void snd_dmaengine_pcm_unregister(struct device *dev)
  223. {
  224. struct snd_soc_platform *platform;
  225. struct dmaengine_pcm *pcm;
  226. unsigned int i;
  227. platform = snd_soc_lookup_platform(dev);
  228. if (!platform)
  229. return;
  230. pcm = soc_platform_to_pcm(platform);
  231. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
  232. if (pcm->chan[i])
  233. dma_release_channel(pcm->chan[i]);
  234. }
  235. snd_soc_remove_platform(platform);
  236. kfree(pcm);
  237. }
  238. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
  239. MODULE_LICENSE("GPL");