imx-pcm-dma-mx2.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. dma_cap_zero(mask);
  77. dma_cap_set(DMA_SLAVE, mask);
  78. iprtd->dma_chan = dma_request_channel(mask, filter, iprtd);
  79. if (!iprtd->dma_chan)
  80. return -EINVAL;
  81. switch (params_format(params)) {
  82. case SNDRV_PCM_FORMAT_S16_LE:
  83. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  84. break;
  85. case SNDRV_PCM_FORMAT_S20_3LE:
  86. case SNDRV_PCM_FORMAT_S24_LE:
  87. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  88. break;
  89. default:
  90. return 0;
  91. }
  92. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  93. slave_config.direction = DMA_TO_DEVICE;
  94. slave_config.dst_addr = dma_params->dma_addr;
  95. slave_config.dst_addr_width = buswidth;
  96. slave_config.dst_maxburst = dma_params->burstsize;
  97. } else {
  98. slave_config.direction = DMA_FROM_DEVICE;
  99. slave_config.src_addr = dma_params->dma_addr;
  100. slave_config.src_addr_width = buswidth;
  101. slave_config.src_maxburst = dma_params->burstsize;
  102. }
  103. ret = dmaengine_slave_config(iprtd->dma_chan, &slave_config);
  104. if (ret)
  105. return ret;
  106. return 0;
  107. }
  108. static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
  109. struct snd_pcm_hw_params *params)
  110. {
  111. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  112. struct snd_pcm_runtime *runtime = substream->runtime;
  113. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  114. unsigned long dma_addr;
  115. struct dma_chan *chan;
  116. struct imx_pcm_dma_params *dma_params;
  117. int ret;
  118. dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  119. ret = imx_ssi_dma_alloc(substream, params);
  120. if (ret)
  121. return ret;
  122. chan = iprtd->dma_chan;
  123. iprtd->size = params_buffer_bytes(params);
  124. iprtd->periods = params_periods(params);
  125. iprtd->period_bytes = params_period_bytes(params);
  126. iprtd->offset = 0;
  127. iprtd->period_time = HZ / (params_rate(params) /
  128. params_period_size(params));
  129. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  130. dma_addr = runtime->dma_addr;
  131. iprtd->buf = (unsigned int *)substream->dma_buffer.area;
  132. iprtd->desc = chan->device->device_prep_dma_cyclic(chan, dma_addr,
  133. iprtd->period_bytes * iprtd->periods,
  134. iprtd->period_bytes,
  135. substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  136. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  137. if (!iprtd->desc) {
  138. dev_err(&chan->dev->device, "cannot prepare slave dma\n");
  139. return -EINVAL;
  140. }
  141. iprtd->desc->callback = audio_dma_irq;
  142. iprtd->desc->callback_param = substream;
  143. return 0;
  144. }
  145. static int snd_imx_pcm_hw_free(struct snd_pcm_substream *substream)
  146. {
  147. struct snd_pcm_runtime *runtime = substream->runtime;
  148. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  149. if (iprtd->dma_chan) {
  150. dma_release_channel(iprtd->dma_chan);
  151. iprtd->dma_chan = NULL;
  152. }
  153. return 0;
  154. }
  155. static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
  156. {
  157. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  158. struct imx_pcm_dma_params *dma_params;
  159. dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  160. return 0;
  161. }
  162. static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  163. {
  164. struct snd_pcm_runtime *runtime = substream->runtime;
  165. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  166. switch (cmd) {
  167. case SNDRV_PCM_TRIGGER_START:
  168. case SNDRV_PCM_TRIGGER_RESUME:
  169. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  170. dmaengine_submit(iprtd->desc);
  171. break;
  172. case SNDRV_PCM_TRIGGER_STOP:
  173. case SNDRV_PCM_TRIGGER_SUSPEND:
  174. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  175. dmaengine_terminate_all(iprtd->dma_chan);
  176. break;
  177. default:
  178. return -EINVAL;
  179. }
  180. return 0;
  181. }
  182. static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
  183. {
  184. struct snd_pcm_runtime *runtime = substream->runtime;
  185. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  186. pr_debug("%s: %ld %ld\n", __func__, iprtd->offset,
  187. bytes_to_frames(substream->runtime, iprtd->offset));
  188. return bytes_to_frames(substream->runtime, iprtd->offset);
  189. }
  190. static struct snd_pcm_hardware snd_imx_hardware = {
  191. .info = SNDRV_PCM_INFO_INTERLEAVED |
  192. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  193. SNDRV_PCM_INFO_MMAP |
  194. SNDRV_PCM_INFO_MMAP_VALID |
  195. SNDRV_PCM_INFO_PAUSE |
  196. SNDRV_PCM_INFO_RESUME,
  197. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  198. .rate_min = 8000,
  199. .channels_min = 2,
  200. .channels_max = 2,
  201. .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
  202. .period_bytes_min = 128,
  203. .period_bytes_max = 65535, /* Limited by SDMA engine */
  204. .periods_min = 2,
  205. .periods_max = 255,
  206. .fifo_size = 0,
  207. };
  208. static int snd_imx_open(struct snd_pcm_substream *substream)
  209. {
  210. struct snd_pcm_runtime *runtime = substream->runtime;
  211. struct imx_pcm_runtime_data *iprtd;
  212. int ret;
  213. iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
  214. if (iprtd == NULL)
  215. return -ENOMEM;
  216. runtime->private_data = iprtd;
  217. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  218. SNDRV_PCM_HW_PARAM_PERIODS);
  219. if (ret < 0) {
  220. kfree(iprtd);
  221. return ret;
  222. }
  223. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  224. return 0;
  225. }
  226. static int snd_imx_close(struct snd_pcm_substream *substream)
  227. {
  228. struct snd_pcm_runtime *runtime = substream->runtime;
  229. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  230. kfree(iprtd);
  231. return 0;
  232. }
  233. static struct snd_pcm_ops imx_pcm_ops = {
  234. .open = snd_imx_open,
  235. .close = snd_imx_close,
  236. .ioctl = snd_pcm_lib_ioctl,
  237. .hw_params = snd_imx_pcm_hw_params,
  238. .hw_free = snd_imx_pcm_hw_free,
  239. .prepare = snd_imx_pcm_prepare,
  240. .trigger = snd_imx_pcm_trigger,
  241. .pointer = snd_imx_pcm_pointer,
  242. .mmap = snd_imx_pcm_mmap,
  243. };
  244. static struct snd_soc_platform_driver imx_soc_platform_mx2 = {
  245. .ops = &imx_pcm_ops,
  246. .pcm_new = imx_pcm_new,
  247. .pcm_free = imx_pcm_free,
  248. };
  249. static int __devinit imx_soc_platform_probe(struct platform_device *pdev)
  250. {
  251. return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2);
  252. }
  253. static int __devexit imx_soc_platform_remove(struct platform_device *pdev)
  254. {
  255. snd_soc_unregister_platform(&pdev->dev);
  256. return 0;
  257. }
  258. static struct platform_driver imx_pcm_driver = {
  259. .driver = {
  260. .name = "imx-pcm-audio",
  261. .owner = THIS_MODULE,
  262. },
  263. .probe = imx_soc_platform_probe,
  264. .remove = __devexit_p(imx_soc_platform_remove),
  265. };
  266. static int __init snd_imx_pcm_init(void)
  267. {
  268. return platform_driver_register(&imx_pcm_driver);
  269. }
  270. module_init(snd_imx_pcm_init);
  271. static void __exit snd_imx_pcm_exit(void)
  272. {
  273. platform_driver_unregister(&imx_pcm_driver);
  274. }
  275. module_exit(snd_imx_pcm_exit);