imx-pcm-dma-mx2.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <sound/core.h>
  23. #include <sound/initval.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <mach/dma-mx1-mx2.h>
  28. #include "imx-ssi.h"
  29. struct imx_pcm_runtime_data {
  30. int sg_count;
  31. struct scatterlist *sg_list;
  32. int period;
  33. int periods;
  34. unsigned long dma_addr;
  35. int dma;
  36. struct snd_pcm_substream *substream;
  37. unsigned long offset;
  38. unsigned long size;
  39. unsigned long period_cnt;
  40. void *buf;
  41. int period_time;
  42. };
  43. /* Called by the DMA framework when a period has elapsed */
  44. static void imx_ssi_dma_progression(int channel, void *data,
  45. struct scatterlist *sg)
  46. {
  47. struct snd_pcm_substream *substream = data;
  48. struct snd_pcm_runtime *runtime = substream->runtime;
  49. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  50. if (!sg)
  51. return;
  52. runtime = iprtd->substream->runtime;
  53. iprtd->offset = sg->dma_address - runtime->dma_addr;
  54. snd_pcm_period_elapsed(iprtd->substream);
  55. }
  56. static void imx_ssi_dma_callback(int channel, void *data)
  57. {
  58. pr_err("%s shouldn't be called\n", __func__);
  59. }
  60. static void snd_imx_dma_err_callback(int channel, void *data, int err)
  61. {
  62. pr_err("DMA error callback called\n");
  63. pr_err("DMA timeout on channel %d -%s%s%s%s\n",
  64. channel,
  65. err & IMX_DMA_ERR_BURST ? " burst" : "",
  66. err & IMX_DMA_ERR_REQUEST ? " request" : "",
  67. err & IMX_DMA_ERR_TRANSFER ? " transfer" : "",
  68. err & IMX_DMA_ERR_BUFFER ? " buffer" : "");
  69. }
  70. static int imx_ssi_dma_alloc(struct snd_pcm_substream *substream)
  71. {
  72. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  73. struct imx_pcm_dma_params *dma_params = rtd->dai->cpu_dai->dma_data;
  74. struct snd_pcm_runtime *runtime = substream->runtime;
  75. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  76. int ret;
  77. iprtd->dma = imx_dma_request_by_prio(DRV_NAME, DMA_PRIO_HIGH);
  78. if (iprtd->dma < 0) {
  79. pr_err("Failed to claim the audio DMA\n");
  80. return -ENODEV;
  81. }
  82. ret = imx_dma_setup_handlers(iprtd->dma,
  83. imx_ssi_dma_callback,
  84. snd_imx_dma_err_callback, substream);
  85. if (ret)
  86. goto out;
  87. ret = imx_dma_setup_progression_handler(iprtd->dma,
  88. imx_ssi_dma_progression);
  89. if (ret) {
  90. pr_err("Failed to setup the DMA handler\n");
  91. goto out;
  92. }
  93. ret = imx_dma_config_channel(iprtd->dma,
  94. IMX_DMA_MEMSIZE_16 | IMX_DMA_TYPE_FIFO,
  95. IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
  96. dma_params->dma, 1);
  97. if (ret < 0) {
  98. pr_err("Cannot configure DMA channel: %d\n", ret);
  99. goto out;
  100. }
  101. imx_dma_config_burstlen(iprtd->dma, dma_params->burstsize * 2);
  102. return 0;
  103. out:
  104. imx_dma_free(iprtd->dma);
  105. return ret;
  106. }
  107. static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
  108. struct snd_pcm_hw_params *params)
  109. {
  110. struct snd_pcm_runtime *runtime = substream->runtime;
  111. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  112. int i;
  113. unsigned long dma_addr;
  114. imx_ssi_dma_alloc(substream);
  115. iprtd->size = params_buffer_bytes(params);
  116. iprtd->periods = params_periods(params);
  117. iprtd->period = params_period_bytes(params);
  118. iprtd->offset = 0;
  119. iprtd->period_time = HZ / (params_rate(params) /
  120. params_period_size(params));
  121. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  122. if (iprtd->sg_count != iprtd->periods) {
  123. kfree(iprtd->sg_list);
  124. iprtd->sg_list = kcalloc(iprtd->periods + 1,
  125. sizeof(struct scatterlist), GFP_KERNEL);
  126. if (!iprtd->sg_list)
  127. return -ENOMEM;
  128. iprtd->sg_count = iprtd->periods + 1;
  129. }
  130. sg_init_table(iprtd->sg_list, iprtd->sg_count);
  131. dma_addr = runtime->dma_addr;
  132. for (i = 0; i < iprtd->periods; i++) {
  133. iprtd->sg_list[i].page_link = 0;
  134. iprtd->sg_list[i].offset = 0;
  135. iprtd->sg_list[i].dma_address = dma_addr;
  136. iprtd->sg_list[i].length = iprtd->period;
  137. dma_addr += iprtd->period;
  138. }
  139. /* close the loop */
  140. iprtd->sg_list[iprtd->sg_count - 1].offset = 0;
  141. iprtd->sg_list[iprtd->sg_count - 1].length = 0;
  142. iprtd->sg_list[iprtd->sg_count - 1].page_link =
  143. ((unsigned long) iprtd->sg_list | 0x01) & ~0x02;
  144. return 0;
  145. }
  146. static int snd_imx_pcm_hw_free(struct snd_pcm_substream *substream)
  147. {
  148. struct snd_pcm_runtime *runtime = substream->runtime;
  149. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  150. if (iprtd->dma >= 0) {
  151. imx_dma_free(iprtd->dma);
  152. iprtd->dma = -EINVAL;
  153. }
  154. kfree(iprtd->sg_list);
  155. iprtd->sg_list = NULL;
  156. return 0;
  157. }
  158. static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
  159. {
  160. struct snd_pcm_runtime *runtime = substream->runtime;
  161. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  162. struct imx_pcm_dma_params *dma_params = rtd->dai->cpu_dai->dma_data;
  163. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  164. int err;
  165. iprtd->substream = substream;
  166. iprtd->buf = (unsigned int *)substream->dma_buffer.area;
  167. iprtd->period_cnt = 0;
  168. pr_debug("%s: buf: %p period: %d periods: %d\n",
  169. __func__, iprtd->buf, iprtd->period, iprtd->periods);
  170. err = imx_dma_setup_sg(iprtd->dma, iprtd->sg_list, iprtd->sg_count,
  171. IMX_DMA_LENGTH_LOOP, dma_params->dma_addr,
  172. substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  173. DMA_MODE_WRITE : DMA_MODE_READ);
  174. if (err)
  175. return err;
  176. return 0;
  177. }
  178. static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  179. {
  180. struct snd_pcm_runtime *runtime = substream->runtime;
  181. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  182. switch (cmd) {
  183. case SNDRV_PCM_TRIGGER_START:
  184. case SNDRV_PCM_TRIGGER_RESUME:
  185. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  186. imx_dma_enable(iprtd->dma);
  187. break;
  188. case SNDRV_PCM_TRIGGER_STOP:
  189. case SNDRV_PCM_TRIGGER_SUSPEND:
  190. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  191. imx_dma_disable(iprtd->dma);
  192. break;
  193. default:
  194. return -EINVAL;
  195. }
  196. return 0;
  197. }
  198. static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
  199. {
  200. struct snd_pcm_runtime *runtime = substream->runtime;
  201. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  202. return bytes_to_frames(substream->runtime, iprtd->offset);
  203. }
  204. static struct snd_pcm_hardware snd_imx_hardware = {
  205. .info = SNDRV_PCM_INFO_INTERLEAVED |
  206. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  207. SNDRV_PCM_INFO_MMAP |
  208. SNDRV_PCM_INFO_MMAP_VALID |
  209. SNDRV_PCM_INFO_PAUSE |
  210. SNDRV_PCM_INFO_RESUME,
  211. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  212. .rate_min = 8000,
  213. .channels_min = 2,
  214. .channels_max = 2,
  215. .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
  216. .period_bytes_min = 128,
  217. .period_bytes_max = 16 * 1024,
  218. .periods_min = 2,
  219. .periods_max = 255,
  220. .fifo_size = 0,
  221. };
  222. static int snd_imx_open(struct snd_pcm_substream *substream)
  223. {
  224. struct snd_pcm_runtime *runtime = substream->runtime;
  225. struct imx_pcm_runtime_data *iprtd;
  226. int ret;
  227. iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
  228. runtime->private_data = iprtd;
  229. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  230. SNDRV_PCM_HW_PARAM_PERIODS);
  231. if (ret < 0)
  232. return ret;
  233. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  234. return 0;
  235. }
  236. static struct snd_pcm_ops imx_pcm_ops = {
  237. .open = snd_imx_open,
  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 imx_soc_platform_dma = {
  247. .name = "imx-audio",
  248. .pcm_ops = &imx_pcm_ops,
  249. .pcm_new = imx_pcm_new,
  250. .pcm_free = imx_pcm_free,
  251. };
  252. struct snd_soc_platform *imx_ssi_dma_mx2_init(struct platform_device *pdev,
  253. struct imx_ssi *ssi)
  254. {
  255. ssi->dma_params_tx.burstsize = DMA_TXFIFO_BURST;
  256. ssi->dma_params_rx.burstsize = DMA_RXFIFO_BURST;
  257. return &imx_soc_platform_dma;
  258. }