davinci-pcm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * ALSA PCM interface for the TI DAVINCI processor
  3. *
  4. * Author: Vladimir Barinov, <vbarinov@embeddedalley.com>
  5. * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/kernel.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #include <asm/dma.h>
  22. #include <mach/edma.h>
  23. #include "davinci-pcm.h"
  24. static struct snd_pcm_hardware davinci_pcm_hardware = {
  25. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  26. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  27. SNDRV_PCM_INFO_PAUSE),
  28. .formats = (SNDRV_PCM_FMTBIT_S16_LE),
  29. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
  30. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
  31. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  32. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
  33. SNDRV_PCM_RATE_KNOT),
  34. .rate_min = 8000,
  35. .rate_max = 96000,
  36. .channels_min = 2,
  37. .channels_max = 2,
  38. .buffer_bytes_max = 128 * 1024,
  39. .period_bytes_min = 32,
  40. .period_bytes_max = 8 * 1024,
  41. .periods_min = 16,
  42. .periods_max = 255,
  43. .fifo_size = 0,
  44. };
  45. struct davinci_runtime_data {
  46. spinlock_t lock;
  47. int period; /* current DMA period */
  48. int master_lch; /* Master DMA channel */
  49. int slave_lch; /* linked parameter RAM reload slot */
  50. struct davinci_pcm_dma_params *params; /* DMA params */
  51. };
  52. static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
  53. {
  54. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  55. struct snd_pcm_runtime *runtime = substream->runtime;
  56. int lch = prtd->slave_lch;
  57. unsigned int period_size;
  58. unsigned int dma_offset;
  59. dma_addr_t dma_pos;
  60. dma_addr_t src, dst;
  61. unsigned short src_bidx, dst_bidx;
  62. unsigned int data_type;
  63. unsigned int count;
  64. period_size = snd_pcm_lib_period_bytes(substream);
  65. dma_offset = prtd->period * period_size;
  66. dma_pos = runtime->dma_addr + dma_offset;
  67. pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
  68. "dma_ptr = %x period_size=%x\n", lch, dma_pos, period_size);
  69. data_type = prtd->params->data_type;
  70. count = period_size / data_type;
  71. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  72. src = dma_pos;
  73. dst = prtd->params->dma_addr;
  74. src_bidx = data_type;
  75. dst_bidx = 0;
  76. } else {
  77. src = prtd->params->dma_addr;
  78. dst = dma_pos;
  79. src_bidx = 0;
  80. dst_bidx = data_type;
  81. }
  82. edma_set_src(lch, src, INCR, W8BIT);
  83. edma_set_dest(lch, dst, INCR, W8BIT);
  84. edma_set_src_index(lch, src_bidx, 0);
  85. edma_set_dest_index(lch, dst_bidx, 0);
  86. edma_set_transfer_params(lch, data_type, count, 1, 0, ASYNC);
  87. prtd->period++;
  88. if (unlikely(prtd->period >= runtime->periods))
  89. prtd->period = 0;
  90. }
  91. static void davinci_pcm_dma_irq(unsigned lch, u16 ch_status, void *data)
  92. {
  93. struct snd_pcm_substream *substream = data;
  94. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  95. pr_debug("davinci_pcm: lch=%d, status=0x%x\n", lch, ch_status);
  96. if (unlikely(ch_status != DMA_COMPLETE))
  97. return;
  98. if (snd_pcm_running(substream)) {
  99. snd_pcm_period_elapsed(substream);
  100. spin_lock(&prtd->lock);
  101. davinci_pcm_enqueue_dma(substream);
  102. spin_unlock(&prtd->lock);
  103. }
  104. }
  105. static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
  106. {
  107. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  108. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109. struct davinci_pcm_dma_params *dma_data = rtd->dai->cpu_dai->dma_data;
  110. struct edmacc_param p_ram;
  111. int ret;
  112. if (!dma_data)
  113. return -ENODEV;
  114. prtd->params = dma_data;
  115. /* Request master DMA channel */
  116. ret = edma_alloc_channel(prtd->params->channel,
  117. davinci_pcm_dma_irq, substream,
  118. EVENTQ_0);
  119. if (ret < 0)
  120. return ret;
  121. prtd->master_lch = ret;
  122. /* Request parameter RAM reload slot */
  123. ret = edma_alloc_slot(EDMA_SLOT_ANY);
  124. if (ret < 0) {
  125. edma_free_channel(prtd->master_lch);
  126. return ret;
  127. }
  128. prtd->slave_lch = ret;
  129. /* Issue transfer completion IRQ when the channel completes a
  130. * transfer, then always reload from the same slot (by a kind
  131. * of loopback link). The completion IRQ handler will update
  132. * the reload slot with a new buffer.
  133. *
  134. * REVISIT save p_ram here after setting up everything except
  135. * the buffer and its length (ccnt) ... use it as a template
  136. * so davinci_pcm_enqueue_dma() takes less time in IRQ.
  137. */
  138. edma_read_slot(prtd->slave_lch, &p_ram);
  139. p_ram.opt |= TCINTEN | EDMA_TCC(prtd->master_lch);
  140. p_ram.link_bcntrld = prtd->slave_lch << 5;
  141. edma_write_slot(prtd->slave_lch, &p_ram);
  142. return 0;
  143. }
  144. static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  145. {
  146. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  147. int ret = 0;
  148. spin_lock(&prtd->lock);
  149. switch (cmd) {
  150. case SNDRV_PCM_TRIGGER_START:
  151. case SNDRV_PCM_TRIGGER_RESUME:
  152. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  153. edma_start(prtd->master_lch);
  154. break;
  155. case SNDRV_PCM_TRIGGER_STOP:
  156. case SNDRV_PCM_TRIGGER_SUSPEND:
  157. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  158. edma_stop(prtd->master_lch);
  159. break;
  160. default:
  161. ret = -EINVAL;
  162. break;
  163. }
  164. spin_unlock(&prtd->lock);
  165. return ret;
  166. }
  167. static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
  168. {
  169. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  170. struct edmacc_param temp;
  171. prtd->period = 0;
  172. davinci_pcm_enqueue_dma(substream);
  173. /* Copy self-linked parameter RAM entry into master channel */
  174. edma_read_slot(prtd->slave_lch, &temp);
  175. edma_write_slot(prtd->master_lch, &temp);
  176. return 0;
  177. }
  178. static snd_pcm_uframes_t
  179. davinci_pcm_pointer(struct snd_pcm_substream *substream)
  180. {
  181. struct snd_pcm_runtime *runtime = substream->runtime;
  182. struct davinci_runtime_data *prtd = runtime->private_data;
  183. unsigned int offset;
  184. dma_addr_t count;
  185. dma_addr_t src, dst;
  186. spin_lock(&prtd->lock);
  187. edma_get_position(prtd->master_lch, &src, &dst);
  188. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  189. count = src - runtime->dma_addr;
  190. else
  191. count = dst - runtime->dma_addr;
  192. spin_unlock(&prtd->lock);
  193. offset = bytes_to_frames(runtime, count);
  194. if (offset >= runtime->buffer_size)
  195. offset = 0;
  196. return offset;
  197. }
  198. static int davinci_pcm_open(struct snd_pcm_substream *substream)
  199. {
  200. struct snd_pcm_runtime *runtime = substream->runtime;
  201. struct davinci_runtime_data *prtd;
  202. int ret = 0;
  203. snd_soc_set_runtime_hwparams(substream, &davinci_pcm_hardware);
  204. prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
  205. if (prtd == NULL)
  206. return -ENOMEM;
  207. spin_lock_init(&prtd->lock);
  208. runtime->private_data = prtd;
  209. ret = davinci_pcm_dma_request(substream);
  210. if (ret) {
  211. printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
  212. kfree(prtd);
  213. }
  214. return ret;
  215. }
  216. static int davinci_pcm_close(struct snd_pcm_substream *substream)
  217. {
  218. struct snd_pcm_runtime *runtime = substream->runtime;
  219. struct davinci_runtime_data *prtd = runtime->private_data;
  220. edma_unlink(prtd->slave_lch);
  221. edma_free_slot(prtd->slave_lch);
  222. edma_free_channel(prtd->master_lch);
  223. kfree(prtd);
  224. return 0;
  225. }
  226. static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
  227. struct snd_pcm_hw_params *hw_params)
  228. {
  229. return snd_pcm_lib_malloc_pages(substream,
  230. params_buffer_bytes(hw_params));
  231. }
  232. static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
  233. {
  234. return snd_pcm_lib_free_pages(substream);
  235. }
  236. static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
  237. struct vm_area_struct *vma)
  238. {
  239. struct snd_pcm_runtime *runtime = substream->runtime;
  240. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  241. runtime->dma_area,
  242. runtime->dma_addr,
  243. runtime->dma_bytes);
  244. }
  245. static struct snd_pcm_ops davinci_pcm_ops = {
  246. .open = davinci_pcm_open,
  247. .close = davinci_pcm_close,
  248. .ioctl = snd_pcm_lib_ioctl,
  249. .hw_params = davinci_pcm_hw_params,
  250. .hw_free = davinci_pcm_hw_free,
  251. .prepare = davinci_pcm_prepare,
  252. .trigger = davinci_pcm_trigger,
  253. .pointer = davinci_pcm_pointer,
  254. .mmap = davinci_pcm_mmap,
  255. };
  256. static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  257. {
  258. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  259. struct snd_dma_buffer *buf = &substream->dma_buffer;
  260. size_t size = davinci_pcm_hardware.buffer_bytes_max;
  261. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  262. buf->dev.dev = pcm->card->dev;
  263. buf->private_data = NULL;
  264. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  265. &buf->addr, GFP_KERNEL);
  266. pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
  267. "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
  268. if (!buf->area)
  269. return -ENOMEM;
  270. buf->bytes = size;
  271. return 0;
  272. }
  273. static void davinci_pcm_free(struct snd_pcm *pcm)
  274. {
  275. struct snd_pcm_substream *substream;
  276. struct snd_dma_buffer *buf;
  277. int stream;
  278. for (stream = 0; stream < 2; stream++) {
  279. substream = pcm->streams[stream].substream;
  280. if (!substream)
  281. continue;
  282. buf = &substream->dma_buffer;
  283. if (!buf->area)
  284. continue;
  285. dma_free_writecombine(pcm->card->dev, buf->bytes,
  286. buf->area, buf->addr);
  287. buf->area = NULL;
  288. }
  289. }
  290. static u64 davinci_pcm_dmamask = 0xffffffff;
  291. static int davinci_pcm_new(struct snd_card *card,
  292. struct snd_soc_dai *dai, struct snd_pcm *pcm)
  293. {
  294. int ret;
  295. if (!card->dev->dma_mask)
  296. card->dev->dma_mask = &davinci_pcm_dmamask;
  297. if (!card->dev->coherent_dma_mask)
  298. card->dev->coherent_dma_mask = 0xffffffff;
  299. if (dai->playback.channels_min) {
  300. ret = davinci_pcm_preallocate_dma_buffer(pcm,
  301. SNDRV_PCM_STREAM_PLAYBACK);
  302. if (ret)
  303. return ret;
  304. }
  305. if (dai->capture.channels_min) {
  306. ret = davinci_pcm_preallocate_dma_buffer(pcm,
  307. SNDRV_PCM_STREAM_CAPTURE);
  308. if (ret)
  309. return ret;
  310. }
  311. return 0;
  312. }
  313. struct snd_soc_platform davinci_soc_platform = {
  314. .name = "davinci-audio",
  315. .pcm_ops = &davinci_pcm_ops,
  316. .pcm_new = davinci_pcm_new,
  317. .pcm_free = davinci_pcm_free,
  318. };
  319. EXPORT_SYMBOL_GPL(davinci_soc_platform);
  320. static int __init davinci_soc_platform_init(void)
  321. {
  322. return snd_soc_register_platform(&davinci_soc_platform);
  323. }
  324. module_init(davinci_soc_platform_init);
  325. static void __exit davinci_soc_platform_exit(void)
  326. {
  327. snd_soc_unregister_platform(&davinci_soc_platform);
  328. }
  329. module_exit(davinci_soc_platform_exit);
  330. MODULE_AUTHOR("Vladimir Barinov");
  331. MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
  332. MODULE_LICENSE("GPL");