imx-pcm-fiq.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 dma_addr;
  34. int dma;
  35. unsigned long offset;
  36. unsigned long size;
  37. unsigned long period_cnt;
  38. void *buf;
  39. struct timer_list timer;
  40. int period_time;
  41. };
  42. static void imx_ssi_timer_callback(unsigned long data)
  43. {
  44. struct snd_pcm_substream *substream = (void *)data;
  45. struct snd_pcm_runtime *runtime = substream->runtime;
  46. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  47. struct pt_regs regs;
  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. iprtd->timer.expires = jiffies + iprtd->period_time;
  54. add_timer(&iprtd->timer);
  55. snd_pcm_period_elapsed(substream);
  56. }
  57. static struct fiq_handler fh = {
  58. .name = DRV_NAME,
  59. };
  60. static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
  61. struct snd_pcm_hw_params *params)
  62. {
  63. struct snd_pcm_runtime *runtime = substream->runtime;
  64. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  65. iprtd->size = params_buffer_bytes(params);
  66. iprtd->periods = params_periods(params);
  67. iprtd->period = params_period_bytes(params);
  68. iprtd->offset = 0;
  69. iprtd->period_time = HZ / (params_rate(params) / params_period_size(params));
  70. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  71. return 0;
  72. }
  73. static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
  74. {
  75. struct snd_pcm_runtime *runtime = substream->runtime;
  76. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  77. struct pt_regs regs;
  78. get_fiq_regs(&regs);
  79. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  80. regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16;
  81. else
  82. regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16;
  83. set_fiq_regs(&regs);
  84. return 0;
  85. }
  86. static int fiq_enable;
  87. static int imx_pcm_fiq;
  88. static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  89. {
  90. struct snd_pcm_runtime *runtime = substream->runtime;
  91. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  92. switch (cmd) {
  93. case SNDRV_PCM_TRIGGER_START:
  94. case SNDRV_PCM_TRIGGER_RESUME:
  95. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  96. iprtd->timer.expires = jiffies + iprtd->period_time;
  97. add_timer(&iprtd->timer);
  98. if (++fiq_enable == 1)
  99. enable_fiq(imx_pcm_fiq);
  100. break;
  101. case SNDRV_PCM_TRIGGER_STOP:
  102. case SNDRV_PCM_TRIGGER_SUSPEND:
  103. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  104. del_timer(&iprtd->timer);
  105. if (--fiq_enable == 0)
  106. disable_fiq(imx_pcm_fiq);
  107. break;
  108. default:
  109. return -EINVAL;
  110. }
  111. return 0;
  112. }
  113. static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
  114. {
  115. struct snd_pcm_runtime *runtime = substream->runtime;
  116. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  117. return bytes_to_frames(substream->runtime, iprtd->offset);
  118. }
  119. static struct snd_pcm_hardware snd_imx_hardware = {
  120. .info = SNDRV_PCM_INFO_INTERLEAVED |
  121. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  122. SNDRV_PCM_INFO_MMAP |
  123. SNDRV_PCM_INFO_MMAP_VALID |
  124. SNDRV_PCM_INFO_PAUSE |
  125. SNDRV_PCM_INFO_RESUME,
  126. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  127. .rate_min = 8000,
  128. .channels_min = 2,
  129. .channels_max = 2,
  130. .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
  131. .period_bytes_min = 128,
  132. .period_bytes_max = 16 * 1024,
  133. .periods_min = 2,
  134. .periods_max = 255,
  135. .fifo_size = 0,
  136. };
  137. static int snd_imx_open(struct snd_pcm_substream *substream)
  138. {
  139. struct snd_pcm_runtime *runtime = substream->runtime;
  140. struct imx_pcm_runtime_data *iprtd;
  141. int ret;
  142. iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
  143. runtime->private_data = iprtd;
  144. init_timer(&iprtd->timer);
  145. iprtd->timer.data = (unsigned long)substream;
  146. iprtd->timer.function = imx_ssi_timer_callback;
  147. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  148. SNDRV_PCM_HW_PARAM_PERIODS);
  149. if (ret < 0)
  150. return ret;
  151. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  152. return 0;
  153. }
  154. static int snd_imx_close(struct snd_pcm_substream *substream)
  155. {
  156. struct snd_pcm_runtime *runtime = substream->runtime;
  157. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  158. del_timer_sync(&iprtd->timer);
  159. kfree(iprtd);
  160. return 0;
  161. }
  162. static struct snd_pcm_ops imx_pcm_ops = {
  163. .open = snd_imx_open,
  164. .close = snd_imx_close,
  165. .ioctl = snd_pcm_lib_ioctl,
  166. .hw_params = snd_imx_pcm_hw_params,
  167. .prepare = snd_imx_pcm_prepare,
  168. .trigger = snd_imx_pcm_trigger,
  169. .pointer = snd_imx_pcm_pointer,
  170. .mmap = snd_imx_pcm_mmap,
  171. };
  172. static int imx_pcm_fiq_new(struct snd_card *card, struct snd_soc_dai *dai,
  173. struct snd_pcm *pcm)
  174. {
  175. int ret;
  176. ret = imx_pcm_new(card, dai, pcm);
  177. if (ret)
  178. return ret;
  179. if (dai->playback.channels_min) {
  180. struct snd_pcm_substream *substream =
  181. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  182. struct snd_dma_buffer *buf = &substream->dma_buffer;
  183. imx_ssi_fiq_tx_buffer = (unsigned long)buf->area;
  184. }
  185. if (dai->capture.channels_min) {
  186. struct snd_pcm_substream *substream =
  187. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  188. struct snd_dma_buffer *buf = &substream->dma_buffer;
  189. imx_ssi_fiq_rx_buffer = (unsigned long)buf->area;
  190. }
  191. set_fiq_handler(&imx_ssi_fiq_start,
  192. &imx_ssi_fiq_end - &imx_ssi_fiq_start);
  193. return 0;
  194. }
  195. static struct snd_soc_platform imx_soc_platform_fiq = {
  196. .pcm_ops = &imx_pcm_ops,
  197. .pcm_new = imx_pcm_fiq_new,
  198. .pcm_free = imx_pcm_free,
  199. };
  200. struct snd_soc_platform *imx_ssi_fiq_init(struct platform_device *pdev,
  201. struct imx_ssi *ssi)
  202. {
  203. int ret = 0;
  204. ret = claim_fiq(&fh);
  205. if (ret) {
  206. dev_err(&pdev->dev, "failed to claim fiq: %d", ret);
  207. return ERR_PTR(ret);
  208. }
  209. mxc_set_irq_fiq(ssi->irq, 1);
  210. imx_pcm_fiq = ssi->irq;
  211. imx_ssi_fiq_base = (unsigned long)ssi->base;
  212. ssi->dma_params_tx.burstsize = 4;
  213. ssi->dma_params_rx.burstsize = 6;
  214. return &imx_soc_platform_fiq;
  215. }
  216. void imx_ssi_fiq_exit(struct platform_device *pdev,
  217. struct imx_ssi *ssi)
  218. {
  219. mxc_set_irq_fiq(ssi->irq, 0);
  220. release_fiq(&fh);
  221. }