2 * Generic TXx9 ACLC platform driver
4 * Copyright (C) 2009 Atsushi Nemoto
6 * Based on RBTX49xx patch from CELF patch archive.
7 * (C) Copyright TOSHIBA CORPORATION 2004-2006
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
17 #include <linux/scatterlist.h>
18 #include <linux/slab.h>
19 #include <linux/dmaengine.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
26 #define DRV_NAME "txx9aclc"
28 static struct txx9aclc_soc_device {
29 struct txx9aclc_dmadata dmadata[2];
30 } txx9aclc_soc_device;
32 /* REVISIT: How to find txx9aclc_drvdata from snd_ac97? */
33 static struct txx9aclc_plat_drvdata *txx9aclc_drvdata;
35 static int txx9aclc_dma_init(struct txx9aclc_soc_device *dev,
36 struct txx9aclc_dmadata *dmadata);
38 static const struct snd_pcm_hardware txx9aclc_pcm_hardware = {
40 * REVISIT: SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
41 * needs more works for noncoherent MIPS.
43 .info = SNDRV_PCM_INFO_INTERLEAVED |
44 SNDRV_PCM_INFO_BATCH |
46 .period_bytes_min = 1024,
47 .period_bytes_max = 8 * 1024,
50 .buffer_bytes_max = 32 * 1024,
53 static int txx9aclc_pcm_hw_params(struct snd_pcm_substream *substream,
54 struct snd_pcm_hw_params *params)
56 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
57 struct snd_pcm_runtime *runtime = substream->runtime;
58 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
59 struct txx9aclc_dmadata *dmadata = runtime->private_data;
62 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
66 dev_dbg(component->dev,
67 "runtime->dma_area = %#lx dma_addr = %#lx dma_bytes = %zd "
68 "runtime->min_align %ld\n",
69 (unsigned long)runtime->dma_area,
70 (unsigned long)runtime->dma_addr, runtime->dma_bytes,
72 dev_dbg(component->dev,
73 "periods %d period_bytes %d stream %d\n",
74 params_periods(params), params_period_bytes(params),
77 dmadata->substream = substream;
82 static int txx9aclc_pcm_hw_free(struct snd_pcm_substream *substream)
84 return snd_pcm_lib_free_pages(substream);
87 static int txx9aclc_pcm_prepare(struct snd_pcm_substream *substream)
89 struct snd_pcm_runtime *runtime = substream->runtime;
90 struct txx9aclc_dmadata *dmadata = runtime->private_data;
92 dmadata->dma_addr = runtime->dma_addr;
93 dmadata->buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
94 dmadata->period_bytes = snd_pcm_lib_period_bytes(substream);
96 if (dmadata->buffer_bytes == dmadata->period_bytes) {
97 dmadata->frag_bytes = dmadata->period_bytes >> 1;
100 dmadata->frag_bytes = dmadata->period_bytes;
101 dmadata->frags = dmadata->buffer_bytes / dmadata->period_bytes;
103 dmadata->frag_count = 0;
108 static void txx9aclc_dma_complete(void *arg)
110 struct txx9aclc_dmadata *dmadata = arg;
113 /* dma completion handler cannot submit new operations */
114 spin_lock_irqsave(&dmadata->dma_lock, flags);
115 if (dmadata->frag_count >= 0) {
117 if (!WARN_ON(dmadata->dmacount < 0))
118 tasklet_schedule(&dmadata->tasklet);
120 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
123 static struct dma_async_tx_descriptor *
124 txx9aclc_dma_submit(struct txx9aclc_dmadata *dmadata, dma_addr_t buf_dma_addr)
126 struct dma_chan *chan = dmadata->dma_chan;
127 struct dma_async_tx_descriptor *desc;
128 struct scatterlist sg;
130 sg_init_table(&sg, 1);
131 sg_set_page(&sg, pfn_to_page(PFN_DOWN(buf_dma_addr)),
132 dmadata->frag_bytes, buf_dma_addr & (PAGE_SIZE - 1));
133 sg_dma_address(&sg) = buf_dma_addr;
134 desc = dmaengine_prep_slave_sg(chan, &sg, 1,
135 dmadata->substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
136 DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
137 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
139 dev_err(&chan->dev->device, "cannot prepare slave dma\n");
142 desc->callback = txx9aclc_dma_complete;
143 desc->callback_param = dmadata;
144 dmaengine_submit(desc);
148 #define NR_DMA_CHAIN 2
150 static void txx9aclc_dma_tasklet(unsigned long data)
152 struct txx9aclc_dmadata *dmadata = (struct txx9aclc_dmadata *)data;
153 struct dma_chan *chan = dmadata->dma_chan;
154 struct dma_async_tx_descriptor *desc;
155 struct snd_pcm_substream *substream = dmadata->substream;
156 u32 ctlbit = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
157 ACCTL_AUDODMA : ACCTL_AUDIDMA;
161 spin_lock_irqsave(&dmadata->dma_lock, flags);
162 if (dmadata->frag_count < 0) {
163 struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
164 void __iomem *base = drvdata->base;
166 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
167 dmaengine_terminate_all(chan);
169 for (i = 0; i < NR_DMA_CHAIN; i++) {
170 desc = txx9aclc_dma_submit(dmadata,
171 dmadata->dma_addr + i * dmadata->frag_bytes);
175 dmadata->dmacount = NR_DMA_CHAIN;
176 dma_async_issue_pending(chan);
177 spin_lock_irqsave(&dmadata->dma_lock, flags);
178 __raw_writel(ctlbit, base + ACCTLEN);
179 dmadata->frag_count = NR_DMA_CHAIN % dmadata->frags;
180 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
183 if (WARN_ON(dmadata->dmacount >= NR_DMA_CHAIN)) {
184 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
187 while (dmadata->dmacount < NR_DMA_CHAIN) {
189 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
190 desc = txx9aclc_dma_submit(dmadata,
192 dmadata->frag_count * dmadata->frag_bytes);
195 dma_async_issue_pending(chan);
197 spin_lock_irqsave(&dmadata->dma_lock, flags);
198 dmadata->frag_count++;
199 dmadata->frag_count %= dmadata->frags;
200 dmadata->pos += dmadata->frag_bytes;
201 dmadata->pos %= dmadata->buffer_bytes;
202 if ((dmadata->frag_count * dmadata->frag_bytes) %
203 dmadata->period_bytes == 0)
204 snd_pcm_period_elapsed(substream);
206 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
209 static int txx9aclc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
211 struct txx9aclc_dmadata *dmadata = substream->runtime->private_data;
212 struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
213 void __iomem *base = drvdata->base;
216 u32 ctlbit = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
217 ACCTL_AUDODMA : ACCTL_AUDIDMA;
219 spin_lock_irqsave(&dmadata->dma_lock, flags);
221 case SNDRV_PCM_TRIGGER_START:
222 dmadata->frag_count = -1;
223 tasklet_schedule(&dmadata->tasklet);
225 case SNDRV_PCM_TRIGGER_STOP:
226 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
227 case SNDRV_PCM_TRIGGER_SUSPEND:
228 __raw_writel(ctlbit, base + ACCTLDIS);
230 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
231 case SNDRV_PCM_TRIGGER_RESUME:
232 __raw_writel(ctlbit, base + ACCTLEN);
237 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
241 static snd_pcm_uframes_t
242 txx9aclc_pcm_pointer(struct snd_pcm_substream *substream)
244 struct txx9aclc_dmadata *dmadata = substream->runtime->private_data;
246 return bytes_to_frames(substream->runtime, dmadata->pos);
249 static int txx9aclc_pcm_open(struct snd_pcm_substream *substream)
251 struct txx9aclc_soc_device *dev = &txx9aclc_soc_device;
252 struct txx9aclc_dmadata *dmadata = &dev->dmadata[substream->stream];
255 ret = snd_soc_set_runtime_hwparams(substream, &txx9aclc_pcm_hardware);
258 /* ensure that buffer size is a multiple of period size */
259 ret = snd_pcm_hw_constraint_integer(substream->runtime,
260 SNDRV_PCM_HW_PARAM_PERIODS);
263 substream->runtime->private_data = dmadata;
267 static int txx9aclc_pcm_close(struct snd_pcm_substream *substream)
269 struct txx9aclc_dmadata *dmadata = substream->runtime->private_data;
270 struct dma_chan *chan = dmadata->dma_chan;
272 dmadata->frag_count = -1;
273 dmaengine_terminate_all(chan);
277 static const struct snd_pcm_ops txx9aclc_pcm_ops = {
278 .open = txx9aclc_pcm_open,
279 .close = txx9aclc_pcm_close,
280 .ioctl = snd_pcm_lib_ioctl,
281 .hw_params = txx9aclc_pcm_hw_params,
282 .hw_free = txx9aclc_pcm_hw_free,
283 .prepare = txx9aclc_pcm_prepare,
284 .trigger = txx9aclc_pcm_trigger,
285 .pointer = txx9aclc_pcm_pointer,
288 static int txx9aclc_pcm_new(struct snd_soc_pcm_runtime *rtd)
290 struct snd_card *card = rtd->card->snd_card;
291 struct snd_soc_dai *dai = rtd->cpu_dai;
292 struct snd_pcm *pcm = rtd->pcm;
293 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
294 struct platform_device *pdev = to_platform_device(component->dev);
295 struct txx9aclc_soc_device *dev;
300 /* at this point onwards the AC97 component has probed and this will be valid */
301 dev = snd_soc_dai_get_drvdata(dai);
303 dev->dmadata[0].stream = SNDRV_PCM_STREAM_PLAYBACK;
304 dev->dmadata[1].stream = SNDRV_PCM_STREAM_CAPTURE;
305 for (i = 0; i < 2; i++) {
306 r = platform_get_resource(pdev, IORESOURCE_DMA, i);
311 dev->dmadata[i].dma_res = r;
312 ret = txx9aclc_dma_init(dev, &dev->dmadata[i]);
316 return snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
317 card->dev, 64 * 1024, 4 * 1024 * 1024);
320 for (i = 0; i < 2; i++) {
321 if (dev->dmadata[i].dma_chan)
322 dma_release_channel(dev->dmadata[i].dma_chan);
323 dev->dmadata[i].dma_chan = NULL;
328 static bool filter(struct dma_chan *chan, void *param)
330 struct txx9aclc_dmadata *dmadata = param;
334 devname = kasprintf(GFP_KERNEL, "%s.%d", dmadata->dma_res->name,
335 (int)dmadata->dma_res->start);
336 if (strcmp(dev_name(chan->device->dev), devname) == 0) {
337 chan->private = &dmadata->dma_slave;
344 static int txx9aclc_dma_init(struct txx9aclc_soc_device *dev,
345 struct txx9aclc_dmadata *dmadata)
347 struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
348 struct txx9dmac_slave *ds = &dmadata->dma_slave;
351 spin_lock_init(&dmadata->dma_lock);
353 ds->reg_width = sizeof(u32);
354 if (dmadata->stream == SNDRV_PCM_STREAM_PLAYBACK) {
355 ds->tx_reg = drvdata->physbase + ACAUDODAT;
359 ds->rx_reg = drvdata->physbase + ACAUDIDAT;
362 /* Try to grab a DMA channel */
364 dma_cap_set(DMA_SLAVE, mask);
365 dmadata->dma_chan = dma_request_channel(mask, filter, dmadata);
366 if (!dmadata->dma_chan) {
368 "DMA channel for %s is not available\n",
369 dmadata->stream == SNDRV_PCM_STREAM_PLAYBACK ?
370 "playback" : "capture");
373 tasklet_init(&dmadata->tasklet, txx9aclc_dma_tasklet,
374 (unsigned long)dmadata);
378 static int txx9aclc_pcm_probe(struct snd_soc_component *component)
380 snd_soc_component_set_drvdata(component, &txx9aclc_soc_device);
384 static void txx9aclc_pcm_remove(struct snd_soc_component *component)
386 struct txx9aclc_soc_device *dev = snd_soc_component_get_drvdata(component);
387 struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
388 void __iomem *base = drvdata->base;
391 /* disable all FIFO DMAs */
392 __raw_writel(ACCTL_AUDODMA | ACCTL_AUDIDMA, base + ACCTLDIS);
393 /* dummy R/W to clear pending DMAREQ if any */
394 __raw_writel(__raw_readl(base + ACAUDIDAT), base + ACAUDODAT);
396 for (i = 0; i < 2; i++) {
397 struct txx9aclc_dmadata *dmadata = &dev->dmadata[i];
398 struct dma_chan *chan = dmadata->dma_chan;
401 dmadata->frag_count = -1;
402 dmaengine_terminate_all(chan);
403 dma_release_channel(chan);
405 dev->dmadata[i].dma_chan = NULL;
409 static const struct snd_soc_component_driver txx9aclc_soc_component = {
411 .probe = txx9aclc_pcm_probe,
412 .remove = txx9aclc_pcm_remove,
413 .ops = &txx9aclc_pcm_ops,
414 .pcm_new = txx9aclc_pcm_new,
417 static int txx9aclc_soc_platform_probe(struct platform_device *pdev)
419 return devm_snd_soc_register_component(&pdev->dev,
420 &txx9aclc_soc_component, NULL, 0);
423 static struct platform_driver txx9aclc_pcm_driver = {
425 .name = "txx9aclc-pcm-audio",
428 .probe = txx9aclc_soc_platform_probe,
431 module_platform_driver(txx9aclc_pcm_driver);
433 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
434 MODULE_DESCRIPTION("TXx9 ACLC Audio DMA driver");
435 MODULE_LICENSE("GPL");