imx-pcm-fiq.c 7.1 KB

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