bf5xx-tdm-pcm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * File: sound/soc/blackfin/bf5xx-tdm-pcm.c
  3. * Author: Barry Song <Barry.Song@analog.com>
  4. *
  5. * Created: Tue June 06 2009
  6. * Description: DMA driver for tdm codec
  7. *
  8. * Modified:
  9. * Copyright 2009 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-tdm-pcm.h"
  39. #include "bf5xx-tdm.h"
  40. #include "bf5xx-sport.h"
  41. #define PCM_BUFFER_MAX 0x10000
  42. #define FRAGMENT_SIZE_MIN (4*1024)
  43. #define FRAGMENTS_MIN 2
  44. #define FRAGMENTS_MAX 32
  45. static void bf5xx_dma_irq(void *data)
  46. {
  47. struct snd_pcm_substream *pcm = data;
  48. snd_pcm_period_elapsed(pcm);
  49. }
  50. static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
  51. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  52. SNDRV_PCM_INFO_RESUME),
  53. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  54. .rates = SNDRV_PCM_RATE_48000,
  55. .channels_min = 2,
  56. .channels_max = 8,
  57. .buffer_bytes_max = PCM_BUFFER_MAX,
  58. .period_bytes_min = FRAGMENT_SIZE_MIN,
  59. .period_bytes_max = PCM_BUFFER_MAX/2,
  60. .periods_min = FRAGMENTS_MIN,
  61. .periods_max = FRAGMENTS_MAX,
  62. };
  63. static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,
  64. struct snd_pcm_hw_params *params)
  65. {
  66. size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
  67. snd_pcm_lib_malloc_pages(substream, size * 4);
  68. return 0;
  69. }
  70. static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)
  71. {
  72. snd_pcm_lib_free_pages(substream);
  73. return 0;
  74. }
  75. static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)
  76. {
  77. struct snd_pcm_runtime *runtime = substream->runtime;
  78. struct sport_device *sport = runtime->private_data;
  79. int fragsize_bytes = frames_to_bytes(runtime, runtime->period_size);
  80. fragsize_bytes /= runtime->channels;
  81. /* inflate the fragsize to match the dma width of SPORT */
  82. fragsize_bytes *= 8;
  83. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  84. sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
  85. sport_config_tx_dma(sport, runtime->dma_area,
  86. runtime->periods, fragsize_bytes);
  87. } else {
  88. sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
  89. sport_config_rx_dma(sport, runtime->dma_area,
  90. runtime->periods, fragsize_bytes);
  91. }
  92. return 0;
  93. }
  94. static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  95. {
  96. struct snd_pcm_runtime *runtime = substream->runtime;
  97. struct sport_device *sport = runtime->private_data;
  98. int ret = 0;
  99. switch (cmd) {
  100. case SNDRV_PCM_TRIGGER_START:
  101. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  102. sport_tx_start(sport);
  103. else
  104. sport_rx_start(sport);
  105. break;
  106. case SNDRV_PCM_TRIGGER_STOP:
  107. case SNDRV_PCM_TRIGGER_SUSPEND:
  108. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  109. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  110. sport_tx_stop(sport);
  111. else
  112. sport_rx_stop(sport);
  113. break;
  114. default:
  115. ret = -EINVAL;
  116. }
  117. return ret;
  118. }
  119. static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)
  120. {
  121. struct snd_pcm_runtime *runtime = substream->runtime;
  122. struct sport_device *sport = runtime->private_data;
  123. unsigned int diff;
  124. snd_pcm_uframes_t frames;
  125. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  126. diff = sport_curr_offset_tx(sport);
  127. frames = diff / (8*4); /* 32 bytes per frame */
  128. } else {
  129. diff = sport_curr_offset_rx(sport);
  130. frames = diff / (8*4);
  131. }
  132. return frames;
  133. }
  134. static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
  135. {
  136. struct snd_pcm_runtime *runtime = substream->runtime;
  137. int ret = 0;
  138. snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);
  139. ret = snd_pcm_hw_constraint_integer(runtime,
  140. SNDRV_PCM_HW_PARAM_PERIODS);
  141. if (ret < 0)
  142. goto out;
  143. if (sport_handle != NULL)
  144. runtime->private_data = sport_handle;
  145. else {
  146. pr_err("sport_handle is NULL\n");
  147. ret = -ENODEV;
  148. }
  149. out:
  150. return ret;
  151. }
  152. static int bf5xx_pcm_copy(struct snd_pcm_substream *substream, int channel,
  153. snd_pcm_uframes_t pos, void *buf, snd_pcm_uframes_t count)
  154. {
  155. unsigned int *src;
  156. unsigned int *dst;
  157. int i;
  158. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  159. src = buf;
  160. dst = (unsigned int *)substream->runtime->dma_area;
  161. dst += pos * 8;
  162. while (count--) {
  163. for (i = 0; i < substream->runtime->channels; i++)
  164. *(dst + i) = *src++;
  165. dst += 8;
  166. }
  167. } else {
  168. src = (unsigned int *)substream->runtime->dma_area;
  169. dst = buf;
  170. src += pos * 8;
  171. while (count--) {
  172. for (i = 0; i < substream->runtime->channels; i++)
  173. *dst++ = *(src+i);
  174. src += 8;
  175. }
  176. }
  177. return 0;
  178. }
  179. static int bf5xx_pcm_silence(struct snd_pcm_substream *substream,
  180. int channel, snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
  181. {
  182. unsigned char *buf = substream->runtime->dma_area;
  183. buf += pos * 8 * 4;
  184. memset(buf, '\0', count * 8 * 4);
  185. return 0;
  186. }
  187. struct snd_pcm_ops bf5xx_pcm_tdm_ops = {
  188. .open = bf5xx_pcm_open,
  189. .ioctl = snd_pcm_lib_ioctl,
  190. .hw_params = bf5xx_pcm_hw_params,
  191. .hw_free = bf5xx_pcm_hw_free,
  192. .prepare = bf5xx_pcm_prepare,
  193. .trigger = bf5xx_pcm_trigger,
  194. .pointer = bf5xx_pcm_pointer,
  195. .copy = bf5xx_pcm_copy,
  196. .silence = bf5xx_pcm_silence,
  197. };
  198. static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  199. {
  200. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  201. struct snd_dma_buffer *buf = &substream->dma_buffer;
  202. size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
  203. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  204. buf->dev.dev = pcm->card->dev;
  205. buf->private_data = NULL;
  206. buf->area = dma_alloc_coherent(pcm->card->dev, size * 4,
  207. &buf->addr, GFP_KERNEL);
  208. if (!buf->area) {
  209. pr_err("Failed to allocate dma memory \
  210. Please increase uncached DMA memory region\n");
  211. return -ENOMEM;
  212. }
  213. buf->bytes = size;
  214. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  215. sport_handle->tx_buf = buf->area;
  216. else
  217. sport_handle->rx_buf = buf->area;
  218. return 0;
  219. }
  220. static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  221. {
  222. struct snd_pcm_substream *substream;
  223. struct snd_dma_buffer *buf;
  224. int stream;
  225. for (stream = 0; stream < 2; stream++) {
  226. substream = pcm->streams[stream].substream;
  227. if (!substream)
  228. continue;
  229. buf = &substream->dma_buffer;
  230. if (!buf->area)
  231. continue;
  232. dma_free_coherent(NULL, buf->bytes, buf->area, 0);
  233. buf->area = NULL;
  234. }
  235. if (sport_handle)
  236. sport_done(sport_handle);
  237. }
  238. static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);
  239. static int bf5xx_pcm_tdm_new(struct snd_card *card, struct snd_soc_dai *dai,
  240. struct snd_pcm *pcm)
  241. {
  242. int ret = 0;
  243. if (!card->dev->dma_mask)
  244. card->dev->dma_mask = &bf5xx_pcm_dmamask;
  245. if (!card->dev->coherent_dma_mask)
  246. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  247. if (dai->playback.channels_min) {
  248. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  249. SNDRV_PCM_STREAM_PLAYBACK);
  250. if (ret)
  251. goto out;
  252. }
  253. if (dai->capture.channels_min) {
  254. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  255. SNDRV_PCM_STREAM_CAPTURE);
  256. if (ret)
  257. goto out;
  258. }
  259. out:
  260. return ret;
  261. }
  262. struct snd_soc_platform bf5xx_tdm_soc_platform = {
  263. .name = "bf5xx-audio",
  264. .pcm_ops = &bf5xx_pcm_tdm_ops,
  265. .pcm_new = bf5xx_pcm_tdm_new,
  266. .pcm_free = bf5xx_pcm_free_dma_buffers,
  267. };
  268. EXPORT_SYMBOL_GPL(bf5xx_tdm_soc_platform);
  269. static int __init bfin_pcm_tdm_init(void)
  270. {
  271. return snd_soc_register_platform(&bf5xx_tdm_soc_platform);
  272. }
  273. module_init(bfin_pcm_tdm_init);
  274. static void __exit bfin_pcm_tdm_exit(void)
  275. {
  276. snd_soc_unregister_platform(&bf5xx_tdm_soc_platform);
  277. }
  278. module_exit(bfin_pcm_tdm_exit);
  279. MODULE_AUTHOR("Barry Song");
  280. MODULE_DESCRIPTION("ADI Blackfin TDM PCM DMA module");
  281. MODULE_LICENSE("GPL");