davinci-pcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 asp_channel; /* Master DMA channel */
  49. int asp_link[2]; /* asp parameter link channel, ping/pong */
  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 link = prtd->asp_link[0];
  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 short src_cidx, dst_cidx;
  63. unsigned int data_type;
  64. unsigned short acnt;
  65. unsigned int count;
  66. unsigned int fifo_level;
  67. period_size = snd_pcm_lib_period_bytes(substream);
  68. dma_offset = prtd->period * period_size;
  69. dma_pos = runtime->dma_addr + dma_offset;
  70. fifo_level = prtd->params->fifo_level;
  71. pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
  72. "dma_ptr = %x period_size=%x\n", link, dma_pos, period_size);
  73. data_type = prtd->params->data_type;
  74. count = period_size / data_type;
  75. if (fifo_level)
  76. count /= fifo_level;
  77. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  78. src = dma_pos;
  79. dst = prtd->params->dma_addr;
  80. src_bidx = data_type;
  81. dst_bidx = 0;
  82. src_cidx = data_type * fifo_level;
  83. dst_cidx = 0;
  84. } else {
  85. src = prtd->params->dma_addr;
  86. dst = dma_pos;
  87. src_bidx = 0;
  88. dst_bidx = data_type;
  89. src_cidx = 0;
  90. dst_cidx = data_type * fifo_level;
  91. }
  92. acnt = prtd->params->acnt;
  93. edma_set_src(link, src, INCR, W8BIT);
  94. edma_set_dest(link, dst, INCR, W8BIT);
  95. edma_set_src_index(link, src_bidx, src_cidx);
  96. edma_set_dest_index(link, dst_bidx, dst_cidx);
  97. if (!fifo_level)
  98. edma_set_transfer_params(link, acnt, count, 1, 0, ASYNC);
  99. else
  100. edma_set_transfer_params(link, acnt, fifo_level, count,
  101. fifo_level, ABSYNC);
  102. prtd->period++;
  103. if (unlikely(prtd->period >= runtime->periods))
  104. prtd->period = 0;
  105. }
  106. static void davinci_pcm_dma_irq(unsigned link, u16 ch_status, void *data)
  107. {
  108. struct snd_pcm_substream *substream = data;
  109. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  110. pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status);
  111. if (unlikely(ch_status != DMA_COMPLETE))
  112. return;
  113. if (snd_pcm_running(substream)) {
  114. snd_pcm_period_elapsed(substream);
  115. spin_lock(&prtd->lock);
  116. davinci_pcm_enqueue_dma(substream);
  117. spin_unlock(&prtd->lock);
  118. }
  119. }
  120. static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
  121. {
  122. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  123. struct edmacc_param p_ram;
  124. int ret;
  125. /* Request master DMA channel */
  126. ret = edma_alloc_channel(prtd->params->channel,
  127. davinci_pcm_dma_irq, substream,
  128. EVENTQ_0);
  129. if (ret < 0)
  130. return ret;
  131. prtd->asp_channel = ret;
  132. /* Request parameter RAM reload slot */
  133. ret = edma_alloc_slot(EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
  134. if (ret < 0) {
  135. edma_free_channel(prtd->asp_channel);
  136. return ret;
  137. }
  138. prtd->asp_link[0] = ret;
  139. /* Issue transfer completion IRQ when the channel completes a
  140. * transfer, then always reload from the same slot (by a kind
  141. * of loopback link). The completion IRQ handler will update
  142. * the reload slot with a new buffer.
  143. *
  144. * REVISIT save p_ram here after setting up everything except
  145. * the buffer and its length (ccnt) ... use it as a template
  146. * so davinci_pcm_enqueue_dma() takes less time in IRQ.
  147. */
  148. edma_read_slot(prtd->asp_link[0], &p_ram);
  149. p_ram.opt |= TCINTEN | EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel));
  150. p_ram.link_bcntrld = EDMA_CHAN_SLOT(prtd->asp_link[0]) << 5;
  151. edma_write_slot(prtd->asp_link[0], &p_ram);
  152. return 0;
  153. }
  154. static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  155. {
  156. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  157. int ret = 0;
  158. spin_lock(&prtd->lock);
  159. switch (cmd) {
  160. case SNDRV_PCM_TRIGGER_START:
  161. case SNDRV_PCM_TRIGGER_RESUME:
  162. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  163. edma_start(prtd->asp_channel);
  164. break;
  165. case SNDRV_PCM_TRIGGER_STOP:
  166. case SNDRV_PCM_TRIGGER_SUSPEND:
  167. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  168. edma_stop(prtd->asp_channel);
  169. break;
  170. default:
  171. ret = -EINVAL;
  172. break;
  173. }
  174. spin_unlock(&prtd->lock);
  175. return ret;
  176. }
  177. static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
  178. {
  179. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  180. struct edmacc_param temp;
  181. prtd->period = 0;
  182. davinci_pcm_enqueue_dma(substream);
  183. /* Copy self-linked parameter RAM entry into master channel */
  184. edma_read_slot(prtd->asp_link[0], &temp);
  185. edma_write_slot(prtd->asp_channel, &temp);
  186. davinci_pcm_enqueue_dma(substream);
  187. return 0;
  188. }
  189. static snd_pcm_uframes_t
  190. davinci_pcm_pointer(struct snd_pcm_substream *substream)
  191. {
  192. struct snd_pcm_runtime *runtime = substream->runtime;
  193. struct davinci_runtime_data *prtd = runtime->private_data;
  194. unsigned int offset;
  195. int asp_count;
  196. dma_addr_t asp_src, asp_dst;
  197. spin_lock(&prtd->lock);
  198. edma_get_position(prtd->asp_channel, &asp_src, &asp_dst);
  199. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  200. asp_count = asp_src - runtime->dma_addr;
  201. else
  202. asp_count = asp_dst - runtime->dma_addr;
  203. spin_unlock(&prtd->lock);
  204. offset = bytes_to_frames(runtime, asp_count);
  205. if (offset >= runtime->buffer_size)
  206. offset = 0;
  207. return offset;
  208. }
  209. static int davinci_pcm_open(struct snd_pcm_substream *substream)
  210. {
  211. struct snd_pcm_runtime *runtime = substream->runtime;
  212. struct davinci_runtime_data *prtd;
  213. int ret = 0;
  214. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  215. struct davinci_pcm_dma_params *pa = rtd->dai->cpu_dai->dma_data;
  216. struct davinci_pcm_dma_params *params;
  217. if (!pa)
  218. return -ENODEV;
  219. params = &pa[substream->stream];
  220. snd_soc_set_runtime_hwparams(substream, &davinci_pcm_hardware);
  221. /* ensure that buffer size is a multiple of period size */
  222. ret = snd_pcm_hw_constraint_integer(runtime,
  223. SNDRV_PCM_HW_PARAM_PERIODS);
  224. if (ret < 0)
  225. return ret;
  226. prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
  227. if (prtd == NULL)
  228. return -ENOMEM;
  229. spin_lock_init(&prtd->lock);
  230. prtd->params = params;
  231. runtime->private_data = prtd;
  232. ret = davinci_pcm_dma_request(substream);
  233. if (ret) {
  234. printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
  235. kfree(prtd);
  236. }
  237. return ret;
  238. }
  239. static int davinci_pcm_close(struct snd_pcm_substream *substream)
  240. {
  241. struct snd_pcm_runtime *runtime = substream->runtime;
  242. struct davinci_runtime_data *prtd = runtime->private_data;
  243. edma_unlink(prtd->asp_link[0]);
  244. edma_free_slot(prtd->asp_link[0]);
  245. edma_free_channel(prtd->asp_channel);
  246. kfree(prtd);
  247. return 0;
  248. }
  249. static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
  250. struct snd_pcm_hw_params *hw_params)
  251. {
  252. return snd_pcm_lib_malloc_pages(substream,
  253. params_buffer_bytes(hw_params));
  254. }
  255. static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
  256. {
  257. return snd_pcm_lib_free_pages(substream);
  258. }
  259. static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
  260. struct vm_area_struct *vma)
  261. {
  262. struct snd_pcm_runtime *runtime = substream->runtime;
  263. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  264. runtime->dma_area,
  265. runtime->dma_addr,
  266. runtime->dma_bytes);
  267. }
  268. static struct snd_pcm_ops davinci_pcm_ops = {
  269. .open = davinci_pcm_open,
  270. .close = davinci_pcm_close,
  271. .ioctl = snd_pcm_lib_ioctl,
  272. .hw_params = davinci_pcm_hw_params,
  273. .hw_free = davinci_pcm_hw_free,
  274. .prepare = davinci_pcm_prepare,
  275. .trigger = davinci_pcm_trigger,
  276. .pointer = davinci_pcm_pointer,
  277. .mmap = davinci_pcm_mmap,
  278. };
  279. static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  280. {
  281. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  282. struct snd_dma_buffer *buf = &substream->dma_buffer;
  283. size_t size = davinci_pcm_hardware.buffer_bytes_max;
  284. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  285. buf->dev.dev = pcm->card->dev;
  286. buf->private_data = NULL;
  287. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  288. &buf->addr, GFP_KERNEL);
  289. pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
  290. "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
  291. if (!buf->area)
  292. return -ENOMEM;
  293. buf->bytes = size;
  294. return 0;
  295. }
  296. static void davinci_pcm_free(struct snd_pcm *pcm)
  297. {
  298. struct snd_pcm_substream *substream;
  299. struct snd_dma_buffer *buf;
  300. int stream;
  301. for (stream = 0; stream < 2; stream++) {
  302. substream = pcm->streams[stream].substream;
  303. if (!substream)
  304. continue;
  305. buf = &substream->dma_buffer;
  306. if (!buf->area)
  307. continue;
  308. dma_free_writecombine(pcm->card->dev, buf->bytes,
  309. buf->area, buf->addr);
  310. buf->area = NULL;
  311. }
  312. }
  313. static u64 davinci_pcm_dmamask = 0xffffffff;
  314. static int davinci_pcm_new(struct snd_card *card,
  315. struct snd_soc_dai *dai, struct snd_pcm *pcm)
  316. {
  317. int ret;
  318. if (!card->dev->dma_mask)
  319. card->dev->dma_mask = &davinci_pcm_dmamask;
  320. if (!card->dev->coherent_dma_mask)
  321. card->dev->coherent_dma_mask = 0xffffffff;
  322. if (dai->playback.channels_min) {
  323. ret = davinci_pcm_preallocate_dma_buffer(pcm,
  324. SNDRV_PCM_STREAM_PLAYBACK);
  325. if (ret)
  326. return ret;
  327. }
  328. if (dai->capture.channels_min) {
  329. ret = davinci_pcm_preallocate_dma_buffer(pcm,
  330. SNDRV_PCM_STREAM_CAPTURE);
  331. if (ret)
  332. return ret;
  333. }
  334. return 0;
  335. }
  336. struct snd_soc_platform davinci_soc_platform = {
  337. .name = "davinci-audio",
  338. .pcm_ops = &davinci_pcm_ops,
  339. .pcm_new = davinci_pcm_new,
  340. .pcm_free = davinci_pcm_free,
  341. };
  342. EXPORT_SYMBOL_GPL(davinci_soc_platform);
  343. static int __init davinci_soc_platform_init(void)
  344. {
  345. return snd_soc_register_platform(&davinci_soc_platform);
  346. }
  347. module_init(davinci_soc_platform_init);
  348. static void __exit davinci_soc_platform_exit(void)
  349. {
  350. snd_soc_unregister_platform(&davinci_soc_platform);
  351. }
  352. module_exit(davinci_soc_platform_exit);
  353. MODULE_AUTHOR("Vladimir Barinov");
  354. MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
  355. MODULE_LICENSE("GPL");