Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/soc/fsl/imx-pcm-fiq.c
26427 views
1
// SPDX-License-Identifier: GPL-2.0+
2
// imx-pcm-fiq.c -- ALSA Soc Audio Layer
3
//
4
// Copyright 2009 Sascha Hauer <[email protected]>
5
//
6
// This code is based on code copyrighted by Freescale,
7
// Liam Girdwood, Javier Martin and probably others.
8
9
#include <linux/clk.h>
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>
18
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>
25
26
#include <asm/fiq.h>
27
28
#include <linux/platform_data/asoc-imx-ssi.h>
29
30
#include "imx-ssi.h"
31
#include "imx-pcm.h"
32
33
struct imx_pcm_runtime_data {
34
unsigned int period;
35
int periods;
36
unsigned long offset;
37
struct hrtimer hrt;
38
int poll_time_ns;
39
struct snd_pcm_substream *substream;
40
atomic_t playing;
41
atomic_t capturing;
42
};
43
44
static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt)
45
{
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;
49
struct pt_regs regs;
50
51
if (!atomic_read(&iprtd->playing) && !atomic_read(&iprtd->capturing))
52
return HRTIMER_NORESTART;
53
54
get_fiq_regs(&regs);
55
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
61
snd_pcm_period_elapsed(substream);
62
63
hrtimer_forward_now(hrt, ns_to_ktime(iprtd->poll_time_ns));
64
65
return HRTIMER_RESTART;
66
}
67
68
static struct fiq_handler fh = {
69
.name = DRV_NAME,
70
};
71
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)
75
{
76
struct snd_pcm_runtime *runtime = substream->runtime;
77
struct imx_pcm_runtime_data *iprtd = runtime->private_data;
78
79
iprtd->periods = params_periods(params);
80
iprtd->period = params_period_bytes(params);
81
iprtd->offset = 0;
82
iprtd->poll_time_ns = 1000000000 / params_rate(params) *
83
params_period_size(params);
84
85
return 0;
86
}
87
88
static int snd_imx_pcm_prepare(struct snd_soc_component *component,
89
struct snd_pcm_substream *substream)
90
{
91
struct snd_pcm_runtime *runtime = substream->runtime;
92
struct imx_pcm_runtime_data *iprtd = runtime->private_data;
93
struct pt_regs regs;
94
95
get_fiq_regs(&regs);
96
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
97
regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16;
98
else
99
regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16;
100
101
set_fiq_regs(&regs);
102
103
return 0;
104
}
105
106
static int imx_pcm_fiq;
107
108
static int snd_imx_pcm_trigger(struct snd_soc_component *component,
109
struct snd_pcm_substream *substream, int cmd)
110
{
111
struct snd_pcm_runtime *runtime = substream->runtime;
112
struct imx_pcm_runtime_data *iprtd = runtime->private_data;
113
114
switch (cmd) {
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);
120
else
121
atomic_set(&iprtd->capturing, 1);
122
hrtimer_start(&iprtd->hrt, ns_to_ktime(iprtd->poll_time_ns),
123
HRTIMER_MODE_REL);
124
enable_fiq(imx_pcm_fiq);
125
break;
126
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);
132
else
133
atomic_set(&iprtd->capturing, 0);
134
if (!atomic_read(&iprtd->playing) &&
135
!atomic_read(&iprtd->capturing))
136
disable_fiq(imx_pcm_fiq);
137
break;
138
139
default:
140
return -EINVAL;
141
}
142
143
return 0;
144
}
145
146
static snd_pcm_uframes_t
147
snd_imx_pcm_pointer(struct snd_soc_component *component,
148
struct snd_pcm_substream *substream)
149
{
150
struct snd_pcm_runtime *runtime = substream->runtime;
151
struct imx_pcm_runtime_data *iprtd = runtime->private_data;
152
153
return bytes_to_frames(substream->runtime, iprtd->offset);
154
}
155
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,
167
.periods_min = 4,
168
.periods_max = 255,
169
.fifo_size = 0,
170
};
171
172
static int snd_imx_open(struct snd_soc_component *component,
173
struct snd_pcm_substream *substream)
174
{
175
struct snd_pcm_runtime *runtime = substream->runtime;
176
struct imx_pcm_runtime_data *iprtd;
177
int ret;
178
179
iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
180
if (iprtd == NULL)
181
return -ENOMEM;
182
runtime->private_data = iprtd;
183
184
iprtd->substream = substream;
185
186
atomic_set(&iprtd->playing, 0);
187
atomic_set(&iprtd->capturing, 0);
188
hrtimer_setup(&iprtd->hrt, snd_hrtimer_callback, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
189
190
ret = snd_pcm_hw_constraint_integer(substream->runtime,
191
SNDRV_PCM_HW_PARAM_PERIODS);
192
if (ret < 0) {
193
kfree(iprtd);
194
return ret;
195
}
196
197
snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
198
return 0;
199
}
200
201
static int snd_imx_close(struct snd_soc_component *component,
202
struct snd_pcm_substream *substream)
203
{
204
struct snd_pcm_runtime *runtime = substream->runtime;
205
struct imx_pcm_runtime_data *iprtd = runtime->private_data;
206
207
hrtimer_cancel(&iprtd->hrt);
208
209
kfree(iprtd);
210
211
return 0;
212
}
213
214
static int imx_pcm_new(struct snd_soc_pcm_runtime *rtd)
215
{
216
struct snd_card *card = rtd->card->snd_card;
217
struct snd_pcm *pcm = rtd->pcm;
218
int ret;
219
220
ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
221
if (ret)
222
return ret;
223
224
return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_WC,
225
pcm->card->dev,
226
IMX_SSI_DMABUF_SIZE);
227
}
228
229
static int ssi_irq;
230
231
static int snd_imx_pcm_new(struct snd_soc_component *component,
232
struct snd_soc_pcm_runtime *rtd)
233
{
234
struct snd_pcm *pcm = rtd->pcm;
235
struct snd_pcm_substream *substream;
236
int ret;
237
238
ret = imx_pcm_new(rtd);
239
if (ret)
240
return ret;
241
242
substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
243
if (substream) {
244
struct snd_dma_buffer *buf = &substream->dma_buffer;
245
246
imx_ssi_fiq_tx_buffer = (unsigned long)buf->area;
247
}
248
249
substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
250
if (substream) {
251
struct snd_dma_buffer *buf = &substream->dma_buffer;
252
253
imx_ssi_fiq_rx_buffer = (unsigned long)buf->area;
254
}
255
256
set_fiq_handler(&imx_ssi_fiq_start,
257
&imx_ssi_fiq_end - &imx_ssi_fiq_start);
258
259
return 0;
260
}
261
262
static void snd_imx_pcm_free(struct snd_soc_component *component,
263
struct snd_pcm *pcm)
264
{
265
mxc_set_irq_fiq(ssi_irq, 0);
266
release_fiq(&fh);
267
}
268
269
static const struct snd_soc_component_driver imx_soc_component_fiq = {
270
.open = snd_imx_open,
271
.close = snd_imx_close,
272
.hw_params = snd_imx_pcm_hw_params,
273
.prepare = snd_imx_pcm_prepare,
274
.trigger = snd_imx_pcm_trigger,
275
.pointer = snd_imx_pcm_pointer,
276
.pcm_construct = snd_imx_pcm_new,
277
.pcm_destruct = snd_imx_pcm_free,
278
};
279
280
int imx_pcm_fiq_init(struct platform_device *pdev,
281
struct imx_pcm_fiq_params *params)
282
{
283
int ret;
284
285
ret = claim_fiq(&fh);
286
if (ret) {
287
dev_err(&pdev->dev, "failed to claim fiq: %d", ret);
288
return ret;
289
}
290
291
mxc_set_irq_fiq(params->irq, 1);
292
ssi_irq = params->irq;
293
294
imx_pcm_fiq = params->irq;
295
296
imx_ssi_fiq_base = (unsigned long)params->base;
297
298
params->dma_params_tx->maxburst = 4;
299
params->dma_params_rx->maxburst = 6;
300
301
ret = devm_snd_soc_register_component(&pdev->dev, &imx_soc_component_fiq,
302
NULL, 0);
303
if (ret)
304
goto failed_register;
305
306
return 0;
307
308
failed_register:
309
mxc_set_irq_fiq(ssi_irq, 0);
310
release_fiq(&fh);
311
312
return ret;
313
}
314
EXPORT_SYMBOL_GPL(imx_pcm_fiq_init);
315
316
void imx_pcm_fiq_exit(struct platform_device *pdev)
317
{
318
}
319
EXPORT_SYMBOL_GPL(imx_pcm_fiq_exit);
320
321
MODULE_DESCRIPTION("Freescale i.MX PCM FIQ handler");
322
MODULE_LICENSE("GPL");
323
324