imx-pcm-fiq.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/initval.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <asm/fiq.h>
  29. #include <mach/ssi.h>
  30. #include "imx-ssi.h"
  31. struct imx_pcm_runtime_data {
  32. int period;
  33. int periods;
  34. unsigned long offset;
  35. unsigned long last_offset;
  36. unsigned long size;
  37. struct hrtimer hrt;
  38. int poll_time_ns;
  39. struct snd_pcm_substream *substream;
  40. atomic_t running;
  41. };
  42. static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt)
  43. {
  44. struct imx_pcm_runtime_data *iprtd =
  45. container_of(hrt, struct imx_pcm_runtime_data, hrt);
  46. struct snd_pcm_substream *substream = iprtd->substream;
  47. struct snd_pcm_runtime *runtime = substream->runtime;
  48. struct pt_regs regs;
  49. unsigned long delta;
  50. if (!atomic_read(&iprtd->running))
  51. return HRTIMER_NORESTART;
  52. get_fiq_regs(&regs);
  53. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  54. iprtd->offset = regs.ARM_r8 & 0xffff;
  55. else
  56. iprtd->offset = regs.ARM_r9 & 0xffff;
  57. /* How much data have we transferred since the last period report? */
  58. if (iprtd->offset >= iprtd->last_offset)
  59. delta = iprtd->offset - iprtd->last_offset;
  60. else
  61. delta = runtime->buffer_size + iprtd->offset
  62. - iprtd->last_offset;
  63. /* If we've transferred at least a period then report it and
  64. * reset our poll time */
  65. if (delta >= iprtd->period) {
  66. snd_pcm_period_elapsed(substream);
  67. iprtd->last_offset = iprtd->offset;
  68. }
  69. hrtimer_forward_now(hrt, ns_to_ktime(iprtd->poll_time_ns));
  70. return HRTIMER_RESTART;
  71. }
  72. static struct fiq_handler fh = {
  73. .name = DRV_NAME,
  74. };
  75. static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
  76. struct snd_pcm_hw_params *params)
  77. {
  78. struct snd_pcm_runtime *runtime = substream->runtime;
  79. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  80. iprtd->size = params_buffer_bytes(params);
  81. iprtd->periods = params_periods(params);
  82. iprtd->period = params_period_bytes(params) ;
  83. iprtd->offset = 0;
  84. iprtd->last_offset = 0;
  85. iprtd->poll_time_ns = 1000000000 / params_rate(params) *
  86. params_period_size(params);
  87. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  88. return 0;
  89. }
  90. static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
  91. {
  92. struct snd_pcm_runtime *runtime = substream->runtime;
  93. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  94. struct pt_regs regs;
  95. get_fiq_regs(&regs);
  96. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  97. regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16;
  98. else
  99. regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16;
  100. set_fiq_regs(&regs);
  101. return 0;
  102. }
  103. static int fiq_enable;
  104. static int imx_pcm_fiq;
  105. static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  106. {
  107. struct snd_pcm_runtime *runtime = substream->runtime;
  108. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  109. switch (cmd) {
  110. case SNDRV_PCM_TRIGGER_START:
  111. case SNDRV_PCM_TRIGGER_RESUME:
  112. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  113. atomic_set(&iprtd->running, 1);
  114. hrtimer_start(&iprtd->hrt, ns_to_ktime(iprtd->poll_time_ns),
  115. HRTIMER_MODE_REL);
  116. if (++fiq_enable == 1)
  117. enable_fiq(imx_pcm_fiq);
  118. break;
  119. case SNDRV_PCM_TRIGGER_STOP:
  120. case SNDRV_PCM_TRIGGER_SUSPEND:
  121. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  122. atomic_set(&iprtd->running, 0);
  123. if (--fiq_enable == 0)
  124. disable_fiq(imx_pcm_fiq);
  125. break;
  126. default:
  127. return -EINVAL;
  128. }
  129. return 0;
  130. }
  131. static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
  132. {
  133. struct snd_pcm_runtime *runtime = substream->runtime;
  134. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  135. return bytes_to_frames(substream->runtime, iprtd->offset);
  136. }
  137. static struct snd_pcm_hardware snd_imx_hardware = {
  138. .info = SNDRV_PCM_INFO_INTERLEAVED |
  139. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  140. SNDRV_PCM_INFO_MMAP |
  141. SNDRV_PCM_INFO_MMAP_VALID |
  142. SNDRV_PCM_INFO_PAUSE |
  143. SNDRV_PCM_INFO_RESUME,
  144. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  145. .rate_min = 8000,
  146. .channels_min = 2,
  147. .channels_max = 2,
  148. .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
  149. .period_bytes_min = 128,
  150. .period_bytes_max = 16 * 1024,
  151. .periods_min = 4,
  152. .periods_max = 255,
  153. .fifo_size = 0,
  154. };
  155. static int snd_imx_open(struct snd_pcm_substream *substream)
  156. {
  157. struct snd_pcm_runtime *runtime = substream->runtime;
  158. struct imx_pcm_runtime_data *iprtd;
  159. int ret;
  160. iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
  161. if (iprtd == NULL)
  162. return -ENOMEM;
  163. runtime->private_data = iprtd;
  164. iprtd->substream = substream;
  165. atomic_set(&iprtd->running, 0);
  166. hrtimer_init(&iprtd->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  167. iprtd->hrt.function = snd_hrtimer_callback;
  168. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  169. SNDRV_PCM_HW_PARAM_PERIODS);
  170. if (ret < 0) {
  171. kfree(iprtd);
  172. return ret;
  173. }
  174. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  175. return 0;
  176. }
  177. static int snd_imx_close(struct snd_pcm_substream *substream)
  178. {
  179. struct snd_pcm_runtime *runtime = substream->runtime;
  180. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  181. hrtimer_cancel(&iprtd->hrt);
  182. kfree(iprtd);
  183. return 0;
  184. }
  185. static struct snd_pcm_ops imx_pcm_ops = {
  186. .open = snd_imx_open,
  187. .close = snd_imx_close,
  188. .ioctl = snd_pcm_lib_ioctl,
  189. .hw_params = snd_imx_pcm_hw_params,
  190. .prepare = snd_imx_pcm_prepare,
  191. .trigger = snd_imx_pcm_trigger,
  192. .pointer = snd_imx_pcm_pointer,
  193. .mmap = snd_imx_pcm_mmap,
  194. };
  195. static int ssi_irq = 0;
  196. static int imx_pcm_fiq_new(struct snd_card *card, struct snd_soc_dai *dai,
  197. struct snd_pcm *pcm)
  198. {
  199. int ret;
  200. ret = imx_pcm_new(card, dai, pcm);
  201. if (ret)
  202. return ret;
  203. if (dai->driver->playback.channels_min) {
  204. struct snd_pcm_substream *substream =
  205. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  206. struct snd_dma_buffer *buf = &substream->dma_buffer;
  207. imx_ssi_fiq_tx_buffer = (unsigned long)buf->area;
  208. }
  209. if (dai->driver->capture.channels_min) {
  210. struct snd_pcm_substream *substream =
  211. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  212. struct snd_dma_buffer *buf = &substream->dma_buffer;
  213. imx_ssi_fiq_rx_buffer = (unsigned long)buf->area;
  214. }
  215. set_fiq_handler(&imx_ssi_fiq_start,
  216. &imx_ssi_fiq_end - &imx_ssi_fiq_start);
  217. return 0;
  218. }
  219. static void imx_pcm_fiq_free(struct snd_pcm *pcm)
  220. {
  221. mxc_set_irq_fiq(ssi_irq, 0);
  222. release_fiq(&fh);
  223. imx_pcm_free(pcm);
  224. }
  225. static struct snd_soc_platform_driver imx_soc_platform_fiq = {
  226. .ops = &imx_pcm_ops,
  227. .pcm_new = imx_pcm_fiq_new,
  228. .pcm_free = imx_pcm_fiq_free,
  229. };
  230. static int __devinit imx_soc_platform_probe(struct platform_device *pdev)
  231. {
  232. struct imx_ssi *ssi = platform_get_drvdata(pdev);
  233. int ret;
  234. ret = claim_fiq(&fh);
  235. if (ret) {
  236. dev_err(&pdev->dev, "failed to claim fiq: %d", ret);
  237. return ret;
  238. }
  239. mxc_set_irq_fiq(ssi->irq, 1);
  240. ssi_irq = ssi->irq;
  241. imx_pcm_fiq = ssi->irq;
  242. imx_ssi_fiq_base = (unsigned long)ssi->base;
  243. ssi->dma_params_tx.burstsize = 4;
  244. ssi->dma_params_rx.burstsize = 6;
  245. ret = snd_soc_register_platform(&pdev->dev, &imx_soc_platform_fiq);
  246. if (ret)
  247. goto failed_register;
  248. return 0;
  249. failed_register:
  250. mxc_set_irq_fiq(ssi_irq, 0);
  251. release_fiq(&fh);
  252. return ret;
  253. }
  254. static int __devexit imx_soc_platform_remove(struct platform_device *pdev)
  255. {
  256. snd_soc_unregister_platform(&pdev->dev);
  257. return 0;
  258. }
  259. static struct platform_driver imx_pcm_driver = {
  260. .driver = {
  261. .name = "imx-fiq-pcm-audio",
  262. .owner = THIS_MODULE,
  263. },
  264. .probe = imx_soc_platform_probe,
  265. .remove = __devexit_p(imx_soc_platform_remove),
  266. };
  267. static int __init snd_imx_pcm_init(void)
  268. {
  269. return platform_driver_register(&imx_pcm_driver);
  270. }
  271. module_init(snd_imx_pcm_init);
  272. static void __exit snd_imx_pcm_exit(void)
  273. {
  274. platform_driver_unregister(&imx_pcm_driver);
  275. }
  276. module_exit(snd_imx_pcm_exit);