imx-pcm-fiq.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * imx-pcm-fiq.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/dmaengine_pcm.h>
  25. #include <sound/initval.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <asm/fiq.h>
  30. #include <linux/platform_data/asoc-imx-ssi.h>
  31. #include "imx-ssi.h"
  32. #include "imx-pcm.h"
  33. struct imx_pcm_runtime_data {
  34. unsigned int period;
  35. int periods;
  36. unsigned long offset;
  37. struct hrtimer hrt;
  38. int poll_time_ns;
  39. struct snd_pcm_substream *substream;
  40. atomic_t playing;
  41. atomic_t capturing;
  42. };
  43. static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt)
  44. {
  45. struct imx_pcm_runtime_data *iprtd =
  46. container_of(hrt, struct imx_pcm_runtime_data, hrt);
  47. struct snd_pcm_substream *substream = iprtd->substream;
  48. struct pt_regs regs;
  49. if (!atomic_read(&iprtd->playing) && !atomic_read(&iprtd->capturing))
  50. return HRTIMER_NORESTART;
  51. get_fiq_regs(&regs);
  52. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  53. iprtd->offset = regs.ARM_r8 & 0xffff;
  54. else
  55. iprtd->offset = regs.ARM_r9 & 0xffff;
  56. snd_pcm_period_elapsed(substream);
  57. hrtimer_forward_now(hrt, ns_to_ktime(iprtd->poll_time_ns));
  58. return HRTIMER_RESTART;
  59. }
  60. static struct fiq_handler fh = {
  61. .name = DRV_NAME,
  62. };
  63. static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
  64. struct snd_pcm_hw_params *params)
  65. {
  66. struct snd_pcm_runtime *runtime = substream->runtime;
  67. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  68. iprtd->periods = params_periods(params);
  69. iprtd->period = params_period_bytes(params);
  70. iprtd->offset = 0;
  71. iprtd->poll_time_ns = 1000000000 / params_rate(params) *
  72. params_period_size(params);
  73. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  74. return 0;
  75. }
  76. static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
  77. {
  78. struct snd_pcm_runtime *runtime = substream->runtime;
  79. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  80. struct pt_regs regs;
  81. get_fiq_regs(&regs);
  82. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  83. regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16;
  84. else
  85. regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16;
  86. set_fiq_regs(&regs);
  87. return 0;
  88. }
  89. static int imx_pcm_fiq;
  90. static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  91. {
  92. struct snd_pcm_runtime *runtime = substream->runtime;
  93. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  94. switch (cmd) {
  95. case SNDRV_PCM_TRIGGER_START:
  96. case SNDRV_PCM_TRIGGER_RESUME:
  97. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  98. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  99. atomic_set(&iprtd->playing, 1);
  100. else
  101. atomic_set(&iprtd->capturing, 1);
  102. hrtimer_start(&iprtd->hrt, ns_to_ktime(iprtd->poll_time_ns),
  103. HRTIMER_MODE_REL);
  104. enable_fiq(imx_pcm_fiq);
  105. break;
  106. case SNDRV_PCM_TRIGGER_STOP:
  107. case SNDRV_PCM_TRIGGER_SUSPEND:
  108. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  109. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  110. atomic_set(&iprtd->playing, 0);
  111. else
  112. atomic_set(&iprtd->capturing, 0);
  113. if (!atomic_read(&iprtd->playing) &&
  114. !atomic_read(&iprtd->capturing))
  115. disable_fiq(imx_pcm_fiq);
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. return 0;
  121. }
  122. static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
  123. {
  124. struct snd_pcm_runtime *runtime = substream->runtime;
  125. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  126. return bytes_to_frames(substream->runtime, iprtd->offset);
  127. }
  128. static struct snd_pcm_hardware snd_imx_hardware = {
  129. .info = SNDRV_PCM_INFO_INTERLEAVED |
  130. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  131. SNDRV_PCM_INFO_MMAP |
  132. SNDRV_PCM_INFO_MMAP_VALID |
  133. SNDRV_PCM_INFO_PAUSE |
  134. SNDRV_PCM_INFO_RESUME,
  135. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  136. .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
  137. .period_bytes_min = 128,
  138. .period_bytes_max = 16 * 1024,
  139. .periods_min = 4,
  140. .periods_max = 255,
  141. .fifo_size = 0,
  142. };
  143. static int snd_imx_open(struct snd_pcm_substream *substream)
  144. {
  145. struct snd_pcm_runtime *runtime = substream->runtime;
  146. struct imx_pcm_runtime_data *iprtd;
  147. int ret;
  148. iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
  149. if (iprtd == NULL)
  150. return -ENOMEM;
  151. runtime->private_data = iprtd;
  152. iprtd->substream = substream;
  153. atomic_set(&iprtd->playing, 0);
  154. atomic_set(&iprtd->capturing, 0);
  155. hrtimer_init(&iprtd->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  156. iprtd->hrt.function = snd_hrtimer_callback;
  157. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  158. SNDRV_PCM_HW_PARAM_PERIODS);
  159. if (ret < 0) {
  160. kfree(iprtd);
  161. return ret;
  162. }
  163. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  164. return 0;
  165. }
  166. static int snd_imx_close(struct snd_pcm_substream *substream)
  167. {
  168. struct snd_pcm_runtime *runtime = substream->runtime;
  169. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  170. hrtimer_cancel(&iprtd->hrt);
  171. kfree(iprtd);
  172. return 0;
  173. }
  174. static int snd_imx_pcm_mmap(struct snd_pcm_substream *substream,
  175. struct vm_area_struct *vma)
  176. {
  177. struct snd_pcm_runtime *runtime = substream->runtime;
  178. int ret;
  179. ret = dma_mmap_wc(substream->pcm->card->dev, vma, runtime->dma_area,
  180. runtime->dma_addr, runtime->dma_bytes);
  181. pr_debug("%s: ret: %d %p %pad 0x%08zx\n", __func__, ret,
  182. runtime->dma_area,
  183. &runtime->dma_addr,
  184. runtime->dma_bytes);
  185. return ret;
  186. }
  187. static struct snd_pcm_ops imx_pcm_ops = {
  188. .open = snd_imx_open,
  189. .close = snd_imx_close,
  190. .ioctl = snd_pcm_lib_ioctl,
  191. .hw_params = snd_imx_pcm_hw_params,
  192. .prepare = snd_imx_pcm_prepare,
  193. .trigger = snd_imx_pcm_trigger,
  194. .pointer = snd_imx_pcm_pointer,
  195. .mmap = snd_imx_pcm_mmap,
  196. };
  197. static int imx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  198. {
  199. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  200. struct snd_dma_buffer *buf = &substream->dma_buffer;
  201. size_t size = IMX_SSI_DMABUF_SIZE;
  202. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  203. buf->dev.dev = pcm->card->dev;
  204. buf->private_data = NULL;
  205. buf->area = dma_alloc_wc(pcm->card->dev, size, &buf->addr, GFP_KERNEL);
  206. if (!buf->area)
  207. return -ENOMEM;
  208. buf->bytes = size;
  209. return 0;
  210. }
  211. static int imx_pcm_new(struct snd_soc_pcm_runtime *rtd)
  212. {
  213. struct snd_card *card = rtd->card->snd_card;
  214. struct snd_pcm *pcm = rtd->pcm;
  215. int ret;
  216. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  217. if (ret)
  218. return ret;
  219. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  220. ret = imx_pcm_preallocate_dma_buffer(pcm,
  221. SNDRV_PCM_STREAM_PLAYBACK);
  222. if (ret)
  223. return ret;
  224. }
  225. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  226. ret = imx_pcm_preallocate_dma_buffer(pcm,
  227. SNDRV_PCM_STREAM_CAPTURE);
  228. if (ret)
  229. return ret;
  230. }
  231. return 0;
  232. }
  233. static int ssi_irq = 0;
  234. static int imx_pcm_fiq_new(struct snd_soc_pcm_runtime *rtd)
  235. {
  236. struct snd_pcm *pcm = rtd->pcm;
  237. struct snd_pcm_substream *substream;
  238. int ret;
  239. ret = imx_pcm_new(rtd);
  240. if (ret)
  241. return ret;
  242. substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  243. if (substream) {
  244. struct snd_dma_buffer *buf = &substream->dma_buffer;
  245. imx_ssi_fiq_tx_buffer = (unsigned long)buf->area;
  246. }
  247. substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  248. if (substream) {
  249. struct snd_dma_buffer *buf = &substream->dma_buffer;
  250. imx_ssi_fiq_rx_buffer = (unsigned long)buf->area;
  251. }
  252. set_fiq_handler(&imx_ssi_fiq_start,
  253. &imx_ssi_fiq_end - &imx_ssi_fiq_start);
  254. return 0;
  255. }
  256. static void imx_pcm_free(struct snd_pcm *pcm)
  257. {
  258. struct snd_pcm_substream *substream;
  259. struct snd_dma_buffer *buf;
  260. int stream;
  261. for (stream = 0; stream < 2; stream++) {
  262. substream = pcm->streams[stream].substream;
  263. if (!substream)
  264. continue;
  265. buf = &substream->dma_buffer;
  266. if (!buf->area)
  267. continue;
  268. dma_free_wc(pcm->card->dev, buf->bytes, buf->area, buf->addr);
  269. buf->area = NULL;
  270. }
  271. }
  272. static void imx_pcm_fiq_free(struct snd_pcm *pcm)
  273. {
  274. mxc_set_irq_fiq(ssi_irq, 0);
  275. release_fiq(&fh);
  276. imx_pcm_free(pcm);
  277. }
  278. static struct snd_soc_platform_driver imx_soc_platform_fiq = {
  279. .ops = &imx_pcm_ops,
  280. .pcm_new = imx_pcm_fiq_new,
  281. .pcm_free = imx_pcm_fiq_free,
  282. };
  283. int imx_pcm_fiq_init(struct platform_device *pdev,
  284. struct imx_pcm_fiq_params *params)
  285. {
  286. int ret;
  287. ret = claim_fiq(&fh);
  288. if (ret) {
  289. dev_err(&pdev->dev, "failed to claim fiq: %d", ret);
  290. return ret;
  291. }
  292. mxc_set_irq_fiq(params->irq, 1);
  293. ssi_irq = params->irq;
  294. imx_pcm_fiq = params->irq;
  295. imx_ssi_fiq_base = (unsigned long)params->base;
  296. params->dma_params_tx->maxburst = 4;
  297. params->dma_params_rx->maxburst = 6;
  298. ret = snd_soc_register_platform(&pdev->dev, &imx_soc_platform_fiq);
  299. if (ret)
  300. goto failed_register;
  301. return 0;
  302. failed_register:
  303. mxc_set_irq_fiq(ssi_irq, 0);
  304. release_fiq(&fh);
  305. return ret;
  306. }
  307. EXPORT_SYMBOL_GPL(imx_pcm_fiq_init);
  308. void imx_pcm_fiq_exit(struct platform_device *pdev)
  309. {
  310. snd_soc_unregister_platform(&pdev->dev);
  311. }
  312. EXPORT_SYMBOL_GPL(imx_pcm_fiq_exit);
  313. MODULE_LICENSE("GPL");