davinci-pcm.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <sound/core.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/soc.h>
  20. #include <asm/dma.h>
  21. #include "davinci-pcm.h"
  22. #define DAVINCI_PCM_DEBUG 0
  23. #if DAVINCI_PCM_DEBUG
  24. #define DPRINTK(x...) printk(KERN_DEBUG x)
  25. #else
  26. #define DPRINTK(x...)
  27. #endif
  28. static struct snd_pcm_hardware davinci_pcm_hardware = {
  29. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  30. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  31. SNDRV_PCM_INFO_PAUSE),
  32. .formats = (SNDRV_PCM_FMTBIT_S16_LE),
  33. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
  34. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
  35. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  36. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
  37. SNDRV_PCM_RATE_KNOT),
  38. .rate_min = 8000,
  39. .rate_max = 96000,
  40. .channels_min = 2,
  41. .channels_max = 2,
  42. .buffer_bytes_max = 128 * 1024,
  43. .period_bytes_min = 32,
  44. .period_bytes_max = 8 * 1024,
  45. .periods_min = 16,
  46. .periods_max = 255,
  47. .fifo_size = 0,
  48. };
  49. struct davinci_runtime_data {
  50. spinlock_t lock;
  51. int period; /* current DMA period */
  52. int master_lch; /* Master DMA channel */
  53. int slave_lch; /* Slave DMA channel */
  54. struct davinci_pcm_dma_params *params; /* DMA params */
  55. };
  56. static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
  57. {
  58. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  59. struct snd_pcm_runtime *runtime = substream->runtime;
  60. int lch = prtd->slave_lch;
  61. unsigned int period_size;
  62. unsigned int dma_offset;
  63. dma_addr_t dma_pos;
  64. dma_addr_t src, dst;
  65. unsigned short src_bidx, dst_bidx;
  66. unsigned int data_type;
  67. unsigned int count;
  68. period_size = snd_pcm_lib_period_bytes(substream);
  69. dma_offset = prtd->period * period_size;
  70. dma_pos = runtime->dma_addr + dma_offset;
  71. DPRINTK("audio_set_dma_params_play channel = %d dma_ptr = %x "
  72. "period_size=%x\n", lch, dma_pos, period_size);
  73. data_type = prtd->params->data_type;
  74. count = period_size / data_type;
  75. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  76. src = dma_pos;
  77. dst = prtd->params->dma_addr;
  78. src_bidx = data_type;
  79. dst_bidx = 0;
  80. } else {
  81. src = prtd->params->dma_addr;
  82. dst = dma_pos;
  83. src_bidx = 0;
  84. dst_bidx = data_type;
  85. }
  86. davinci_set_dma_src_params(lch, src, INCR, W8BIT);
  87. davinci_set_dma_dest_params(lch, dst, INCR, W8BIT);
  88. davinci_set_dma_src_index(lch, src_bidx, 0);
  89. davinci_set_dma_dest_index(lch, dst_bidx, 0);
  90. davinci_set_dma_transfer_params(lch, data_type, count, 1, 0, ASYNC);
  91. prtd->period++;
  92. if (unlikely(prtd->period >= runtime->periods))
  93. prtd->period = 0;
  94. }
  95. static void davinci_pcm_dma_irq(int lch, u16 ch_status, void *data)
  96. {
  97. struct snd_pcm_substream *substream = data;
  98. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  99. DPRINTK("lch=%d, status=0x%x\n", lch, ch_status);
  100. if (unlikely(ch_status != DMA_COMPLETE))
  101. return;
  102. if (snd_pcm_running(substream)) {
  103. snd_pcm_period_elapsed(substream);
  104. spin_lock(&prtd->lock);
  105. davinci_pcm_enqueue_dma(substream);
  106. spin_unlock(&prtd->lock);
  107. }
  108. }
  109. static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
  110. {
  111. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  112. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  113. struct davinci_pcm_dma_params *dma_data = rtd->dai->cpu_dai->dma_data;
  114. int tcc = TCC_ANY;
  115. int ret;
  116. if (!dma_data)
  117. return -ENODEV;
  118. prtd->params = dma_data;
  119. /* Request master DMA channel */
  120. ret = davinci_request_dma(prtd->params->channel, prtd->params->name,
  121. davinci_pcm_dma_irq, substream,
  122. &prtd->master_lch, &tcc, EVENTQ_0);
  123. if (ret)
  124. return ret;
  125. /* Request slave DMA channel */
  126. ret = davinci_request_dma(PARAM_ANY, "Link",
  127. NULL, NULL, &prtd->slave_lch, &tcc, EVENTQ_0);
  128. if (ret) {
  129. davinci_free_dma(prtd->master_lch);
  130. return ret;
  131. }
  132. /* Link slave DMA channel in loopback */
  133. davinci_dma_link_lch(prtd->slave_lch, prtd->slave_lch);
  134. return 0;
  135. }
  136. static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  137. {
  138. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  139. int ret = 0;
  140. spin_lock(&prtd->lock);
  141. switch (cmd) {
  142. case SNDRV_PCM_TRIGGER_START:
  143. case SNDRV_PCM_TRIGGER_RESUME:
  144. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  145. davinci_start_dma(prtd->master_lch);
  146. break;
  147. case SNDRV_PCM_TRIGGER_STOP:
  148. case SNDRV_PCM_TRIGGER_SUSPEND:
  149. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  150. davinci_stop_dma(prtd->master_lch);
  151. break;
  152. default:
  153. ret = -EINVAL;
  154. break;
  155. }
  156. spin_unlock(&prtd->lock);
  157. return ret;
  158. }
  159. static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
  160. {
  161. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  162. struct paramentry_descriptor temp;
  163. prtd->period = 0;
  164. davinci_pcm_enqueue_dma(substream);
  165. /* Get slave channel dma params for master channel startup */
  166. davinci_get_dma_params(prtd->slave_lch, &temp);
  167. davinci_set_dma_params(prtd->master_lch, &temp);
  168. return 0;
  169. }
  170. static snd_pcm_uframes_t
  171. davinci_pcm_pointer(struct snd_pcm_substream *substream)
  172. {
  173. struct snd_pcm_runtime *runtime = substream->runtime;
  174. struct davinci_runtime_data *prtd = runtime->private_data;
  175. unsigned int offset;
  176. dma_addr_t count;
  177. dma_addr_t src, dst;
  178. spin_lock(&prtd->lock);
  179. davinci_dma_getposition(prtd->master_lch, &src, &dst);
  180. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  181. count = src - runtime->dma_addr;
  182. else
  183. count = dst - runtime->dma_addr;;
  184. spin_unlock(&prtd->lock);
  185. offset = bytes_to_frames(runtime, count);
  186. if (offset >= runtime->buffer_size)
  187. offset = 0;
  188. return offset;
  189. }
  190. static int davinci_pcm_open(struct snd_pcm_substream *substream)
  191. {
  192. struct snd_pcm_runtime *runtime = substream->runtime;
  193. struct davinci_runtime_data *prtd;
  194. int ret = 0;
  195. snd_soc_set_runtime_hwparams(substream, &davinci_pcm_hardware);
  196. prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
  197. if (prtd == NULL)
  198. return -ENOMEM;
  199. spin_lock_init(&prtd->lock);
  200. runtime->private_data = prtd;
  201. ret = davinci_pcm_dma_request(substream);
  202. if (ret) {
  203. printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
  204. kfree(prtd);
  205. }
  206. return ret;
  207. }
  208. static int davinci_pcm_close(struct snd_pcm_substream *substream)
  209. {
  210. struct snd_pcm_runtime *runtime = substream->runtime;
  211. struct davinci_runtime_data *prtd = runtime->private_data;
  212. davinci_dma_unlink_lch(prtd->slave_lch, prtd->slave_lch);
  213. davinci_free_dma(prtd->slave_lch);
  214. davinci_free_dma(prtd->master_lch);
  215. kfree(prtd);
  216. return 0;
  217. }
  218. static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
  219. struct snd_pcm_hw_params *hw_params)
  220. {
  221. return snd_pcm_lib_malloc_pages(substream,
  222. params_buffer_bytes(hw_params));
  223. }
  224. static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
  225. {
  226. return snd_pcm_lib_free_pages(substream);
  227. }
  228. static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
  229. struct vm_area_struct *vma)
  230. {
  231. struct snd_pcm_runtime *runtime = substream->runtime;
  232. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  233. runtime->dma_area,
  234. runtime->dma_addr,
  235. runtime->dma_bytes);
  236. }
  237. struct snd_pcm_ops davinci_pcm_ops = {
  238. .open = davinci_pcm_open,
  239. .close = davinci_pcm_close,
  240. .ioctl = snd_pcm_lib_ioctl,
  241. .hw_params = davinci_pcm_hw_params,
  242. .hw_free = davinci_pcm_hw_free,
  243. .prepare = davinci_pcm_prepare,
  244. .trigger = davinci_pcm_trigger,
  245. .pointer = davinci_pcm_pointer,
  246. .mmap = davinci_pcm_mmap,
  247. };
  248. static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  249. {
  250. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  251. struct snd_dma_buffer *buf = &substream->dma_buffer;
  252. size_t size = davinci_pcm_hardware.buffer_bytes_max;
  253. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  254. buf->dev.dev = pcm->card->dev;
  255. buf->private_data = NULL;
  256. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  257. &buf->addr, GFP_KERNEL);
  258. DPRINTK("preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
  259. (void *) buf->area, (void *) buf->addr, size);
  260. if (!buf->area)
  261. return -ENOMEM;
  262. buf->bytes = size;
  263. return 0;
  264. }
  265. static void davinci_pcm_free(struct snd_pcm *pcm)
  266. {
  267. struct snd_pcm_substream *substream;
  268. struct snd_dma_buffer *buf;
  269. int stream;
  270. for (stream = 0; stream < 2; stream++) {
  271. substream = pcm->streams[stream].substream;
  272. if (!substream)
  273. continue;
  274. buf = &substream->dma_buffer;
  275. if (!buf->area)
  276. continue;
  277. dma_free_writecombine(pcm->card->dev, buf->bytes,
  278. buf->area, buf->addr);
  279. buf->area = NULL;
  280. }
  281. }
  282. static u64 davinci_pcm_dmamask = 0xffffffff;
  283. static int davinci_pcm_new(struct snd_card *card,
  284. struct snd_soc_dai *dai, struct snd_pcm *pcm)
  285. {
  286. int ret;
  287. if (!card->dev->dma_mask)
  288. card->dev->dma_mask = &davinci_pcm_dmamask;
  289. if (!card->dev->coherent_dma_mask)
  290. card->dev->coherent_dma_mask = 0xffffffff;
  291. if (dai->playback.channels_min) {
  292. ret = davinci_pcm_preallocate_dma_buffer(pcm,
  293. SNDRV_PCM_STREAM_PLAYBACK);
  294. if (ret)
  295. return ret;
  296. }
  297. if (dai->capture.channels_min) {
  298. ret = davinci_pcm_preallocate_dma_buffer(pcm,
  299. SNDRV_PCM_STREAM_CAPTURE);
  300. if (ret)
  301. return ret;
  302. }
  303. return 0;
  304. }
  305. struct snd_soc_platform davinci_soc_platform = {
  306. .name = "davinci-audio",
  307. .pcm_ops = &davinci_pcm_ops,
  308. .pcm_new = davinci_pcm_new,
  309. .pcm_free = davinci_pcm_free,
  310. };
  311. EXPORT_SYMBOL_GPL(davinci_soc_platform);
  312. MODULE_AUTHOR("Vladimir Barinov");
  313. MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
  314. MODULE_LICENSE("GPL");