imx-pcm-dma-mx2.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * imx-pcm-dma-mx2.c -- ALSA Soc Audio Layer
  3. *
  4. * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
  5. *
  6. * This code is based on code copyrighted by Freescale,
  7. * Liam Girdwood, Javier Martin and probably others.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/types.h>
  25. #include <sound/core.h>
  26. #include <sound/initval.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/soc.h>
  30. #include <mach/dma.h>
  31. #include "imx-ssi.h"
  32. struct imx_pcm_runtime_data {
  33. int period_bytes;
  34. int periods;
  35. int dma;
  36. unsigned long offset;
  37. unsigned long size;
  38. void *buf;
  39. int period_time;
  40. struct dma_async_tx_descriptor *desc;
  41. struct dma_chan *dma_chan;
  42. struct imx_dma_data dma_data;
  43. };
  44. static void audio_dma_irq(void *data)
  45. {
  46. struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data;
  47. struct snd_pcm_runtime *runtime = substream->runtime;
  48. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  49. iprtd->offset += iprtd->period_bytes;
  50. iprtd->offset %= iprtd->period_bytes * iprtd->periods;
  51. snd_pcm_period_elapsed(substream);
  52. }
  53. static bool filter(struct dma_chan *chan, void *param)
  54. {
  55. struct imx_pcm_runtime_data *iprtd = param;
  56. if (!imx_dma_is_general_purpose(chan))
  57. return false;
  58. chan->private = &iprtd->dma_data;
  59. return true;
  60. }
  61. static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream,
  62. struct snd_pcm_hw_params *params)
  63. {
  64. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  65. struct imx_pcm_dma_params *dma_params;
  66. struct snd_pcm_runtime *runtime = substream->runtime;
  67. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  68. struct dma_slave_config slave_config;
  69. dma_cap_mask_t mask;
  70. enum dma_slave_buswidth buswidth;
  71. int ret;
  72. dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  73. iprtd->dma_data.peripheral_type = IMX_DMATYPE_SSI;
  74. iprtd->dma_data.priority = DMA_PRIO_HIGH;
  75. iprtd->dma_data.dma_request = dma_params->dma;
  76. /* Try to grab a DMA channel */
  77. if (!iprtd->dma_chan) {
  78. dma_cap_zero(mask);
  79. dma_cap_set(DMA_SLAVE, mask);
  80. iprtd->dma_chan = dma_request_channel(mask, filter, iprtd);
  81. if (!iprtd->dma_chan)
  82. return -EINVAL;
  83. }
  84. switch (params_format(params)) {
  85. case SNDRV_PCM_FORMAT_S16_LE:
  86. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  87. break;
  88. case SNDRV_PCM_FORMAT_S20_3LE:
  89. case SNDRV_PCM_FORMAT_S24_LE:
  90. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  91. break;
  92. default:
  93. return 0;
  94. }
  95. slave_config.device_fc = false;
  96. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  97. slave_config.direction = DMA_MEM_TO_DEV;
  98. slave_config.dst_addr = dma_params->dma_addr;
  99. slave_config.dst_addr_width = buswidth;
  100. slave_config.dst_maxburst = dma_params->burstsize;
  101. } else {
  102. slave_config.direction = DMA_DEV_TO_MEM;
  103. slave_config.src_addr = dma_params->dma_addr;
  104. slave_config.src_addr_width = buswidth;
  105. slave_config.src_maxburst = dma_params->burstsize;
  106. }
  107. ret = dmaengine_slave_config(iprtd->dma_chan, &slave_config);
  108. if (ret)
  109. return ret;
  110. return 0;
  111. }
  112. static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
  113. struct snd_pcm_hw_params *params)
  114. {
  115. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  116. struct snd_pcm_runtime *runtime = substream->runtime;
  117. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  118. unsigned long dma_addr;
  119. struct dma_chan *chan;
  120. struct imx_pcm_dma_params *dma_params;
  121. int ret;
  122. dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  123. ret = imx_ssi_dma_alloc(substream, params);
  124. if (ret)
  125. return ret;
  126. chan = iprtd->dma_chan;
  127. iprtd->size = params_buffer_bytes(params);
  128. iprtd->periods = params_periods(params);
  129. iprtd->period_bytes = params_period_bytes(params);
  130. iprtd->offset = 0;
  131. iprtd->period_time = HZ / (params_rate(params) /
  132. params_period_size(params));
  133. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  134. dma_addr = runtime->dma_addr;
  135. iprtd->buf = (unsigned int *)substream->dma_buffer.area;
  136. iprtd->desc = chan->device->device_prep_dma_cyclic(chan, dma_addr,
  137. iprtd->period_bytes * iprtd->periods,
  138. iprtd->period_bytes,
  139. substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  140. DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
  141. if (!iprtd->desc) {
  142. dev_err(&chan->dev->device, "cannot prepare slave dma\n");
  143. return -EINVAL;
  144. }
  145. iprtd->desc->callback = audio_dma_irq;
  146. iprtd->desc->callback_param = substream;
  147. return 0;
  148. }
  149. static int snd_imx_pcm_hw_free(struct snd_pcm_substream *substream)
  150. {
  151. struct snd_pcm_runtime *runtime = substream->runtime;
  152. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  153. if (iprtd->dma_chan) {
  154. dma_release_channel(iprtd->dma_chan);
  155. iprtd->dma_chan = NULL;
  156. }
  157. return 0;
  158. }
  159. static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
  160. {
  161. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  162. struct imx_pcm_dma_params *dma_params;
  163. dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  164. return 0;
  165. }
  166. static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  167. {
  168. struct snd_pcm_runtime *runtime = substream->runtime;
  169. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  170. switch (cmd) {
  171. case SNDRV_PCM_TRIGGER_START:
  172. case SNDRV_PCM_TRIGGER_RESUME:
  173. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  174. dmaengine_submit(iprtd->desc);
  175. dma_async_issue_pending(iprtd->dma_chan);
  176. break;
  177. case SNDRV_PCM_TRIGGER_STOP:
  178. case SNDRV_PCM_TRIGGER_SUSPEND:
  179. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  180. dmaengine_terminate_all(iprtd->dma_chan);
  181. break;
  182. default:
  183. return -EINVAL;
  184. }
  185. return 0;
  186. }
  187. static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
  188. {
  189. struct snd_pcm_runtime *runtime = substream->runtime;
  190. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  191. pr_debug("%s: %ld %ld\n", __func__, iprtd->offset,
  192. bytes_to_frames(substream->runtime, iprtd->offset));
  193. return bytes_to_frames(substream->runtime, iprtd->offset);
  194. }
  195. static struct snd_pcm_hardware snd_imx_hardware = {
  196. .info = SNDRV_PCM_INFO_INTERLEAVED |
  197. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  198. SNDRV_PCM_INFO_MMAP |
  199. SNDRV_PCM_INFO_MMAP_VALID |
  200. SNDRV_PCM_INFO_PAUSE |
  201. SNDRV_PCM_INFO_RESUME,
  202. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  203. .rate_min = 8000,
  204. .channels_min = 2,
  205. .channels_max = 2,
  206. .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
  207. .period_bytes_min = 128,
  208. .period_bytes_max = 65535, /* Limited by SDMA engine */
  209. .periods_min = 2,
  210. .periods_max = 255,
  211. .fifo_size = 0,
  212. };
  213. static int snd_imx_open(struct snd_pcm_substream *substream)
  214. {
  215. struct snd_pcm_runtime *runtime = substream->runtime;
  216. struct imx_pcm_runtime_data *iprtd;
  217. int ret;
  218. iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
  219. if (iprtd == NULL)
  220. return -ENOMEM;
  221. runtime->private_data = iprtd;
  222. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  223. SNDRV_PCM_HW_PARAM_PERIODS);
  224. if (ret < 0) {
  225. kfree(iprtd);
  226. return ret;
  227. }
  228. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  229. return 0;
  230. }
  231. static int snd_imx_close(struct snd_pcm_substream *substream)
  232. {
  233. struct snd_pcm_runtime *runtime = substream->runtime;
  234. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  235. kfree(iprtd);
  236. return 0;
  237. }
  238. static struct snd_pcm_ops imx_pcm_ops = {
  239. .open = snd_imx_open,
  240. .close = snd_imx_close,
  241. .ioctl = snd_pcm_lib_ioctl,
  242. .hw_params = snd_imx_pcm_hw_params,
  243. .hw_free = snd_imx_pcm_hw_free,
  244. .prepare = snd_imx_pcm_prepare,
  245. .trigger = snd_imx_pcm_trigger,
  246. .pointer = snd_imx_pcm_pointer,
  247. .mmap = snd_imx_pcm_mmap,
  248. };
  249. static struct snd_soc_platform_driver imx_soc_platform_mx2 = {
  250. .ops = &imx_pcm_ops,
  251. .pcm_new = imx_pcm_new,
  252. .pcm_free = imx_pcm_free,
  253. };
  254. static int __devinit imx_soc_platform_probe(struct platform_device *pdev)
  255. {
  256. struct imx_ssi *ssi = platform_get_drvdata(pdev);
  257. ssi->dma_params_tx.burstsize = 6;
  258. ssi->dma_params_rx.burstsize = 4;
  259. return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2);
  260. }
  261. static int __devexit imx_soc_platform_remove(struct platform_device *pdev)
  262. {
  263. snd_soc_unregister_platform(&pdev->dev);
  264. return 0;
  265. }
  266. static struct platform_driver imx_pcm_driver = {
  267. .driver = {
  268. .name = "imx-pcm-audio",
  269. .owner = THIS_MODULE,
  270. },
  271. .probe = imx_soc_platform_probe,
  272. .remove = __devexit_p(imx_soc_platform_remove),
  273. };
  274. module_platform_driver(imx_pcm_driver);
  275. MODULE_LICENSE("GPL");
  276. MODULE_ALIAS("platform:imx-pcm-audio");