bf5xx-i2s-pcm.c 8.3 KB

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