bf5xx-i2s-pcm.c 9.6 KB

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