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
);
88 static int snd_imx_pcm_prepare(struct snd_soc_component
*component
,
89 struct snd_pcm_substream
*substream
)
91 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
92 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
96 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
97 regs
.ARM_r8
= (iprtd
->period
* iprtd
->periods
- 1) << 16;
99 regs
.ARM_r9
= (iprtd
->period
* iprtd
->periods
- 1) << 16;
106 static int imx_pcm_fiq
;
108 static int snd_imx_pcm_trigger(struct snd_soc_component
*component
,
109 struct snd_pcm_substream
*substream
, int cmd
)
111 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
112 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
115 case SNDRV_PCM_TRIGGER_START
:
116 case SNDRV_PCM_TRIGGER_RESUME
:
117 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
118 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
119 atomic_set(&iprtd
->playing
, 1);
121 atomic_set(&iprtd
->capturing
, 1);
122 hrtimer_start(&iprtd
->hrt
, ns_to_ktime(iprtd
->poll_time_ns
),
124 enable_fiq(imx_pcm_fiq
);
127 case SNDRV_PCM_TRIGGER_STOP
:
128 case SNDRV_PCM_TRIGGER_SUSPEND
:
129 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
130 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
131 atomic_set(&iprtd
->playing
, 0);
133 atomic_set(&iprtd
->capturing
, 0);
134 if (!atomic_read(&iprtd
->playing
) &&
135 !atomic_read(&iprtd
->capturing
))
136 disable_fiq(imx_pcm_fiq
);
146 static snd_pcm_uframes_t
147 snd_imx_pcm_pointer(struct snd_soc_component
*component
,
148 struct snd_pcm_substream
*substream
)
150 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
151 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
153 return bytes_to_frames(substream
->runtime
, iprtd
->offset
);
156 static const struct snd_pcm_hardware snd_imx_hardware
= {
157 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
158 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
159 SNDRV_PCM_INFO_MMAP
|
160 SNDRV_PCM_INFO_MMAP_VALID
|
161 SNDRV_PCM_INFO_PAUSE
|
162 SNDRV_PCM_INFO_RESUME
,
163 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
164 .buffer_bytes_max
= IMX_SSI_DMABUF_SIZE
,
165 .period_bytes_min
= 128,
166 .period_bytes_max
= 16 * 1024,
172 static int snd_imx_open(struct snd_soc_component
*component
,
173 struct snd_pcm_substream
*substream
)
175 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
176 struct imx_pcm_runtime_data
*iprtd
;
179 iprtd
= kzalloc(sizeof(*iprtd
), GFP_KERNEL
);
182 runtime
->private_data
= iprtd
;
184 iprtd
->substream
= substream
;
186 atomic_set(&iprtd
->playing
, 0);
187 atomic_set(&iprtd
->capturing
, 0);
188 hrtimer_init(&iprtd
->hrt
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
189 iprtd
->hrt
.function
= snd_hrtimer_callback
;
191 ret
= snd_pcm_hw_constraint_integer(substream
->runtime
,
192 SNDRV_PCM_HW_PARAM_PERIODS
);
198 snd_soc_set_runtime_hwparams(substream
, &snd_imx_hardware
);
202 static int snd_imx_close(struct snd_soc_component
*component
,
203 struct snd_pcm_substream
*substream
)
205 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
206 struct imx_pcm_runtime_data
*iprtd
= runtime
->private_data
;
208 hrtimer_cancel(&iprtd
->hrt
);
215 static int imx_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
217 struct snd_card
*card
= rtd
->card
->snd_card
;
218 struct snd_pcm
*pcm
= rtd
->pcm
;
221 ret
= dma_coerce_mask_and_coherent(card
->dev
, DMA_BIT_MASK(32));
225 return snd_pcm_set_fixed_buffer_all(pcm
, SNDRV_DMA_TYPE_DEV_WC
,
227 IMX_SSI_DMABUF_SIZE
);
232 static int snd_imx_pcm_new(struct snd_soc_component
*component
,
233 struct snd_soc_pcm_runtime
*rtd
)
235 struct snd_pcm
*pcm
= rtd
->pcm
;
236 struct snd_pcm_substream
*substream
;
239 ret
= imx_pcm_new(rtd
);
243 substream
= pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
;
245 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
247 imx_ssi_fiq_tx_buffer
= (unsigned long)buf
->area
;
250 substream
= pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
;
252 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
254 imx_ssi_fiq_rx_buffer
= (unsigned long)buf
->area
;
257 set_fiq_handler(&imx_ssi_fiq_start
,
258 &imx_ssi_fiq_end
- &imx_ssi_fiq_start
);
263 static void snd_imx_pcm_free(struct snd_soc_component
*component
,
266 mxc_set_irq_fiq(ssi_irq
, 0);
270 static const struct snd_soc_component_driver imx_soc_component_fiq
= {
271 .open
= snd_imx_open
,
272 .close
= snd_imx_close
,
273 .hw_params
= snd_imx_pcm_hw_params
,
274 .prepare
= snd_imx_pcm_prepare
,
275 .trigger
= snd_imx_pcm_trigger
,
276 .pointer
= snd_imx_pcm_pointer
,
277 .pcm_construct
= snd_imx_pcm_new
,
278 .pcm_destruct
= snd_imx_pcm_free
,
281 int imx_pcm_fiq_init(struct platform_device
*pdev
,
282 struct imx_pcm_fiq_params
*params
)
286 ret
= claim_fiq(&fh
);
288 dev_err(&pdev
->dev
, "failed to claim fiq: %d", ret
);
292 mxc_set_irq_fiq(params
->irq
, 1);
293 ssi_irq
= params
->irq
;
295 imx_pcm_fiq
= params
->irq
;
297 imx_ssi_fiq_base
= (unsigned long)params
->base
;
299 params
->dma_params_tx
->maxburst
= 4;
300 params
->dma_params_rx
->maxburst
= 6;
302 ret
= devm_snd_soc_register_component(&pdev
->dev
, &imx_soc_component_fiq
,
305 goto failed_register
;
310 mxc_set_irq_fiq(ssi_irq
, 0);
315 EXPORT_SYMBOL_GPL(imx_pcm_fiq_init
);
317 void imx_pcm_fiq_exit(struct platform_device
*pdev
)
320 EXPORT_SYMBOL_GPL(imx_pcm_fiq_exit
);
322 MODULE_DESCRIPTION("Freescale i.MX PCM FIQ handler");
323 MODULE_LICENSE("GPL");