bf5xx-i2s-pcm.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * File: sound/soc/blackfin/bf5xx-i2s-pcm.c
  3. * Author: Cliff Cai <Cliff.Cai@analog.com>
  4. *
  5. * Created: Tue June 06 2008
  6. * Description: DMA driver for i2s codec
  7. *
  8. * Modified:
  9. * Copyright 2008 Analog Devices Inc.
  10. *
  11. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, see the file COPYING, or write
  25. * to the Free Software Foundation, Inc.,
  26. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/gfp.h>
  33. #include <sound/core.h>
  34. #include <sound/pcm.h>
  35. #include <sound/pcm_params.h>
  36. #include <sound/soc.h>
  37. #include <asm/dma.h>
  38. #include "bf5xx-sport.h"
  39. static void bf5xx_dma_irq(void *data)
  40. {
  41. struct snd_pcm_substream *pcm = data;
  42. snd_pcm_period_elapsed(pcm);
  43. }
  44. static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
  45. .info = SNDRV_PCM_INFO_INTERLEAVED |
  46. SNDRV_PCM_INFO_MMAP |
  47. SNDRV_PCM_INFO_MMAP_VALID |
  48. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  49. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  50. SNDRV_PCM_FMTBIT_S24_LE |
  51. SNDRV_PCM_FMTBIT_S32_LE,
  52. .period_bytes_min = 32,
  53. .period_bytes_max = 0x10000,
  54. .periods_min = 1,
  55. .periods_max = PAGE_SIZE/32,
  56. .buffer_bytes_max = 0x20000, /* 128 kbytes */
  57. .fifo_size = 16,
  58. };
  59. static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,
  60. struct snd_pcm_hw_params *params)
  61. {
  62. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  63. }
  64. static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)
  65. {
  66. snd_pcm_lib_free_pages(substream);
  67. return 0;
  68. }
  69. static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)
  70. {
  71. struct snd_pcm_runtime *runtime = substream->runtime;
  72. struct sport_device *sport = runtime->private_data;
  73. int period_bytes = frames_to_bytes(runtime, runtime->period_size);
  74. pr_debug("%s enter\n", __func__);
  75. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  76. sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
  77. sport_config_tx_dma(sport, runtime->dma_area,
  78. runtime->periods, period_bytes);
  79. } else {
  80. sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
  81. sport_config_rx_dma(sport, runtime->dma_area,
  82. runtime->periods, period_bytes);
  83. }
  84. return 0;
  85. }
  86. static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  87. {
  88. struct snd_pcm_runtime *runtime = substream->runtime;
  89. struct sport_device *sport = runtime->private_data;
  90. int ret = 0;
  91. pr_debug("%s enter\n", __func__);
  92. switch (cmd) {
  93. case SNDRV_PCM_TRIGGER_START:
  94. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  95. sport_tx_start(sport);
  96. else
  97. sport_rx_start(sport);
  98. break;
  99. case SNDRV_PCM_TRIGGER_STOP:
  100. case SNDRV_PCM_TRIGGER_SUSPEND:
  101. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  102. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  103. sport_tx_stop(sport);
  104. else
  105. sport_rx_stop(sport);
  106. break;
  107. default:
  108. ret = -EINVAL;
  109. }
  110. return ret;
  111. }
  112. static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)
  113. {
  114. struct snd_pcm_runtime *runtime = substream->runtime;
  115. struct sport_device *sport = runtime->private_data;
  116. unsigned int diff;
  117. snd_pcm_uframes_t frames;
  118. pr_debug("%s enter\n", __func__);
  119. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  120. diff = sport_curr_offset_tx(sport);
  121. } else {
  122. diff = sport_curr_offset_rx(sport);
  123. }
  124. /*
  125. * TX at least can report one frame beyond the end of the
  126. * buffer if we hit the wraparound case - clamp to within the
  127. * buffer as the ALSA APIs require.
  128. */
  129. if (diff == snd_pcm_lib_buffer_bytes(substream))
  130. diff = 0;
  131. frames = bytes_to_frames(substream->runtime, diff);
  132. return frames;
  133. }
  134. static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
  135. {
  136. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  137. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  138. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai);
  139. struct snd_pcm_runtime *runtime = substream->runtime;
  140. struct snd_dma_buffer *buf = &substream->dma_buffer;
  141. int ret;
  142. pr_debug("%s enter\n", __func__);
  143. snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);
  144. ret = snd_pcm_hw_constraint_integer(runtime,
  145. SNDRV_PCM_HW_PARAM_PERIODS);
  146. if (ret < 0)
  147. goto out;
  148. if (sport_handle != NULL) {
  149. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  150. sport_handle->tx_buf = buf->area;
  151. else
  152. sport_handle->rx_buf = buf->area;
  153. runtime->private_data = sport_handle;
  154. } else {
  155. pr_err("sport_handle is NULL\n");
  156. return -1;
  157. }
  158. return 0;
  159. out:
  160. return ret;
  161. }
  162. static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,
  163. struct vm_area_struct *vma)
  164. {
  165. struct snd_pcm_runtime *runtime = substream->runtime;
  166. size_t size = vma->vm_end - vma->vm_start;
  167. vma->vm_start = (unsigned long)runtime->dma_area;
  168. vma->vm_end = vma->vm_start + size;
  169. vma->vm_flags |= VM_SHARED;
  170. return 0 ;
  171. }
  172. static struct snd_pcm_ops bf5xx_pcm_i2s_ops = {
  173. .open = bf5xx_pcm_open,
  174. .ioctl = snd_pcm_lib_ioctl,
  175. .hw_params = bf5xx_pcm_hw_params,
  176. .hw_free = bf5xx_pcm_hw_free,
  177. .prepare = bf5xx_pcm_prepare,
  178. .trigger = bf5xx_pcm_trigger,
  179. .pointer = bf5xx_pcm_pointer,
  180. .mmap = bf5xx_pcm_mmap,
  181. };
  182. static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);
  183. static int bf5xx_pcm_i2s_new(struct snd_soc_pcm_runtime *rtd)
  184. {
  185. struct snd_card *card = rtd->card->snd_card;
  186. size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
  187. pr_debug("%s enter\n", __func__);
  188. if (!card->dev->dma_mask)
  189. card->dev->dma_mask = &bf5xx_pcm_dmamask;
  190. if (!card->dev->coherent_dma_mask)
  191. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  192. return snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
  193. SNDRV_DMA_TYPE_DEV, card->dev, size, size);
  194. }
  195. static struct snd_soc_platform_driver bf5xx_i2s_soc_platform = {
  196. .ops = &bf5xx_pcm_i2s_ops,
  197. .pcm_new = bf5xx_pcm_i2s_new,
  198. };
  199. static int bfin_i2s_soc_platform_probe(struct platform_device *pdev)
  200. {
  201. return snd_soc_register_platform(&pdev->dev, &bf5xx_i2s_soc_platform);
  202. }
  203. static int bfin_i2s_soc_platform_remove(struct platform_device *pdev)
  204. {
  205. snd_soc_unregister_platform(&pdev->dev);
  206. return 0;
  207. }
  208. static struct platform_driver bfin_i2s_pcm_driver = {
  209. .driver = {
  210. .name = "bfin-i2s-pcm-audio",
  211. .owner = THIS_MODULE,
  212. },
  213. .probe = bfin_i2s_soc_platform_probe,
  214. .remove = bfin_i2s_soc_platform_remove,
  215. };
  216. module_platform_driver(bfin_i2s_pcm_driver);
  217. MODULE_AUTHOR("Cliff Cai");
  218. MODULE_DESCRIPTION("ADI Blackfin I2S PCM DMA module");
  219. MODULE_LICENSE("GPL");