imx-pcm-dma-mx2.c 8.7 KB

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