imx-pcm-dma-mx2.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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;
  74. struct snd_pcm_runtime *runtime = substream->runtime;
  75. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  76. int ret;
  77. dma_params = snd_soc_get_dma_data(rtd->dai->cpu_dai, substream);
  78. iprtd->dma = imx_dma_request_by_prio(DRV_NAME, DMA_PRIO_HIGH);
  79. if (iprtd->dma < 0) {
  80. pr_err("Failed to claim the audio DMA\n");
  81. return -ENODEV;
  82. }
  83. ret = imx_dma_setup_handlers(iprtd->dma,
  84. imx_ssi_dma_callback,
  85. snd_imx_dma_err_callback, substream);
  86. if (ret)
  87. goto out;
  88. ret = imx_dma_setup_progression_handler(iprtd->dma,
  89. imx_ssi_dma_progression);
  90. if (ret) {
  91. pr_err("Failed to setup the DMA handler\n");
  92. goto out;
  93. }
  94. ret = imx_dma_config_channel(iprtd->dma,
  95. IMX_DMA_MEMSIZE_16 | IMX_DMA_TYPE_FIFO,
  96. IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
  97. dma_params->dma, 1);
  98. if (ret < 0) {
  99. pr_err("Cannot configure DMA channel: %d\n", ret);
  100. goto out;
  101. }
  102. imx_dma_config_burstlen(iprtd->dma, dma_params->burstsize * 2);
  103. return 0;
  104. out:
  105. imx_dma_free(iprtd->dma);
  106. return ret;
  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_pcm_runtime *runtime = substream->runtime;
  112. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  113. int i;
  114. unsigned long dma_addr;
  115. imx_ssi_dma_alloc(substream);
  116. iprtd->size = params_buffer_bytes(params);
  117. iprtd->periods = params_periods(params);
  118. iprtd->period = params_period_bytes(params);
  119. iprtd->offset = 0;
  120. iprtd->period_time = HZ / (params_rate(params) /
  121. params_period_size(params));
  122. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  123. if (iprtd->sg_count != iprtd->periods) {
  124. kfree(iprtd->sg_list);
  125. iprtd->sg_list = kcalloc(iprtd->periods + 1,
  126. sizeof(struct scatterlist), GFP_KERNEL);
  127. if (!iprtd->sg_list)
  128. return -ENOMEM;
  129. iprtd->sg_count = iprtd->periods + 1;
  130. }
  131. sg_init_table(iprtd->sg_list, iprtd->sg_count);
  132. dma_addr = runtime->dma_addr;
  133. for (i = 0; i < iprtd->periods; i++) {
  134. iprtd->sg_list[i].page_link = 0;
  135. iprtd->sg_list[i].offset = 0;
  136. iprtd->sg_list[i].dma_address = dma_addr;
  137. iprtd->sg_list[i].length = iprtd->period;
  138. dma_addr += iprtd->period;
  139. }
  140. /* close the loop */
  141. iprtd->sg_list[iprtd->sg_count - 1].offset = 0;
  142. iprtd->sg_list[iprtd->sg_count - 1].length = 0;
  143. iprtd->sg_list[iprtd->sg_count - 1].page_link =
  144. ((unsigned long) iprtd->sg_list | 0x01) & ~0x02;
  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 >= 0) {
  152. imx_dma_free(iprtd->dma);
  153. iprtd->dma = -EINVAL;
  154. }
  155. kfree(iprtd->sg_list);
  156. iprtd->sg_list = NULL;
  157. return 0;
  158. }
  159. static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
  160. {
  161. struct snd_pcm_runtime *runtime = substream->runtime;
  162. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  163. struct imx_pcm_dma_params *dma_params;
  164. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  165. int err;
  166. dma_params = snd_soc_get_dma_data(rtd->dai->cpu_dai, substream);
  167. iprtd->substream = substream;
  168. iprtd->buf = (unsigned int *)substream->dma_buffer.area;
  169. iprtd->period_cnt = 0;
  170. pr_debug("%s: buf: %p period: %d periods: %d\n",
  171. __func__, iprtd->buf, iprtd->period, iprtd->periods);
  172. err = imx_dma_setup_sg(iprtd->dma, iprtd->sg_list, iprtd->sg_count,
  173. IMX_DMA_LENGTH_LOOP, dma_params->dma_addr,
  174. substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  175. DMA_MODE_WRITE : DMA_MODE_READ);
  176. if (err)
  177. return err;
  178. return 0;
  179. }
  180. static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  181. {
  182. struct snd_pcm_runtime *runtime = substream->runtime;
  183. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  184. switch (cmd) {
  185. case SNDRV_PCM_TRIGGER_START:
  186. case SNDRV_PCM_TRIGGER_RESUME:
  187. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  188. imx_dma_enable(iprtd->dma);
  189. break;
  190. case SNDRV_PCM_TRIGGER_STOP:
  191. case SNDRV_PCM_TRIGGER_SUSPEND:
  192. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  193. imx_dma_disable(iprtd->dma);
  194. break;
  195. default:
  196. return -EINVAL;
  197. }
  198. return 0;
  199. }
  200. static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
  201. {
  202. struct snd_pcm_runtime *runtime = substream->runtime;
  203. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  204. return bytes_to_frames(substream->runtime, iprtd->offset);
  205. }
  206. static struct snd_pcm_hardware snd_imx_hardware = {
  207. .info = SNDRV_PCM_INFO_INTERLEAVED |
  208. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  209. SNDRV_PCM_INFO_MMAP |
  210. SNDRV_PCM_INFO_MMAP_VALID |
  211. SNDRV_PCM_INFO_PAUSE |
  212. SNDRV_PCM_INFO_RESUME,
  213. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  214. .rate_min = 8000,
  215. .channels_min = 2,
  216. .channels_max = 2,
  217. .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
  218. .period_bytes_min = 128,
  219. .period_bytes_max = 16 * 1024,
  220. .periods_min = 2,
  221. .periods_max = 255,
  222. .fifo_size = 0,
  223. };
  224. static int snd_imx_open(struct snd_pcm_substream *substream)
  225. {
  226. struct snd_pcm_runtime *runtime = substream->runtime;
  227. struct imx_pcm_runtime_data *iprtd;
  228. int ret;
  229. iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
  230. runtime->private_data = iprtd;
  231. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  232. SNDRV_PCM_HW_PARAM_PERIODS);
  233. if (ret < 0)
  234. return ret;
  235. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  236. return 0;
  237. }
  238. static struct snd_pcm_ops imx_pcm_ops = {
  239. .open = snd_imx_open,
  240. .ioctl = snd_pcm_lib_ioctl,
  241. .hw_params = snd_imx_pcm_hw_params,
  242. .hw_free = snd_imx_pcm_hw_free,
  243. .prepare = snd_imx_pcm_prepare,
  244. .trigger = snd_imx_pcm_trigger,
  245. .pointer = snd_imx_pcm_pointer,
  246. .mmap = snd_imx_pcm_mmap,
  247. };
  248. static struct snd_soc_platform imx_soc_platform_dma = {
  249. .name = "imx-audio",
  250. .pcm_ops = &imx_pcm_ops,
  251. .pcm_new = imx_pcm_new,
  252. .pcm_free = imx_pcm_free,
  253. };
  254. struct snd_soc_platform *imx_ssi_dma_mx2_init(struct platform_device *pdev,
  255. struct imx_ssi *ssi)
  256. {
  257. ssi->dma_params_tx.burstsize = DMA_TXFIFO_BURST;
  258. ssi->dma_params_rx.burstsize = DMA_RXFIFO_BURST;
  259. return &imx_soc_platform_dma;
  260. }