imx-pcm-fiq.c 7.1 KB

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