ux500_pcm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_MIN_RATE 8000
  27. #define UX500_PLATFORM_MAX_RATE 48000
  28. #define UX500_PLATFORM_MIN_CHANNELS 1
  29. #define UX500_PLATFORM_MAX_CHANNELS 8
  30. #define UX500_PLATFORM_PERIODS_BYTES_MIN 128
  31. #define UX500_PLATFORM_PERIODS_BYTES_MAX (64 * PAGE_SIZE)
  32. #define UX500_PLATFORM_PERIODS_MIN 2
  33. #define UX500_PLATFORM_PERIODS_MAX 48
  34. #define UX500_PLATFORM_BUFFER_BYTES_MAX (2048 * PAGE_SIZE)
  35. static struct snd_pcm_hardware ux500_pcm_hw = {
  36. .info = SNDRV_PCM_INFO_INTERLEAVED |
  37. SNDRV_PCM_INFO_MMAP |
  38. SNDRV_PCM_INFO_RESUME |
  39. SNDRV_PCM_INFO_PAUSE,
  40. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  41. SNDRV_PCM_FMTBIT_U16_LE |
  42. SNDRV_PCM_FMTBIT_S16_BE |
  43. SNDRV_PCM_FMTBIT_U16_BE,
  44. .rates = SNDRV_PCM_RATE_KNOT,
  45. .rate_min = UX500_PLATFORM_MIN_RATE,
  46. .rate_max = UX500_PLATFORM_MAX_RATE,
  47. .channels_min = UX500_PLATFORM_MIN_CHANNELS,
  48. .channels_max = UX500_PLATFORM_MAX_CHANNELS,
  49. .buffer_bytes_max = UX500_PLATFORM_BUFFER_BYTES_MAX,
  50. .period_bytes_min = UX500_PLATFORM_PERIODS_BYTES_MIN,
  51. .period_bytes_max = UX500_PLATFORM_PERIODS_BYTES_MAX,
  52. .periods_min = UX500_PLATFORM_PERIODS_MIN,
  53. .periods_max = UX500_PLATFORM_PERIODS_MAX,
  54. };
  55. static void ux500_pcm_dma_hw_free(struct device *dev,
  56. struct snd_pcm_substream *substream)
  57. {
  58. struct snd_pcm_runtime *runtime = substream->runtime;
  59. struct snd_dma_buffer *buf = runtime->dma_buffer_p;
  60. if (runtime->dma_area == NULL)
  61. return;
  62. if (buf != &substream->dma_buffer) {
  63. dma_free_coherent(buf->dev.dev, buf->bytes, buf->area,
  64. buf->addr);
  65. kfree(runtime->dma_buffer_p);
  66. }
  67. snd_pcm_set_runtime_buffer(substream, NULL);
  68. }
  69. static int ux500_pcm_open(struct snd_pcm_substream *substream)
  70. {
  71. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  72. struct snd_soc_dai *dai = rtd->cpu_dai;
  73. struct device *dev = dai->dev;
  74. int ret;
  75. struct ux500_msp_dma_params *dma_params;
  76. u16 per_data_width, mem_data_width;
  77. struct stedma40_chan_cfg *dma_cfg;
  78. dev_dbg(dev, "%s: MSP %d (%s): Enter.\n", __func__, dai->id,
  79. snd_pcm_stream_str(substream));
  80. dev_dbg(dev, "%s: Set runtime hwparams.\n", __func__);
  81. snd_soc_set_runtime_hwparams(substream, &ux500_pcm_hw);
  82. mem_data_width = STEDMA40_HALFWORD_WIDTH;
  83. dma_params = snd_soc_dai_get_dma_data(dai, substream);
  84. switch (dma_params->data_size) {
  85. case 32:
  86. per_data_width = STEDMA40_WORD_WIDTH;
  87. break;
  88. case 16:
  89. per_data_width = STEDMA40_HALFWORD_WIDTH;
  90. break;
  91. case 8:
  92. per_data_width = STEDMA40_BYTE_WIDTH;
  93. break;
  94. default:
  95. per_data_width = STEDMA40_WORD_WIDTH;
  96. dev_warn(rtd->platform->dev,
  97. "%s: Unknown data-size (%d)! Assuming 32 bits.\n",
  98. __func__, dma_params->data_size);
  99. }
  100. dma_cfg = dma_params->dma_cfg;
  101. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  102. dma_cfg->src_info.data_width = mem_data_width;
  103. dma_cfg->dst_info.data_width = per_data_width;
  104. } else {
  105. dma_cfg->src_info.data_width = per_data_width;
  106. dma_cfg->dst_info.data_width = mem_data_width;
  107. }
  108. ret = snd_dmaengine_pcm_open_request_chan(substream, stedma40_filter,
  109. dma_cfg);
  110. if (ret) {
  111. dev_dbg(dai->dev,
  112. "%s: ERROR: snd_dmaengine_pcm_open failed (%d)!\n",
  113. __func__, ret);
  114. return ret;
  115. }
  116. return 0;
  117. }
  118. static int ux500_pcm_hw_params(struct snd_pcm_substream *substream,
  119. struct snd_pcm_hw_params *hw_params)
  120. {
  121. struct snd_pcm_runtime *runtime = substream->runtime;
  122. struct snd_dma_buffer *buf = runtime->dma_buffer_p;
  123. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  124. int ret = 0;
  125. int size;
  126. dev_dbg(rtd->platform->dev, "%s: Enter\n", __func__);
  127. size = params_buffer_bytes(hw_params);
  128. if (buf) {
  129. if (buf->bytes >= size)
  130. goto out;
  131. ux500_pcm_dma_hw_free(NULL, substream);
  132. }
  133. if (substream->dma_buffer.area != NULL &&
  134. substream->dma_buffer.bytes >= size) {
  135. buf = &substream->dma_buffer;
  136. } else {
  137. buf = kmalloc(sizeof(struct snd_dma_buffer), GFP_KERNEL);
  138. if (!buf)
  139. goto nomem;
  140. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  141. buf->dev.dev = NULL;
  142. buf->area = dma_alloc_coherent(NULL, size, &buf->addr,
  143. GFP_KERNEL);
  144. buf->bytes = size;
  145. buf->private_data = NULL;
  146. if (!buf->area)
  147. goto free;
  148. }
  149. snd_pcm_set_runtime_buffer(substream, buf);
  150. ret = 1;
  151. out:
  152. runtime->dma_bytes = size;
  153. return ret;
  154. free:
  155. kfree(buf);
  156. nomem:
  157. return -ENOMEM;
  158. }
  159. static int ux500_pcm_hw_free(struct snd_pcm_substream *substream)
  160. {
  161. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  162. dev_dbg(rtd->platform->dev, "%s: Enter\n", __func__);
  163. ux500_pcm_dma_hw_free(NULL, substream);
  164. return 0;
  165. }
  166. static int ux500_pcm_mmap(struct snd_pcm_substream *substream,
  167. struct vm_area_struct *vma)
  168. {
  169. struct snd_pcm_runtime *runtime = substream->runtime;
  170. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  171. dev_dbg(rtd->platform->dev, "%s: Enter.\n", __func__);
  172. return dma_mmap_coherent(NULL, vma, runtime->dma_area,
  173. runtime->dma_addr, runtime->dma_bytes);
  174. }
  175. static struct snd_pcm_ops ux500_pcm_ops = {
  176. .open = ux500_pcm_open,
  177. .close = snd_dmaengine_pcm_close_release_chan,
  178. .ioctl = snd_pcm_lib_ioctl,
  179. .hw_params = ux500_pcm_hw_params,
  180. .hw_free = ux500_pcm_hw_free,
  181. .trigger = snd_dmaengine_pcm_trigger,
  182. .pointer = snd_dmaengine_pcm_pointer_no_residue,
  183. .mmap = ux500_pcm_mmap
  184. };
  185. int ux500_pcm_new(struct snd_soc_pcm_runtime *rtd)
  186. {
  187. struct snd_pcm *pcm = rtd->pcm;
  188. dev_dbg(rtd->platform->dev, "%s: Enter (id = '%s').\n", __func__,
  189. pcm->id);
  190. pcm->info_flags = 0;
  191. return 0;
  192. }
  193. static struct snd_soc_platform_driver ux500_pcm_soc_drv = {
  194. .ops = &ux500_pcm_ops,
  195. .pcm_new = ux500_pcm_new,
  196. };
  197. int ux500_pcm_register_platform(struct platform_device *pdev)
  198. {
  199. int ret;
  200. ret = snd_soc_register_platform(&pdev->dev, &ux500_pcm_soc_drv);
  201. if (ret < 0) {
  202. dev_err(&pdev->dev,
  203. "%s: ERROR: Failed to register platform '%s' (%d)!\n",
  204. __func__, pdev->name, ret);
  205. return ret;
  206. }
  207. return 0;
  208. }
  209. EXPORT_SYMBOL_GPL(ux500_pcm_register_platform);
  210. int ux500_pcm_unregister_platform(struct platform_device *pdev)
  211. {
  212. snd_soc_unregister_platform(&pdev->dev);
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_GPL(ux500_pcm_unregister_platform);