bf5xx-tdm-pcm.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/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-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_soc_pcm_runtime *rtd = substream->private_data;
  137. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  138. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai);
  139. struct snd_pcm_runtime *runtime = substream->runtime;
  140. struct snd_dma_buffer *buf = &substream->dma_buffer;
  141. int ret = 0;
  142. snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);
  143. ret = snd_pcm_hw_constraint_integer(runtime,
  144. SNDRV_PCM_HW_PARAM_PERIODS);
  145. if (ret < 0)
  146. goto out;
  147. if (sport_handle != NULL) {
  148. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  149. sport_handle->tx_buf = buf->area;
  150. else
  151. sport_handle->rx_buf = buf->area;
  152. runtime->private_data = sport_handle;
  153. } else {
  154. pr_err("sport_handle is NULL\n");
  155. ret = -ENODEV;
  156. }
  157. out:
  158. return ret;
  159. }
  160. static int bf5xx_pcm_copy(struct snd_pcm_substream *substream, int channel,
  161. snd_pcm_uframes_t pos, void *buf, snd_pcm_uframes_t count)
  162. {
  163. struct snd_pcm_runtime *runtime = substream->runtime;
  164. struct sport_device *sport = runtime->private_data;
  165. struct bf5xx_tdm_port *tdm_port = sport->private_data;
  166. unsigned int *src;
  167. unsigned int *dst;
  168. int i;
  169. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  170. src = buf;
  171. dst = (unsigned int *)substream->runtime->dma_area;
  172. dst += pos * 8;
  173. while (count--) {
  174. for (i = 0; i < substream->runtime->channels; i++)
  175. *(dst + tdm_port->tx_map[i]) = *src++;
  176. dst += 8;
  177. }
  178. } else {
  179. src = (unsigned int *)substream->runtime->dma_area;
  180. dst = buf;
  181. src += pos * 8;
  182. while (count--) {
  183. for (i = 0; i < substream->runtime->channels; i++)
  184. *dst++ = *(src + tdm_port->rx_map[i]);
  185. src += 8;
  186. }
  187. }
  188. return 0;
  189. }
  190. static int bf5xx_pcm_silence(struct snd_pcm_substream *substream,
  191. int channel, snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
  192. {
  193. unsigned char *buf = substream->runtime->dma_area;
  194. buf += pos * 8 * 4;
  195. memset(buf, '\0', count * 8 * 4);
  196. return 0;
  197. }
  198. struct snd_pcm_ops bf5xx_pcm_tdm_ops = {
  199. .open = bf5xx_pcm_open,
  200. .ioctl = snd_pcm_lib_ioctl,
  201. .hw_params = bf5xx_pcm_hw_params,
  202. .hw_free = bf5xx_pcm_hw_free,
  203. .prepare = bf5xx_pcm_prepare,
  204. .trigger = bf5xx_pcm_trigger,
  205. .pointer = bf5xx_pcm_pointer,
  206. .copy = bf5xx_pcm_copy,
  207. .silence = bf5xx_pcm_silence,
  208. };
  209. static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  210. {
  211. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  212. struct snd_dma_buffer *buf = &substream->dma_buffer;
  213. size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
  214. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  215. buf->dev.dev = pcm->card->dev;
  216. buf->private_data = NULL;
  217. buf->area = dma_alloc_coherent(pcm->card->dev, size * 4,
  218. &buf->addr, GFP_KERNEL);
  219. if (!buf->area) {
  220. pr_err("Failed to allocate dma memory - Please increase uncached DMA memory region\n");
  221. return -ENOMEM;
  222. }
  223. buf->bytes = size;
  224. return 0;
  225. }
  226. static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  227. {
  228. struct snd_pcm_substream *substream;
  229. struct snd_dma_buffer *buf;
  230. int stream;
  231. for (stream = 0; stream < 2; stream++) {
  232. substream = pcm->streams[stream].substream;
  233. if (!substream)
  234. continue;
  235. buf = &substream->dma_buffer;
  236. if (!buf->area)
  237. continue;
  238. dma_free_coherent(NULL, buf->bytes, buf->area, 0);
  239. buf->area = NULL;
  240. }
  241. }
  242. static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);
  243. static int bf5xx_pcm_tdm_new(struct snd_soc_pcm_runtime *rtd)
  244. {
  245. struct snd_card *card = rtd->card->snd_card;
  246. struct snd_pcm *pcm = rtd->pcm;
  247. int ret = 0;
  248. if (!card->dev->dma_mask)
  249. card->dev->dma_mask = &bf5xx_pcm_dmamask;
  250. if (!card->dev->coherent_dma_mask)
  251. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  252. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  253. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  254. SNDRV_PCM_STREAM_PLAYBACK);
  255. if (ret)
  256. goto out;
  257. }
  258. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  259. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  260. SNDRV_PCM_STREAM_CAPTURE);
  261. if (ret)
  262. goto out;
  263. }
  264. out:
  265. return ret;
  266. }
  267. static struct snd_soc_platform_driver bf5xx_tdm_soc_platform = {
  268. .ops = &bf5xx_pcm_tdm_ops,
  269. .pcm_new = bf5xx_pcm_tdm_new,
  270. .pcm_free = bf5xx_pcm_free_dma_buffers,
  271. };
  272. static int __devinit bf5xx_soc_platform_probe(struct platform_device *pdev)
  273. {
  274. return snd_soc_register_platform(&pdev->dev, &bf5xx_tdm_soc_platform);
  275. }
  276. static int __devexit bf5xx_soc_platform_remove(struct platform_device *pdev)
  277. {
  278. snd_soc_unregister_platform(&pdev->dev);
  279. return 0;
  280. }
  281. static struct platform_driver bfin_tdm_driver = {
  282. .driver = {
  283. .name = "bfin-tdm-pcm-audio",
  284. .owner = THIS_MODULE,
  285. },
  286. .probe = bf5xx_soc_platform_probe,
  287. .remove = __devexit_p(bf5xx_soc_platform_remove),
  288. };
  289. module_platform_driver(bfin_tdm_driver);
  290. MODULE_AUTHOR("Barry Song");
  291. MODULE_DESCRIPTION("ADI Blackfin TDM PCM DMA module");
  292. MODULE_LICENSE("GPL");