xref: /linux/sound/soc/kirkwood/kirkwood-dma.c (revision 0d456bad36d42d16022be045c8a53ddbb59ee478)
1 /*
2  * kirkwood-dma.c
3  *
4  * (c) 2010 Arnaud Patard <apatard@mandriva.com>
5  * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
6  *
7  *  This program is free software; you can redistribute  it and/or modify it
8  *  under  the terms of  the GNU General  Public License as published by the
9  *  Free Software Foundation;  either version 2 of the  License, or (at your
10  *  option) any later version.
11  */
12 
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/io.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/mbus.h>
21 #include <sound/soc.h>
22 #include "kirkwood.h"
23 
24 #define KIRKWOOD_RATES \
25 	(SNDRV_PCM_RATE_8000_192000 |		\
26 	 SNDRV_PCM_RATE_CONTINUOUS |		\
27 	 SNDRV_PCM_RATE_KNOT)
28 
29 #define KIRKWOOD_FORMATS \
30 	(SNDRV_PCM_FMTBIT_S16_LE | \
31 	 SNDRV_PCM_FMTBIT_S24_LE | \
32 	 SNDRV_PCM_FMTBIT_S32_LE | \
33 	 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | \
34 	 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE)
35 
36 struct kirkwood_dma_priv {
37 	struct snd_pcm_substream *play_stream;
38 	struct snd_pcm_substream *rec_stream;
39 	struct kirkwood_dma_data *data;
40 };
41 
42 static struct snd_pcm_hardware kirkwood_dma_snd_hw = {
43 	.info = (SNDRV_PCM_INFO_INTERLEAVED |
44 		 SNDRV_PCM_INFO_MMAP |
45 		 SNDRV_PCM_INFO_MMAP_VALID |
46 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
47 		 SNDRV_PCM_INFO_PAUSE),
48 	.formats		= KIRKWOOD_FORMATS,
49 	.rates			= KIRKWOOD_RATES,
50 	.rate_min		= 8000,
51 	.rate_max		= 384000,
52 	.channels_min		= 1,
53 	.channels_max		= 8,
54 	.buffer_bytes_max	= KIRKWOOD_SND_MAX_PERIOD_BYTES * KIRKWOOD_SND_MAX_PERIODS,
55 	.period_bytes_min	= KIRKWOOD_SND_MIN_PERIOD_BYTES,
56 	.period_bytes_max	= KIRKWOOD_SND_MAX_PERIOD_BYTES,
57 	.periods_min		= KIRKWOOD_SND_MIN_PERIODS,
58 	.periods_max		= KIRKWOOD_SND_MAX_PERIODS,
59 	.fifo_size		= 0,
60 };
61 
62 static u64 kirkwood_dma_dmamask = DMA_BIT_MASK(32);
63 
64 static irqreturn_t kirkwood_dma_irq(int irq, void *dev_id)
65 {
66 	struct kirkwood_dma_priv *prdata = dev_id;
67 	struct kirkwood_dma_data *priv = prdata->data;
68 	unsigned long mask, status, cause;
69 
70 	mask = readl(priv->io + KIRKWOOD_INT_MASK);
71 	status = readl(priv->io + KIRKWOOD_INT_CAUSE) & mask;
72 
73 	cause = readl(priv->io + KIRKWOOD_ERR_CAUSE);
74 	if (unlikely(cause)) {
75 		printk(KERN_WARNING "%s: got err interrupt 0x%lx\n",
76 				__func__, cause);
77 		writel(cause, priv->io + KIRKWOOD_ERR_CAUSE);
78 	}
79 
80 	/* we've enabled only bytes interrupts ... */
81 	if (status & ~(KIRKWOOD_INT_CAUSE_PLAY_BYTES | \
82 			KIRKWOOD_INT_CAUSE_REC_BYTES)) {
83 		printk(KERN_WARNING "%s: unexpected interrupt %lx\n",
84 			__func__, status);
85 		return IRQ_NONE;
86 	}
87 
88 	/* ack int */
89 	writel(status, priv->io + KIRKWOOD_INT_CAUSE);
90 
91 	if (status & KIRKWOOD_INT_CAUSE_PLAY_BYTES)
92 		snd_pcm_period_elapsed(prdata->play_stream);
93 
94 	if (status & KIRKWOOD_INT_CAUSE_REC_BYTES)
95 		snd_pcm_period_elapsed(prdata->rec_stream);
96 
97 	return IRQ_HANDLED;
98 }
99 
100 static void
101 kirkwood_dma_conf_mbus_windows(void __iomem *base, int win,
102 			       unsigned long dma,
103 			       const struct mbus_dram_target_info *dram)
104 {
105 	int i;
106 
107 	/* First disable and clear windows */
108 	writel(0, base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
109 	writel(0, base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
110 
111 	/* try to find matching cs for current dma address */
112 	for (i = 0; i < dram->num_cs; i++) {
113 		const struct mbus_dram_window *cs = dram->cs + i;
114 		if ((cs->base & 0xffff0000) < (dma & 0xffff0000)) {
115 			writel(cs->base & 0xffff0000,
116 				base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
117 			writel(((cs->size - 1) & 0xffff0000) |
118 				(cs->mbus_attr << 8) |
119 				(dram->mbus_dram_target_id << 4) | 1,
120 				base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
121 		}
122 	}
123 }
124 
125 static int kirkwood_dma_open(struct snd_pcm_substream *substream)
126 {
127 	int err;
128 	struct snd_pcm_runtime *runtime = substream->runtime;
129 	struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
130 	struct snd_soc_platform *platform = soc_runtime->platform;
131 	struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
132 	struct kirkwood_dma_data *priv;
133 	struct kirkwood_dma_priv *prdata = snd_soc_platform_get_drvdata(platform);
134 	const struct mbus_dram_target_info *dram;
135 	unsigned long addr;
136 
137 	priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
138 	snd_soc_set_runtime_hwparams(substream, &kirkwood_dma_snd_hw);
139 
140 	/* Ensure that all constraints linked to dma burst are fulfilled */
141 	err = snd_pcm_hw_constraint_minmax(runtime,
142 			SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
143 			priv->burst * 2,
144 			KIRKWOOD_AUDIO_BUF_MAX-1);
145 	if (err < 0)
146 		return err;
147 
148 	err = snd_pcm_hw_constraint_step(runtime, 0,
149 			SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
150 			priv->burst);
151 	if (err < 0)
152 		return err;
153 
154 	err = snd_pcm_hw_constraint_step(substream->runtime, 0,
155 			 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
156 			 priv->burst);
157 	if (err < 0)
158 		return err;
159 
160 	if (prdata == NULL) {
161 		prdata = kzalloc(sizeof(struct kirkwood_dma_priv), GFP_KERNEL);
162 		if (prdata == NULL)
163 			return -ENOMEM;
164 
165 		prdata->data = priv;
166 
167 		err = request_irq(priv->irq, kirkwood_dma_irq, IRQF_SHARED,
168 				  "kirkwood-i2s", prdata);
169 		if (err) {
170 			kfree(prdata);
171 			return -EBUSY;
172 		}
173 
174 		snd_soc_platform_set_drvdata(platform, prdata);
175 
176 		/*
177 		 * Enable Error interrupts. We're only ack'ing them but
178 		 * it's useful for diagnostics
179 		 */
180 		writel((unsigned long)-1, priv->io + KIRKWOOD_ERR_MASK);
181 	}
182 
183 	dram = mv_mbus_dram_info();
184 	addr = substream->dma_buffer.addr;
185 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
186 		prdata->play_stream = substream;
187 		kirkwood_dma_conf_mbus_windows(priv->io,
188 			KIRKWOOD_PLAYBACK_WIN, addr, dram);
189 	} else {
190 		prdata->rec_stream = substream;
191 		kirkwood_dma_conf_mbus_windows(priv->io,
192 			KIRKWOOD_RECORD_WIN, addr, dram);
193 	}
194 
195 	return 0;
196 }
197 
198 static int kirkwood_dma_close(struct snd_pcm_substream *substream)
199 {
200 	struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
201 	struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
202 	struct snd_soc_platform *platform = soc_runtime->platform;
203 	struct kirkwood_dma_priv *prdata = snd_soc_platform_get_drvdata(platform);
204 	struct kirkwood_dma_data *priv;
205 
206 	priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
207 
208 	if (!prdata || !priv)
209 		return 0;
210 
211 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
212 		prdata->play_stream = NULL;
213 	else
214 		prdata->rec_stream = NULL;
215 
216 	if (!prdata->play_stream && !prdata->rec_stream) {
217 		writel(0, priv->io + KIRKWOOD_ERR_MASK);
218 		free_irq(priv->irq, prdata);
219 		kfree(prdata);
220 		snd_soc_platform_set_drvdata(platform, NULL);
221 	}
222 
223 	return 0;
224 }
225 
226 static int kirkwood_dma_hw_params(struct snd_pcm_substream *substream,
227 				struct snd_pcm_hw_params *params)
228 {
229 	struct snd_pcm_runtime *runtime = substream->runtime;
230 
231 	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
232 	runtime->dma_bytes = params_buffer_bytes(params);
233 
234 	return 0;
235 }
236 
237 static int kirkwood_dma_hw_free(struct snd_pcm_substream *substream)
238 {
239 	snd_pcm_set_runtime_buffer(substream, NULL);
240 	return 0;
241 }
242 
243 static int kirkwood_dma_prepare(struct snd_pcm_substream *substream)
244 {
245 	struct snd_pcm_runtime *runtime = substream->runtime;
246 	struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
247 	struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
248 	struct kirkwood_dma_data *priv;
249 	unsigned long size, count;
250 
251 	priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
252 
253 	/* compute buffer size in term of "words" as requested in specs */
254 	size = frames_to_bytes(runtime, runtime->buffer_size);
255 	size = (size>>2)-1;
256 	count = snd_pcm_lib_period_bytes(substream);
257 
258 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
259 		writel(count, priv->io + KIRKWOOD_PLAY_BYTE_INT_COUNT);
260 		writel(runtime->dma_addr, priv->io + KIRKWOOD_PLAY_BUF_ADDR);
261 		writel(size, priv->io + KIRKWOOD_PLAY_BUF_SIZE);
262 	} else {
263 		writel(count, priv->io + KIRKWOOD_REC_BYTE_INT_COUNT);
264 		writel(runtime->dma_addr, priv->io + KIRKWOOD_REC_BUF_ADDR);
265 		writel(size, priv->io + KIRKWOOD_REC_BUF_SIZE);
266 	}
267 
268 
269 	return 0;
270 }
271 
272 static snd_pcm_uframes_t kirkwood_dma_pointer(struct snd_pcm_substream
273 						*substream)
274 {
275 	struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
276 	struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
277 	struct kirkwood_dma_data *priv;
278 	snd_pcm_uframes_t count;
279 
280 	priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
281 
282 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
283 		count = bytes_to_frames(substream->runtime,
284 			readl(priv->io + KIRKWOOD_PLAY_BYTE_COUNT));
285 	else
286 		count = bytes_to_frames(substream->runtime,
287 			readl(priv->io + KIRKWOOD_REC_BYTE_COUNT));
288 
289 	return count;
290 }
291 
292 struct snd_pcm_ops kirkwood_dma_ops = {
293 	.open =		kirkwood_dma_open,
294 	.close =        kirkwood_dma_close,
295 	.ioctl =	snd_pcm_lib_ioctl,
296 	.hw_params =	kirkwood_dma_hw_params,
297 	.hw_free =      kirkwood_dma_hw_free,
298 	.prepare =      kirkwood_dma_prepare,
299 	.pointer =	kirkwood_dma_pointer,
300 };
301 
302 static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm *pcm,
303 		int stream)
304 {
305 	struct snd_pcm_substream *substream = pcm->streams[stream].substream;
306 	struct snd_dma_buffer *buf = &substream->dma_buffer;
307 	size_t size = kirkwood_dma_snd_hw.buffer_bytes_max;
308 
309 	buf->dev.type = SNDRV_DMA_TYPE_DEV;
310 	buf->dev.dev = pcm->card->dev;
311 	buf->area = dma_alloc_coherent(pcm->card->dev, size,
312 			&buf->addr, GFP_KERNEL);
313 	if (!buf->area)
314 		return -ENOMEM;
315 	buf->bytes = size;
316 	buf->private_data = NULL;
317 
318 	return 0;
319 }
320 
321 static int kirkwood_dma_new(struct snd_soc_pcm_runtime *rtd)
322 {
323 	struct snd_card *card = rtd->card->snd_card;
324 	struct snd_pcm *pcm = rtd->pcm;
325 	int ret;
326 
327 	if (!card->dev->dma_mask)
328 		card->dev->dma_mask = &kirkwood_dma_dmamask;
329 	if (!card->dev->coherent_dma_mask)
330 		card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
331 
332 	if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
333 		ret = kirkwood_dma_preallocate_dma_buffer(pcm,
334 				SNDRV_PCM_STREAM_PLAYBACK);
335 		if (ret)
336 			return ret;
337 	}
338 
339 	if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
340 		ret = kirkwood_dma_preallocate_dma_buffer(pcm,
341 				SNDRV_PCM_STREAM_CAPTURE);
342 		if (ret)
343 			return ret;
344 	}
345 
346 	return 0;
347 }
348 
349 static void kirkwood_dma_free_dma_buffers(struct snd_pcm *pcm)
350 {
351 	struct snd_pcm_substream *substream;
352 	struct snd_dma_buffer *buf;
353 	int stream;
354 
355 	for (stream = 0; stream < 2; stream++) {
356 		substream = pcm->streams[stream].substream;
357 		if (!substream)
358 			continue;
359 		buf = &substream->dma_buffer;
360 		if (!buf->area)
361 			continue;
362 
363 		dma_free_coherent(pcm->card->dev, buf->bytes,
364 				buf->area, buf->addr);
365 		buf->area = NULL;
366 	}
367 }
368 
369 static struct snd_soc_platform_driver kirkwood_soc_platform = {
370 	.ops		= &kirkwood_dma_ops,
371 	.pcm_new	= kirkwood_dma_new,
372 	.pcm_free	= kirkwood_dma_free_dma_buffers,
373 };
374 
375 static int kirkwood_soc_platform_probe(struct platform_device *pdev)
376 {
377 	return snd_soc_register_platform(&pdev->dev, &kirkwood_soc_platform);
378 }
379 
380 static int kirkwood_soc_platform_remove(struct platform_device *pdev)
381 {
382 	snd_soc_unregister_platform(&pdev->dev);
383 	return 0;
384 }
385 
386 static struct platform_driver kirkwood_pcm_driver = {
387 	.driver = {
388 			.name = "kirkwood-pcm-audio",
389 			.owner = THIS_MODULE,
390 	},
391 
392 	.probe = kirkwood_soc_platform_probe,
393 	.remove = kirkwood_soc_platform_remove,
394 };
395 
396 module_platform_driver(kirkwood_pcm_driver);
397 
398 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
399 MODULE_DESCRIPTION("Marvell Kirkwood Audio DMA module");
400 MODULE_LICENSE("GPL");
401 MODULE_ALIAS("platform:kirkwood-pcm-audio");
402