imx-pcm-fiq.c 7.3 KB

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