xref: /openbmc/linux/sound/soc/fsl/imx-pcm-fiq.c (revision 12eb4683)
1 /*
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.
8  *
9  *  This program is free software; you can redistribute  it and/or modify it
10  *  under  the terms of  the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the  License, or (at your
12  *  option) any later version.
13  */
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 
24 #include <sound/core.h>
25 #include <sound/dmaengine_pcm.h>
26 #include <sound/initval.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 
31 #include <asm/fiq.h>
32 
33 #include <linux/platform_data/asoc-imx-ssi.h>
34 
35 #include "imx-ssi.h"
36 #include "imx-pcm.h"
37 
38 struct imx_pcm_runtime_data {
39 	unsigned int period;
40 	int periods;
41 	unsigned long offset;
42 	struct hrtimer hrt;
43 	int poll_time_ns;
44 	struct snd_pcm_substream *substream;
45 	atomic_t playing;
46 	atomic_t capturing;
47 };
48 
49 static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt)
50 {
51 	struct imx_pcm_runtime_data *iprtd =
52 		container_of(hrt, struct imx_pcm_runtime_data, hrt);
53 	struct snd_pcm_substream *substream = iprtd->substream;
54 	struct pt_regs regs;
55 
56 	if (!atomic_read(&iprtd->playing) && !atomic_read(&iprtd->capturing))
57 		return HRTIMER_NORESTART;
58 
59 	get_fiq_regs(&regs);
60 
61 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
62 		iprtd->offset = regs.ARM_r8 & 0xffff;
63 	else
64 		iprtd->offset = regs.ARM_r9 & 0xffff;
65 
66 	snd_pcm_period_elapsed(substream);
67 
68 	hrtimer_forward_now(hrt, ns_to_ktime(iprtd->poll_time_ns));
69 
70 	return HRTIMER_RESTART;
71 }
72 
73 static struct fiq_handler fh = {
74 	.name		= DRV_NAME,
75 };
76 
77 static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
78 				struct snd_pcm_hw_params *params)
79 {
80 	struct snd_pcm_runtime *runtime = substream->runtime;
81 	struct imx_pcm_runtime_data *iprtd = runtime->private_data;
82 
83 	iprtd->periods = params_periods(params);
84 	iprtd->period = params_period_bytes(params);
85 	iprtd->offset = 0;
86 	iprtd->poll_time_ns = 1000000000 / params_rate(params) *
87 				params_period_size(params);
88 	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
89 
90 	return 0;
91 }
92 
93 static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream)
94 {
95 	struct snd_pcm_runtime *runtime = substream->runtime;
96 	struct imx_pcm_runtime_data *iprtd = runtime->private_data;
97 	struct pt_regs regs;
98 
99 	get_fiq_regs(&regs);
100 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
101 		regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16;
102 	else
103 		regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16;
104 
105 	set_fiq_regs(&regs);
106 
107 	return 0;
108 }
109 
110 static int imx_pcm_fiq;
111 
112 static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
113 {
114 	struct snd_pcm_runtime *runtime = substream->runtime;
115 	struct imx_pcm_runtime_data *iprtd = runtime->private_data;
116 
117 	switch (cmd) {
118 	case SNDRV_PCM_TRIGGER_START:
119 	case SNDRV_PCM_TRIGGER_RESUME:
120 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
121 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
122 			atomic_set(&iprtd->playing, 1);
123 		else
124 			atomic_set(&iprtd->capturing, 1);
125 		hrtimer_start(&iprtd->hrt, ns_to_ktime(iprtd->poll_time_ns),
126 		      HRTIMER_MODE_REL);
127 		enable_fiq(imx_pcm_fiq);
128 		break;
129 
130 	case SNDRV_PCM_TRIGGER_STOP:
131 	case SNDRV_PCM_TRIGGER_SUSPEND:
132 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
133 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
134 			atomic_set(&iprtd->playing, 0);
135 		else
136 			atomic_set(&iprtd->capturing, 0);
137 		if (!atomic_read(&iprtd->playing) &&
138 				!atomic_read(&iprtd->capturing))
139 			disable_fiq(imx_pcm_fiq);
140 		break;
141 
142 	default:
143 		return -EINVAL;
144 	}
145 
146 	return 0;
147 }
148 
149 static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream)
150 {
151 	struct snd_pcm_runtime *runtime = substream->runtime;
152 	struct imx_pcm_runtime_data *iprtd = runtime->private_data;
153 
154 	return bytes_to_frames(substream->runtime, iprtd->offset);
155 }
156 
157 static 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 	.rate_min = 8000,
166 	.channels_min = 2,
167 	.channels_max = 2,
168 	.buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
169 	.period_bytes_min = 128,
170 	.period_bytes_max = 16 * 1024,
171 	.periods_min = 4,
172 	.periods_max = 255,
173 	.fifo_size = 0,
174 };
175 
176 static int snd_imx_open(struct snd_pcm_substream *substream)
177 {
178 	struct snd_pcm_runtime *runtime = substream->runtime;
179 	struct imx_pcm_runtime_data *iprtd;
180 	int ret;
181 
182 	iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
183 	if (iprtd == NULL)
184 		return -ENOMEM;
185 	runtime->private_data = iprtd;
186 
187 	iprtd->substream = substream;
188 
189 	atomic_set(&iprtd->playing, 0);
190 	atomic_set(&iprtd->capturing, 0);
191 	hrtimer_init(&iprtd->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
192 	iprtd->hrt.function = snd_hrtimer_callback;
193 
194 	ret = snd_pcm_hw_constraint_integer(substream->runtime,
195 			SNDRV_PCM_HW_PARAM_PERIODS);
196 	if (ret < 0) {
197 		kfree(iprtd);
198 		return ret;
199 	}
200 
201 	snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
202 	return 0;
203 }
204 
205 static int snd_imx_close(struct snd_pcm_substream *substream)
206 {
207 	struct snd_pcm_runtime *runtime = substream->runtime;
208 	struct imx_pcm_runtime_data *iprtd = runtime->private_data;
209 
210 	hrtimer_cancel(&iprtd->hrt);
211 
212 	kfree(iprtd);
213 
214 	return 0;
215 }
216 
217 static int snd_imx_pcm_mmap(struct snd_pcm_substream *substream,
218 		struct vm_area_struct *vma)
219 {
220 	struct snd_pcm_runtime *runtime = substream->runtime;
221 	int ret;
222 
223 	ret = dma_mmap_writecombine(substream->pcm->card->dev, vma,
224 		runtime->dma_area, runtime->dma_addr, runtime->dma_bytes);
225 
226 	pr_debug("%s: ret: %d %p 0x%08x 0x%08x\n", __func__, ret,
227 			runtime->dma_area,
228 			runtime->dma_addr,
229 			runtime->dma_bytes);
230 	return ret;
231 }
232 
233 static struct snd_pcm_ops imx_pcm_ops = {
234 	.open		= snd_imx_open,
235 	.close		= snd_imx_close,
236 	.ioctl		= snd_pcm_lib_ioctl,
237 	.hw_params	= snd_imx_pcm_hw_params,
238 	.prepare	= snd_imx_pcm_prepare,
239 	.trigger	= snd_imx_pcm_trigger,
240 	.pointer	= snd_imx_pcm_pointer,
241 	.mmap		= snd_imx_pcm_mmap,
242 };
243 
244 static int imx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
245 {
246 	struct snd_pcm_substream *substream = pcm->streams[stream].substream;
247 	struct snd_dma_buffer *buf = &substream->dma_buffer;
248 	size_t size = IMX_SSI_DMABUF_SIZE;
249 
250 	buf->dev.type = SNDRV_DMA_TYPE_DEV;
251 	buf->dev.dev = pcm->card->dev;
252 	buf->private_data = NULL;
253 	buf->area = dma_alloc_writecombine(pcm->card->dev, size,
254 					   &buf->addr, GFP_KERNEL);
255 	if (!buf->area)
256 		return -ENOMEM;
257 	buf->bytes = size;
258 
259 	return 0;
260 }
261 
262 static int imx_pcm_new(struct snd_soc_pcm_runtime *rtd)
263 {
264 	struct snd_card *card = rtd->card->snd_card;
265 	struct snd_pcm *pcm = rtd->pcm;
266 	int ret;
267 
268 	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
269 	if (ret)
270 		return ret;
271 
272 	if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
273 		ret = imx_pcm_preallocate_dma_buffer(pcm,
274 			SNDRV_PCM_STREAM_PLAYBACK);
275 		if (ret)
276 			goto out;
277 	}
278 
279 	if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
280 		ret = imx_pcm_preallocate_dma_buffer(pcm,
281 			SNDRV_PCM_STREAM_CAPTURE);
282 		if (ret)
283 			goto out;
284 	}
285 
286 out:
287 	return ret;
288 }
289 
290 static int ssi_irq = 0;
291 
292 static int imx_pcm_fiq_new(struct snd_soc_pcm_runtime *rtd)
293 {
294 	struct snd_pcm *pcm = rtd->pcm;
295 	struct snd_pcm_substream *substream;
296 	int ret;
297 
298 	ret = imx_pcm_new(rtd);
299 	if (ret)
300 		return ret;
301 
302 	substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
303 	if (substream) {
304 		struct snd_dma_buffer *buf = &substream->dma_buffer;
305 
306 		imx_ssi_fiq_tx_buffer = (unsigned long)buf->area;
307 	}
308 
309 	substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
310 	if (substream) {
311 		struct snd_dma_buffer *buf = &substream->dma_buffer;
312 
313 		imx_ssi_fiq_rx_buffer = (unsigned long)buf->area;
314 	}
315 
316 	set_fiq_handler(&imx_ssi_fiq_start,
317 		&imx_ssi_fiq_end - &imx_ssi_fiq_start);
318 
319 	return 0;
320 }
321 
322 static void imx_pcm_free(struct snd_pcm *pcm)
323 {
324 	struct snd_pcm_substream *substream;
325 	struct snd_dma_buffer *buf;
326 	int stream;
327 
328 	for (stream = 0; stream < 2; stream++) {
329 		substream = pcm->streams[stream].substream;
330 		if (!substream)
331 			continue;
332 
333 		buf = &substream->dma_buffer;
334 		if (!buf->area)
335 			continue;
336 
337 		dma_free_writecombine(pcm->card->dev, buf->bytes,
338 				      buf->area, buf->addr);
339 		buf->area = NULL;
340 	}
341 }
342 
343 static void imx_pcm_fiq_free(struct snd_pcm *pcm)
344 {
345 	mxc_set_irq_fiq(ssi_irq, 0);
346 	release_fiq(&fh);
347 	imx_pcm_free(pcm);
348 }
349 
350 static struct snd_soc_platform_driver imx_soc_platform_fiq = {
351 	.ops		= &imx_pcm_ops,
352 	.pcm_new	= imx_pcm_fiq_new,
353 	.pcm_free	= imx_pcm_fiq_free,
354 };
355 
356 int imx_pcm_fiq_init(struct platform_device *pdev,
357 		struct imx_pcm_fiq_params *params)
358 {
359 	int ret;
360 
361 	ret = claim_fiq(&fh);
362 	if (ret) {
363 		dev_err(&pdev->dev, "failed to claim fiq: %d", ret);
364 		return ret;
365 	}
366 
367 	mxc_set_irq_fiq(params->irq, 1);
368 	ssi_irq = params->irq;
369 
370 	imx_pcm_fiq = params->irq;
371 
372 	imx_ssi_fiq_base = (unsigned long)params->base;
373 
374 	params->dma_params_tx->maxburst = 4;
375 	params->dma_params_rx->maxburst = 6;
376 
377 	ret = snd_soc_register_platform(&pdev->dev, &imx_soc_platform_fiq);
378 	if (ret)
379 		goto failed_register;
380 
381 	return 0;
382 
383 failed_register:
384 	mxc_set_irq_fiq(ssi_irq, 0);
385 	release_fiq(&fh);
386 
387 	return ret;
388 }
389 EXPORT_SYMBOL_GPL(imx_pcm_fiq_init);
390 
391 void imx_pcm_fiq_exit(struct platform_device *pdev)
392 {
393 	snd_soc_unregister_platform(&pdev->dev);
394 }
395 EXPORT_SYMBOL_GPL(imx_pcm_fiq_exit);
396 
397 MODULE_LICENSE("GPL");
398