imx-pcm-dma-mx2.c 8.5 KB

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