ux500_pcm.c 4.8 KB

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