imx-pcm-fiq.c 10 KB

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