imx-pcm-dma-mx2.c 8.6 KB

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