treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / fsl / imx-pcm-fiq.c
blobf20d5b1c3848e5b03666c5d4968769b23150cc42
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.
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>
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>
26 #include <asm/fiq.h>
28 #include <linux/platform_data/asoc-imx-ssi.h>
30 #include "imx-ssi.h"
31 #include "imx-pcm.h"
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;
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;
49 struct pt_regs regs;
51 if (!atomic_read(&iprtd->playing) && !atomic_read(&iprtd->capturing))
52 return HRTIMER_NORESTART;
54 get_fiq_regs(&regs);
56 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
57 iprtd->offset = regs.ARM_r8 & 0xffff;
58 else
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 = {
69 .name = DRV_NAME,
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);
81 iprtd->offset = 0;
82 iprtd->poll_time_ns = 1000000000 / params_rate(params) *
83 params_period_size(params);
84 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
86 return 0;
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;
94 struct pt_regs regs;
96 get_fiq_regs(&regs);
97 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
98 regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16;
99 else
100 regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16;
102 set_fiq_regs(&regs);
104 return 0;
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;
115 switch (cmd) {
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);
121 else
122 atomic_set(&iprtd->capturing, 1);
123 hrtimer_start(&iprtd->hrt, ns_to_ktime(iprtd->poll_time_ns),
124 HRTIMER_MODE_REL);
125 enable_fiq(imx_pcm_fiq);
126 break;
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);
133 else
134 atomic_set(&iprtd->capturing, 0);
135 if (!atomic_read(&iprtd->playing) &&
136 !atomic_read(&iprtd->capturing))
137 disable_fiq(imx_pcm_fiq);
138 break;
140 default:
141 return -EINVAL;
144 return 0;
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,
168 .periods_min = 4,
169 .periods_max = 255,
170 .fifo_size = 0,
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;
178 int ret;
180 iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
181 if (iprtd == NULL)
182 return -ENOMEM;
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);
194 if (ret < 0) {
195 kfree(iprtd);
196 return ret;
199 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
200 return 0;
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);
211 kfree(iprtd);
213 return 0;
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;
221 int ret;
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,
227 runtime->dma_area,
228 &runtime->dma_addr,
229 runtime->dma_bytes);
230 return 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);
243 if (!buf->area)
244 return -ENOMEM;
245 buf->bytes = size;
247 return 0;
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;
254 int ret;
256 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
257 if (ret)
258 return ret;
260 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
261 ret = imx_pcm_preallocate_dma_buffer(pcm,
262 SNDRV_PCM_STREAM_PLAYBACK);
263 if (ret)
264 return ret;
267 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
268 ret = imx_pcm_preallocate_dma_buffer(pcm,
269 SNDRV_PCM_STREAM_CAPTURE);
270 if (ret)
271 return ret;
274 return 0;
277 static int ssi_irq;
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;
284 int ret;
286 ret = imx_pcm_new(rtd);
287 if (ret)
288 return ret;
290 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
291 if (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;
298 if (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);
307 return 0;
310 static void imx_pcm_free(struct snd_pcm *pcm)
312 struct snd_pcm_substream *substream;
313 struct snd_dma_buffer *buf;
314 int stream;
316 for (stream = 0; stream < 2; stream++) {
317 substream = pcm->streams[stream].substream;
318 if (!substream)
319 continue;
321 buf = &substream->dma_buffer;
322 if (!buf->area)
323 continue;
325 dma_free_wc(pcm->card->dev, buf->bytes, buf->area, buf->addr);
326 buf->area = NULL;
330 static void snd_imx_pcm_free(struct snd_soc_component *component,
331 struct snd_pcm *pcm)
333 mxc_set_irq_fiq(ssi_irq, 0);
334 release_fiq(&fh);
335 imx_pcm_free(pcm);
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)
353 int ret;
355 ret = claim_fiq(&fh);
356 if (ret) {
357 dev_err(&pdev->dev, "failed to claim fiq: %d", ret);
358 return 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,
372 NULL, 0);
373 if (ret)
374 goto failed_register;
376 return 0;
378 failed_register:
379 mxc_set_irq_fiq(ssi_irq, 0);
380 release_fiq(&fh);
382 return ret;
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");