imx-pcm-dma-mx2.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * imx-pcm-dma-mx2.c -- ALSA Soc Audio Layer
  3. *
  4. * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
  5. *
  6. * This code is based on code copyrighted by Freescale,
  7. * Liam Girdwood, Javier Martin and probably others.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/dmaengine.h>
  24. #include <sound/core.h>
  25. #include <sound/initval.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/dmaengine_pcm.h>
  30. #include <mach/dma.h>
  31. #include "imx-pcm.h"
  32. static bool filter(struct dma_chan *chan, void *param)
  33. {
  34. if (!imx_dma_is_general_purpose(chan))
  35. return false;
  36. chan->private = param;
  37. return true;
  38. }
  39. static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
  40. struct snd_pcm_hw_params *params)
  41. {
  42. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  43. struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
  44. struct imx_pcm_dma_params *dma_params;
  45. struct dma_slave_config slave_config;
  46. int ret;
  47. dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  48. ret = snd_hwparams_to_dma_slave_config(substream, params, &slave_config);
  49. if (ret)
  50. return ret;
  51. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  52. slave_config.dst_addr = dma_params->dma_addr;
  53. slave_config.dst_maxburst = dma_params->burstsize;
  54. } else {
  55. slave_config.src_addr = dma_params->dma_addr;
  56. slave_config.src_maxburst = dma_params->burstsize;
  57. }
  58. ret = dmaengine_slave_config(chan, &slave_config);
  59. if (ret)
  60. return ret;
  61. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  62. return 0;
  63. }
  64. static struct snd_pcm_hardware snd_imx_hardware = {
  65. .info = SNDRV_PCM_INFO_INTERLEAVED |
  66. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  67. SNDRV_PCM_INFO_MMAP |
  68. SNDRV_PCM_INFO_MMAP_VALID |
  69. SNDRV_PCM_INFO_PAUSE |
  70. SNDRV_PCM_INFO_RESUME,
  71. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  72. .rate_min = 8000,
  73. .channels_min = 2,
  74. .channels_max = 2,
  75. .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
  76. .period_bytes_min = 128,
  77. .period_bytes_max = 65535, /* Limited by SDMA engine */
  78. .periods_min = 2,
  79. .periods_max = 255,
  80. .fifo_size = 0,
  81. };
  82. static int snd_imx_open(struct snd_pcm_substream *substream)
  83. {
  84. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  85. struct imx_pcm_dma_params *dma_params;
  86. struct imx_dma_data *dma_data;
  87. int ret;
  88. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  89. dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  90. dma_data = kzalloc(sizeof(*dma_data), GFP_KERNEL);
  91. dma_data->peripheral_type = IMX_DMATYPE_SSI;
  92. dma_data->priority = DMA_PRIO_HIGH;
  93. dma_data->dma_request = dma_params->dma;
  94. ret = snd_dmaengine_pcm_open(substream, filter, dma_data);
  95. if (ret) {
  96. kfree(dma_data);
  97. return 0;
  98. }
  99. snd_dmaengine_pcm_set_data(substream, dma_data);
  100. return 0;
  101. }
  102. static int snd_imx_close(struct snd_pcm_substream *substream)
  103. {
  104. struct imx_dma_data *dma_data = snd_dmaengine_pcm_get_data(substream);
  105. snd_dmaengine_pcm_close(substream);
  106. kfree(dma_data);
  107. return 0;
  108. }
  109. static struct snd_pcm_ops imx_pcm_ops = {
  110. .open = snd_imx_open,
  111. .close = snd_imx_close,
  112. .ioctl = snd_pcm_lib_ioctl,
  113. .hw_params = snd_imx_pcm_hw_params,
  114. .trigger = snd_dmaengine_pcm_trigger,
  115. .pointer = snd_dmaengine_pcm_pointer,
  116. .mmap = snd_imx_pcm_mmap,
  117. };
  118. static struct snd_soc_platform_driver imx_soc_platform_mx2 = {
  119. .ops = &imx_pcm_ops,
  120. .pcm_new = imx_pcm_new,
  121. .pcm_free = imx_pcm_free,
  122. };
  123. static int __devinit imx_soc_platform_probe(struct platform_device *pdev)
  124. {
  125. return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2);
  126. }
  127. static int __devexit imx_soc_platform_remove(struct platform_device *pdev)
  128. {
  129. snd_soc_unregister_platform(&pdev->dev);
  130. return 0;
  131. }
  132. static struct platform_driver imx_pcm_driver = {
  133. .driver = {
  134. .name = "imx-pcm-audio",
  135. .owner = THIS_MODULE,
  136. },
  137. .probe = imx_soc_platform_probe,
  138. .remove = __devexit_p(imx_soc_platform_remove),
  139. };
  140. module_platform_driver(imx_pcm_driver);
  141. MODULE_LICENSE("GPL");
  142. MODULE_ALIAS("platform:imx-pcm-audio");