imx-pcm-fiq.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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/dmaengine_pcm.h>
  25. #include <sound/initval.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <asm/fiq.h>
  30. #include <linux/platform_data/asoc-imx-ssi.h>
  31. #include "imx-ssi.h"
  32. #include "imx-pcm.h"
  33. struct imx_pcm_runtime_data {
  34. unsigned int period;
  35. int periods;
  36. unsigned long offset;
  37. struct hrtimer hrt;
  38. int poll_time_ns;
  39. struct snd_pcm_substream *substream;
  40. atomic_t playing;
  41. atomic_t capturing;
  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 pt_regs regs;
  49. if (!atomic_read(&iprtd->playing) && !atomic_read(&iprtd->capturing))
  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. snd_pcm_period_elapsed(substream);
  57. hrtimer_forward_now(hrt, ns_to_ktime(iprtd->poll_time_ns));
  58. return HRTIMER_RESTART;
  59. }
  60. static struct fiq_handler fh = {
  61. .name = DRV_NAME,
  62. };
  63. static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
  64. struct snd_pcm_hw_params *params)
  65. {
  66. struct snd_pcm_runtime *runtime = substream->runtime;
  67. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  68. iprtd->periods = params_periods(params);
  69. iprtd->period = params_period_bytes(params);
  70. iprtd->offset = 0;
  71. iprtd->poll_time_ns = 1000000000 / params_rate(params) *
  72. params_period_size(params);
  73. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  74. return 0;
  75. }
  76. static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
  77. {
  78. struct snd_pcm_runtime *runtime = substream->runtime;
  79. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  80. struct pt_regs regs;
  81. get_fiq_regs(&regs);
  82. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  83. regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16;
  84. else
  85. regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16;
  86. set_fiq_regs(&regs);
  87. return 0;
  88. }
  89. static int imx_pcm_fiq;
  90. static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  91. {
  92. struct snd_pcm_runtime *runtime = substream->runtime;
  93. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  94. switch (cmd) {
  95. case SNDRV_PCM_TRIGGER_START:
  96. case SNDRV_PCM_TRIGGER_RESUME:
  97. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  98. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  99. atomic_set(&iprtd->playing, 1);
  100. else
  101. atomic_set(&iprtd->capturing, 1);
  102. hrtimer_start(&iprtd->hrt, ns_to_ktime(iprtd->poll_time_ns),
  103. HRTIMER_MODE_REL);
  104. enable_fiq(imx_pcm_fiq);
  105. break;
  106. case SNDRV_PCM_TRIGGER_STOP:
  107. case SNDRV_PCM_TRIGGER_SUSPEND:
  108. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  109. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  110. atomic_set(&iprtd->playing, 0);
  111. else
  112. atomic_set(&iprtd->capturing, 0);
  113. if (!atomic_read(&iprtd->playing) &&
  114. !atomic_read(&iprtd->capturing))
  115. disable_fiq(imx_pcm_fiq);
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. return 0;
  121. }
  122. static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
  123. {
  124. struct snd_pcm_runtime *runtime = substream->runtime;
  125. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  126. return bytes_to_frames(substream->runtime, iprtd->offset);
  127. }
  128. static struct snd_pcm_hardware snd_imx_hardware = {
  129. .info = SNDRV_PCM_INFO_INTERLEAVED |
  130. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  131. SNDRV_PCM_INFO_MMAP |
  132. SNDRV_PCM_INFO_MMAP_VALID |
  133. SNDRV_PCM_INFO_PAUSE |
  134. SNDRV_PCM_INFO_RESUME,
  135. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  136. .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
  137. .period_bytes_min = 128,
  138. .period_bytes_max = 16 * 1024,
  139. .periods_min = 4,
  140. .periods_max = 255,
  141. .fifo_size = 0,
  142. };
  143. static int snd_imx_open(struct snd_pcm_substream *substream)
  144. {
  145. struct snd_pcm_runtime *runtime = substream->runtime;
  146. struct imx_pcm_runtime_data *iprtd;
  147. int ret;
  148. iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
  149. if (iprtd == NULL)
  150. return -ENOMEM;
  151. runtime->private_data = iprtd;
  152. iprtd->substream = substream;
  153. atomic_set(&iprtd->playing, 0);
  154. atomic_set(&iprtd->capturing, 0);
  155. hrtimer_init(&iprtd->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  156. iprtd->hrt.function = snd_hrtimer_callback;
  157. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  158. SNDRV_PCM_HW_PARAM_PERIODS);
  159. if (ret < 0) {
  160. kfree(iprtd);
  161. return ret;
  162. }
  163. snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
  164. return 0;
  165. }
  166. static int snd_imx_close(struct snd_pcm_substream *substream)
  167. {
  168. struct snd_pcm_runtime *runtime = substream->runtime;
  169. struct imx_pcm_runtime_data *iprtd = runtime->private_data;
  170. hrtimer_cancel(&iprtd->hrt);
  171. kfree(iprtd);
  172. return 0;
  173. }
  174. static int snd_imx_pcm_mmap(struct snd_pcm_substream *substream,
  175. struct vm_area_struct *vma)
  176. {
  177. struct snd_pcm_runtime *runtime = substream->runtime;
  178. int ret;
  179. ret = dma_mmap_writecombine(substream->pcm->card->dev, vma,
  180. runtime->dma_area, runtime->dma_addr, runtime->dma_bytes);
  181. pr_debug("%s: ret: %d %p 0x%08x 0x%08x\n", __func__, ret,
  182. runtime->dma_area,
  183. runtime->dma_addr,
  184. runtime->dma_bytes);
  185. return ret;
  186. }
  187. static struct snd_pcm_ops imx_pcm_ops = {
  188. .open = snd_imx_open,
  189. .close = snd_imx_close,
  190. .ioctl = snd_pcm_lib_ioctl,
  191. .hw_params = snd_imx_pcm_hw_params,
  192. .prepare = snd_imx_pcm_prepare,
  193. .trigger = snd_imx_pcm_trigger,
  194. .pointer = snd_imx_pcm_pointer,
  195. .mmap = snd_imx_pcm_mmap,
  196. };
  197. static int imx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  198. {
  199. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  200. struct snd_dma_buffer *buf = &substream->dma_buffer;
  201. size_t size = IMX_SSI_DMABUF_SIZE;
  202. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  203. buf->dev.dev = pcm->card->dev;
  204. buf->private_data = NULL;
  205. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  206. &buf->addr, GFP_KERNEL);
  207. if (!buf->area)
  208. return -ENOMEM;
  209. buf->bytes = size;
  210. return 0;
  211. }
  212. static int imx_pcm_new(struct snd_soc_pcm_runtime *rtd)
  213. {
  214. struct snd_card *card = rtd->card->snd_card;
  215. struct snd_pcm *pcm = rtd->pcm;
  216. int ret;
  217. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  218. if (ret)
  219. return ret;
  220. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  221. ret = imx_pcm_preallocate_dma_buffer(pcm,
  222. SNDRV_PCM_STREAM_PLAYBACK);
  223. if (ret)
  224. return ret;
  225. }
  226. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  227. ret = imx_pcm_preallocate_dma_buffer(pcm,
  228. SNDRV_PCM_STREAM_CAPTURE);
  229. if (ret)
  230. return ret;
  231. }
  232. return 0;
  233. }
  234. static int ssi_irq = 0;
  235. static int imx_pcm_fiq_new(struct snd_soc_pcm_runtime *rtd)
  236. {
  237. struct snd_pcm *pcm = rtd->pcm;
  238. struct snd_pcm_substream *substream;
  239. int ret;
  240. ret = imx_pcm_new(rtd);
  241. if (ret)
  242. return ret;
  243. substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  244. if (substream) {
  245. struct snd_dma_buffer *buf = &substream->dma_buffer;
  246. imx_ssi_fiq_tx_buffer = (unsigned long)buf->area;
  247. }
  248. substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  249. if (substream) {
  250. struct snd_dma_buffer *buf = &substream->dma_buffer;
  251. imx_ssi_fiq_rx_buffer = (unsigned long)buf->area;
  252. }
  253. set_fiq_handler(&imx_ssi_fiq_start,
  254. &imx_ssi_fiq_end - &imx_ssi_fiq_start);
  255. return 0;
  256. }
  257. static void imx_pcm_free(struct snd_pcm *pcm)
  258. {
  259. struct snd_pcm_substream *substream;
  260. struct snd_dma_buffer *buf;
  261. int stream;
  262. for (stream = 0; stream < 2; stream++) {
  263. substream = pcm->streams[stream].substream;
  264. if (!substream)
  265. continue;
  266. buf = &substream->dma_buffer;
  267. if (!buf->area)
  268. continue;
  269. dma_free_writecombine(pcm->card->dev, buf->bytes,
  270. buf->area, buf->addr);
  271. buf->area = NULL;
  272. }
  273. }
  274. static void imx_pcm_fiq_free(struct snd_pcm *pcm)
  275. {
  276. mxc_set_irq_fiq(ssi_irq, 0);
  277. release_fiq(&fh);
  278. imx_pcm_free(pcm);
  279. }
  280. static struct snd_soc_platform_driver imx_soc_platform_fiq = {
  281. .ops = &imx_pcm_ops,
  282. .pcm_new = imx_pcm_fiq_new,
  283. .pcm_free = imx_pcm_fiq_free,
  284. };
  285. int imx_pcm_fiq_init(struct platform_device *pdev,
  286. struct imx_pcm_fiq_params *params)
  287. {
  288. int ret;
  289. ret = claim_fiq(&fh);
  290. if (ret) {
  291. dev_err(&pdev->dev, "failed to claim fiq: %d", ret);
  292. return ret;
  293. }
  294. mxc_set_irq_fiq(params->irq, 1);
  295. ssi_irq = params->irq;
  296. imx_pcm_fiq = params->irq;
  297. imx_ssi_fiq_base = (unsigned long)params->base;
  298. params->dma_params_tx->maxburst = 4;
  299. params->dma_params_rx->maxburst = 6;
  300. ret = snd_soc_register_platform(&pdev->dev, &imx_soc_platform_fiq);
  301. if (ret)
  302. goto failed_register;
  303. return 0;
  304. failed_register:
  305. mxc_set_irq_fiq(ssi_irq, 0);
  306. release_fiq(&fh);
  307. return ret;
  308. }
  309. EXPORT_SYMBOL_GPL(imx_pcm_fiq_init);
  310. void imx_pcm_fiq_exit(struct platform_device *pdev)
  311. {
  312. snd_soc_unregister_platform(&pdev->dev);
  313. }
  314. EXPORT_SYMBOL_GPL(imx_pcm_fiq_exit);
  315. MODULE_LICENSE("GPL");