imx-pcm-fiq.c 9.3 KB

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