bf5xx-tdm-pcm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 0x8000
  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. struct snd_pcm_runtime *runtime = substream->runtime;
  156. struct sport_device *sport = runtime->private_data;
  157. struct bf5xx_tdm_port *tdm_port = sport->private_data;
  158. unsigned int *src;
  159. unsigned int *dst;
  160. int i;
  161. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  162. src = buf;
  163. dst = (unsigned int *)substream->runtime->dma_area;
  164. dst += pos * 8;
  165. while (count--) {
  166. for (i = 0; i < substream->runtime->channels; i++)
  167. *(dst + tdm_port->tx_map[i]) = *src++;
  168. dst += 8;
  169. }
  170. } else {
  171. src = (unsigned int *)substream->runtime->dma_area;
  172. dst = buf;
  173. src += pos * 8;
  174. while (count--) {
  175. for (i = 0; i < substream->runtime->channels; i++)
  176. *dst++ = *(src + tdm_port->rx_map[i]);
  177. src += 8;
  178. }
  179. }
  180. return 0;
  181. }
  182. static int bf5xx_pcm_silence(struct snd_pcm_substream *substream,
  183. int channel, snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
  184. {
  185. unsigned char *buf = substream->runtime->dma_area;
  186. buf += pos * 8 * 4;
  187. memset(buf, '\0', count * 8 * 4);
  188. return 0;
  189. }
  190. struct snd_pcm_ops bf5xx_pcm_tdm_ops = {
  191. .open = bf5xx_pcm_open,
  192. .ioctl = snd_pcm_lib_ioctl,
  193. .hw_params = bf5xx_pcm_hw_params,
  194. .hw_free = bf5xx_pcm_hw_free,
  195. .prepare = bf5xx_pcm_prepare,
  196. .trigger = bf5xx_pcm_trigger,
  197. .pointer = bf5xx_pcm_pointer,
  198. .copy = bf5xx_pcm_copy,
  199. .silence = bf5xx_pcm_silence,
  200. };
  201. static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  202. {
  203. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  204. struct snd_dma_buffer *buf = &substream->dma_buffer;
  205. size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
  206. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  207. buf->dev.dev = pcm->card->dev;
  208. buf->private_data = NULL;
  209. buf->area = dma_alloc_coherent(pcm->card->dev, size * 4,
  210. &buf->addr, GFP_KERNEL);
  211. if (!buf->area) {
  212. pr_err("Failed to allocate dma memory \
  213. Please increase uncached DMA memory region\n");
  214. return -ENOMEM;
  215. }
  216. buf->bytes = size;
  217. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  218. sport_handle->tx_buf = buf->area;
  219. else
  220. sport_handle->rx_buf = buf->area;
  221. return 0;
  222. }
  223. static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  224. {
  225. struct snd_pcm_substream *substream;
  226. struct snd_dma_buffer *buf;
  227. int stream;
  228. for (stream = 0; stream < 2; stream++) {
  229. substream = pcm->streams[stream].substream;
  230. if (!substream)
  231. continue;
  232. buf = &substream->dma_buffer;
  233. if (!buf->area)
  234. continue;
  235. dma_free_coherent(NULL, buf->bytes, buf->area, 0);
  236. buf->area = NULL;
  237. }
  238. if (sport_handle)
  239. sport_done(sport_handle);
  240. }
  241. static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);
  242. static int bf5xx_pcm_tdm_new(struct snd_card *card, struct snd_soc_dai *dai,
  243. struct snd_pcm *pcm)
  244. {
  245. int ret = 0;
  246. if (!card->dev->dma_mask)
  247. card->dev->dma_mask = &bf5xx_pcm_dmamask;
  248. if (!card->dev->coherent_dma_mask)
  249. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  250. if (dai->playback.channels_min) {
  251. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  252. SNDRV_PCM_STREAM_PLAYBACK);
  253. if (ret)
  254. goto out;
  255. }
  256. if (dai->capture.channels_min) {
  257. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  258. SNDRV_PCM_STREAM_CAPTURE);
  259. if (ret)
  260. goto out;
  261. }
  262. out:
  263. return ret;
  264. }
  265. struct snd_soc_platform bf5xx_tdm_soc_platform = {
  266. .name = "bf5xx-audio",
  267. .pcm_ops = &bf5xx_pcm_tdm_ops,
  268. .pcm_new = bf5xx_pcm_tdm_new,
  269. .pcm_free = bf5xx_pcm_free_dma_buffers,
  270. };
  271. EXPORT_SYMBOL_GPL(bf5xx_tdm_soc_platform);
  272. static int __init bfin_pcm_tdm_init(void)
  273. {
  274. return snd_soc_register_platform(&bf5xx_tdm_soc_platform);
  275. }
  276. module_init(bfin_pcm_tdm_init);
  277. static void __exit bfin_pcm_tdm_exit(void)
  278. {
  279. snd_soc_unregister_platform(&bf5xx_tdm_soc_platform);
  280. }
  281. module_exit(bfin_pcm_tdm_exit);
  282. MODULE_AUTHOR("Barry Song");
  283. MODULE_DESCRIPTION("ADI Blackfin TDM PCM DMA module");
  284. MODULE_LICENSE("GPL");