davinci-pcm.c 9.9 KB

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