bf5xx-i2s-pcm.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. frames = bytes_to_frames(substream->runtime, diff);
  125. } else {
  126. diff = sport_curr_offset_rx(sport);
  127. frames = bytes_to_frames(substream->runtime, diff);
  128. }
  129. return frames;
  130. }
  131. static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
  132. {
  133. struct snd_pcm_runtime *runtime = substream->runtime;
  134. int ret;
  135. pr_debug("%s enter\n", __func__);
  136. snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);
  137. ret = snd_pcm_hw_constraint_integer(runtime, \
  138. SNDRV_PCM_HW_PARAM_PERIODS);
  139. if (ret < 0)
  140. goto out;
  141. if (sport_handle != NULL)
  142. runtime->private_data = sport_handle;
  143. else {
  144. pr_err("sport_handle is NULL\n");
  145. return -1;
  146. }
  147. return 0;
  148. out:
  149. return ret;
  150. }
  151. static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,
  152. struct vm_area_struct *vma)
  153. {
  154. struct snd_pcm_runtime *runtime = substream->runtime;
  155. size_t size = vma->vm_end - vma->vm_start;
  156. vma->vm_start = (unsigned long)runtime->dma_area;
  157. vma->vm_end = vma->vm_start + size;
  158. vma->vm_flags |= VM_SHARED;
  159. return 0 ;
  160. }
  161. static struct snd_pcm_ops bf5xx_pcm_i2s_ops = {
  162. .open = bf5xx_pcm_open,
  163. .ioctl = snd_pcm_lib_ioctl,
  164. .hw_params = bf5xx_pcm_hw_params,
  165. .hw_free = bf5xx_pcm_hw_free,
  166. .prepare = bf5xx_pcm_prepare,
  167. .trigger = bf5xx_pcm_trigger,
  168. .pointer = bf5xx_pcm_pointer,
  169. .mmap = bf5xx_pcm_mmap,
  170. };
  171. static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  172. {
  173. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  174. struct snd_dma_buffer *buf = &substream->dma_buffer;
  175. size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
  176. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  177. buf->dev.dev = pcm->card->dev;
  178. buf->private_data = NULL;
  179. buf->area = dma_alloc_coherent(pcm->card->dev, size,
  180. &buf->addr, GFP_KERNEL);
  181. if (!buf->area) {
  182. pr_err("Failed to allocate dma memory - Please increase uncached DMA memory region\n");
  183. return -ENOMEM;
  184. }
  185. buf->bytes = size;
  186. pr_debug("%s, area:%p, size:0x%08lx\n", __func__,
  187. buf->area, buf->bytes);
  188. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  189. sport_handle->tx_buf = buf->area;
  190. else
  191. sport_handle->rx_buf = buf->area;
  192. return 0;
  193. }
  194. static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  195. {
  196. struct snd_pcm_substream *substream;
  197. struct snd_dma_buffer *buf;
  198. int stream;
  199. for (stream = 0; stream < 2; stream++) {
  200. substream = pcm->streams[stream].substream;
  201. if (!substream)
  202. continue;
  203. buf = &substream->dma_buffer;
  204. if (!buf->area)
  205. continue;
  206. dma_free_coherent(NULL, buf->bytes, buf->area, 0);
  207. buf->area = NULL;
  208. }
  209. if (sport_handle)
  210. sport_done(sport_handle);
  211. }
  212. static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);
  213. int bf5xx_pcm_i2s_new(struct snd_card *card, struct snd_soc_dai *dai,
  214. struct snd_pcm *pcm)
  215. {
  216. int ret = 0;
  217. pr_debug("%s enter\n", __func__);
  218. if (!card->dev->dma_mask)
  219. card->dev->dma_mask = &bf5xx_pcm_dmamask;
  220. if (!card->dev->coherent_dma_mask)
  221. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  222. if (dai->driver->playback.channels_min) {
  223. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  224. SNDRV_PCM_STREAM_PLAYBACK);
  225. if (ret)
  226. goto out;
  227. }
  228. if (dai->driver->capture.channels_min) {
  229. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  230. SNDRV_PCM_STREAM_CAPTURE);
  231. if (ret)
  232. goto out;
  233. }
  234. out:
  235. return ret;
  236. }
  237. static struct snd_soc_platform_driver bf5xx_i2s_soc_platform = {
  238. .ops = &bf5xx_pcm_i2s_ops,
  239. .pcm_new = bf5xx_pcm_i2s_new,
  240. .pcm_free = bf5xx_pcm_free_dma_buffers,
  241. };
  242. static int __devinit bfin_i2s_soc_platform_probe(struct platform_device *pdev)
  243. {
  244. return snd_soc_register_platform(&pdev->dev, &bf5xx_i2s_soc_platform);
  245. }
  246. static int __devexit bfin_i2s_soc_platform_remove(struct platform_device *pdev)
  247. {
  248. snd_soc_unregister_platform(&pdev->dev);
  249. return 0;
  250. }
  251. static struct platform_driver bfin_i2s_pcm_driver = {
  252. .driver = {
  253. .name = "bfin-pcm-audio",
  254. .owner = THIS_MODULE,
  255. },
  256. .probe = bfin_i2s_soc_platform_probe,
  257. .remove = __devexit_p(bfin_i2s_soc_platform_remove),
  258. };
  259. static int __init snd_bfin_i2s_pcm_init(void)
  260. {
  261. return platform_driver_register(&bfin_i2s_pcm_driver);
  262. }
  263. module_init(snd_bfin_i2s_pcm_init);
  264. static void __exit snd_bfin_i2s_pcm_exit(void)
  265. {
  266. platform_driver_unregister(&bfin_i2s_pcm_driver);
  267. }
  268. module_exit(snd_bfin_i2s_pcm_exit);
  269. MODULE_AUTHOR("Cliff Cai");
  270. MODULE_DESCRIPTION("ADI Blackfin I2S PCM DMA module");
  271. MODULE_LICENSE("GPL");