imx-pcm-fiq.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/irqs.h>
  30. #include <mach/ssi.h>
  31. #include "imx-ssi.h"
  32. struct imx_pcm_runtime_data {
  33. int period;
  34. int periods;
  35. unsigned long offset;
  36. unsigned long last_offset;
  37. unsigned long size;
  38. struct hrtimer hrt;
  39. int poll_time_ns;
  40. struct snd_pcm_substream *substream;
  41. atomic_t running;
  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 snd_pcm_runtime *runtime = substream->runtime;
  49. struct pt_regs regs;
  50. unsigned long delta;
  51. if (!atomic_read(&iprtd->running))
  52. return HRTIMER_NORESTART;
  53. get_fiq_regs(&regs);
  54. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  55. iprtd->offset = regs.ARM_r8 & 0xffff;
  56. else
  57. iprtd->offset = regs.ARM_r9 & 0xffff;
  58. /* How much data have we transferred since the last period report? */
  59. if (iprtd->offset >= iprtd->last_offset)
  60. delta = iprtd->offset - iprtd->last_offset;
  61. else
  62. delta = runtime->buffer_size + iprtd->offset
  63. - iprtd->last_offset;
  64. /* If we've transferred at least a period then report it and
  65. * reset our poll time */
  66. if (delta >= iprtd->period) {
  67. snd_pcm_period_elapsed(substream);
  68. iprtd->last_offset = iprtd->offset;
  69. }
  70. hrtimer_forward_now(hrt, ns_to_ktime(iprtd->poll_time_ns));
  71. return HRTIMER_RESTART;
  72. }
  73. static struct fiq_handler fh = {
  74. .name = DRV_NAME,
  75. };
  76. static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
  77. struct snd_pcm_hw_params *params)
  78. {
  79. struct snd_pcm_runtime *runtime = substream->runtime;
  80. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  81. iprtd->size = params_buffer_bytes(params);
  82. iprtd->periods = params_periods(params);
  83. iprtd->period = params_period_bytes(params) ;
  84. iprtd->offset = 0;
  85. iprtd->last_offset = 0;
  86. iprtd->poll_time_ns = 1000000000 / params_rate(params) *
  87. params_period_size(params);
  88. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  89. return 0;
  90. }
  91. static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
  92. {
  93. struct snd_pcm_runtime *runtime = substream->runtime;
  94. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  95. struct pt_regs regs;
  96. get_fiq_regs(&regs);
  97. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  98. regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16;
  99. else
  100. regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16;
  101. set_fiq_regs(&regs);
  102. return 0;
  103. }
  104. static int fiq_enable;
  105. static int imx_pcm_fiq;
  106. static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  107. {
  108. struct snd_pcm_runtime *runtime = substream->runtime;
  109. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  110. switch (cmd) {
  111. case SNDRV_PCM_TRIGGER_START:
  112. case SNDRV_PCM_TRIGGER_RESUME:
  113. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  114. atomic_set(&iprtd->running, 1);
  115. hrtimer_start(&iprtd->hrt, ns_to_ktime(iprtd->poll_time_ns),
  116. HRTIMER_MODE_REL);
  117. if (++fiq_enable == 1)
  118. enable_fiq(imx_pcm_fiq);
  119. break;
  120. case SNDRV_PCM_TRIGGER_STOP:
  121. case SNDRV_PCM_TRIGGER_SUSPEND:
  122. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  123. atomic_set(&iprtd->running, 0);
  124. if (--fiq_enable == 0)
  125. disable_fiq(imx_pcm_fiq);
  126. break;
  127. default:
  128. return -EINVAL;
  129. }
  130. return 0;
  131. }
  132. static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
  133. {
  134. struct snd_pcm_runtime *runtime = substream->runtime;
  135. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  136. return bytes_to_frames(substream->runtime, iprtd->offset);
  137. }
  138. static struct snd_pcm_hardware snd_imx_hardware = {
  139. .info = SNDRV_PCM_INFO_INTERLEAVED |
  140. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  141. SNDRV_PCM_INFO_MMAP |
  142. SNDRV_PCM_INFO_MMAP_VALID |
  143. SNDRV_PCM_INFO_PAUSE |
  144. SNDRV_PCM_INFO_RESUME,
  145. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  146. .rate_min = 8000,
  147. .channels_min = 2,
  148. .channels_max = 2,
  149. .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
  150. .period_bytes_min = 128,
  151. .period_bytes_max = 16 * 1024,
  152. .periods_min = 4,
  153. .periods_max = 255,
  154. .fifo_size = 0,
  155. };
  156. static int snd_imx_open(struct snd_pcm_substream *substream)
  157. {
  158. struct snd_pcm_runtime *runtime = substream->runtime;
  159. struct imx_pcm_runtime_data *iprtd;
  160. int ret;
  161. iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
  162. if (iprtd == NULL)
  163. return -ENOMEM;
  164. runtime->private_data = iprtd;
  165. iprtd->substream = substream;
  166. atomic_set(&iprtd->running, 0);
  167. hrtimer_init(&iprtd->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  168. iprtd->hrt.function = snd_hrtimer_callback;
  169. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  170. SNDRV_PCM_HW_PARAM_PERIODS);
  171. if (ret < 0) {
  172. kfree(iprtd);
  173. return ret;
  174. }
  175. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  176. return 0;
  177. }
  178. static int snd_imx_close(struct snd_pcm_substream *substream)
  179. {
  180. struct snd_pcm_runtime *runtime = substream->runtime;
  181. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  182. hrtimer_cancel(&iprtd->hrt);
  183. kfree(iprtd);
  184. return 0;
  185. }
  186. static struct snd_pcm_ops imx_pcm_ops = {
  187. .open = snd_imx_open,
  188. .close = snd_imx_close,
  189. .ioctl = snd_pcm_lib_ioctl,
  190. .hw_params = snd_imx_pcm_hw_params,
  191. .prepare = snd_imx_pcm_prepare,
  192. .trigger = snd_imx_pcm_trigger,
  193. .pointer = snd_imx_pcm_pointer,
  194. .mmap = snd_imx_pcm_mmap,
  195. };
  196. static int ssi_irq = 0;
  197. static int imx_pcm_fiq_new(struct snd_soc_pcm_runtime *rtd)
  198. {
  199. struct snd_pcm *pcm = rtd->pcm;
  200. struct snd_pcm_substream *substream;
  201. int ret;
  202. ret = imx_pcm_new(rtd);
  203. if (ret)
  204. return ret;
  205. substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  206. if (substream) {
  207. struct snd_dma_buffer *buf = &substream->dma_buffer;
  208. imx_ssi_fiq_tx_buffer = (unsigned long)buf->area;
  209. }
  210. substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  211. if (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. module_platform_driver(imx_pcm_driver);
  268. MODULE_LICENSE("GPL");