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_soc_component
*component
,
73 struct snd_pcm_substream
*substream
,
74 struct snd_pcm_hw_params
*params
)
76 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
77 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
79 iprtd
->periods
= params_periods(params
);
80 iprtd
->period
= params_period_bytes(params
);
82 iprtd
->poll_time_ns
= 1000000000 / params_rate(params
) *
83 params_period_size(params
);
84 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
89 static int snd_imx_pcm_prepare(struct snd_soc_component
*component
,
90 struct snd_pcm_substream
*substream
)
92 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
93 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
97 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
98 regs
.ARM_r8
= (iprtd
->period
* iprtd
->periods
- 1) << 16;
100 regs
.ARM_r9
= (iprtd
->period
* iprtd
->periods
- 1) << 16;
107 static int imx_pcm_fiq
;
109 static int snd_imx_pcm_trigger(struct snd_soc_component
*component
,
110 struct snd_pcm_substream
*substream
, int cmd
)
112 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
113 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
116 case SNDRV_PCM_TRIGGER_START
:
117 case SNDRV_PCM_TRIGGER_RESUME
:
118 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
119 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
120 atomic_set(&iprtd
->playing
, 1);
122 atomic_set(&iprtd
->capturing
, 1);
123 hrtimer_start(&iprtd
->hrt
, ns_to_ktime(iprtd
->poll_time_ns
),
125 enable_fiq(imx_pcm_fiq
);
128 case SNDRV_PCM_TRIGGER_STOP
:
129 case SNDRV_PCM_TRIGGER_SUSPEND
:
130 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
131 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
132 atomic_set(&iprtd
->playing
, 0);
134 atomic_set(&iprtd
->capturing
, 0);
135 if (!atomic_read(&iprtd
->playing
) &&
136 !atomic_read(&iprtd
->capturing
))
137 disable_fiq(imx_pcm_fiq
);
147 static snd_pcm_uframes_t
148 snd_imx_pcm_pointer(struct snd_soc_component
*component
,
149 struct snd_pcm_substream
*substream
)
151 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
152 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
154 return bytes_to_frames(substream
->runtime
, iprtd
->offset
);
157 static const struct snd_pcm_hardware snd_imx_hardware
= {
158 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
159 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
160 SNDRV_PCM_INFO_MMAP
|
161 SNDRV_PCM_INFO_MMAP_VALID
|
162 SNDRV_PCM_INFO_PAUSE
|
163 SNDRV_PCM_INFO_RESUME
,
164 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
165 .buffer_bytes_max
= IMX_SSI_DMABUF_SIZE
,
166 .period_bytes_min
= 128,
167 .period_bytes_max
= 16 * 1024,
173 static int snd_imx_open(struct snd_soc_component
*component
,
174 struct snd_pcm_substream
*substream
)
176 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
177 struct imx_pcm_runtime_data
*iprtd
;
180 iprtd
= kzalloc(sizeof(*iprtd
), GFP_KERNEL
);
183 runtime
->private_data
= iprtd
;
185 iprtd
->substream
= substream
;
187 atomic_set(&iprtd
->playing
, 0);
188 atomic_set(&iprtd
->capturing
, 0);
189 hrtimer_init(&iprtd
->hrt
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
190 iprtd
->hrt
.function
= snd_hrtimer_callback
;
192 ret
= snd_pcm_hw_constraint_integer(substream
->runtime
,
193 SNDRV_PCM_HW_PARAM_PERIODS
);
199 snd_soc_set_runtime_hwparams(substream
, &snd_imx_hardware
);
203 static int snd_imx_close(struct snd_soc_component
*component
,
204 struct snd_pcm_substream
*substream
)
206 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
207 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
209 hrtimer_cancel(&iprtd
->hrt
);
216 static int snd_imx_pcm_mmap(struct snd_soc_component
*component
,
217 struct snd_pcm_substream
*substream
,
218 struct vm_area_struct
*vma
)
220 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
223 ret
= dma_mmap_wc(substream
->pcm
->card
->dev
, vma
, runtime
->dma_area
,
224 runtime
->dma_addr
, runtime
->dma_bytes
);
226 pr_debug("%s: ret: %d %p %pad 0x%08zx\n", __func__
, ret
,
233 static int imx_pcm_preallocate_dma_buffer(struct snd_pcm
*pcm
, int stream
)
235 struct snd_pcm_substream
*substream
= pcm
->streams
[stream
].substream
;
236 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
237 size_t size
= IMX_SSI_DMABUF_SIZE
;
239 buf
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
240 buf
->dev
.dev
= pcm
->card
->dev
;
241 buf
->private_data
= NULL
;
242 buf
->area
= dma_alloc_wc(pcm
->card
->dev
, size
, &buf
->addr
, GFP_KERNEL
);
250 static int imx_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
252 struct snd_card
*card
= rtd
->card
->snd_card
;
253 struct snd_pcm
*pcm
= rtd
->pcm
;
256 ret
= dma_coerce_mask_and_coherent(card
->dev
, DMA_BIT_MASK(32));
260 if (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
) {
261 ret
= imx_pcm_preallocate_dma_buffer(pcm
,
262 SNDRV_PCM_STREAM_PLAYBACK
);
267 if (pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
) {
268 ret
= imx_pcm_preallocate_dma_buffer(pcm
,
269 SNDRV_PCM_STREAM_CAPTURE
);
279 static int snd_imx_pcm_new(struct snd_soc_component
*component
,
280 struct snd_soc_pcm_runtime
*rtd
)
282 struct snd_pcm
*pcm
= rtd
->pcm
;
283 struct snd_pcm_substream
*substream
;
286 ret
= imx_pcm_new(rtd
);
290 substream
= pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
;
292 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
294 imx_ssi_fiq_tx_buffer
= (unsigned long)buf
->area
;
297 substream
= pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
;
299 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
301 imx_ssi_fiq_rx_buffer
= (unsigned long)buf
->area
;
304 set_fiq_handler(&imx_ssi_fiq_start
,
305 &imx_ssi_fiq_end
- &imx_ssi_fiq_start
);
310 static void imx_pcm_free(struct snd_pcm
*pcm
)
312 struct snd_pcm_substream
*substream
;
313 struct snd_dma_buffer
*buf
;
316 for (stream
= 0; stream
< 2; stream
++) {
317 substream
= pcm
->streams
[stream
].substream
;
321 buf
= &substream
->dma_buffer
;
325 dma_free_wc(pcm
->card
->dev
, buf
->bytes
, buf
->area
, buf
->addr
);
330 static void snd_imx_pcm_free(struct snd_soc_component
*component
,
333 mxc_set_irq_fiq(ssi_irq
, 0);
338 static const struct snd_soc_component_driver imx_soc_component_fiq
= {
339 .open
= snd_imx_open
,
340 .close
= snd_imx_close
,
341 .hw_params
= snd_imx_pcm_hw_params
,
342 .prepare
= snd_imx_pcm_prepare
,
343 .trigger
= snd_imx_pcm_trigger
,
344 .pointer
= snd_imx_pcm_pointer
,
345 .mmap
= snd_imx_pcm_mmap
,
346 .pcm_construct
= snd_imx_pcm_new
,
347 .pcm_destruct
= snd_imx_pcm_free
,
350 int imx_pcm_fiq_init(struct platform_device
*pdev
,
351 struct imx_pcm_fiq_params
*params
)
355 ret
= claim_fiq(&fh
);
357 dev_err(&pdev
->dev
, "failed to claim fiq: %d", ret
);
361 mxc_set_irq_fiq(params
->irq
, 1);
362 ssi_irq
= params
->irq
;
364 imx_pcm_fiq
= params
->irq
;
366 imx_ssi_fiq_base
= (unsigned long)params
->base
;
368 params
->dma_params_tx
->maxburst
= 4;
369 params
->dma_params_rx
->maxburst
= 6;
371 ret
= devm_snd_soc_register_component(&pdev
->dev
, &imx_soc_component_fiq
,
374 goto failed_register
;
379 mxc_set_irq_fiq(ssi_irq
, 0);
384 EXPORT_SYMBOL_GPL(imx_pcm_fiq_init
);
386 void imx_pcm_fiq_exit(struct platform_device
*pdev
)
389 EXPORT_SYMBOL_GPL(imx_pcm_fiq_exit
);
391 MODULE_LICENSE("GPL");