Merge tag 'asoc-v3.13-4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[cascardo/linux.git] / sound / soc / txx9 / txx9aclc.c
1 /*
2  * Generic TXx9 ACLC platform driver
3  *
4  * Copyright (C) 2009 Atsushi Nemoto
5  *
6  * Based on RBTX49xx patch from CELF patch archive.
7  * (C) Copyright TOSHIBA CORPORATION 2004-2006
8  *
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.
12  */
13
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 <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include "txx9aclc.h"
24
25 static struct txx9aclc_soc_device {
26         struct txx9aclc_dmadata dmadata[2];
27 } txx9aclc_soc_device;
28
29 /* REVISIT: How to find txx9aclc_drvdata from snd_ac97? */
30 static struct txx9aclc_plat_drvdata *txx9aclc_drvdata;
31
32 static int txx9aclc_dma_init(struct txx9aclc_soc_device *dev,
33                              struct txx9aclc_dmadata *dmadata);
34
35 static const struct snd_pcm_hardware txx9aclc_pcm_hardware = {
36         /*
37          * REVISIT: SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
38          * needs more works for noncoherent MIPS.
39          */
40         .info             = SNDRV_PCM_INFO_INTERLEAVED |
41                             SNDRV_PCM_INFO_BATCH |
42                             SNDRV_PCM_INFO_PAUSE,
43 #ifdef __BIG_ENDIAN
44         .formats          = SNDRV_PCM_FMTBIT_S16_BE,
45 #else
46         .formats          = SNDRV_PCM_FMTBIT_S16_LE,
47 #endif
48         .period_bytes_min = 1024,
49         .period_bytes_max = 8 * 1024,
50         .periods_min      = 2,
51         .periods_max      = 4096,
52         .buffer_bytes_max = 32 * 1024,
53 };
54
55 static int txx9aclc_pcm_hw_params(struct snd_pcm_substream *substream,
56                                   struct snd_pcm_hw_params *params)
57 {
58         struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
59         struct snd_pcm_runtime *runtime = substream->runtime;
60         struct txx9aclc_dmadata *dmadata = runtime->private_data;
61         int ret;
62
63         ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
64         if (ret < 0)
65                 return ret;
66
67         dev_dbg(rtd->platform->dev,
68                 "runtime->dma_area = %#lx dma_addr = %#lx dma_bytes = %zd "
69                 "runtime->min_align %ld\n",
70                 (unsigned long)runtime->dma_area,
71                 (unsigned long)runtime->dma_addr, runtime->dma_bytes,
72                 runtime->min_align);
73         dev_dbg(rtd->platform->dev,
74                 "periods %d period_bytes %d stream %d\n",
75                 params_periods(params), params_period_bytes(params),
76                 substream->stream);
77
78         dmadata->substream = substream;
79         dmadata->pos = 0;
80         return 0;
81 }
82
83 static int txx9aclc_pcm_hw_free(struct snd_pcm_substream *substream)
84 {
85         return snd_pcm_lib_free_pages(substream);
86 }
87
88 static int txx9aclc_pcm_prepare(struct snd_pcm_substream *substream)
89 {
90         struct snd_pcm_runtime *runtime = substream->runtime;
91         struct txx9aclc_dmadata *dmadata = runtime->private_data;
92
93         dmadata->dma_addr = runtime->dma_addr;
94         dmadata->buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
95         dmadata->period_bytes = snd_pcm_lib_period_bytes(substream);
96
97         if (dmadata->buffer_bytes == dmadata->period_bytes) {
98                 dmadata->frag_bytes = dmadata->period_bytes >> 1;
99                 dmadata->frags = 2;
100         } else {
101                 dmadata->frag_bytes = dmadata->period_bytes;
102                 dmadata->frags = dmadata->buffer_bytes / dmadata->period_bytes;
103         }
104         dmadata->frag_count = 0;
105         dmadata->pos = 0;
106         return 0;
107 }
108
109 static void txx9aclc_dma_complete(void *arg)
110 {
111         struct txx9aclc_dmadata *dmadata = arg;
112         unsigned long flags;
113
114         /* dma completion handler cannot submit new operations */
115         spin_lock_irqsave(&dmadata->dma_lock, flags);
116         if (dmadata->frag_count >= 0) {
117                 dmadata->dmacount--;
118                 if (!WARN_ON(dmadata->dmacount < 0))
119                         tasklet_schedule(&dmadata->tasklet);
120         }
121         spin_unlock_irqrestore(&dmadata->dma_lock, flags);
122 }
123
124 static struct dma_async_tx_descriptor *
125 txx9aclc_dma_submit(struct txx9aclc_dmadata *dmadata, dma_addr_t buf_dma_addr)
126 {
127         struct dma_chan *chan = dmadata->dma_chan;
128         struct dma_async_tx_descriptor *desc;
129         struct scatterlist sg;
130
131         sg_init_table(&sg, 1);
132         sg_set_page(&sg, pfn_to_page(PFN_DOWN(buf_dma_addr)),
133                     dmadata->frag_bytes, buf_dma_addr & (PAGE_SIZE - 1));
134         sg_dma_address(&sg) = buf_dma_addr;
135         desc = dmaengine_prep_slave_sg(chan, &sg, 1,
136                 dmadata->substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
137                 DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
138                 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
139         if (!desc) {
140                 dev_err(&chan->dev->device, "cannot prepare slave dma\n");
141                 return NULL;
142         }
143         desc->callback = txx9aclc_dma_complete;
144         desc->callback_param = dmadata;
145         desc->tx_submit(desc);
146         return desc;
147 }
148
149 #define NR_DMA_CHAIN            2
150
151 static void txx9aclc_dma_tasklet(unsigned long data)
152 {
153         struct txx9aclc_dmadata *dmadata = (struct txx9aclc_dmadata *)data;
154         struct dma_chan *chan = dmadata->dma_chan;
155         struct dma_async_tx_descriptor *desc;
156         struct snd_pcm_substream *substream = dmadata->substream;
157         u32 ctlbit = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
158                 ACCTL_AUDODMA : ACCTL_AUDIDMA;
159         int i;
160         unsigned long flags;
161
162         spin_lock_irqsave(&dmadata->dma_lock, flags);
163         if (dmadata->frag_count < 0) {
164                 struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
165                 void __iomem *base = drvdata->base;
166
167                 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
168                 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
169                 /* first time */
170                 for (i = 0; i < NR_DMA_CHAIN; i++) {
171                         desc = txx9aclc_dma_submit(dmadata,
172                                 dmadata->dma_addr + i * dmadata->frag_bytes);
173                         if (!desc)
174                                 return;
175                 }
176                 dmadata->dmacount = NR_DMA_CHAIN;
177                 chan->device->device_issue_pending(chan);
178                 spin_lock_irqsave(&dmadata->dma_lock, flags);
179                 __raw_writel(ctlbit, base + ACCTLEN);
180                 dmadata->frag_count = NR_DMA_CHAIN % dmadata->frags;
181                 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
182                 return;
183         }
184         if (WARN_ON(dmadata->dmacount >= NR_DMA_CHAIN)) {
185                 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
186                 return;
187         }
188         while (dmadata->dmacount < NR_DMA_CHAIN) {
189                 dmadata->dmacount++;
190                 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
191                 desc = txx9aclc_dma_submit(dmadata,
192                         dmadata->dma_addr +
193                         dmadata->frag_count * dmadata->frag_bytes);
194                 if (!desc)
195                         return;
196                 chan->device->device_issue_pending(chan);
197
198                 spin_lock_irqsave(&dmadata->dma_lock, flags);
199                 dmadata->frag_count++;
200                 dmadata->frag_count %= dmadata->frags;
201                 dmadata->pos += dmadata->frag_bytes;
202                 dmadata->pos %= dmadata->buffer_bytes;
203                 if ((dmadata->frag_count * dmadata->frag_bytes) %
204                     dmadata->period_bytes == 0)
205                         snd_pcm_period_elapsed(substream);
206         }
207         spin_unlock_irqrestore(&dmadata->dma_lock, flags);
208 }
209
210 static int txx9aclc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
211 {
212         struct txx9aclc_dmadata *dmadata = substream->runtime->private_data;
213         struct txx9aclc_plat_drvdata *drvdata =txx9aclc_drvdata;
214         void __iomem *base = drvdata->base;
215         unsigned long flags;
216         int ret = 0;
217         u32 ctlbit = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
218                 ACCTL_AUDODMA : ACCTL_AUDIDMA;
219
220         spin_lock_irqsave(&dmadata->dma_lock, flags);
221         switch (cmd) {
222         case SNDRV_PCM_TRIGGER_START:
223                 dmadata->frag_count = -1;
224                 tasklet_schedule(&dmadata->tasklet);
225                 break;
226         case SNDRV_PCM_TRIGGER_STOP:
227         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
228         case SNDRV_PCM_TRIGGER_SUSPEND:
229                 __raw_writel(ctlbit, base + ACCTLDIS);
230                 break;
231         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
232         case SNDRV_PCM_TRIGGER_RESUME:
233                 __raw_writel(ctlbit, base + ACCTLEN);
234                 break;
235         default:
236                 ret = -EINVAL;
237         }
238         spin_unlock_irqrestore(&dmadata->dma_lock, flags);
239         return ret;
240 }
241
242 static snd_pcm_uframes_t
243 txx9aclc_pcm_pointer(struct snd_pcm_substream *substream)
244 {
245         struct txx9aclc_dmadata *dmadata = substream->runtime->private_data;
246
247         return bytes_to_frames(substream->runtime, dmadata->pos);
248 }
249
250 static int txx9aclc_pcm_open(struct snd_pcm_substream *substream)
251 {
252         struct txx9aclc_soc_device *dev = &txx9aclc_soc_device;
253         struct txx9aclc_dmadata *dmadata = &dev->dmadata[substream->stream];
254         int ret;
255
256         ret = snd_soc_set_runtime_hwparams(substream, &txx9aclc_pcm_hardware);
257         if (ret)
258                 return ret;
259         /* ensure that buffer size is a multiple of period size */
260         ret = snd_pcm_hw_constraint_integer(substream->runtime,
261                                             SNDRV_PCM_HW_PARAM_PERIODS);
262         if (ret < 0)
263                 return ret;
264         substream->runtime->private_data = dmadata;
265         return 0;
266 }
267
268 static int txx9aclc_pcm_close(struct snd_pcm_substream *substream)
269 {
270         struct txx9aclc_dmadata *dmadata = substream->runtime->private_data;
271         struct dma_chan *chan = dmadata->dma_chan;
272
273         dmadata->frag_count = -1;
274         chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
275         return 0;
276 }
277
278 static struct snd_pcm_ops txx9aclc_pcm_ops = {
279         .open           = txx9aclc_pcm_open,
280         .close          = txx9aclc_pcm_close,
281         .ioctl          = snd_pcm_lib_ioctl,
282         .hw_params      = txx9aclc_pcm_hw_params,
283         .hw_free        = txx9aclc_pcm_hw_free,
284         .prepare        = txx9aclc_pcm_prepare,
285         .trigger        = txx9aclc_pcm_trigger,
286         .pointer        = txx9aclc_pcm_pointer,
287 };
288
289 static void txx9aclc_pcm_free_dma_buffers(struct snd_pcm *pcm)
290 {
291         snd_pcm_lib_preallocate_free_for_all(pcm);
292 }
293
294 static int txx9aclc_pcm_new(struct snd_soc_pcm_runtime *rtd)
295 {
296         struct snd_card *card = rtd->card->snd_card;
297         struct snd_soc_dai *dai = rtd->cpu_dai;
298         struct snd_pcm *pcm = rtd->pcm;
299         struct platform_device *pdev = to_platform_device(dai->platform->dev);
300         struct txx9aclc_soc_device *dev;
301         struct resource *r;
302         int i;
303         int ret;
304
305         /* at this point onwards the AC97 component has probed and this will be valid */
306         dev = snd_soc_dai_get_drvdata(dai);
307
308         dev->dmadata[0].stream = SNDRV_PCM_STREAM_PLAYBACK;
309         dev->dmadata[1].stream = SNDRV_PCM_STREAM_CAPTURE;
310         for (i = 0; i < 2; i++) {
311                 r = platform_get_resource(pdev, IORESOURCE_DMA, i);
312                 if (!r) {
313                         ret = -EBUSY;
314                         goto exit;
315                 }
316                 dev->dmadata[i].dma_res = r;
317                 ret = txx9aclc_dma_init(dev, &dev->dmadata[i]);
318                 if (ret)
319                         goto exit;
320         }
321         return snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
322                 card->dev, 64 * 1024, 4 * 1024 * 1024);
323
324 exit:
325         for (i = 0; i < 2; i++) {
326                 if (dev->dmadata[i].dma_chan)
327                         dma_release_channel(dev->dmadata[i].dma_chan);
328                 dev->dmadata[i].dma_chan = NULL;
329         }
330         return ret;
331 }
332
333 static bool filter(struct dma_chan *chan, void *param)
334 {
335         struct txx9aclc_dmadata *dmadata = param;
336         char *devname;
337         bool found = false;
338
339         devname = kasprintf(GFP_KERNEL, "%s.%d", dmadata->dma_res->name,
340                 (int)dmadata->dma_res->start);
341         if (strcmp(dev_name(chan->device->dev), devname) == 0) {
342                 chan->private = &dmadata->dma_slave;
343                 found = true;
344         }
345         kfree(devname);
346         return found;
347 }
348
349 static int txx9aclc_dma_init(struct txx9aclc_soc_device *dev,
350                              struct txx9aclc_dmadata *dmadata)
351 {
352         struct txx9aclc_plat_drvdata *drvdata =txx9aclc_drvdata;
353         struct txx9dmac_slave *ds = &dmadata->dma_slave;
354         dma_cap_mask_t mask;
355
356         spin_lock_init(&dmadata->dma_lock);
357
358         ds->reg_width = sizeof(u32);
359         if (dmadata->stream == SNDRV_PCM_STREAM_PLAYBACK) {
360                 ds->tx_reg = drvdata->physbase + ACAUDODAT;
361                 ds->rx_reg = 0;
362         } else {
363                 ds->tx_reg = 0;
364                 ds->rx_reg = drvdata->physbase + ACAUDIDAT;
365         }
366
367         /* Try to grab a DMA channel */
368         dma_cap_zero(mask);
369         dma_cap_set(DMA_SLAVE, mask);
370         dmadata->dma_chan = dma_request_channel(mask, filter, dmadata);
371         if (!dmadata->dma_chan) {
372                 printk(KERN_ERR
373                         "DMA channel for %s is not available\n",
374                         dmadata->stream == SNDRV_PCM_STREAM_PLAYBACK ?
375                         "playback" : "capture");
376                 return -EBUSY;
377         }
378         tasklet_init(&dmadata->tasklet, txx9aclc_dma_tasklet,
379                      (unsigned long)dmadata);
380         return 0;
381 }
382
383 static int txx9aclc_pcm_probe(struct snd_soc_platform *platform)
384 {
385         snd_soc_platform_set_drvdata(platform, &txx9aclc_soc_device);
386         return 0;
387 }
388
389 static int txx9aclc_pcm_remove(struct snd_soc_platform *platform)
390 {
391         struct txx9aclc_soc_device *dev = snd_soc_platform_get_drvdata(platform);
392         struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
393         void __iomem *base = drvdata->base;
394         int i;
395
396         /* disable all FIFO DMAs */
397         __raw_writel(ACCTL_AUDODMA | ACCTL_AUDIDMA, base + ACCTLDIS);
398         /* dummy R/W to clear pending DMAREQ if any */
399         __raw_writel(__raw_readl(base + ACAUDIDAT), base + ACAUDODAT);
400
401         for (i = 0; i < 2; i++) {
402                 struct txx9aclc_dmadata *dmadata = &dev->dmadata[i];
403                 struct dma_chan *chan = dmadata->dma_chan;
404                 if (chan) {
405                         dmadata->frag_count = -1;
406                         chan->device->device_control(chan,
407                                                      DMA_TERMINATE_ALL, 0);
408                         dma_release_channel(chan);
409                 }
410                 dev->dmadata[i].dma_chan = NULL;
411         }
412         return 0;
413 }
414
415 static struct snd_soc_platform_driver txx9aclc_soc_platform = {
416         .probe          = txx9aclc_pcm_probe,
417         .remove         = txx9aclc_pcm_remove,
418         .ops            = &txx9aclc_pcm_ops,
419         .pcm_new        = txx9aclc_pcm_new,
420         .pcm_free       = txx9aclc_pcm_free_dma_buffers,
421 };
422
423 static int txx9aclc_soc_platform_probe(struct platform_device *pdev)
424 {
425         return snd_soc_register_platform(&pdev->dev, &txx9aclc_soc_platform);
426 }
427
428 static int txx9aclc_soc_platform_remove(struct platform_device *pdev)
429 {
430         snd_soc_unregister_platform(&pdev->dev);
431         return 0;
432 }
433
434 static struct platform_driver txx9aclc_pcm_driver = {
435         .driver = {
436                         .name = "txx9aclc-pcm-audio",
437                         .owner = THIS_MODULE,
438         },
439
440         .probe = txx9aclc_soc_platform_probe,
441         .remove = txx9aclc_soc_platform_remove,
442 };
443
444 module_platform_driver(txx9aclc_pcm_driver);
445
446 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
447 MODULE_DESCRIPTION("TXx9 ACLC Audio DMA driver");
448 MODULE_LICENSE("GPL");