bf5xx-i2s-pcm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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-sport.h"
  39. #include "bf5xx-i2s-pcm.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_VALID |
  48. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  49. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  50. SNDRV_PCM_FMTBIT_S24_LE |
  51. SNDRV_PCM_FMTBIT_S32_LE,
  52. .period_bytes_min = 32,
  53. .period_bytes_max = 0x10000,
  54. .periods_min = 1,
  55. .periods_max = PAGE_SIZE/32,
  56. .buffer_bytes_max = 0x20000, /* 128 kbytes */
  57. .fifo_size = 16,
  58. };
  59. static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,
  60. struct snd_pcm_hw_params *params)
  61. {
  62. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  63. unsigned int buffer_size = params_buffer_bytes(params);
  64. struct bf5xx_i2s_pcm_data *dma_data;
  65. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  66. if (dma_data->tdm_mode)
  67. buffer_size = buffer_size / params_channels(params) * 8;
  68. return snd_pcm_lib_malloc_pages(substream, buffer_size);
  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_soc_pcm_runtime *rtd = substream->private_data;
  78. struct snd_pcm_runtime *runtime = substream->runtime;
  79. struct sport_device *sport = runtime->private_data;
  80. int period_bytes = frames_to_bytes(runtime, runtime->period_size);
  81. struct bf5xx_i2s_pcm_data *dma_data;
  82. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  83. if (dma_data->tdm_mode)
  84. period_bytes = period_bytes / runtime->channels * 8;
  85. pr_debug("%s enter\n", __func__);
  86. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  87. sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
  88. sport_config_tx_dma(sport, runtime->dma_area,
  89. runtime->periods, period_bytes);
  90. } else {
  91. sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
  92. sport_config_rx_dma(sport, runtime->dma_area,
  93. runtime->periods, period_bytes);
  94. }
  95. return 0;
  96. }
  97. static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  98. {
  99. struct snd_pcm_runtime *runtime = substream->runtime;
  100. struct sport_device *sport = runtime->private_data;
  101. int ret = 0;
  102. pr_debug("%s enter\n", __func__);
  103. switch (cmd) {
  104. case SNDRV_PCM_TRIGGER_START:
  105. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  106. sport_tx_start(sport);
  107. else
  108. sport_rx_start(sport);
  109. break;
  110. case SNDRV_PCM_TRIGGER_STOP:
  111. case SNDRV_PCM_TRIGGER_SUSPEND:
  112. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  113. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  114. sport_tx_stop(sport);
  115. else
  116. sport_rx_stop(sport);
  117. break;
  118. default:
  119. ret = -EINVAL;
  120. }
  121. return ret;
  122. }
  123. static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)
  124. {
  125. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  126. struct snd_pcm_runtime *runtime = substream->runtime;
  127. struct sport_device *sport = runtime->private_data;
  128. unsigned int diff;
  129. snd_pcm_uframes_t frames;
  130. struct bf5xx_i2s_pcm_data *dma_data;
  131. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  132. pr_debug("%s enter\n", __func__);
  133. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  134. diff = sport_curr_offset_tx(sport);
  135. } else {
  136. diff = sport_curr_offset_rx(sport);
  137. }
  138. /*
  139. * TX at least can report one frame beyond the end of the
  140. * buffer if we hit the wraparound case - clamp to within the
  141. * buffer as the ALSA APIs require.
  142. */
  143. if (diff == snd_pcm_lib_buffer_bytes(substream))
  144. diff = 0;
  145. frames = bytes_to_frames(substream->runtime, diff);
  146. if (dma_data->tdm_mode)
  147. frames = frames * runtime->channels / 8;
  148. return frames;
  149. }
  150. static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
  151. {
  152. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  153. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  154. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai);
  155. struct snd_pcm_runtime *runtime = substream->runtime;
  156. struct snd_dma_buffer *buf = &substream->dma_buffer;
  157. struct bf5xx_i2s_pcm_data *dma_data;
  158. int ret;
  159. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  160. pr_debug("%s enter\n", __func__);
  161. snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);
  162. if (dma_data->tdm_mode)
  163. runtime->hw.buffer_bytes_max /= 4;
  164. else
  165. runtime->hw.info |= SNDRV_PCM_INFO_MMAP;
  166. ret = snd_pcm_hw_constraint_integer(runtime,
  167. SNDRV_PCM_HW_PARAM_PERIODS);
  168. if (ret < 0)
  169. goto out;
  170. if (sport_handle != NULL) {
  171. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  172. sport_handle->tx_buf = buf->area;
  173. else
  174. sport_handle->rx_buf = buf->area;
  175. runtime->private_data = sport_handle;
  176. } else {
  177. pr_err("sport_handle is NULL\n");
  178. return -1;
  179. }
  180. return 0;
  181. out:
  182. return ret;
  183. }
  184. static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,
  185. struct vm_area_struct *vma)
  186. {
  187. struct snd_pcm_runtime *runtime = substream->runtime;
  188. size_t size = vma->vm_end - vma->vm_start;
  189. vma->vm_start = (unsigned long)runtime->dma_area;
  190. vma->vm_end = vma->vm_start + size;
  191. vma->vm_flags |= VM_SHARED;
  192. return 0 ;
  193. }
  194. static int bf5xx_pcm_copy(struct snd_pcm_substream *substream, int channel,
  195. snd_pcm_uframes_t pos, void *buf, snd_pcm_uframes_t count)
  196. {
  197. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  198. struct snd_pcm_runtime *runtime = substream->runtime;
  199. unsigned int sample_size = runtime->sample_bits / 8;
  200. struct bf5xx_i2s_pcm_data *dma_data;
  201. unsigned int i;
  202. void *src, *dst;
  203. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  204. if (dma_data->tdm_mode) {
  205. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  206. src = buf;
  207. dst = runtime->dma_area;
  208. dst += pos * sample_size * 8;
  209. while (count--) {
  210. for (i = 0; i < runtime->channels; i++) {
  211. memcpy(dst + dma_data->map[i] *
  212. sample_size, src, sample_size);
  213. src += sample_size;
  214. }
  215. dst += 8 * sample_size;
  216. }
  217. } else {
  218. src = runtime->dma_area;
  219. src += pos * sample_size * 8;
  220. dst = buf;
  221. while (count--) {
  222. for (i = 0; i < runtime->channels; i++) {
  223. memcpy(dst, src + dma_data->map[i] *
  224. sample_size, sample_size);
  225. dst += sample_size;
  226. }
  227. src += 8 * sample_size;
  228. }
  229. }
  230. } else {
  231. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  232. src = buf;
  233. dst = runtime->dma_area;
  234. dst += frames_to_bytes(runtime, pos);
  235. } else {
  236. src = runtime->dma_area;
  237. src += frames_to_bytes(runtime, pos);
  238. dst = buf;
  239. }
  240. memcpy(dst, src, frames_to_bytes(runtime, count));
  241. }
  242. return 0;
  243. }
  244. static int bf5xx_pcm_silence(struct snd_pcm_substream *substream,
  245. int channel, snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
  246. {
  247. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  248. struct snd_pcm_runtime *runtime = substream->runtime;
  249. unsigned int sample_size = runtime->sample_bits / 8;
  250. void *buf = runtime->dma_area;
  251. struct bf5xx_i2s_pcm_data *dma_data;
  252. unsigned int offset, size;
  253. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  254. if (dma_data->tdm_mode) {
  255. offset = pos * 8 * sample_size;
  256. size = count * 8 * sample_size;
  257. } else {
  258. offset = frames_to_bytes(runtime, pos);
  259. size = frames_to_bytes(runtime, count);
  260. }
  261. snd_pcm_format_set_silence(runtime->format, buf + offset, size);
  262. return 0;
  263. }
  264. static struct snd_pcm_ops bf5xx_pcm_i2s_ops = {
  265. .open = bf5xx_pcm_open,
  266. .ioctl = snd_pcm_lib_ioctl,
  267. .hw_params = bf5xx_pcm_hw_params,
  268. .hw_free = bf5xx_pcm_hw_free,
  269. .prepare = bf5xx_pcm_prepare,
  270. .trigger = bf5xx_pcm_trigger,
  271. .pointer = bf5xx_pcm_pointer,
  272. .mmap = bf5xx_pcm_mmap,
  273. .copy = bf5xx_pcm_copy,
  274. .silence = bf5xx_pcm_silence,
  275. };
  276. static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);
  277. static int bf5xx_pcm_i2s_new(struct snd_soc_pcm_runtime *rtd)
  278. {
  279. struct snd_card *card = rtd->card->snd_card;
  280. size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
  281. pr_debug("%s enter\n", __func__);
  282. if (!card->dev->dma_mask)
  283. card->dev->dma_mask = &bf5xx_pcm_dmamask;
  284. if (!card->dev->coherent_dma_mask)
  285. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  286. return snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
  287. SNDRV_DMA_TYPE_DEV, card->dev, size, size);
  288. }
  289. static struct snd_soc_platform_driver bf5xx_i2s_soc_platform = {
  290. .ops = &bf5xx_pcm_i2s_ops,
  291. .pcm_new = bf5xx_pcm_i2s_new,
  292. };
  293. static int bfin_i2s_soc_platform_probe(struct platform_device *pdev)
  294. {
  295. return snd_soc_register_platform(&pdev->dev, &bf5xx_i2s_soc_platform);
  296. }
  297. static int bfin_i2s_soc_platform_remove(struct platform_device *pdev)
  298. {
  299. snd_soc_unregister_platform(&pdev->dev);
  300. return 0;
  301. }
  302. static struct platform_driver bfin_i2s_pcm_driver = {
  303. .driver = {
  304. .name = "bfin-i2s-pcm-audio",
  305. .owner = THIS_MODULE,
  306. },
  307. .probe = bfin_i2s_soc_platform_probe,
  308. .remove = bfin_i2s_soc_platform_remove,
  309. };
  310. module_platform_driver(bfin_i2s_pcm_driver);
  311. MODULE_AUTHOR("Cliff Cai");
  312. MODULE_DESCRIPTION("ADI Blackfin I2S PCM DMA module");
  313. MODULE_LICENSE("GPL");