blob: 685039213f05dfbf0970942b17001533b5506e7a [file] [log] [blame]
Jon Smirl89dd0842009-05-23 19:12:59 -04001/*
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 */
Jon Smirlcebe7762009-05-23 19:13:01 -040037static irqreturn_t psc_dma_status_irq(int irq, void *_psc_dma)
Jon Smirl89dd0842009-05-23 19:12:59 -040038{
Jon Smirlcebe7762009-05-23 19:13:01 -040039 struct psc_dma *psc_dma = _psc_dma;
40 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
Jon Smirl89dd0842009-05-23 19:12:59 -040041 u16 isr;
42
43 isr = in_be16(&regs->mpc52xx_psc_isr);
44
45 /* Playback underrun error */
Jon Smirlcebe7762009-05-23 19:13:01 -040046 if (psc_dma->playback.active && (isr & MPC52xx_PSC_IMR_TXEMP))
47 psc_dma->stats.underrun_count++;
Jon Smirl89dd0842009-05-23 19:12:59 -040048
49 /* Capture overrun error */
Jon Smirlcebe7762009-05-23 19:13:01 -040050 if (psc_dma->capture.active && (isr & MPC52xx_PSC_IMR_ORERR))
51 psc_dma->stats.overrun_count++;
Jon Smirl89dd0842009-05-23 19:12:59 -040052
53 out_8(&regs->command, 4 << 4); /* reset the error status */
54
55 return IRQ_HANDLED;
56}
57
58/**
Jon Smirlcebe7762009-05-23 19:13:01 -040059 * psc_dma_bcom_enqueue_next_buffer - Enqueue another audio buffer
Jon Smirl89dd0842009-05-23 19:12:59 -040060 * @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 */
Jon Smirlcebe7762009-05-23 19:13:01 -040068static void psc_dma_bcom_enqueue_next_buffer(struct psc_dma_stream *s)
Jon Smirl89dd0842009-05-23 19:12:59 -040069{
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 */
Jon Smirlcebe7762009-05-23 19:13:01 -040085static irqreturn_t psc_dma_bcom_irq(int irq, void *_psc_dma_stream)
Jon Smirl89dd0842009-05-23 19:12:59 -040086{
Jon Smirlcebe7762009-05-23 19:13:01 -040087 struct psc_dma_stream *s = _psc_dma_stream;
Jon Smirl89dd0842009-05-23 19:12:59 -040088
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;
Jon Smirlcebe7762009-05-23 19:13:01 -040096 psc_dma_bcom_enqueue_next_buffer(s);
Jon Smirl89dd0842009-05-23 19:12:59 -040097 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/**
Jon Smirlcebe7762009-05-23 19:13:01 -0400109 * psc_dma_startup: create a new substream
Jon Smirl89dd0842009-05-23 19:12:59 -0400110 *
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 */
Jon Smirlcebe7762009-05-23 19:13:01 -0400116int psc_dma_startup(struct snd_pcm_substream *substream,
Jon Smirl89dd0842009-05-23 19:12:59 -0400117 struct snd_soc_dai *dai)
118{
119 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Jon Smirlcebe7762009-05-23 19:13:01 -0400120 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
Jon Smirl89dd0842009-05-23 19:12:59 -0400121 int rc;
122
Jon Smirlcebe7762009-05-23 19:13:01 -0400123 dev_dbg(psc_dma->dev, "psc_dma_startup(substream=%p)\n", substream);
Jon Smirl89dd0842009-05-23 19:12:59 -0400124
Jon Smirlcebe7762009-05-23 19:13:01 -0400125 if (!psc_dma->playback.active &&
126 !psc_dma->capture.active) {
Jon Smirl89dd0842009-05-23 19:12:59 -0400127 /* Setup the IRQs */
Jon Smirlcebe7762009-05-23 19:13:01 -0400128 rc = request_irq(psc_dma->irq, &psc_dma_status_irq, IRQF_SHARED,
129 "psc-dma-status", psc_dma);
130 rc |= request_irq(psc_dma->capture.irq,
131 &psc_dma_bcom_irq, IRQF_SHARED,
132 "psc-dma-capture", &psc_dma->capture);
133 rc |= request_irq(psc_dma->playback.irq,
134 &psc_dma_bcom_irq, IRQF_SHARED,
135 "psc-dma-playback", &psc_dma->playback);
Jon Smirl89dd0842009-05-23 19:12:59 -0400136 if (rc) {
Jon Smirlcebe7762009-05-23 19:13:01 -0400137 free_irq(psc_dma->irq, psc_dma);
138 free_irq(psc_dma->capture.irq,
139 &psc_dma->capture);
140 free_irq(psc_dma->playback.irq,
141 &psc_dma->playback);
Jon Smirl89dd0842009-05-23 19:12:59 -0400142 return -ENODEV;
143 }
144 }
145
146 return 0;
147}
148
Jon Smirlcebe7762009-05-23 19:13:01 -0400149int psc_dma_hw_free(struct snd_pcm_substream *substream,
Jon Smirl89dd0842009-05-23 19:12:59 -0400150 struct snd_soc_dai *dai)
151{
152 snd_pcm_set_runtime_buffer(substream, NULL);
153 return 0;
154}
155
156/**
Jon Smirlcebe7762009-05-23 19:13:01 -0400157 * psc_dma_trigger: start and stop the DMA transfer.
Jon Smirl89dd0842009-05-23 19:12:59 -0400158 *
159 * This function is called by ALSA to start, stop, pause, and resume the DMA
160 * transfer of data.
161 */
Jon Smirlcebe7762009-05-23 19:13:01 -0400162int psc_dma_trigger(struct snd_pcm_substream *substream, int cmd,
Jon Smirl89dd0842009-05-23 19:12:59 -0400163 struct snd_soc_dai *dai)
164{
165 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Jon Smirlcebe7762009-05-23 19:13:01 -0400166 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
Jon Smirl89dd0842009-05-23 19:12:59 -0400167 struct snd_pcm_runtime *runtime = substream->runtime;
Jon Smirlcebe7762009-05-23 19:13:01 -0400168 struct psc_dma_stream *s;
169 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
Jon Smirl89dd0842009-05-23 19:12:59 -0400170 u16 imr;
171 u8 psc_cmd;
172 unsigned long flags;
173
174 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
Jon Smirlcebe7762009-05-23 19:13:01 -0400175 s = &psc_dma->capture;
Jon Smirl89dd0842009-05-23 19:12:59 -0400176 else
Jon Smirlcebe7762009-05-23 19:13:01 -0400177 s = &psc_dma->playback;
Jon Smirl89dd0842009-05-23 19:12:59 -0400178
Jon Smirlcebe7762009-05-23 19:13:01 -0400179 dev_dbg(psc_dma->dev, "psc_dma_trigger(substream=%p, cmd=%i)"
Jon Smirl89dd0842009-05-23 19:12:59 -0400180 " 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))
Jon Smirlcebe7762009-05-23 19:13:01 -0400210 psc_dma_bcom_enqueue_next_buffer(s);
Jon Smirl89dd0842009-05-23 19:12:59 -0400211 bcom_enable(s->bcom_task);
212
Jon Smirlcebe7762009-05-23 19:13:01 -0400213 /* Due to errata in the dma mode; need to line up enabling
Jon Smirl89dd0842009-05-23 19:12:59 -0400214 * the transmitter with a transition on the frame sync
215 * line */
216
Jon Smirlcebe7762009-05-23 19:13:01 -0400217 spin_lock_irqsave(&psc_dma->lock, flags);
Jon Smirl89dd0842009-05-23 19:12:59 -0400218 /* 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);
Jon Smirlcebe7762009-05-23 19:13:01 -0400231 spin_unlock_irqrestore(&psc_dma->lock, flags);
Jon Smirl89dd0842009-05-23 19:12:59 -0400232
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) {
Jon Smirlcebe7762009-05-23 19:13:01 -0400239 if (!psc_dma->playback.active) {
Jon Smirl89dd0842009-05-23 19:12:59 -0400240 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 */
Jon Smirlcebe7762009-05-23 19:13:01 -0400247 if (!psc_dma->capture.active)
Jon Smirl89dd0842009-05-23 19:12:59 -0400248 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:
Jon Smirlcebe7762009-05-23 19:13:01 -0400258 dev_dbg(psc_dma->dev, "invalid command\n");
Jon Smirl89dd0842009-05-23 19:12:59 -0400259 return -EINVAL;
260 }
261
262 /* Update interrupt enable settings */
263 imr = 0;
Jon Smirlcebe7762009-05-23 19:13:01 -0400264 if (psc_dma->playback.active)
Jon Smirl89dd0842009-05-23 19:12:59 -0400265 imr |= MPC52xx_PSC_IMR_TXEMP;
Jon Smirlcebe7762009-05-23 19:13:01 -0400266 if (psc_dma->capture.active)
Jon Smirl89dd0842009-05-23 19:12:59 -0400267 imr |= MPC52xx_PSC_IMR_ORERR;
268 out_be16(&regs->isr_imr.imr, imr);
269
270 return 0;
271}
272
273/**
Jon Smirlcebe7762009-05-23 19:13:01 -0400274 * psc_dma_shutdown: shutdown the data transfer on a stream
Jon Smirl89dd0842009-05-23 19:12:59 -0400275 *
276 * Shutdown the PSC if there are no other substreams open.
277 */
Jon Smirlcebe7762009-05-23 19:13:01 -0400278void psc_dma_shutdown(struct snd_pcm_substream *substream,
Jon Smirl89dd0842009-05-23 19:12:59 -0400279 struct snd_soc_dai *dai)
280{
281 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Jon Smirlcebe7762009-05-23 19:13:01 -0400282 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
Jon Smirl89dd0842009-05-23 19:12:59 -0400283
Jon Smirlcebe7762009-05-23 19:13:01 -0400284 dev_dbg(psc_dma->dev, "psc_dma_shutdown(substream=%p)\n", substream);
Jon Smirl89dd0842009-05-23 19:12:59 -0400285
286 /*
287 * If this is the last active substream, disable the PSC and release
288 * the IRQ.
289 */
Jon Smirlcebe7762009-05-23 19:13:01 -0400290 if (!psc_dma->playback.active &&
291 !psc_dma->capture.active) {
Jon Smirl89dd0842009-05-23 19:12:59 -0400292
293 /* Disable all interrupts and reset the PSC */
Jon Smirlcebe7762009-05-23 19:13:01 -0400294 out_be16(&psc_dma->psc_regs->isr_imr.imr, 0);
295 out_8(&psc_dma->psc_regs->command, 3 << 4); /* reset tx */
296 out_8(&psc_dma->psc_regs->command, 2 << 4); /* reset rx */
297 out_8(&psc_dma->psc_regs->command, 1 << 4); /* reset mode */
298 out_8(&psc_dma->psc_regs->command, 4 << 4); /* reset error */
Jon Smirl89dd0842009-05-23 19:12:59 -0400299
300 /* Release irqs */
Jon Smirlcebe7762009-05-23 19:13:01 -0400301 free_irq(psc_dma->irq, psc_dma);
302 free_irq(psc_dma->capture.irq, &psc_dma->capture);
303 free_irq(psc_dma->playback.irq, &psc_dma->playback);
Jon Smirl89dd0842009-05-23 19:12:59 -0400304 }
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
Jon Smirlcebe7762009-05-23 19:13:01 -0400315static const struct snd_pcm_hardware psc_dma_pcm_hardware = {
Jon Smirl89dd0842009-05-23 19:12:59 -0400316 .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
Jon Smirlcebe7762009-05-23 19:13:01 -0400333static int psc_dma_pcm_open(struct snd_pcm_substream *substream)
Jon Smirl89dd0842009-05-23 19:12:59 -0400334{
335 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Jon Smirlcebe7762009-05-23 19:13:01 -0400336 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
337 struct psc_dma_stream *s;
Jon Smirl89dd0842009-05-23 19:12:59 -0400338
Jon Smirlcebe7762009-05-23 19:13:01 -0400339 dev_dbg(psc_dma->dev, "psc_dma_pcm_open(substream=%p)\n", substream);
Jon Smirl89dd0842009-05-23 19:12:59 -0400340
341 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
Jon Smirlcebe7762009-05-23 19:13:01 -0400342 s = &psc_dma->capture;
Jon Smirl89dd0842009-05-23 19:12:59 -0400343 else
Jon Smirlcebe7762009-05-23 19:13:01 -0400344 s = &psc_dma->playback;
Jon Smirl89dd0842009-05-23 19:12:59 -0400345
Jon Smirlcebe7762009-05-23 19:13:01 -0400346 snd_soc_set_runtime_hwparams(substream, &psc_dma_pcm_hardware);
Jon Smirl89dd0842009-05-23 19:12:59 -0400347
348 s->stream = substream;
349 return 0;
350}
351
Jon Smirlcebe7762009-05-23 19:13:01 -0400352static int psc_dma_pcm_close(struct snd_pcm_substream *substream)
Jon Smirl89dd0842009-05-23 19:12:59 -0400353{
354 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Jon Smirlcebe7762009-05-23 19:13:01 -0400355 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
356 struct psc_dma_stream *s;
Jon Smirl89dd0842009-05-23 19:12:59 -0400357
Jon Smirlcebe7762009-05-23 19:13:01 -0400358 dev_dbg(psc_dma->dev, "psc_dma_pcm_close(substream=%p)\n", substream);
Jon Smirl89dd0842009-05-23 19:12:59 -0400359
360 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
Jon Smirlcebe7762009-05-23 19:13:01 -0400361 s = &psc_dma->capture;
Jon Smirl89dd0842009-05-23 19:12:59 -0400362 else
Jon Smirlcebe7762009-05-23 19:13:01 -0400363 s = &psc_dma->playback;
Jon Smirl89dd0842009-05-23 19:12:59 -0400364
365 s->stream = NULL;
366 return 0;
367}
368
369static snd_pcm_uframes_t
Jon Smirlcebe7762009-05-23 19:13:01 -0400370psc_dma_pcm_pointer(struct snd_pcm_substream *substream)
Jon Smirl89dd0842009-05-23 19:12:59 -0400371{
372 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Jon Smirlcebe7762009-05-23 19:13:01 -0400373 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
374 struct psc_dma_stream *s;
Jon Smirl89dd0842009-05-23 19:12:59 -0400375 dma_addr_t count;
376
377 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
Jon Smirlcebe7762009-05-23 19:13:01 -0400378 s = &psc_dma->capture;
Jon Smirl89dd0842009-05-23 19:12:59 -0400379 else
Jon Smirlcebe7762009-05-23 19:13:01 -0400380 s = &psc_dma->playback;
Jon Smirl89dd0842009-05-23 19:12:59 -0400381
382 count = s->period_current_pt - s->period_start;
383
384 return bytes_to_frames(substream->runtime, count);
385}
386
Jon Smirlcebe7762009-05-23 19:13:01 -0400387static struct snd_pcm_ops psc_dma_pcm_ops = {
388 .open = psc_dma_pcm_open,
389 .close = psc_dma_pcm_close,
Jon Smirl89dd0842009-05-23 19:12:59 -0400390 .ioctl = snd_pcm_lib_ioctl,
Jon Smirlcebe7762009-05-23 19:13:01 -0400391 .pointer = psc_dma_pcm_pointer,
Jon Smirl89dd0842009-05-23 19:12:59 -0400392};
393
Jon Smirlcebe7762009-05-23 19:13:01 -0400394static u64 psc_dma_pcm_dmamask = 0xffffffff;
395static int psc_dma_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
Jon Smirl89dd0842009-05-23 19:12:59 -0400396 struct snd_pcm *pcm)
397{
398 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
Jon Smirlcebe7762009-05-23 19:13:01 -0400399 size_t size = psc_dma_pcm_hardware.buffer_bytes_max;
Jon Smirl89dd0842009-05-23 19:12:59 -0400400 int rc = 0;
401
Jon Smirlcebe7762009-05-23 19:13:01 -0400402 dev_dbg(rtd->socdev->dev, "psc_dma_pcm_new(card=%p, dai=%p, pcm=%p)\n",
Jon Smirl89dd0842009-05-23 19:12:59 -0400403 card, dai, pcm);
404
405 if (!card->dev->dma_mask)
Jon Smirlcebe7762009-05-23 19:13:01 -0400406 card->dev->dma_mask = &psc_dma_pcm_dmamask;
Jon Smirl89dd0842009-05-23 19:12:59 -0400407 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
Jon Smirlcebe7762009-05-23 19:13:01 -0400434static void psc_dma_pcm_free(struct snd_pcm *pcm)
Jon Smirl89dd0842009-05-23 19:12:59 -0400435{
436 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
437 struct snd_pcm_substream *substream;
438 int stream;
439
Jon Smirlcebe7762009-05-23 19:13:01 -0400440 dev_dbg(rtd->socdev->dev, "psc_dma_pcm_free(pcm=%p)\n", pcm);
Jon Smirl89dd0842009-05-23 19:12:59 -0400441
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
Jon Smirlcebe7762009-05-23 19:13:01 -0400452struct snd_soc_platform psc_dma_pcm_soc_platform = {
Jon Smirl89dd0842009-05-23 19:12:59 -0400453 .name = "mpc5200-psc-audio",
Jon Smirlcebe7762009-05-23 19:13:01 -0400454 .pcm_ops = &psc_dma_pcm_ops,
455 .pcm_new = &psc_dma_pcm_new,
456 .pcm_free = &psc_dma_pcm_free,
Jon Smirl89dd0842009-05-23 19:12:59 -0400457};
458