ux500_pcm.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2012
  4. *
  5. * Author: Ola Lilja <ola.o.lilja@stericsson.com>,
  6. * Roger Nilsson <roger.xr.nilsson@stericsson.com>
  7. * for ST-Ericsson.
  8. *
  9. * License terms:
  10. */
  11. #include <asm/page.h>
  12. #include <linux/module.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/slab.h>
  16. #include <linux/platform_data/dma-ste-dma40.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/soc.h>
  20. #include <sound/dmaengine_pcm.h>
  21. #include "ux500_msp_i2s.h"
  22. #include "ux500_pcm.h"
  23. #define UX500_PLATFORM_PERIODS_BYTES_MIN 128
  24. #define UX500_PLATFORM_PERIODS_BYTES_MAX (64 * PAGE_SIZE)
  25. #define UX500_PLATFORM_PERIODS_MIN 2
  26. #define UX500_PLATFORM_PERIODS_MAX 48
  27. #define UX500_PLATFORM_BUFFER_BYTES_MAX (2048 * PAGE_SIZE)
  28. static const struct snd_pcm_hardware ux500_pcm_hw = {
  29. .info = SNDRV_PCM_INFO_INTERLEAVED |
  30. SNDRV_PCM_INFO_MMAP |
  31. SNDRV_PCM_INFO_RESUME |
  32. SNDRV_PCM_INFO_PAUSE,
  33. .buffer_bytes_max = UX500_PLATFORM_BUFFER_BYTES_MAX,
  34. .period_bytes_min = UX500_PLATFORM_PERIODS_BYTES_MIN,
  35. .period_bytes_max = UX500_PLATFORM_PERIODS_BYTES_MAX,
  36. .periods_min = UX500_PLATFORM_PERIODS_MIN,
  37. .periods_max = UX500_PLATFORM_PERIODS_MAX,
  38. };
  39. static struct dma_chan *ux500_pcm_request_chan(struct snd_soc_pcm_runtime *rtd,
  40. struct snd_pcm_substream *substream)
  41. {
  42. struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0);
  43. u16 per_data_width, mem_data_width;
  44. struct stedma40_chan_cfg *dma_cfg;
  45. struct ux500_msp_dma_params *dma_params;
  46. dma_params = snd_soc_dai_get_dma_data(dai, substream);
  47. dma_cfg = dma_params->dma_cfg;
  48. mem_data_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  49. switch (dma_params->data_size) {
  50. case 32:
  51. per_data_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  52. break;
  53. case 16:
  54. per_data_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  55. break;
  56. case 8:
  57. per_data_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  58. break;
  59. default:
  60. per_data_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  61. }
  62. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  63. dma_cfg->src_info.data_width = mem_data_width;
  64. dma_cfg->dst_info.data_width = per_data_width;
  65. } else {
  66. dma_cfg->src_info.data_width = per_data_width;
  67. dma_cfg->dst_info.data_width = mem_data_width;
  68. }
  69. return snd_dmaengine_pcm_request_channel(stedma40_filter, dma_cfg);
  70. }
  71. static int ux500_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
  72. struct snd_pcm_hw_params *params,
  73. struct dma_slave_config *slave_config)
  74. {
  75. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  76. struct msp_i2s_platform_data *pdata = asoc_rtd_to_cpu(rtd, 0)->dev->platform_data;
  77. struct snd_dmaengine_dai_dma_data *snd_dma_params;
  78. struct ux500_msp_dma_params *ste_dma_params;
  79. dma_addr_t dma_addr;
  80. int ret;
  81. if (pdata) {
  82. ste_dma_params =
  83. snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  84. dma_addr = ste_dma_params->tx_rx_addr;
  85. } else {
  86. snd_dma_params =
  87. snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  88. dma_addr = snd_dma_params->addr;
  89. }
  90. ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
  91. if (ret)
  92. return ret;
  93. slave_config->dst_maxburst = 4;
  94. slave_config->src_maxburst = 4;
  95. slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  96. slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  97. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  98. slave_config->dst_addr = dma_addr;
  99. else
  100. slave_config->src_addr = dma_addr;
  101. return 0;
  102. }
  103. static const struct snd_dmaengine_pcm_config ux500_dmaengine_pcm_config = {
  104. .pcm_hardware = &ux500_pcm_hw,
  105. .compat_request_channel = ux500_pcm_request_chan,
  106. .prealloc_buffer_size = 128 * 1024,
  107. .prepare_slave_config = ux500_pcm_prepare_slave_config,
  108. };
  109. static const struct snd_dmaengine_pcm_config ux500_dmaengine_of_pcm_config = {
  110. .compat_request_channel = ux500_pcm_request_chan,
  111. .prepare_slave_config = ux500_pcm_prepare_slave_config,
  112. };
  113. int ux500_pcm_register_platform(struct platform_device *pdev)
  114. {
  115. const struct snd_dmaengine_pcm_config *pcm_config;
  116. struct device_node *np = pdev->dev.of_node;
  117. int ret;
  118. if (np)
  119. pcm_config = &ux500_dmaengine_of_pcm_config;
  120. else
  121. pcm_config = &ux500_dmaengine_pcm_config;
  122. ret = snd_dmaengine_pcm_register(&pdev->dev, pcm_config,
  123. SND_DMAENGINE_PCM_FLAG_COMPAT);
  124. if (ret < 0) {
  125. dev_err(&pdev->dev,
  126. "%s: ERROR: Failed to register platform '%s' (%d)!\n",
  127. __func__, pdev->name, ret);
  128. return ret;
  129. }
  130. return 0;
  131. }
  132. EXPORT_SYMBOL_GPL(ux500_pcm_register_platform);
  133. int ux500_pcm_unregister_platform(struct platform_device *pdev)
  134. {
  135. snd_dmaengine_pcm_unregister(&pdev->dev);
  136. return 0;
  137. }
  138. EXPORT_SYMBOL_GPL(ux500_pcm_unregister_platform);
  139. MODULE_AUTHOR("Ola Lilja");
  140. MODULE_AUTHOR("Roger Nilsson");
  141. MODULE_DESCRIPTION("ASoC UX500 driver");
  142. MODULE_LICENSE("GPL v2");