aboutsummaryrefslogtreecommitdiffstats
path: root/sound
diff options
context:
space:
mode:
authorJon Smirl <jonsmirl@gmail.com>2009-05-23 19:12:59 -0400
committerMark Brown <broonie@opensource.wolfsonmicro.com>2009-05-24 14:31:03 -0400
commit89dd08425273773fd33fc85d48d152c5679b2fb4 (patch)
tree76ea69d61d9b2e204c77d421dc3d8419681a526e /sound
parent05e1efa2deb42b1bd548208e5c43f471e2cf0da1 (diff)
ASoC: Basic split of mpc5200 DMA code out of mpc5200_psc_i2s
Basic split of mpc5200 DMA code out from i2s into a standalone file. Signed-off-by: Jon Smirl <jonsmirl@gmail.com> Acked-by: Grant Likely <grant.likely@secretlab.ca> Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Diffstat (limited to 'sound')
-rw-r--r--sound/soc/fsl/Kconfig4
-rw-r--r--sound/soc/fsl/Makefile2
-rw-r--r--sound/soc/fsl/mpc5200_dma.c458
-rw-r--r--sound/soc/fsl/mpc5200_dma.h81
-rw-r--r--sound/soc/fsl/mpc5200_psc_i2s.c485
5 files changed, 547 insertions, 483 deletions
diff --git a/sound/soc/fsl/Kconfig b/sound/soc/fsl/Kconfig
index 9fc908283371..dc79bdf33692 100644
--- a/sound/soc/fsl/Kconfig
+++ b/sound/soc/fsl/Kconfig
@@ -1,5 +1,8 @@
1config SND_SOC_OF_SIMPLE 1config SND_SOC_OF_SIMPLE
2 tristate 2 tristate
3
4config SND_MPC52xx_DMA
5 tristate
3 6
4# ASoC platform support for the Freescale MPC8610 SOC. This compiles drivers 7# ASoC platform support for the Freescale MPC8610 SOC. This compiles drivers
5# for the SSI and the Elo DMA controller. You will still need to select 8# for the SSI and the Elo DMA controller. You will still need to select
@@ -23,6 +26,7 @@ config SND_SOC_MPC5200_I2S
23 tristate "Freescale MPC5200 PSC in I2S mode driver" 26 tristate "Freescale MPC5200 PSC in I2S mode driver"
24 depends on PPC_MPC52xx && PPC_BESTCOMM 27 depends on PPC_MPC52xx && PPC_BESTCOMM
25 select SND_SOC_OF_SIMPLE 28 select SND_SOC_OF_SIMPLE
29 select SND_MPC52xx_DMA
26 select PPC_BESTCOMM_GEN_BD 30 select PPC_BESTCOMM_GEN_BD
27 help 31 help
28 Say Y here to support the MPC5200 PSCs in I2S mode. 32 Say Y here to support the MPC5200 PSCs in I2S mode.
diff --git a/sound/soc/fsl/Makefile b/sound/soc/fsl/Makefile
index f85134c86387..7731ef2539bb 100644
--- a/sound/soc/fsl/Makefile
+++ b/sound/soc/fsl/Makefile
@@ -10,5 +10,7 @@ snd-soc-fsl-ssi-objs := fsl_ssi.o
10snd-soc-fsl-dma-objs := fsl_dma.o 10snd-soc-fsl-dma-objs := fsl_dma.o
11obj-$(CONFIG_SND_SOC_MPC8610) += snd-soc-fsl-ssi.o snd-soc-fsl-dma.o 11obj-$(CONFIG_SND_SOC_MPC8610) += snd-soc-fsl-ssi.o snd-soc-fsl-dma.o
12 12
13# MPC5200 Platform Support
14obj-$(CONFIG_SND_MPC52xx_DMA) += mpc5200_dma.o
13obj-$(CONFIG_SND_SOC_MPC5200_I2S) += mpc5200_psc_i2s.o 15obj-$(CONFIG_SND_SOC_MPC5200_I2S) += mpc5200_psc_i2s.o
14 16
diff --git a/sound/soc/fsl/mpc5200_dma.c b/sound/soc/fsl/mpc5200_dma.c
new file mode 100644
index 000000000000..4bae8d6e1a6a
--- /dev/null
+++ b/sound/soc/fsl/mpc5200_dma.c
@@ -0,0 +1,458 @@
1/*
2 * Freescale MPC5200 PSC DMA
3 * ALSA SoC Platform driver
4 *
5 * Copyright (C) 2008 Secret Lab Technologies Ltd.
6 */
7
8#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/interrupt.h>
11#include <linux/device.h>
12#include <linux/delay.h>
13#include <linux/of_device.h>
14#include <linux/of_platform.h>
15#include <linux/dma-mapping.h>
16
17#include <sound/core.h>
18#include <sound/pcm.h>
19#include <sound/pcm_params.h>
20#include <sound/initval.h>
21#include <sound/soc.h>
22#include <sound/soc-of-simple.h>
23
24#include <sysdev/bestcomm/bestcomm.h>
25#include <sysdev/bestcomm/gen_bd.h>
26#include <asm/mpc52xx_psc.h>
27
28#include "mpc5200_dma.h"
29
30MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
31MODULE_DESCRIPTION("Freescale MPC5200 PSC in DMA mode ASoC Driver");
32MODULE_LICENSE("GPL");
33
34/*
35 * Interrupt handlers
36 */
37static irqreturn_t psc_i2s_status_irq(int irq, void *_psc_i2s)
38{
39 struct psc_i2s *psc_i2s = _psc_i2s;
40 struct mpc52xx_psc __iomem *regs = psc_i2s->psc_regs;
41 u16 isr;
42
43 isr = in_be16(&regs->mpc52xx_psc_isr);
44
45 /* Playback underrun error */
46 if (psc_i2s->playback.active && (isr & MPC52xx_PSC_IMR_TXEMP))
47 psc_i2s->stats.underrun_count++;
48
49 /* Capture overrun error */
50 if (psc_i2s->capture.active && (isr & MPC52xx_PSC_IMR_ORERR))
51 psc_i2s->stats.overrun_count++;
52
53 out_8(&regs->command, 4 << 4); /* reset the error status */
54
55 return IRQ_HANDLED;
56}
57
58/**
59 * psc_i2s_bcom_enqueue_next_buffer - Enqueue another audio buffer
60 * @s: pointer to stream private data structure
61 *
62 * Enqueues another audio period buffer into the bestcomm queue.
63 *
64 * Note: The routine must only be called when there is space available in
65 * the queue. Otherwise the enqueue will fail and the audio ring buffer
66 * will get out of sync
67 */
68static void psc_i2s_bcom_enqueue_next_buffer(struct psc_i2s_stream *s)
69{
70 struct bcom_bd *bd;
71
72 /* Prepare and enqueue the next buffer descriptor */
73 bd = bcom_prepare_next_buffer(s->bcom_task);
74 bd->status = s->period_bytes;
75 bd->data[0] = s->period_next_pt;
76 bcom_submit_next_buffer(s->bcom_task, NULL);
77
78 /* Update for next period */
79 s->period_next_pt += s->period_bytes;
80 if (s->period_next_pt >= s->period_end)
81 s->period_next_pt = s->period_start;
82}
83
84/* Bestcomm DMA irq handler */
85static irqreturn_t psc_i2s_bcom_irq(int irq, void *_psc_i2s_stream)
86{
87 struct psc_i2s_stream *s = _psc_i2s_stream;
88
89 /* For each finished period, dequeue the completed period buffer
90 * and enqueue a new one in it's place. */
91 while (bcom_buffer_done(s->bcom_task)) {
92 bcom_retrieve_buffer(s->bcom_task, NULL, NULL);
93 s->period_current_pt += s->period_bytes;
94 if (s->period_current_pt >= s->period_end)
95 s->period_current_pt = s->period_start;
96 psc_i2s_bcom_enqueue_next_buffer(s);
97 bcom_enable(s->bcom_task);
98 }
99
100 /* If the stream is active, then also inform the PCM middle layer
101 * of the period finished event. */
102 if (s->active)
103 snd_pcm_period_elapsed(s->stream);
104
105 return IRQ_HANDLED;
106}
107
108/**
109 * psc_i2s_startup: create a new substream
110 *
111 * This is the first function called when a stream is opened.
112 *
113 * If this is the first stream open, then grab the IRQ and program most of
114 * the PSC registers.
115 */
116int psc_i2s_startup(struct snd_pcm_substream *substream,
117 struct snd_soc_dai *dai)
118{
119 struct snd_soc_pcm_runtime *rtd = substream->private_data;
120 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
121 int rc;
122
123 dev_dbg(psc_i2s->dev, "psc_i2s_startup(substream=%p)\n", substream);
124
125 if (!psc_i2s->playback.active &&
126 !psc_i2s->capture.active) {
127 /* Setup the IRQs */
128 rc = request_irq(psc_i2s->irq, &psc_i2s_status_irq, IRQF_SHARED,
129 "psc-i2s-status", psc_i2s);
130 rc |= request_irq(psc_i2s->capture.irq,
131 &psc_i2s_bcom_irq, IRQF_SHARED,
132 "psc-i2s-capture", &psc_i2s->capture);
133 rc |= request_irq(psc_i2s->playback.irq,
134 &psc_i2s_bcom_irq, IRQF_SHARED,
135 "psc-i2s-playback", &psc_i2s->playback);
136 if (rc) {
137 free_irq(psc_i2s->irq, psc_i2s);
138 free_irq(psc_i2s->capture.irq,
139 &psc_i2s->capture);
140 free_irq(psc_i2s->playback.irq,
141 &psc_i2s->playback);
142 return -ENODEV;
143 }
144 }
145
146 return 0;
147}
148
149int psc_i2s_hw_free(struct snd_pcm_substream *substream,
150 struct snd_soc_dai *dai)
151{
152 snd_pcm_set_runtime_buffer(substream, NULL);
153 return 0;
154}
155
156/**
157 * psc_i2s_trigger: start and stop the DMA transfer.
158 *
159 * This function is called by ALSA to start, stop, pause, and resume the DMA
160 * transfer of data.
161 */
162int psc_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
163 struct snd_soc_dai *dai)
164{
165 struct snd_soc_pcm_runtime *rtd = substream->private_data;
166 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
167 struct snd_pcm_runtime *runtime = substream->runtime;
168 struct psc_i2s_stream *s;
169 struct mpc52xx_psc __iomem *regs = psc_i2s->psc_regs;
170 u16 imr;
171 u8 psc_cmd;
172 unsigned long flags;
173
174 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
175 s = &psc_i2s->capture;
176 else
177 s = &psc_i2s->playback;
178
179 dev_dbg(psc_i2s->dev, "psc_i2s_trigger(substream=%p, cmd=%i)"
180 " stream_id=%i\n",
181 substream, cmd, substream->pstr->stream);
182
183 switch (cmd) {
184 case SNDRV_PCM_TRIGGER_START:
185 s->period_bytes = frames_to_bytes(runtime,
186 runtime->period_size);
187 s->period_start = virt_to_phys(runtime->dma_area);
188 s->period_end = s->period_start +
189 (s->period_bytes * runtime->periods);
190 s->period_next_pt = s->period_start;
191 s->period_current_pt = s->period_start;
192 s->active = 1;
193
194 /* First; reset everything */
195 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
196 out_8(&regs->command, MPC52xx_PSC_RST_RX);
197 out_8(&regs->command, MPC52xx_PSC_RST_ERR_STAT);
198 } else {
199 out_8(&regs->command, MPC52xx_PSC_RST_TX);
200 out_8(&regs->command, MPC52xx_PSC_RST_ERR_STAT);
201 }
202
203 /* Next, fill up the bestcomm bd queue and enable DMA.
204 * This will begin filling the PSC's fifo. */
205 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
206 bcom_gen_bd_rx_reset(s->bcom_task);
207 else
208 bcom_gen_bd_tx_reset(s->bcom_task);
209 while (!bcom_queue_full(s->bcom_task))
210 psc_i2s_bcom_enqueue_next_buffer(s);
211 bcom_enable(s->bcom_task);
212
213 /* Due to errata in the i2s mode; need to line up enabling
214 * the transmitter with a transition on the frame sync
215 * line */
216
217 spin_lock_irqsave(&psc_i2s->lock, flags);
218 /* first make sure it is low */
219 while ((in_8(&regs->ipcr_acr.ipcr) & 0x80) != 0)
220 ;
221 /* then wait for the transition to high */
222 while ((in_8(&regs->ipcr_acr.ipcr) & 0x80) == 0)
223 ;
224 /* Finally, enable the PSC.
225 * Receiver must always be enabled; even when we only want
226 * transmit. (see 15.3.2.3 of MPC5200B User's Guide) */
227 psc_cmd = MPC52xx_PSC_RX_ENABLE;
228 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK)
229 psc_cmd |= MPC52xx_PSC_TX_ENABLE;
230 out_8(&regs->command, psc_cmd);
231 spin_unlock_irqrestore(&psc_i2s->lock, flags);
232
233 break;
234
235 case SNDRV_PCM_TRIGGER_STOP:
236 /* Turn off the PSC */
237 s->active = 0;
238 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
239 if (!psc_i2s->playback.active) {
240 out_8(&regs->command, 2 << 4); /* reset rx */
241 out_8(&regs->command, 3 << 4); /* reset tx */
242 out_8(&regs->command, 4 << 4); /* reset err */
243 }
244 } else {
245 out_8(&regs->command, 3 << 4); /* reset tx */
246 out_8(&regs->command, 4 << 4); /* reset err */
247 if (!psc_i2s->capture.active)
248 out_8(&regs->command, 2 << 4); /* reset rx */
249 }
250
251 bcom_disable(s->bcom_task);
252 while (!bcom_queue_empty(s->bcom_task))
253 bcom_retrieve_buffer(s->bcom_task, NULL, NULL);
254
255 break;
256
257 default:
258 dev_dbg(psc_i2s->dev, "invalid command\n");
259 return -EINVAL;
260 }
261
262 /* Update interrupt enable settings */
263 imr = 0;
264 if (psc_i2s->playback.active)
265 imr |= MPC52xx_PSC_IMR_TXEMP;
266 if (psc_i2s->capture.active)
267 imr |= MPC52xx_PSC_IMR_ORERR;
268 out_be16(&regs->isr_imr.imr, imr);
269
270 return 0;
271}
272
273/**
274 * psc_i2s_shutdown: shutdown the data transfer on a stream
275 *
276 * Shutdown the PSC if there are no other substreams open.
277 */
278void psc_i2s_shutdown(struct snd_pcm_substream *substream,
279 struct snd_soc_dai *dai)
280{
281 struct snd_soc_pcm_runtime *rtd = substream->private_data;
282 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
283
284 dev_dbg(psc_i2s->dev, "psc_i2s_shutdown(substream=%p)\n", substream);
285
286 /*
287 * If this is the last active substream, disable the PSC and release
288 * the IRQ.
289 */
290 if (!psc_i2s->playback.active &&
291 !psc_i2s->capture.active) {
292
293 /* Disable all interrupts and reset the PSC */
294 out_be16(&psc_i2s->psc_regs->isr_imr.imr, 0);
295 out_8(&psc_i2s->psc_regs->command, 3 << 4); /* reset tx */
296 out_8(&psc_i2s->psc_regs->command, 2 << 4); /* reset rx */
297 out_8(&psc_i2s->psc_regs->command, 1 << 4); /* reset mode */
298 out_8(&psc_i2s->psc_regs->command, 4 << 4); /* reset error */
299
300 /* Release irqs */
301 free_irq(psc_i2s->irq, psc_i2s);
302 free_irq(psc_i2s->capture.irq, &psc_i2s->capture);
303 free_irq(psc_i2s->playback.irq, &psc_i2s->playback);
304 }
305}
306
307/* ---------------------------------------------------------------------
308 * The PSC DMA 'ASoC platform' driver
309 *
310 * Can be referenced by an 'ASoC machine' driver
311 * This driver only deals with the audio bus; it doesn't have any
312 * interaction with the attached codec
313 */
314
315static const struct snd_pcm_hardware psc_i2s_pcm_hardware = {
316 .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
317 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
318 SNDRV_PCM_INFO_BATCH,
319 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE |
320 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE,
321 .rate_min = 8000,
322 .rate_max = 48000,
323 .channels_min = 2,
324 .channels_max = 2,
325 .period_bytes_max = 1024 * 1024,
326 .period_bytes_min = 32,
327 .periods_min = 2,
328 .periods_max = 256,
329 .buffer_bytes_max = 2 * 1024 * 1024,
330 .fifo_size = 0,
331};
332
333static int psc_i2s_pcm_open(struct snd_pcm_substream *substream)
334{
335 struct snd_soc_pcm_runtime *rtd = substream->private_data;
336 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
337 struct psc_i2s_stream *s;
338
339 dev_dbg(psc_i2s->dev, "psc_i2s_pcm_open(substream=%p)\n", substream);
340
341 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
342 s = &psc_i2s->capture;
343 else
344 s = &psc_i2s->playback;
345
346 snd_soc_set_runtime_hwparams(substream, &psc_i2s_pcm_hardware);
347
348 s->stream = substream;
349 return 0;
350}
351
352static int psc_i2s_pcm_close(struct snd_pcm_substream *substream)
353{
354 struct snd_soc_pcm_runtime *rtd = substream->private_data;
355 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
356 struct psc_i2s_stream *s;
357
358 dev_dbg(psc_i2s->dev, "psc_i2s_pcm_close(substream=%p)\n", substream);
359
360 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
361 s = &psc_i2s->capture;
362 else
363 s = &psc_i2s->playback;
364
365 s->stream = NULL;
366 return 0;
367}
368
369static snd_pcm_uframes_t
370psc_i2s_pcm_pointer(struct snd_pcm_substream *substream)
371{
372 struct snd_soc_pcm_runtime *rtd = substream->private_data;
373 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
374 struct psc_i2s_stream *s;
375 dma_addr_t count;
376
377 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
378 s = &psc_i2s->capture;
379 else
380 s = &psc_i2s->playback;
381
382 count = s->period_current_pt - s->period_start;
383
384 return bytes_to_frames(substream->runtime, count);
385}
386
387static struct snd_pcm_ops psc_i2s_pcm_ops = {
388 .open = psc_i2s_pcm_open,
389 .close = psc_i2s_pcm_close,
390 .ioctl = snd_pcm_lib_ioctl,
391 .pointer = psc_i2s_pcm_pointer,
392};
393
394static u64 psc_i2s_pcm_dmamask = 0xffffffff;
395static int psc_i2s_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
396 struct snd_pcm *pcm)
397{
398 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
399 size_t size = psc_i2s_pcm_hardware.buffer_bytes_max;
400 int rc = 0;
401
402 dev_dbg(rtd->socdev->dev, "psc_i2s_pcm_new(card=%p, dai=%p, pcm=%p)\n",
403 card, dai, pcm);
404
405 if (!card->dev->dma_mask)
406 card->dev->dma_mask = &psc_i2s_pcm_dmamask;
407 if (!card->dev->coherent_dma_mask)
408 card->dev->coherent_dma_mask = 0xffffffff;
409
410 if (pcm->streams[0].substream) {
411 rc = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->dev, size,
412 &pcm->streams[0].substream->dma_buffer);
413 if (rc)
414 goto playback_alloc_err;
415 }
416
417 if (pcm->streams[1].substream) {
418 rc = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->dev, size,
419 &pcm->streams[1].substream->dma_buffer);
420 if (rc)
421 goto capture_alloc_err;
422 }
423
424 return 0;
425
426 capture_alloc_err:
427 if (pcm->streams[0].substream)
428 snd_dma_free_pages(&pcm->streams[0].substream->dma_buffer);
429 playback_alloc_err:
430 dev_err(card->dev, "Cannot allocate buffer(s)\n");
431 return -ENOMEM;
432}
433
434static void psc_i2s_pcm_free(struct snd_pcm *pcm)
435{
436 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
437 struct snd_pcm_substream *substream;
438 int stream;
439
440 dev_dbg(rtd->socdev->dev, "psc_i2s_pcm_free(pcm=%p)\n", pcm);
441
442 for (stream = 0; stream < 2; stream++) {
443 substream = pcm->streams[stream].substream;
444 if (substream) {
445 snd_dma_free_pages(&substream->dma_buffer);
446 substream->dma_buffer.area = NULL;
447 substream->dma_buffer.addr = 0;
448 }
449 }
450}
451
452struct snd_soc_platform psc_i2s_pcm_soc_platform = {
453 .name = "mpc5200-psc-audio",
454 .pcm_ops = &psc_i2s_pcm_ops,
455 .pcm_new = &psc_i2s_pcm_new,
456 .pcm_free = &psc_i2s_pcm_free,
457};
458
diff --git a/sound/soc/fsl/mpc5200_dma.h b/sound/soc/fsl/mpc5200_dma.h
new file mode 100644
index 000000000000..9a19e8a70c5f
--- /dev/null
+++ b/sound/soc/fsl/mpc5200_dma.h
@@ -0,0 +1,81 @@
1/*
2 * Freescale MPC5200 Audio DMA driver
3 */
4
5#ifndef __SOUND_SOC_FSL_MPC5200_DMA_H__
6#define __SOUND_SOC_FSL_MPC5200_DMA_H__
7
8/**
9 * psc_i2s_stream - Data specific to a single stream (playback or capture)
10 * @active: flag indicating if the stream is active
11 * @psc_i2s: pointer back to parent psc_i2s data structure
12 * @bcom_task: bestcomm task structure
13 * @irq: irq number for bestcomm task
14 * @period_start: physical address of start of DMA region
15 * @period_end: physical address of end of DMA region
16 * @period_next_pt: physical address of next DMA buffer to enqueue
17 * @period_bytes: size of DMA period in bytes
18 */
19struct psc_i2s_stream {
20 int active;
21 struct psc_i2s *psc_i2s;
22 struct bcom_task *bcom_task;
23 int irq;
24 struct snd_pcm_substream *stream;
25 dma_addr_t period_start;
26 dma_addr_t period_end;
27 dma_addr_t period_next_pt;
28 dma_addr_t period_current_pt;
29 int period_bytes;
30};
31
32/**
33 * psc_i2s - Private driver data
34 * @name: short name for this device ("PSC0", "PSC1", etc)
35 * @psc_regs: pointer to the PSC's registers
36 * @fifo_regs: pointer to the PSC's FIFO registers
37 * @irq: IRQ of this PSC
38 * @dev: struct device pointer
39 * @dai: the CPU DAI for this device
40 * @sicr: Base value used in serial interface control register; mode is ORed
41 * with this value.
42 * @playback: Playback stream context data
43 * @capture: Capture stream context data
44 */
45struct psc_i2s {
46 char name[32];
47 struct mpc52xx_psc __iomem *psc_regs;
48 struct mpc52xx_psc_fifo __iomem *fifo_regs;
49 unsigned int irq;
50 struct device *dev;
51 struct snd_soc_dai dai;
52 spinlock_t lock;
53 u32 sicr;
54
55 /* per-stream data */
56 struct psc_i2s_stream playback;
57 struct psc_i2s_stream capture;
58
59 /* Statistics */
60 struct {
61 int overrun_count;
62 int underrun_count;
63 } stats;
64};
65
66
67int psc_i2s_startup(struct snd_pcm_substream *substream,
68 struct snd_soc_dai *dai);
69
70int psc_i2s_hw_free(struct snd_pcm_substream *substream,
71 struct snd_soc_dai *dai);
72
73void psc_i2s_shutdown(struct snd_pcm_substream *substream,
74 struct snd_soc_dai *dai);
75
76int psc_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
77 struct snd_soc_dai *dai);
78
79extern struct snd_soc_platform psc_i2s_pcm_soc_platform;
80
81#endif /* __SOUND_SOC_FSL_MPC5200_DMA_H__ */
diff --git a/sound/soc/fsl/mpc5200_psc_i2s.c b/sound/soc/fsl/mpc5200_psc_i2s.c
index 1111c710118a..8974b53eec9f 100644
--- a/sound/soc/fsl/mpc5200_psc_i2s.c
+++ b/sound/soc/fsl/mpc5200_psc_i2s.c
@@ -25,6 +25,8 @@
25#include <sysdev/bestcomm/gen_bd.h> 25#include <sysdev/bestcomm/gen_bd.h>
26#include <asm/mpc52xx_psc.h> 26#include <asm/mpc52xx_psc.h>
27 27
28#include "mpc5200_dma.h"
29
28MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>"); 30MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
29MODULE_DESCRIPTION("Freescale MPC5200 PSC in I2S mode ASoC Driver"); 31MODULE_DESCRIPTION("Freescale MPC5200 PSC in I2S mode ASoC Driver");
30MODULE_LICENSE("GPL"); 32MODULE_LICENSE("GPL");
@@ -47,179 +49,6 @@ MODULE_LICENSE("GPL");
47 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE | \ 49 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE | \
48 SNDRV_PCM_FMTBIT_S32_BE) 50 SNDRV_PCM_FMTBIT_S32_BE)
49 51
50/**
51 * psc_i2s_stream - Data specific to a single stream (playback or capture)
52 * @active: flag indicating if the stream is active
53 * @psc_i2s: pointer back to parent psc_i2s data structure
54 * @bcom_task: bestcomm task structure
55 * @irq: irq number for bestcomm task
56 * @period_start: physical address of start of DMA region
57 * @period_end: physical address of end of DMA region
58 * @period_next_pt: physical address of next DMA buffer to enqueue
59 * @period_bytes: size of DMA period in bytes
60 */
61struct psc_i2s_stream {
62 int active;
63 struct psc_i2s *psc_i2s;
64 struct bcom_task *bcom_task;
65 int irq;
66 struct snd_pcm_substream *stream;
67 dma_addr_t period_start;
68 dma_addr_t period_end;
69 dma_addr_t period_next_pt;
70 dma_addr_t period_current_pt;
71 int period_bytes;
72};
73
74/**
75 * psc_i2s - Private driver data
76 * @name: short name for this device ("PSC0", "PSC1", etc)
77 * @psc_regs: pointer to the PSC's registers
78 * @fifo_regs: pointer to the PSC's FIFO registers
79 * @irq: IRQ of this PSC
80 * @dev: struct device pointer
81 * @dai: the CPU DAI for this device
82 * @sicr: Base value used in serial interface control register; mode is ORed
83 * with this value.
84 * @playback: Playback stream context data
85 * @capture: Capture stream context data
86 */
87struct psc_i2s {
88 char name[32];
89 struct mpc52xx_psc __iomem *psc_regs;
90 struct mpc52xx_psc_fifo __iomem *fifo_regs;
91 unsigned int irq;
92 struct device *dev;
93 struct snd_soc_dai dai;
94 spinlock_t lock;
95 u32 sicr;
96
97 /* per-stream data */
98 struct psc_i2s_stream playback;
99 struct psc_i2s_stream capture;
100
101 /* Statistics */
102 struct {
103 int overrun_count;
104 int underrun_count;
105 } stats;
106};
107
108/*
109 * Interrupt handlers
110 */
111static irqreturn_t psc_i2s_status_irq(int irq, void *_psc_i2s)
112{
113 struct psc_i2s *psc_i2s = _psc_i2s;
114 struct mpc52xx_psc __iomem *regs = psc_i2s->psc_regs;
115 u16 isr;
116
117 isr = in_be16(&regs->mpc52xx_psc_isr);
118
119 /* Playback underrun error */
120 if (psc_i2s->playback.active && (isr & MPC52xx_PSC_IMR_TXEMP))
121 psc_i2s->stats.underrun_count++;
122
123 /* Capture overrun error */
124 if (psc_i2s->capture.active && (isr & MPC52xx_PSC_IMR_ORERR))
125 psc_i2s->stats.overrun_count++;
126
127 out_8(&regs->command, 4 << 4); /* reset the error status */
128
129 return IRQ_HANDLED;
130}
131
132/**
133 * psc_i2s_bcom_enqueue_next_buffer - Enqueue another audio buffer
134 * @s: pointer to stream private data structure
135 *
136 * Enqueues another audio period buffer into the bestcomm queue.
137 *
138 * Note: The routine must only be called when there is space available in
139 * the queue. Otherwise the enqueue will fail and the audio ring buffer
140 * will get out of sync
141 */
142static void psc_i2s_bcom_enqueue_next_buffer(struct psc_i2s_stream *s)
143{
144 struct bcom_bd *bd;
145
146 /* Prepare and enqueue the next buffer descriptor */
147 bd = bcom_prepare_next_buffer(s->bcom_task);
148 bd->status = s->period_bytes;
149 bd->data[0] = s->period_next_pt;
150 bcom_submit_next_buffer(s->bcom_task, NULL);
151
152 /* Update for next period */
153 s->period_next_pt += s->period_bytes;
154 if (s->period_next_pt >= s->period_end)
155 s->period_next_pt = s->period_start;
156}
157
158/* Bestcomm DMA irq handler */
159static irqreturn_t psc_i2s_bcom_irq(int irq, void *_psc_i2s_stream)
160{
161 struct psc_i2s_stream *s = _psc_i2s_stream;
162
163 /* For each finished period, dequeue the completed period buffer
164 * and enqueue a new one in it's place. */
165 while (bcom_buffer_done(s->bcom_task)) {
166 bcom_retrieve_buffer(s->bcom_task, NULL, NULL);
167 s->period_current_pt += s->period_bytes;
168 if (s->period_current_pt >= s->period_end)
169 s->period_current_pt = s->period_start;
170 psc_i2s_bcom_enqueue_next_buffer(s);
171 bcom_enable(s->bcom_task);
172 }
173
174 /* If the stream is active, then also inform the PCM middle layer
175 * of the period finished event. */
176 if (s->active)
177 snd_pcm_period_elapsed(s->stream);
178
179 return IRQ_HANDLED;
180}
181
182/**
183 * psc_i2s_startup: create a new substream
184 *
185 * This is the first function called when a stream is opened.
186 *
187 * If this is the first stream open, then grab the IRQ and program most of
188 * the PSC registers.
189 */
190static int psc_i2s_startup(struct snd_pcm_substream *substream,
191 struct snd_soc_dai *dai)
192{
193 struct snd_soc_pcm_runtime *rtd = substream->private_data;
194 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
195 int rc;
196
197 dev_dbg(psc_i2s->dev, "psc_i2s_startup(substream=%p)\n", substream);
198
199 if (!psc_i2s->playback.active &&
200 !psc_i2s->capture.active) {
201 /* Setup the IRQs */
202 rc = request_irq(psc_i2s->irq, &psc_i2s_status_irq, IRQF_SHARED,
203 "psc-i2s-status", psc_i2s);
204 rc |= request_irq(psc_i2s->capture.irq,
205 &psc_i2s_bcom_irq, IRQF_SHARED,
206 "psc-i2s-capture", &psc_i2s->capture);
207 rc |= request_irq(psc_i2s->playback.irq,
208 &psc_i2s_bcom_irq, IRQF_SHARED,
209 "psc-i2s-playback", &psc_i2s->playback);
210 if (rc) {
211 free_irq(psc_i2s->irq, psc_i2s);
212 free_irq(psc_i2s->capture.irq,
213 &psc_i2s->capture);
214 free_irq(psc_i2s->playback.irq,
215 &psc_i2s->playback);
216 return -ENODEV;
217 }
218 }
219
220 return 0;
221}
222
223static int psc_i2s_hw_params(struct snd_pcm_substream *substream, 52static int psc_i2s_hw_params(struct snd_pcm_substream *substream,
224 struct snd_pcm_hw_params *params, 53 struct snd_pcm_hw_params *params,
225 struct snd_soc_dai *dai) 54 struct snd_soc_dai *dai)
@@ -258,164 +87,6 @@ static int psc_i2s_hw_params(struct snd_pcm_substream *substream,
258 return 0; 87 return 0;
259} 88}
260 89
261static int psc_i2s_hw_free(struct snd_pcm_substream *substream,
262 struct snd_soc_dai *dai)
263{
264 snd_pcm_set_runtime_buffer(substream, NULL);
265 return 0;
266}
267
268/**
269 * psc_i2s_trigger: start and stop the DMA transfer.
270 *
271 * This function is called by ALSA to start, stop, pause, and resume the DMA
272 * transfer of data.
273 */
274static int psc_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
275 struct snd_soc_dai *dai)
276{
277 struct snd_soc_pcm_runtime *rtd = substream->private_data;
278 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
279 struct snd_pcm_runtime *runtime = substream->runtime;
280 struct psc_i2s_stream *s;
281 struct mpc52xx_psc __iomem *regs = psc_i2s->psc_regs;
282 u16 imr;
283 u8 psc_cmd;
284 unsigned long flags;
285
286 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
287 s = &psc_i2s->capture;
288 else
289 s = &psc_i2s->playback;
290
291 dev_dbg(psc_i2s->dev, "psc_i2s_trigger(substream=%p, cmd=%i)"
292 " stream_id=%i\n",
293 substream, cmd, substream->pstr->stream);
294
295 switch (cmd) {
296 case SNDRV_PCM_TRIGGER_START:
297 s->period_bytes = frames_to_bytes(runtime,
298 runtime->period_size);
299 s->period_start = virt_to_phys(runtime->dma_area);
300 s->period_end = s->period_start +
301 (s->period_bytes * runtime->periods);
302 s->period_next_pt = s->period_start;
303 s->period_current_pt = s->period_start;
304 s->active = 1;
305
306 /* First; reset everything */
307 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
308 out_8(&regs->command, MPC52xx_PSC_RST_RX);
309 out_8(&regs->command, MPC52xx_PSC_RST_ERR_STAT);
310 } else {
311 out_8(&regs->command, MPC52xx_PSC_RST_TX);
312 out_8(&regs->command, MPC52xx_PSC_RST_ERR_STAT);
313 }
314
315 /* Next, fill up the bestcomm bd queue and enable DMA.
316 * This will begin filling the PSC's fifo. */
317 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
318 bcom_gen_bd_rx_reset(s->bcom_task);
319 else
320 bcom_gen_bd_tx_reset(s->bcom_task);
321 while (!bcom_queue_full(s->bcom_task))
322 psc_i2s_bcom_enqueue_next_buffer(s);
323 bcom_enable(s->bcom_task);
324
325 /* Due to errata in the i2s mode; need to line up enabling
326 * the transmitter with a transition on the frame sync
327 * line */
328
329 spin_lock_irqsave(&psc_i2s->lock, flags);
330 /* first make sure it is low */
331 while ((in_8(&regs->ipcr_acr.ipcr) & 0x80) != 0)
332 ;
333 /* then wait for the transition to high */
334 while ((in_8(&regs->ipcr_acr.ipcr) & 0x80) == 0)
335 ;
336 /* Finally, enable the PSC.
337 * Receiver must always be enabled; even when we only want
338 * transmit. (see 15.3.2.3 of MPC5200B User's Guide) */
339 psc_cmd = MPC52xx_PSC_RX_ENABLE;
340 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK)
341 psc_cmd |= MPC52xx_PSC_TX_ENABLE;
342 out_8(&regs->command, psc_cmd);
343 spin_unlock_irqrestore(&psc_i2s->lock, flags);
344
345 break;
346
347 case SNDRV_PCM_TRIGGER_STOP:
348 /* Turn off the PSC */
349 s->active = 0;
350 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
351 if (!psc_i2s->playback.active) {
352 out_8(&regs->command, 2 << 4); /* reset rx */
353 out_8(&regs->command, 3 << 4); /* reset tx */
354 out_8(&regs->command, 4 << 4); /* reset err */
355 }
356 } else {
357 out_8(&regs->command, 3 << 4); /* reset tx */
358 out_8(&regs->command, 4 << 4); /* reset err */
359 if (!psc_i2s->capture.active)
360 out_8(&regs->command, 2 << 4); /* reset rx */
361 }
362
363 bcom_disable(s->bcom_task);
364 while (!bcom_queue_empty(s->bcom_task))
365 bcom_retrieve_buffer(s->bcom_task, NULL, NULL);
366
367 break;
368
369 default:
370 dev_dbg(psc_i2s->dev, "invalid command\n");
371 return -EINVAL;
372 }
373
374 /* Update interrupt enable settings */
375 imr = 0;
376 if (psc_i2s->playback.active)
377 imr |= MPC52xx_PSC_IMR_TXEMP;
378 if (psc_i2s->capture.active)
379 imr |= MPC52xx_PSC_IMR_ORERR;
380 out_be16(&regs->isr_imr.imr, imr);
381
382 return 0;
383}
384
385/**
386 * psc_i2s_shutdown: shutdown the data transfer on a stream
387 *
388 * Shutdown the PSC if there are no other substreams open.
389 */
390static void psc_i2s_shutdown(struct snd_pcm_substream *substream,
391 struct snd_soc_dai *dai)
392{
393 struct snd_soc_pcm_runtime *rtd = substream->private_data;
394 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
395
396 dev_dbg(psc_i2s->dev, "psc_i2s_shutdown(substream=%p)\n", substream);
397
398 /*
399 * If this is the last active substream, disable the PSC and release
400 * the IRQ.
401 */
402 if (!psc_i2s->playback.active &&
403 !psc_i2s->capture.active) {
404
405 /* Disable all interrupts and reset the PSC */
406 out_be16(&psc_i2s->psc_regs->isr_imr.imr, 0);
407 out_8(&psc_i2s->psc_regs->command, 3 << 4); /* reset tx */
408 out_8(&psc_i2s->psc_regs->command, 2 << 4); /* reset rx */
409 out_8(&psc_i2s->psc_regs->command, 1 << 4); /* reset mode */
410 out_8(&psc_i2s->psc_regs->command, 4 << 4); /* reset error */
411
412 /* Release irqs */
413 free_irq(psc_i2s->irq, psc_i2s);
414 free_irq(psc_i2s->capture.irq, &psc_i2s->capture);
415 free_irq(psc_i2s->playback.irq, &psc_i2s->playback);
416 }
417}
418
419/** 90/**
420 * psc_i2s_set_sysclk: set the clock frequency and direction 91 * psc_i2s_set_sysclk: set the clock frequency and direction
421 * 92 *
@@ -495,158 +166,6 @@ static struct snd_soc_dai psc_i2s_dai_template = {
495}; 166};
496 167
497/* --------------------------------------------------------------------- 168/* ---------------------------------------------------------------------
498 * The PSC I2S 'ASoC platform' driver
499 *
500 * Can be referenced by an 'ASoC machine' driver
501 * This driver only deals with the audio bus; it doesn't have any
502 * interaction with the attached codec
503 */
504
505static const struct snd_pcm_hardware psc_i2s_pcm_hardware = {
506 .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
507 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
508 SNDRV_PCM_INFO_BATCH,
509 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE |
510 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE,
511 .rate_min = 8000,
512 .rate_max = 48000,
513 .channels_min = 2,
514 .channels_max = 2,
515 .period_bytes_max = 1024 * 1024,
516 .period_bytes_min = 32,
517 .periods_min = 2,
518 .periods_max = 256,
519 .buffer_bytes_max = 2 * 1024 * 1024,
520 .fifo_size = 0,
521};
522
523static int psc_i2s_pcm_open(struct snd_pcm_substream *substream)
524{
525 struct snd_soc_pcm_runtime *rtd = substream->private_data;
526 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
527 struct psc_i2s_stream *s;
528
529 dev_dbg(psc_i2s->dev, "psc_i2s_pcm_open(substream=%p)\n", substream);
530
531 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
532 s = &psc_i2s->capture;
533 else
534 s = &psc_i2s->playback;
535
536 snd_soc_set_runtime_hwparams(substream, &psc_i2s_pcm_hardware);
537
538 s->stream = substream;
539 return 0;
540}
541
542static int psc_i2s_pcm_close(struct snd_pcm_substream *substream)
543{
544 struct snd_soc_pcm_runtime *rtd = substream->private_data;
545 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
546 struct psc_i2s_stream *s;
547
548 dev_dbg(psc_i2s->dev, "psc_i2s_pcm_close(substream=%p)\n", substream);
549
550 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
551 s = &psc_i2s->capture;
552 else
553 s = &psc_i2s->playback;
554
555 s->stream = NULL;
556 return 0;
557}
558
559static snd_pcm_uframes_t
560psc_i2s_pcm_pointer(struct snd_pcm_substream *substream)
561{
562 struct snd_soc_pcm_runtime *rtd = substream->private_data;
563 struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
564 struct psc_i2s_stream *s;
565 dma_addr_t count;
566
567 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
568 s = &psc_i2s->capture;
569 else
570 s = &psc_i2s->playback;
571
572 count = s->period_current_pt - s->period_start;
573
574 return bytes_to_frames(substream->runtime, count);
575}
576
577static struct snd_pcm_ops psc_i2s_pcm_ops = {
578 .open = psc_i2s_pcm_open,
579 .close = psc_i2s_pcm_close,
580 .ioctl = snd_pcm_lib_ioctl,
581 .pointer = psc_i2s_pcm_pointer,
582};
583
584static u64 psc_i2s_pcm_dmamask = 0xffffffff;
585static int psc_i2s_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
586 struct snd_pcm *pcm)
587{
588 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
589 size_t size = psc_i2s_pcm_hardware.buffer_bytes_max;
590 int rc = 0;
591
592 dev_dbg(rtd->socdev->dev, "psc_i2s_pcm_new(card=%p, dai=%p, pcm=%p)\n",
593 card, dai, pcm);
594
595 if (!card->dev->dma_mask)
596 card->dev->dma_mask = &psc_i2s_pcm_dmamask;
597 if (!card->dev->coherent_dma_mask)
598 card->dev->coherent_dma_mask = 0xffffffff;
599
600 if (pcm->streams[0].substream) {
601 rc = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->dev, size,
602 &pcm->streams[0].substream->dma_buffer);
603 if (rc)
604 goto playback_alloc_err;
605 }
606
607 if (pcm->streams[1].substream) {
608 rc = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->dev, size,
609 &pcm->streams[1].substream->dma_buffer);
610 if (rc)
611 goto capture_alloc_err;
612 }
613
614 return 0;
615
616 capture_alloc_err:
617 if (pcm->streams[0].substream)
618 snd_dma_free_pages(&pcm->streams[0].substream->dma_buffer);
619 playback_alloc_err:
620 dev_err(card->dev, "Cannot allocate buffer(s)\n");
621 return -ENOMEM;
622}
623
624static void psc_i2s_pcm_free(struct snd_pcm *pcm)
625{
626 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
627 struct snd_pcm_substream *substream;
628 int stream;
629
630 dev_dbg(rtd->socdev->dev, "psc_i2s_pcm_free(pcm=%p)\n", pcm);
631
632 for (stream = 0; stream < 2; stream++) {
633 substream = pcm->streams[stream].substream;
634 if (substream) {
635 snd_dma_free_pages(&substream->dma_buffer);
636 substream->dma_buffer.area = NULL;
637 substream->dma_buffer.addr = 0;
638 }
639 }
640}
641
642struct snd_soc_platform psc_i2s_pcm_soc_platform = {
643 .name = "mpc5200-psc-audio",
644 .pcm_ops = &psc_i2s_pcm_ops,
645 .pcm_new = &psc_i2s_pcm_new,
646 .pcm_free = &psc_i2s_pcm_free,
647};
648
649/* ---------------------------------------------------------------------
650 * Sysfs attributes for debugging 169 * Sysfs attributes for debugging
651 */ 170 */
652 171