blob: 9e4f66b6b92b00f6da379c637f2d51bfde6aba1f [file] [log] [blame]
Andra Danciuac097ca2019-04-14 22:14:49 +03001// SPDX-License-Identifier: GPL-2.0-only
Andra Danciu1edfc242019-04-05 14:50:01 +03002//
3// Freescale MPC5200 PSC DMA
4// ALSA SoC Platform driver
5//
6// Copyright (C) 2008 Secret Lab Technologies Ltd.
7// Copyright (C) 2009 Jon Smirl, Digispeaker
Jon Smirl89dd0842009-05-23 19:12:59 -04008
Jon Smirl89dd0842009-05-23 19:12:59 -04009#include <linux/module.h>
Jon Smirl89dd0842009-05-23 19:12:59 -040010#include <linux/of_device.h>
Paul Gortmaker07a38b12012-02-25 16:12:30 -050011#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Rob Herring5af50732013-09-17 14:28:33 -050013#include <linux/of_address.h>
14#include <linux/of_irq.h>
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000015#include <linux/of_platform.h>
Jon Smirl89dd0842009-05-23 19:12:59 -040016
Jon Smirl89dd0842009-05-23 19:12:59 -040017#include <sound/soc.h>
Jon Smirl89dd0842009-05-23 19:12:59 -040018
Philippe De Muyter9a322992012-10-12 17:52:45 +020019#include <linux/fsl/bestcomm/bestcomm.h>
20#include <linux/fsl/bestcomm/gen_bd.h>
Jon Smirl89dd0842009-05-23 19:12:59 -040021#include <asm/mpc52xx_psc.h>
22
23#include "mpc5200_dma.h"
24
Kuninori Morimoto60bceb82018-01-29 02:47:19 +000025#define DRV_NAME "mpc5200_dma"
26
Jon Smirl89dd0842009-05-23 19:12:59 -040027/*
28 * Interrupt handlers
29 */
Jon Smirlcebe7762009-05-23 19:13:01 -040030static irqreturn_t psc_dma_status_irq(int irq, void *_psc_dma)
Jon Smirl89dd0842009-05-23 19:12:59 -040031{
Jon Smirlcebe7762009-05-23 19:13:01 -040032 struct psc_dma *psc_dma = _psc_dma;
33 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
Jon Smirl89dd0842009-05-23 19:12:59 -040034 u16 isr;
35
36 isr = in_be16(&regs->mpc52xx_psc_isr);
37
38 /* Playback underrun error */
Jon Smirlcebe7762009-05-23 19:13:01 -040039 if (psc_dma->playback.active && (isr & MPC52xx_PSC_IMR_TXEMP))
40 psc_dma->stats.underrun_count++;
Jon Smirl89dd0842009-05-23 19:12:59 -040041
42 /* Capture overrun error */
Jon Smirlcebe7762009-05-23 19:13:01 -040043 if (psc_dma->capture.active && (isr & MPC52xx_PSC_IMR_ORERR))
44 psc_dma->stats.overrun_count++;
Jon Smirl89dd0842009-05-23 19:12:59 -040045
Jon Smirldbcc3472009-05-26 08:34:08 -040046 out_8(&regs->command, MPC52xx_PSC_RST_ERR_STAT);
Jon Smirl89dd0842009-05-23 19:12:59 -040047
48 return IRQ_HANDLED;
49}
50
51/**
Jon Smirlcebe7762009-05-23 19:13:01 -040052 * psc_dma_bcom_enqueue_next_buffer - Enqueue another audio buffer
Jon Smirl89dd0842009-05-23 19:12:59 -040053 * @s: pointer to stream private data structure
54 *
55 * Enqueues another audio period buffer into the bestcomm queue.
56 *
57 * Note: The routine must only be called when there is space available in
58 * the queue. Otherwise the enqueue will fail and the audio ring buffer
59 * will get out of sync
60 */
Jon Smirlcebe7762009-05-23 19:13:01 -040061static void psc_dma_bcom_enqueue_next_buffer(struct psc_dma_stream *s)
Jon Smirl89dd0842009-05-23 19:12:59 -040062{
63 struct bcom_bd *bd;
64
65 /* Prepare and enqueue the next buffer descriptor */
66 bd = bcom_prepare_next_buffer(s->bcom_task);
67 bd->status = s->period_bytes;
Grant Likely8f159d72009-11-07 01:33:53 -070068 bd->data[0] = s->runtime->dma_addr + (s->period_next * s->period_bytes);
Jon Smirl89dd0842009-05-23 19:12:59 -040069 bcom_submit_next_buffer(s->bcom_task, NULL);
70
71 /* Update for next period */
Grant Likely8f159d72009-11-07 01:33:53 -070072 s->period_next = (s->period_next + 1) % s->runtime->periods;
Jon Smirl89dd0842009-05-23 19:12:59 -040073}
74
75/* Bestcomm DMA irq handler */
Grant Likelya68cc8d2009-11-09 09:40:09 -070076static irqreturn_t psc_dma_bcom_irq(int irq, void *_psc_dma_stream)
Jon Smirl89dd0842009-05-23 19:12:59 -040077{
Jon Smirldbcc3472009-05-26 08:34:08 -040078 struct psc_dma_stream *s = _psc_dma_stream;
Jon Smirl89dd0842009-05-23 19:12:59 -040079
Jon Smirldbcc3472009-05-26 08:34:08 -040080 spin_lock(&s->psc_dma->lock);
81 /* For each finished period, dequeue the completed period buffer
82 * and enqueue a new one in it's place. */
83 while (bcom_buffer_done(s->bcom_task)) {
84 bcom_retrieve_buffer(s->bcom_task, NULL, NULL);
Jon Smirl89dd0842009-05-23 19:12:59 -040085
Grant Likely8f159d72009-11-07 01:33:53 -070086 s->period_current = (s->period_current+1) % s->runtime->periods;
Grant Likelyc4878272009-11-07 01:34:18 -070087 s->period_count++;
Jon Smirldbcc3472009-05-26 08:34:08 -040088
89 psc_dma_bcom_enqueue_next_buffer(s);
Jon Smirl89dd0842009-05-23 19:12:59 -040090 }
Jon Smirldbcc3472009-05-26 08:34:08 -040091 spin_unlock(&s->psc_dma->lock);
Jon Smirl89dd0842009-05-23 19:12:59 -040092
Jon Smirldbcc3472009-05-26 08:34:08 -040093 /* If the stream is active, then also inform the PCM middle layer
94 * of the period finished event. */
95 if (s->active)
96 snd_pcm_period_elapsed(s->stream);
97
98 return IRQ_HANDLED;
Jon Smirl89dd0842009-05-23 19:12:59 -040099}
100
Kuninori Morimoto6d1048b2019-10-02 14:34:38 +0900101static int psc_dma_hw_free(struct snd_soc_component *component,
102 struct snd_pcm_substream *substream)
Jon Smirl89dd0842009-05-23 19:12:59 -0400103{
104 snd_pcm_set_runtime_buffer(substream, NULL);
105 return 0;
106}
107
108/**
Jon Smirlcebe7762009-05-23 19:13:01 -0400109 * psc_dma_trigger: start and stop the DMA transfer.
Jon Smirl89dd0842009-05-23 19:12:59 -0400110 *
111 * This function is called by ALSA to start, stop, pause, and resume the DMA
112 * transfer of data.
113 */
Kuninori Morimoto6d1048b2019-10-02 14:34:38 +0900114static int psc_dma_trigger(struct snd_soc_component *component,
115 struct snd_pcm_substream *substream, int cmd)
Jon Smirl89dd0842009-05-23 19:12:59 -0400116{
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900117 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900118 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
Jon Smirl89dd0842009-05-23 19:12:59 -0400119 struct snd_pcm_runtime *runtime = substream->runtime;
Grant Likely1d8222e2009-11-07 01:34:31 -0700120 struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);
Jon Smirlcebe7762009-05-23 19:13:01 -0400121 struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
Jon Smirl89dd0842009-05-23 19:12:59 -0400122 u16 imr;
Jon Smirl89dd0842009-05-23 19:12:59 -0400123 unsigned long flags;
Jon Smirldbcc3472009-05-26 08:34:08 -0400124 int i;
Jon Smirl89dd0842009-05-23 19:12:59 -0400125
Jon Smirl89dd0842009-05-23 19:12:59 -0400126 switch (cmd) {
127 case SNDRV_PCM_TRIGGER_START:
Grant Likelyc4878272009-11-07 01:34:18 -0700128 dev_dbg(psc_dma->dev, "START: stream=%i fbits=%u ps=%u #p=%u\n",
129 substream->pstr->stream, runtime->frame_bits,
130 (int)runtime->period_size, runtime->periods);
Jon Smirl89dd0842009-05-23 19:12:59 -0400131 s->period_bytes = frames_to_bytes(runtime,
132 runtime->period_size);
Grant Likely8f159d72009-11-07 01:33:53 -0700133 s->period_next = 0;
134 s->period_current = 0;
Jon Smirl89dd0842009-05-23 19:12:59 -0400135 s->active = 1;
Grant Likelyc4878272009-11-07 01:34:18 -0700136 s->period_count = 0;
Jon Smirldbcc3472009-05-26 08:34:08 -0400137 s->runtime = runtime;
Jon Smirldbcc3472009-05-26 08:34:08 -0400138
139 /* Fill up the bestcomm bd queue and enable DMA.
140 * This will begin filling the PSC's fifo.
141 */
142 spin_lock_irqsave(&psc_dma->lock, flags);
143
Grant Likelyd56b6eb2009-11-07 01:34:05 -0700144 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
Jon Smirldbcc3472009-05-26 08:34:08 -0400145 bcom_gen_bd_rx_reset(s->bcom_task);
Grant Likelyd56b6eb2009-11-07 01:34:05 -0700146 else
Jon Smirldbcc3472009-05-26 08:34:08 -0400147 bcom_gen_bd_tx_reset(s->bcom_task);
Grant Likelyd56b6eb2009-11-07 01:34:05 -0700148
149 for (i = 0; i < runtime->periods; i++)
150 if (!bcom_queue_full(s->bcom_task))
151 psc_dma_bcom_enqueue_next_buffer(s);
Jon Smirl89dd0842009-05-23 19:12:59 -0400152
Jon Smirl89dd0842009-05-23 19:12:59 -0400153 bcom_enable(s->bcom_task);
Jon Smirlcebe7762009-05-23 19:13:01 -0400154 spin_unlock_irqrestore(&psc_dma->lock, flags);
Jon Smirl89dd0842009-05-23 19:12:59 -0400155
Jon Smirldbcc3472009-05-26 08:34:08 -0400156 out_8(&regs->command, MPC52xx_PSC_RST_ERR_STAT);
157
Jon Smirl89dd0842009-05-23 19:12:59 -0400158 break;
159
160 case SNDRV_PCM_TRIGGER_STOP:
Grant Likelyc4878272009-11-07 01:34:18 -0700161 dev_dbg(psc_dma->dev, "STOP: stream=%i periods_count=%i\n",
162 substream->pstr->stream, s->period_count);
Jon Smirl89dd0842009-05-23 19:12:59 -0400163 s->active = 0;
Jon Smirl89dd0842009-05-23 19:12:59 -0400164
Jon Smirldbcc3472009-05-26 08:34:08 -0400165 spin_lock_irqsave(&psc_dma->lock, flags);
Jon Smirl89dd0842009-05-23 19:12:59 -0400166 bcom_disable(s->bcom_task);
Jon Smirldbcc3472009-05-26 08:34:08 -0400167 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
168 bcom_gen_bd_rx_reset(s->bcom_task);
169 else
170 bcom_gen_bd_tx_reset(s->bcom_task);
171 spin_unlock_irqrestore(&psc_dma->lock, flags);
Jon Smirl89dd0842009-05-23 19:12:59 -0400172
173 break;
174
175 default:
Grant Likelyc4878272009-11-07 01:34:18 -0700176 dev_dbg(psc_dma->dev, "unhandled trigger: stream=%i cmd=%i\n",
177 substream->pstr->stream, cmd);
Jon Smirl89dd0842009-05-23 19:12:59 -0400178 return -EINVAL;
179 }
180
181 /* Update interrupt enable settings */
182 imr = 0;
Jon Smirlcebe7762009-05-23 19:13:01 -0400183 if (psc_dma->playback.active)
Jon Smirl89dd0842009-05-23 19:12:59 -0400184 imr |= MPC52xx_PSC_IMR_TXEMP;
Jon Smirlcebe7762009-05-23 19:13:01 -0400185 if (psc_dma->capture.active)
Jon Smirl89dd0842009-05-23 19:12:59 -0400186 imr |= MPC52xx_PSC_IMR_ORERR;
Jon Smirldbcc3472009-05-26 08:34:08 -0400187 out_be16(&regs->isr_imr.imr, psc_dma->imr | imr);
Jon Smirl89dd0842009-05-23 19:12:59 -0400188
189 return 0;
190}
191
Jon Smirl89dd0842009-05-23 19:12:59 -0400192
193/* ---------------------------------------------------------------------
194 * The PSC DMA 'ASoC platform' driver
195 *
196 * Can be referenced by an 'ASoC machine' driver
197 * This driver only deals with the audio bus; it doesn't have any
198 * interaction with the attached codec
199 */
200
Jon Smirldbcc3472009-05-26 08:34:08 -0400201static const struct snd_pcm_hardware psc_dma_hardware = {
Jon Smirl89dd0842009-05-23 19:12:59 -0400202 .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
203 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
204 SNDRV_PCM_INFO_BATCH,
205 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE |
Jon Smirldbcc3472009-05-26 08:34:08 -0400206 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE,
Jon Smirl89dd0842009-05-23 19:12:59 -0400207 .period_bytes_max = 1024 * 1024,
208 .period_bytes_min = 32,
209 .periods_min = 2,
210 .periods_max = 256,
211 .buffer_bytes_max = 2 * 1024 * 1024,
Jon Smirldbcc3472009-05-26 08:34:08 -0400212 .fifo_size = 512,
Jon Smirl89dd0842009-05-23 19:12:59 -0400213};
214
Kuninori Morimoto6d1048b2019-10-02 14:34:38 +0900215static int psc_dma_open(struct snd_soc_component *component,
216 struct snd_pcm_substream *substream)
Jon Smirl89dd0842009-05-23 19:12:59 -0400217{
Jon Smirldbcc3472009-05-26 08:34:08 -0400218 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900219 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900220 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
Jon Smirlcebe7762009-05-23 19:13:01 -0400221 struct psc_dma_stream *s;
Jon Smirldbcc3472009-05-26 08:34:08 -0400222 int rc;
Jon Smirl89dd0842009-05-23 19:12:59 -0400223
Jon Smirldbcc3472009-05-26 08:34:08 -0400224 dev_dbg(psc_dma->dev, "psc_dma_open(substream=%p)\n", substream);
Jon Smirl89dd0842009-05-23 19:12:59 -0400225
226 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
Jon Smirlcebe7762009-05-23 19:13:01 -0400227 s = &psc_dma->capture;
Jon Smirl89dd0842009-05-23 19:12:59 -0400228 else
Jon Smirlcebe7762009-05-23 19:13:01 -0400229 s = &psc_dma->playback;
Jon Smirl89dd0842009-05-23 19:12:59 -0400230
Jon Smirldbcc3472009-05-26 08:34:08 -0400231 snd_soc_set_runtime_hwparams(substream, &psc_dma_hardware);
232
233 rc = snd_pcm_hw_constraint_integer(runtime,
234 SNDRV_PCM_HW_PARAM_PERIODS);
235 if (rc < 0) {
236 dev_err(substream->pcm->card->dev, "invalid buffer size\n");
237 return rc;
238 }
Jon Smirl89dd0842009-05-23 19:12:59 -0400239
240 s->stream = substream;
241 return 0;
242}
243
Kuninori Morimoto6d1048b2019-10-02 14:34:38 +0900244static int psc_dma_close(struct snd_soc_component *component,
245 struct snd_pcm_substream *substream)
Jon Smirl89dd0842009-05-23 19:12:59 -0400246{
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900247 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900248 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
Jon Smirlcebe7762009-05-23 19:13:01 -0400249 struct psc_dma_stream *s;
Jon Smirl89dd0842009-05-23 19:12:59 -0400250
Jon Smirldbcc3472009-05-26 08:34:08 -0400251 dev_dbg(psc_dma->dev, "psc_dma_close(substream=%p)\n", substream);
Jon Smirl89dd0842009-05-23 19:12:59 -0400252
253 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
Jon Smirlcebe7762009-05-23 19:13:01 -0400254 s = &psc_dma->capture;
Jon Smirl89dd0842009-05-23 19:12:59 -0400255 else
Jon Smirlcebe7762009-05-23 19:13:01 -0400256 s = &psc_dma->playback;
Jon Smirl89dd0842009-05-23 19:12:59 -0400257
Jon Smirldbcc3472009-05-26 08:34:08 -0400258 if (!psc_dma->playback.active &&
259 !psc_dma->capture.active) {
260
261 /* Disable all interrupts and reset the PSC */
262 out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
263 out_8(&psc_dma->psc_regs->command, 4 << 4); /* reset error */
264 }
Jon Smirl89dd0842009-05-23 19:12:59 -0400265 s->stream = NULL;
266 return 0;
267}
268
269static snd_pcm_uframes_t
Kuninori Morimoto6d1048b2019-10-02 14:34:38 +0900270psc_dma_pointer(struct snd_soc_component *component,
271 struct snd_pcm_substream *substream)
Jon Smirl89dd0842009-05-23 19:12:59 -0400272{
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900273 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900274 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
Jon Smirlcebe7762009-05-23 19:13:01 -0400275 struct psc_dma_stream *s;
Jon Smirl89dd0842009-05-23 19:12:59 -0400276 dma_addr_t count;
277
278 if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
Jon Smirlcebe7762009-05-23 19:13:01 -0400279 s = &psc_dma->capture;
Jon Smirl89dd0842009-05-23 19:12:59 -0400280 else
Jon Smirlcebe7762009-05-23 19:13:01 -0400281 s = &psc_dma->playback;
Jon Smirl89dd0842009-05-23 19:12:59 -0400282
Grant Likely8f159d72009-11-07 01:33:53 -0700283 count = s->period_current * s->period_bytes;
Jon Smirl89dd0842009-05-23 19:12:59 -0400284
285 return bytes_to_frames(substream->runtime, count);
286}
287
Kuninori Morimoto6d1048b2019-10-02 14:34:38 +0900288static int psc_dma_hw_params(struct snd_soc_component *component,
289 struct snd_pcm_substream *substream,
290 struct snd_pcm_hw_params *params)
Jon Smirldbcc3472009-05-26 08:34:08 -0400291{
292 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
293
294 return 0;
295}
296
Kuninori Morimoto6d1048b2019-10-02 14:34:38 +0900297static int psc_dma_new(struct snd_soc_component *component,
298 struct snd_soc_pcm_runtime *rtd)
Jon Smirl89dd0842009-05-23 19:12:59 -0400299{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100300 struct snd_card *card = rtd->card->snd_card;
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900301 struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0);
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100302 struct snd_pcm *pcm = rtd->pcm;
Jon Smirldbcc3472009-05-26 08:34:08 -0400303 size_t size = psc_dma_hardware.buffer_bytes_max;
Russell Kingc9bd5e62013-06-27 12:53:37 +0100304 int rc;
Jon Smirl89dd0842009-05-23 19:12:59 -0400305
Kuninori Morimoto60bceb82018-01-29 02:47:19 +0000306 dev_dbg(component->dev, "psc_dma_new(card=%p, dai=%p, pcm=%p)\n",
Jon Smirl89dd0842009-05-23 19:12:59 -0400307 card, dai, pcm);
308
Russell Kingc9bd5e62013-06-27 12:53:37 +0100309 rc = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
310 if (rc)
311 return rc;
Jon Smirl89dd0842009-05-23 19:12:59 -0400312
Joachim Eastwood62969142012-01-01 02:14:24 +0100313 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
Jon Smirldbcc3472009-05-26 08:34:08 -0400314 rc = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev,
Joachim Eastwood62969142012-01-01 02:14:24 +0100315 size, &pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->dma_buffer);
Jon Smirl89dd0842009-05-23 19:12:59 -0400316 if (rc)
317 goto playback_alloc_err;
318 }
319
Joachim Eastwood62969142012-01-01 02:14:24 +0100320 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
Jon Smirldbcc3472009-05-26 08:34:08 -0400321 rc = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev,
Joachim Eastwood62969142012-01-01 02:14:24 +0100322 size, &pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->dma_buffer);
Jon Smirl89dd0842009-05-23 19:12:59 -0400323 if (rc)
324 goto capture_alloc_err;
325 }
326
327 return 0;
328
329 capture_alloc_err:
Joachim Eastwood62969142012-01-01 02:14:24 +0100330 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
331 snd_dma_free_pages(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->dma_buffer);
Jon Smirldbcc3472009-05-26 08:34:08 -0400332
Jon Smirl89dd0842009-05-23 19:12:59 -0400333 playback_alloc_err:
334 dev_err(card->dev, "Cannot allocate buffer(s)\n");
Jon Smirldbcc3472009-05-26 08:34:08 -0400335
Jon Smirl89dd0842009-05-23 19:12:59 -0400336 return -ENOMEM;
337}
338
Kuninori Morimoto6d1048b2019-10-02 14:34:38 +0900339static void psc_dma_free(struct snd_soc_component *component,
340 struct snd_pcm *pcm)
Jon Smirl89dd0842009-05-23 19:12:59 -0400341{
342 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
343 struct snd_pcm_substream *substream;
344 int stream;
345
Kuninori Morimoto60bceb82018-01-29 02:47:19 +0000346 dev_dbg(component->dev, "psc_dma_free(pcm=%p)\n", pcm);
Jon Smirl89dd0842009-05-23 19:12:59 -0400347
348 for (stream = 0; stream < 2; stream++) {
349 substream = pcm->streams[stream].substream;
350 if (substream) {
351 snd_dma_free_pages(&substream->dma_buffer);
352 substream->dma_buffer.area = NULL;
353 substream->dma_buffer.addr = 0;
354 }
355 }
356}
357
Kuninori Morimoto60bceb82018-01-29 02:47:19 +0000358static const struct snd_soc_component_driver mpc5200_audio_dma_component = {
359 .name = DRV_NAME,
Kuninori Morimoto6d1048b2019-10-02 14:34:38 +0900360 .open = psc_dma_open,
361 .close = psc_dma_close,
362 .hw_free = psc_dma_hw_free,
Kuninori Morimoto6d1048b2019-10-02 14:34:38 +0900363 .pointer = psc_dma_pointer,
364 .trigger = psc_dma_trigger,
365 .hw_params = psc_dma_hw_params,
366 .pcm_construct = psc_dma_new,
367 .pcm_destruct = psc_dma_free,
Jon Smirl89dd0842009-05-23 19:12:59 -0400368};
369
Eric Millbrandtf515b672012-09-13 17:43:11 -0400370int mpc5200_audio_dma_create(struct platform_device *op)
Jon Smirldbcc3472009-05-26 08:34:08 -0400371{
372 phys_addr_t fifo;
373 struct psc_dma *psc_dma;
374 struct resource res;
375 int size, irq, rc;
376 const __be32 *prop;
377 void __iomem *regs;
Julia Lawall33d7f772009-09-12 14:25:35 +0200378 int ret;
Jon Smirldbcc3472009-05-26 08:34:08 -0400379
380 /* Fetch the registers and IRQ of the PSC */
Grant Likely61c7a082010-04-13 16:12:29 -0700381 irq = irq_of_parse_and_map(op->dev.of_node, 0);
382 if (of_address_to_resource(op->dev.of_node, 0, &res)) {
Jon Smirldbcc3472009-05-26 08:34:08 -0400383 dev_err(&op->dev, "Missing reg property\n");
384 return -ENODEV;
385 }
Joe Perches28f65c112011-06-09 09:13:32 -0700386 regs = ioremap(res.start, resource_size(&res));
Jon Smirldbcc3472009-05-26 08:34:08 -0400387 if (!regs) {
388 dev_err(&op->dev, "Could not map registers\n");
389 return -ENODEV;
390 }
391
392 /* Allocate and initialize the driver private data */
393 psc_dma = kzalloc(sizeof *psc_dma, GFP_KERNEL);
394 if (!psc_dma) {
Julia Lawall33d7f772009-09-12 14:25:35 +0200395 ret = -ENOMEM;
396 goto out_unmap;
Jon Smirldbcc3472009-05-26 08:34:08 -0400397 }
398
399 /* Get the PSC ID */
Grant Likely61c7a082010-04-13 16:12:29 -0700400 prop = of_get_property(op->dev.of_node, "cell-index", &size);
Julia Lawall33d7f772009-09-12 14:25:35 +0200401 if (!prop || size < sizeof *prop) {
402 ret = -ENODEV;
403 goto out_free;
404 }
Jon Smirldbcc3472009-05-26 08:34:08 -0400405
406 spin_lock_init(&psc_dma->lock);
Grant Likely0827d6b2009-07-02 11:57:25 -0600407 mutex_init(&psc_dma->mutex);
Jon Smirldbcc3472009-05-26 08:34:08 -0400408 psc_dma->id = be32_to_cpu(*prop);
409 psc_dma->irq = irq;
410 psc_dma->psc_regs = regs;
411 psc_dma->fifo_regs = regs + sizeof *psc_dma->psc_regs;
412 psc_dma->dev = &op->dev;
413 psc_dma->playback.psc_dma = psc_dma;
414 psc_dma->capture.psc_dma = psc_dma;
415 snprintf(psc_dma->name, sizeof psc_dma->name, "PSC%u", psc_dma->id);
416
417 /* Find the address of the fifo data registers and setup the
418 * DMA tasks */
419 fifo = res.start + offsetof(struct mpc52xx_psc, buffer.buffer_32);
420 psc_dma->capture.bcom_task =
421 bcom_psc_gen_bd_rx_init(psc_dma->id, 10, fifo, 512);
422 psc_dma->playback.bcom_task =
423 bcom_psc_gen_bd_tx_init(psc_dma->id, 10, fifo);
424 if (!psc_dma->capture.bcom_task ||
425 !psc_dma->playback.bcom_task) {
426 dev_err(&op->dev, "Could not allocate bestcomm tasks\n");
Julia Lawall33d7f772009-09-12 14:25:35 +0200427 ret = -ENODEV;
428 goto out_free;
Jon Smirldbcc3472009-05-26 08:34:08 -0400429 }
430
431 /* Disable all interrupts and reset the PSC */
432 out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
433 /* reset receiver */
434 out_8(&psc_dma->psc_regs->command, MPC52xx_PSC_RST_RX);
435 /* reset transmitter */
436 out_8(&psc_dma->psc_regs->command, MPC52xx_PSC_RST_TX);
437 /* reset error */
438 out_8(&psc_dma->psc_regs->command, MPC52xx_PSC_RST_ERR_STAT);
439 /* reset mode */
440 out_8(&psc_dma->psc_regs->command, MPC52xx_PSC_SEL_MODE_REG_1);
441
442 /* Set up mode register;
443 * First write: RxRdy (FIFO Alarm) generates rx FIFO irq
444 * Second write: register Normal mode for non loopback
445 */
446 out_8(&psc_dma->psc_regs->mode, 0);
447 out_8(&psc_dma->psc_regs->mode, 0);
448
449 /* Set the TX and RX fifo alarm thresholds */
450 out_be16(&psc_dma->fifo_regs->rfalarm, 0x100);
451 out_8(&psc_dma->fifo_regs->rfcntl, 0x4);
452 out_be16(&psc_dma->fifo_regs->tfalarm, 0x100);
453 out_8(&psc_dma->fifo_regs->tfcntl, 0x7);
454
455 /* Lookup the IRQ numbers */
456 psc_dma->playback.irq =
457 bcom_get_task_irq(psc_dma->playback.bcom_task);
458 psc_dma->capture.irq =
459 bcom_get_task_irq(psc_dma->capture.bcom_task);
460
461 rc = request_irq(psc_dma->irq, &psc_dma_status_irq, IRQF_SHARED,
462 "psc-dma-status", psc_dma);
Grant Likelya68cc8d2009-11-09 09:40:09 -0700463 rc |= request_irq(psc_dma->capture.irq, &psc_dma_bcom_irq, IRQF_SHARED,
Jon Smirldbcc3472009-05-26 08:34:08 -0400464 "psc-dma-capture", &psc_dma->capture);
Grant Likelya68cc8d2009-11-09 09:40:09 -0700465 rc |= request_irq(psc_dma->playback.irq, &psc_dma_bcom_irq, IRQF_SHARED,
Jon Smirldbcc3472009-05-26 08:34:08 -0400466 "psc-dma-playback", &psc_dma->playback);
467 if (rc) {
Julia Lawall33d7f772009-09-12 14:25:35 +0200468 ret = -ENODEV;
469 goto out_irq;
Jon Smirldbcc3472009-05-26 08:34:08 -0400470 }
471
472 /* Save what we've done so it can be found again later */
473 dev_set_drvdata(&op->dev, psc_dma);
474
475 /* Tell the ASoC OF helpers about it */
Kuninori Morimoto60bceb82018-01-29 02:47:19 +0000476 return devm_snd_soc_register_component(&op->dev,
477 &mpc5200_audio_dma_component, NULL, 0);
Julia Lawall33d7f772009-09-12 14:25:35 +0200478out_irq:
479 free_irq(psc_dma->irq, psc_dma);
480 free_irq(psc_dma->capture.irq, &psc_dma->capture);
481 free_irq(psc_dma->playback.irq, &psc_dma->playback);
482out_free:
483 kfree(psc_dma);
484out_unmap:
485 iounmap(regs);
486 return ret;
Jon Smirldbcc3472009-05-26 08:34:08 -0400487}
Eric Millbrandtf515b672012-09-13 17:43:11 -0400488EXPORT_SYMBOL_GPL(mpc5200_audio_dma_create);
Jon Smirldbcc3472009-05-26 08:34:08 -0400489
Eric Millbrandtf515b672012-09-13 17:43:11 -0400490int mpc5200_audio_dma_destroy(struct platform_device *op)
Jon Smirldbcc3472009-05-26 08:34:08 -0400491{
492 struct psc_dma *psc_dma = dev_get_drvdata(&op->dev);
493
494 dev_dbg(&op->dev, "mpc5200_audio_dma_destroy()\n");
495
Jon Smirldbcc3472009-05-26 08:34:08 -0400496 bcom_gen_bd_rx_release(psc_dma->capture.bcom_task);
497 bcom_gen_bd_tx_release(psc_dma->playback.bcom_task);
498
499 /* Release irqs */
500 free_irq(psc_dma->irq, psc_dma);
501 free_irq(psc_dma->capture.irq, &psc_dma->capture);
502 free_irq(psc_dma->playback.irq, &psc_dma->playback);
503
504 iounmap(psc_dma->psc_regs);
505 kfree(psc_dma);
506 dev_set_drvdata(&op->dev, NULL);
507
508 return 0;
509}
Eric Millbrandtf515b672012-09-13 17:43:11 -0400510EXPORT_SYMBOL_GPL(mpc5200_audio_dma_destroy);
Jon Smirldbcc3472009-05-26 08:34:08 -0400511
512MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
513MODULE_DESCRIPTION("Freescale MPC5200 PSC in DMA mode ASoC Driver");
514MODULE_LICENSE("GPL");