1 // SPDX-License-Identifier: GPL-2.0+
2 // imx-pcm-fiq.c -- ALSA Soc Audio Layer
4 // Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
6 // This code is based on code copyrighted by Freescale,
7 // Liam Girdwood, Javier Martin and probably others.
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
19 #include <sound/core.h>
20 #include <sound/dmaengine_pcm.h>
21 #include <sound/initval.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
28 #include <linux/platform_data/asoc-imx-ssi.h>
33 struct imx_pcm_runtime_data {
39 struct snd_pcm_substream *substream;
44 static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt)
46 struct imx_pcm_runtime_data *iprtd =
47 container_of(hrt, struct imx_pcm_runtime_data, hrt);
48 struct snd_pcm_substream *substream = iprtd->substream;
51 if (!atomic_read(&iprtd->playing) && !atomic_read(&iprtd->capturing))
52 return HRTIMER_NORESTART;
56 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
57 iprtd->offset = regs.ARM_r8 & 0xffff;
59 iprtd->offset = regs.ARM_r9 & 0xffff;
61 snd_pcm_period_elapsed(substream);
63 hrtimer_forward_now(hrt, ns_to_ktime(iprtd->poll_time_ns));
65 return HRTIMER_RESTART;
68 static struct fiq_handler fh = {
72 static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
73 struct snd_pcm_hw_params *params)
75 struct snd_pcm_runtime *runtime = substream->runtime;
76 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
78 iprtd->periods = params_periods(params);
79 iprtd->period = params_period_bytes(params);
81 iprtd->poll_time_ns = 1000000000 / params_rate(params) *
82 params_period_size(params);
83 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
88 static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
90 struct snd_pcm_runtime *runtime = substream->runtime;
91 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
95 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
96 regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16;
98 regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16;
105 static int imx_pcm_fiq;
107 static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
109 struct snd_pcm_runtime *runtime = substream->runtime;
110 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
113 case SNDRV_PCM_TRIGGER_START:
114 case SNDRV_PCM_TRIGGER_RESUME:
115 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
116 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
117 atomic_set(&iprtd->playing, 1);
119 atomic_set(&iprtd->capturing, 1);
120 hrtimer_start(&iprtd->hrt, ns_to_ktime(iprtd->poll_time_ns),
122 enable_fiq(imx_pcm_fiq);
125 case SNDRV_PCM_TRIGGER_STOP:
126 case SNDRV_PCM_TRIGGER_SUSPEND:
127 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
128 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
129 atomic_set(&iprtd->playing, 0);
131 atomic_set(&iprtd->capturing, 0);
132 if (!atomic_read(&iprtd->playing) &&
133 !atomic_read(&iprtd->capturing))
134 disable_fiq(imx_pcm_fiq);
144 static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
146 struct snd_pcm_runtime *runtime = substream->runtime;
147 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
149 return bytes_to_frames(substream->runtime, iprtd->offset);
152 static const struct snd_pcm_hardware snd_imx_hardware = {
153 .info = SNDRV_PCM_INFO_INTERLEAVED |
154 SNDRV_PCM_INFO_BLOCK_TRANSFER |
155 SNDRV_PCM_INFO_MMAP |
156 SNDRV_PCM_INFO_MMAP_VALID |
157 SNDRV_PCM_INFO_PAUSE |
158 SNDRV_PCM_INFO_RESUME,
159 .formats = SNDRV_PCM_FMTBIT_S16_LE,
160 .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
161 .period_bytes_min = 128,
162 .period_bytes_max = 16 * 1024,
168 static int snd_imx_open(struct snd_pcm_substream *substream)
170 struct snd_pcm_runtime *runtime = substream->runtime;
171 struct imx_pcm_runtime_data *iprtd;
174 iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
177 runtime->private_data = iprtd;
179 iprtd->substream = substream;
181 atomic_set(&iprtd->playing, 0);
182 atomic_set(&iprtd->capturing, 0);
183 hrtimer_init(&iprtd->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
184 iprtd->hrt.function = snd_hrtimer_callback;
186 ret = snd_pcm_hw_constraint_integer(substream->runtime,
187 SNDRV_PCM_HW_PARAM_PERIODS);
193 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
197 static int snd_imx_close(struct snd_pcm_substream *substream)
199 struct snd_pcm_runtime *runtime = substream->runtime;
200 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
202 hrtimer_cancel(&iprtd->hrt);
209 static int snd_imx_pcm_mmap(struct snd_pcm_substream *substream,
210 struct vm_area_struct *vma)
212 struct snd_pcm_runtime *runtime = substream->runtime;
215 ret = dma_mmap_wc(substream->pcm->card->dev, vma, runtime->dma_area,
216 runtime->dma_addr, runtime->dma_bytes);
218 pr_debug("%s: ret: %d %p %pad 0x%08zx\n", __func__, ret,
225 static const struct snd_pcm_ops imx_pcm_ops = {
226 .open = snd_imx_open,
227 .close = snd_imx_close,
228 .ioctl = snd_pcm_lib_ioctl,
229 .hw_params = snd_imx_pcm_hw_params,
230 .prepare = snd_imx_pcm_prepare,
231 .trigger = snd_imx_pcm_trigger,
232 .pointer = snd_imx_pcm_pointer,
233 .mmap = snd_imx_pcm_mmap,
236 static int imx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
238 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
239 struct snd_dma_buffer *buf = &substream->dma_buffer;
240 size_t size = IMX_SSI_DMABUF_SIZE;
242 buf->dev.type = SNDRV_DMA_TYPE_DEV;
243 buf->dev.dev = pcm->card->dev;
244 buf->private_data = NULL;
245 buf->area = dma_alloc_wc(pcm->card->dev, size, &buf->addr, GFP_KERNEL);
253 static int imx_pcm_new(struct snd_soc_pcm_runtime *rtd)
255 struct snd_card *card = rtd->card->snd_card;
256 struct snd_pcm *pcm = rtd->pcm;
259 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
263 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
264 ret = imx_pcm_preallocate_dma_buffer(pcm,
265 SNDRV_PCM_STREAM_PLAYBACK);
270 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
271 ret = imx_pcm_preallocate_dma_buffer(pcm,
272 SNDRV_PCM_STREAM_CAPTURE);
282 static int imx_pcm_fiq_new(struct snd_soc_pcm_runtime *rtd)
284 struct snd_pcm *pcm = rtd->pcm;
285 struct snd_pcm_substream *substream;
288 ret = imx_pcm_new(rtd);
292 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
294 struct snd_dma_buffer *buf = &substream->dma_buffer;
296 imx_ssi_fiq_tx_buffer = (unsigned long)buf->area;
299 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
301 struct snd_dma_buffer *buf = &substream->dma_buffer;
303 imx_ssi_fiq_rx_buffer = (unsigned long)buf->area;
306 set_fiq_handler(&imx_ssi_fiq_start,
307 &imx_ssi_fiq_end - &imx_ssi_fiq_start);
312 static void imx_pcm_free(struct snd_pcm *pcm)
314 struct snd_pcm_substream *substream;
315 struct snd_dma_buffer *buf;
318 for (stream = 0; stream < 2; stream++) {
319 substream = pcm->streams[stream].substream;
323 buf = &substream->dma_buffer;
327 dma_free_wc(pcm->card->dev, buf->bytes, buf->area, buf->addr);
332 static void imx_pcm_fiq_free(struct snd_pcm *pcm)
334 mxc_set_irq_fiq(ssi_irq, 0);
339 static const struct snd_soc_component_driver imx_soc_component_fiq = {
341 .pcm_new = imx_pcm_fiq_new,
342 .pcm_free = imx_pcm_fiq_free,
345 int imx_pcm_fiq_init(struct platform_device *pdev,
346 struct imx_pcm_fiq_params *params)
350 ret = claim_fiq(&fh);
352 dev_err(&pdev->dev, "failed to claim fiq: %d", ret);
356 mxc_set_irq_fiq(params->irq, 1);
357 ssi_irq = params->irq;
359 imx_pcm_fiq = params->irq;
361 imx_ssi_fiq_base = (unsigned long)params->base;
363 params->dma_params_tx->maxburst = 4;
364 params->dma_params_rx->maxburst = 6;
366 ret = devm_snd_soc_register_component(&pdev->dev, &imx_soc_component_fiq,
369 goto failed_register;
374 mxc_set_irq_fiq(ssi_irq, 0);
379 EXPORT_SYMBOL_GPL(imx_pcm_fiq_init);
381 void imx_pcm_fiq_exit(struct platform_device *pdev)
384 EXPORT_SYMBOL_GPL(imx_pcm_fiq_exit);
386 MODULE_LICENSE("GPL");