bf5xx-i2s-pcm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/slab.h>
  32. #include <linux/dma-mapping.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-i2s-pcm.h"
  39. #include "bf5xx-i2s.h"
  40. #include "bf5xx-sport.h"
  41. static void bf5xx_dma_irq(void *data)
  42. {
  43. struct snd_pcm_substream *pcm = data;
  44. snd_pcm_period_elapsed(pcm);
  45. }
  46. static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
  47. .info = SNDRV_PCM_INFO_INTERLEAVED |
  48. SNDRV_PCM_INFO_MMAP |
  49. SNDRV_PCM_INFO_MMAP_VALID |
  50. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  51. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  52. SNDRV_PCM_FMTBIT_S24_LE |
  53. SNDRV_PCM_FMTBIT_S32_LE,
  54. .period_bytes_min = 32,
  55. .period_bytes_max = 0x10000,
  56. .periods_min = 1,
  57. .periods_max = PAGE_SIZE/32,
  58. .buffer_bytes_max = 0x20000, /* 128 kbytes */
  59. .fifo_size = 16,
  60. };
  61. static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,
  62. struct snd_pcm_hw_params *params)
  63. {
  64. size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
  65. snd_pcm_lib_malloc_pages(substream, size);
  66. return 0;
  67. }
  68. static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)
  69. {
  70. snd_pcm_lib_free_pages(substream);
  71. return 0;
  72. }
  73. static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)
  74. {
  75. struct snd_pcm_runtime *runtime = substream->runtime;
  76. struct sport_device *sport = runtime->private_data;
  77. int period_bytes = frames_to_bytes(runtime, runtime->period_size);
  78. pr_debug("%s enter\n", __func__);
  79. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  80. sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
  81. sport_config_tx_dma(sport, runtime->dma_area,
  82. runtime->periods, period_bytes);
  83. } else {
  84. sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
  85. sport_config_rx_dma(sport, runtime->dma_area,
  86. runtime->periods, period_bytes);
  87. }
  88. return 0;
  89. }
  90. static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  91. {
  92. struct snd_pcm_runtime *runtime = substream->runtime;
  93. struct sport_device *sport = runtime->private_data;
  94. int ret = 0;
  95. pr_debug("%s enter\n", __func__);
  96. switch (cmd) {
  97. case SNDRV_PCM_TRIGGER_START:
  98. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  99. sport_tx_start(sport);
  100. else
  101. sport_rx_start(sport);
  102. break;
  103. case SNDRV_PCM_TRIGGER_STOP:
  104. case SNDRV_PCM_TRIGGER_SUSPEND:
  105. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  106. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  107. sport_tx_stop(sport);
  108. else
  109. sport_rx_stop(sport);
  110. break;
  111. default:
  112. ret = -EINVAL;
  113. }
  114. return ret;
  115. }
  116. static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)
  117. {
  118. struct snd_pcm_runtime *runtime = substream->runtime;
  119. struct sport_device *sport = runtime->private_data;
  120. unsigned int diff;
  121. snd_pcm_uframes_t frames;
  122. pr_debug("%s enter\n", __func__);
  123. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  124. diff = sport_curr_offset_tx(sport);
  125. frames = bytes_to_frames(substream->runtime, diff);
  126. } else {
  127. diff = sport_curr_offset_rx(sport);
  128. frames = bytes_to_frames(substream->runtime, diff);
  129. }
  130. return frames;
  131. }
  132. static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
  133. {
  134. struct snd_pcm_runtime *runtime = substream->runtime;
  135. int ret;
  136. pr_debug("%s enter\n", __func__);
  137. snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);
  138. ret = snd_pcm_hw_constraint_integer(runtime, \
  139. SNDRV_PCM_HW_PARAM_PERIODS);
  140. if (ret < 0)
  141. goto out;
  142. if (sport_handle != NULL)
  143. runtime->private_data = sport_handle;
  144. else {
  145. pr_err("sport_handle is NULL\n");
  146. return -1;
  147. }
  148. return 0;
  149. out:
  150. return ret;
  151. }
  152. static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,
  153. struct vm_area_struct *vma)
  154. {
  155. struct snd_pcm_runtime *runtime = substream->runtime;
  156. size_t size = vma->vm_end - vma->vm_start;
  157. vma->vm_start = (unsigned long)runtime->dma_area;
  158. vma->vm_end = vma->vm_start + size;
  159. vma->vm_flags |= VM_SHARED;
  160. return 0 ;
  161. }
  162. static struct snd_pcm_ops bf5xx_pcm_i2s_ops = {
  163. .open = bf5xx_pcm_open,
  164. .ioctl = snd_pcm_lib_ioctl,
  165. .hw_params = bf5xx_pcm_hw_params,
  166. .hw_free = bf5xx_pcm_hw_free,
  167. .prepare = bf5xx_pcm_prepare,
  168. .trigger = bf5xx_pcm_trigger,
  169. .pointer = bf5xx_pcm_pointer,
  170. .mmap = bf5xx_pcm_mmap,
  171. };
  172. static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  173. {
  174. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  175. struct snd_dma_buffer *buf = &substream->dma_buffer;
  176. size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
  177. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  178. buf->dev.dev = pcm->card->dev;
  179. buf->private_data = NULL;
  180. buf->area = dma_alloc_coherent(pcm->card->dev, size,
  181. &buf->addr, GFP_KERNEL);
  182. if (!buf->area) {
  183. pr_err("Failed to allocate dma memory \
  184. Please increase uncached DMA memory region\n");
  185. return -ENOMEM;
  186. }
  187. buf->bytes = size;
  188. pr_debug("%s, area:%p, size:0x%08lx\n", __func__,
  189. buf->area, buf->bytes);
  190. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  191. sport_handle->tx_buf = buf->area;
  192. else
  193. sport_handle->rx_buf = buf->area;
  194. return 0;
  195. }
  196. static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  197. {
  198. struct snd_pcm_substream *substream;
  199. struct snd_dma_buffer *buf;
  200. int stream;
  201. for (stream = 0; stream < 2; stream++) {
  202. substream = pcm->streams[stream].substream;
  203. if (!substream)
  204. continue;
  205. buf = &substream->dma_buffer;
  206. if (!buf->area)
  207. continue;
  208. dma_free_coherent(NULL, buf->bytes, buf->area, 0);
  209. buf->area = NULL;
  210. }
  211. if (sport_handle)
  212. sport_done(sport_handle);
  213. }
  214. static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);
  215. int bf5xx_pcm_i2s_new(struct snd_card *card, struct snd_soc_dai *dai,
  216. struct snd_pcm *pcm)
  217. {
  218. int ret = 0;
  219. pr_debug("%s enter\n", __func__);
  220. if (!card->dev->dma_mask)
  221. card->dev->dma_mask = &bf5xx_pcm_dmamask;
  222. if (!card->dev->coherent_dma_mask)
  223. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  224. if (dai->playback.channels_min) {
  225. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  226. SNDRV_PCM_STREAM_PLAYBACK);
  227. if (ret)
  228. goto out;
  229. }
  230. if (dai->capture.channels_min) {
  231. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  232. SNDRV_PCM_STREAM_CAPTURE);
  233. if (ret)
  234. goto out;
  235. }
  236. out:
  237. return ret;
  238. }
  239. struct snd_soc_platform bf5xx_i2s_soc_platform = {
  240. .name = "bf5xx-audio",
  241. .pcm_ops = &bf5xx_pcm_i2s_ops,
  242. .pcm_new = bf5xx_pcm_i2s_new,
  243. .pcm_free = bf5xx_pcm_free_dma_buffers,
  244. };
  245. EXPORT_SYMBOL_GPL(bf5xx_i2s_soc_platform);
  246. static int __init bfin_i2s_init(void)
  247. {
  248. return snd_soc_register_platform(&bf5xx_i2s_soc_platform);
  249. }
  250. module_init(bfin_i2s_init);
  251. static void __exit bfin_i2s_exit(void)
  252. {
  253. snd_soc_unregister_platform(&bf5xx_i2s_soc_platform);
  254. }
  255. module_exit(bfin_i2s_exit);
  256. MODULE_AUTHOR("Cliff Cai");
  257. MODULE_DESCRIPTION("ADI Blackfin I2S PCM DMA module");
  258. MODULE_LICENSE("GPL");