treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / txx9 / txx9aclc.c
blob985487cc3a557124181916ffe59d07df6393477e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic TXx9 ACLC platform driver
5 * Copyright (C) 2009 Atsushi Nemoto
7 * Based on RBTX49xx patch from CELF patch archive.
8 * (C) Copyright TOSHIBA CORPORATION 2004-2006
9 */
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/scatterlist.h>
15 #include <linux/slab.h>
16 #include <linux/dmaengine.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include "txx9aclc.h"
23 #define DRV_NAME "txx9aclc"
25 static struct txx9aclc_soc_device {
26 struct txx9aclc_dmadata dmadata[2];
27 } txx9aclc_soc_device;
29 /* REVISIT: How to find txx9aclc_drvdata from snd_ac97? */
30 static struct txx9aclc_plat_drvdata *txx9aclc_drvdata;
32 static int txx9aclc_dma_init(struct txx9aclc_soc_device *dev,
33 struct txx9aclc_dmadata *dmadata);
35 static const struct snd_pcm_hardware txx9aclc_pcm_hardware = {
37 * REVISIT: SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
38 * needs more works for noncoherent MIPS.
40 .info = SNDRV_PCM_INFO_INTERLEAVED |
41 SNDRV_PCM_INFO_BATCH |
42 SNDRV_PCM_INFO_PAUSE,
43 .period_bytes_min = 1024,
44 .period_bytes_max = 8 * 1024,
45 .periods_min = 2,
46 .periods_max = 4096,
47 .buffer_bytes_max = 32 * 1024,
50 static int txx9aclc_pcm_hw_params(struct snd_soc_component *component,
51 struct snd_pcm_substream *substream,
52 struct snd_pcm_hw_params *params)
54 struct snd_pcm_runtime *runtime = substream->runtime;
55 struct txx9aclc_dmadata *dmadata = runtime->private_data;
57 dev_dbg(component->dev,
58 "runtime->dma_area = %#lx dma_addr = %#lx dma_bytes = %zd "
59 "runtime->min_align %ld\n",
60 (unsigned long)runtime->dma_area,
61 (unsigned long)runtime->dma_addr, runtime->dma_bytes,
62 runtime->min_align);
63 dev_dbg(component->dev,
64 "periods %d period_bytes %d stream %d\n",
65 params_periods(params), params_period_bytes(params),
66 substream->stream);
68 dmadata->substream = substream;
69 dmadata->pos = 0;
70 return 0;
73 static int txx9aclc_pcm_prepare(struct snd_soc_component *component,
74 struct snd_pcm_substream *substream)
76 struct snd_pcm_runtime *runtime = substream->runtime;
77 struct txx9aclc_dmadata *dmadata = runtime->private_data;
79 dmadata->dma_addr = runtime->dma_addr;
80 dmadata->buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
81 dmadata->period_bytes = snd_pcm_lib_period_bytes(substream);
83 if (dmadata->buffer_bytes == dmadata->period_bytes) {
84 dmadata->frag_bytes = dmadata->period_bytes >> 1;
85 dmadata->frags = 2;
86 } else {
87 dmadata->frag_bytes = dmadata->period_bytes;
88 dmadata->frags = dmadata->buffer_bytes / dmadata->period_bytes;
90 dmadata->frag_count = 0;
91 dmadata->pos = 0;
92 return 0;
95 static void txx9aclc_dma_complete(void *arg)
97 struct txx9aclc_dmadata *dmadata = arg;
98 unsigned long flags;
100 /* dma completion handler cannot submit new operations */
101 spin_lock_irqsave(&dmadata->dma_lock, flags);
102 if (dmadata->frag_count >= 0) {
103 dmadata->dmacount--;
104 if (!WARN_ON(dmadata->dmacount < 0))
105 tasklet_schedule(&dmadata->tasklet);
107 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
110 static struct dma_async_tx_descriptor *
111 txx9aclc_dma_submit(struct txx9aclc_dmadata *dmadata, dma_addr_t buf_dma_addr)
113 struct dma_chan *chan = dmadata->dma_chan;
114 struct dma_async_tx_descriptor *desc;
115 struct scatterlist sg;
117 sg_init_table(&sg, 1);
118 sg_set_page(&sg, pfn_to_page(PFN_DOWN(buf_dma_addr)),
119 dmadata->frag_bytes, buf_dma_addr & (PAGE_SIZE - 1));
120 sg_dma_address(&sg) = buf_dma_addr;
121 desc = dmaengine_prep_slave_sg(chan, &sg, 1,
122 dmadata->substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
123 DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
124 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
125 if (!desc) {
126 dev_err(&chan->dev->device, "cannot prepare slave dma\n");
127 return NULL;
129 desc->callback = txx9aclc_dma_complete;
130 desc->callback_param = dmadata;
131 dmaengine_submit(desc);
132 return desc;
135 #define NR_DMA_CHAIN 2
137 static void txx9aclc_dma_tasklet(unsigned long data)
139 struct txx9aclc_dmadata *dmadata = (struct txx9aclc_dmadata *)data;
140 struct dma_chan *chan = dmadata->dma_chan;
141 struct dma_async_tx_descriptor *desc;
142 struct snd_pcm_substream *substream = dmadata->substream;
143 u32 ctlbit = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
144 ACCTL_AUDODMA : ACCTL_AUDIDMA;
145 int i;
146 unsigned long flags;
148 spin_lock_irqsave(&dmadata->dma_lock, flags);
149 if (dmadata->frag_count < 0) {
150 struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
151 void __iomem *base = drvdata->base;
153 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
154 dmaengine_terminate_all(chan);
155 /* first time */
156 for (i = 0; i < NR_DMA_CHAIN; i++) {
157 desc = txx9aclc_dma_submit(dmadata,
158 dmadata->dma_addr + i * dmadata->frag_bytes);
159 if (!desc)
160 return;
162 dmadata->dmacount = NR_DMA_CHAIN;
163 dma_async_issue_pending(chan);
164 spin_lock_irqsave(&dmadata->dma_lock, flags);
165 __raw_writel(ctlbit, base + ACCTLEN);
166 dmadata->frag_count = NR_DMA_CHAIN % dmadata->frags;
167 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
168 return;
170 if (WARN_ON(dmadata->dmacount >= NR_DMA_CHAIN)) {
171 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
172 return;
174 while (dmadata->dmacount < NR_DMA_CHAIN) {
175 dmadata->dmacount++;
176 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
177 desc = txx9aclc_dma_submit(dmadata,
178 dmadata->dma_addr +
179 dmadata->frag_count * dmadata->frag_bytes);
180 if (!desc)
181 return;
182 dma_async_issue_pending(chan);
184 spin_lock_irqsave(&dmadata->dma_lock, flags);
185 dmadata->frag_count++;
186 dmadata->frag_count %= dmadata->frags;
187 dmadata->pos += dmadata->frag_bytes;
188 dmadata->pos %= dmadata->buffer_bytes;
189 if ((dmadata->frag_count * dmadata->frag_bytes) %
190 dmadata->period_bytes == 0)
191 snd_pcm_period_elapsed(substream);
193 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
196 static int txx9aclc_pcm_trigger(struct snd_soc_component *component,
197 struct snd_pcm_substream *substream, int cmd)
199 struct txx9aclc_dmadata *dmadata = substream->runtime->private_data;
200 struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
201 void __iomem *base = drvdata->base;
202 unsigned long flags;
203 int ret = 0;
204 u32 ctlbit = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
205 ACCTL_AUDODMA : ACCTL_AUDIDMA;
207 spin_lock_irqsave(&dmadata->dma_lock, flags);
208 switch (cmd) {
209 case SNDRV_PCM_TRIGGER_START:
210 dmadata->frag_count = -1;
211 tasklet_schedule(&dmadata->tasklet);
212 break;
213 case SNDRV_PCM_TRIGGER_STOP:
214 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
215 case SNDRV_PCM_TRIGGER_SUSPEND:
216 __raw_writel(ctlbit, base + ACCTLDIS);
217 break;
218 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
219 case SNDRV_PCM_TRIGGER_RESUME:
220 __raw_writel(ctlbit, base + ACCTLEN);
221 break;
222 default:
223 ret = -EINVAL;
225 spin_unlock_irqrestore(&dmadata->dma_lock, flags);
226 return ret;
229 static snd_pcm_uframes_t
230 txx9aclc_pcm_pointer(struct snd_soc_component *component,
231 struct snd_pcm_substream *substream)
233 struct txx9aclc_dmadata *dmadata = substream->runtime->private_data;
235 return bytes_to_frames(substream->runtime, dmadata->pos);
238 static int txx9aclc_pcm_open(struct snd_soc_component *component,
239 struct snd_pcm_substream *substream)
241 struct txx9aclc_soc_device *dev = &txx9aclc_soc_device;
242 struct txx9aclc_dmadata *dmadata = &dev->dmadata[substream->stream];
243 int ret;
245 ret = snd_soc_set_runtime_hwparams(substream, &txx9aclc_pcm_hardware);
246 if (ret)
247 return ret;
248 /* ensure that buffer size is a multiple of period size */
249 ret = snd_pcm_hw_constraint_integer(substream->runtime,
250 SNDRV_PCM_HW_PARAM_PERIODS);
251 if (ret < 0)
252 return ret;
253 substream->runtime->private_data = dmadata;
254 return 0;
257 static int txx9aclc_pcm_close(struct snd_soc_component *component,
258 struct snd_pcm_substream *substream)
260 struct txx9aclc_dmadata *dmadata = substream->runtime->private_data;
261 struct dma_chan *chan = dmadata->dma_chan;
263 dmadata->frag_count = -1;
264 dmaengine_terminate_all(chan);
265 return 0;
268 static int txx9aclc_pcm_new(struct snd_soc_component *component,
269 struct snd_soc_pcm_runtime *rtd)
271 struct snd_card *card = rtd->card->snd_card;
272 struct snd_soc_dai *dai = rtd->cpu_dai;
273 struct snd_pcm *pcm = rtd->pcm;
274 struct platform_device *pdev = to_platform_device(component->dev);
275 struct txx9aclc_soc_device *dev;
276 struct resource *r;
277 int i;
278 int ret;
280 /* at this point onwards the AC97 component has probed and this will be valid */
281 dev = snd_soc_dai_get_drvdata(dai);
283 dev->dmadata[0].stream = SNDRV_PCM_STREAM_PLAYBACK;
284 dev->dmadata[1].stream = SNDRV_PCM_STREAM_CAPTURE;
285 for (i = 0; i < 2; i++) {
286 r = platform_get_resource(pdev, IORESOURCE_DMA, i);
287 if (!r) {
288 ret = -EBUSY;
289 goto exit;
291 dev->dmadata[i].dma_res = r;
292 ret = txx9aclc_dma_init(dev, &dev->dmadata[i]);
293 if (ret)
294 goto exit;
297 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
298 card->dev, 64 * 1024, 4 * 1024 * 1024);
299 return 0;
301 exit:
302 for (i = 0; i < 2; i++) {
303 if (dev->dmadata[i].dma_chan)
304 dma_release_channel(dev->dmadata[i].dma_chan);
305 dev->dmadata[i].dma_chan = NULL;
307 return ret;
310 static bool filter(struct dma_chan *chan, void *param)
312 struct txx9aclc_dmadata *dmadata = param;
313 char *devname;
314 bool found = false;
316 devname = kasprintf(GFP_KERNEL, "%s.%d", dmadata->dma_res->name,
317 (int)dmadata->dma_res->start);
318 if (strcmp(dev_name(chan->device->dev), devname) == 0) {
319 chan->private = &dmadata->dma_slave;
320 found = true;
322 kfree(devname);
323 return found;
326 static int txx9aclc_dma_init(struct txx9aclc_soc_device *dev,
327 struct txx9aclc_dmadata *dmadata)
329 struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
330 struct txx9dmac_slave *ds = &dmadata->dma_slave;
331 dma_cap_mask_t mask;
333 spin_lock_init(&dmadata->dma_lock);
335 ds->reg_width = sizeof(u32);
336 if (dmadata->stream == SNDRV_PCM_STREAM_PLAYBACK) {
337 ds->tx_reg = drvdata->physbase + ACAUDODAT;
338 ds->rx_reg = 0;
339 } else {
340 ds->tx_reg = 0;
341 ds->rx_reg = drvdata->physbase + ACAUDIDAT;
344 /* Try to grab a DMA channel */
345 dma_cap_zero(mask);
346 dma_cap_set(DMA_SLAVE, mask);
347 dmadata->dma_chan = dma_request_channel(mask, filter, dmadata);
348 if (!dmadata->dma_chan) {
349 printk(KERN_ERR
350 "DMA channel for %s is not available\n",
351 dmadata->stream == SNDRV_PCM_STREAM_PLAYBACK ?
352 "playback" : "capture");
353 return -EBUSY;
355 tasklet_init(&dmadata->tasklet, txx9aclc_dma_tasklet,
356 (unsigned long)dmadata);
357 return 0;
360 static int txx9aclc_pcm_probe(struct snd_soc_component *component)
362 snd_soc_component_set_drvdata(component, &txx9aclc_soc_device);
363 return 0;
366 static void txx9aclc_pcm_remove(struct snd_soc_component *component)
368 struct txx9aclc_soc_device *dev = snd_soc_component_get_drvdata(component);
369 struct txx9aclc_plat_drvdata *drvdata = txx9aclc_drvdata;
370 void __iomem *base = drvdata->base;
371 int i;
373 /* disable all FIFO DMAs */
374 __raw_writel(ACCTL_AUDODMA | ACCTL_AUDIDMA, base + ACCTLDIS);
375 /* dummy R/W to clear pending DMAREQ if any */
376 __raw_writel(__raw_readl(base + ACAUDIDAT), base + ACAUDODAT);
378 for (i = 0; i < 2; i++) {
379 struct txx9aclc_dmadata *dmadata = &dev->dmadata[i];
380 struct dma_chan *chan = dmadata->dma_chan;
382 if (chan) {
383 dmadata->frag_count = -1;
384 dmaengine_terminate_all(chan);
385 dma_release_channel(chan);
387 dev->dmadata[i].dma_chan = NULL;
391 static const struct snd_soc_component_driver txx9aclc_soc_component = {
392 .name = DRV_NAME,
393 .probe = txx9aclc_pcm_probe,
394 .remove = txx9aclc_pcm_remove,
395 .open = txx9aclc_pcm_open,
396 .close = txx9aclc_pcm_close,
397 .hw_params = txx9aclc_pcm_hw_params,
398 .prepare = txx9aclc_pcm_prepare,
399 .trigger = txx9aclc_pcm_trigger,
400 .pointer = txx9aclc_pcm_pointer,
401 .pcm_construct = txx9aclc_pcm_new,
404 static int txx9aclc_soc_platform_probe(struct platform_device *pdev)
406 return devm_snd_soc_register_component(&pdev->dev,
407 &txx9aclc_soc_component, NULL, 0);
410 static struct platform_driver txx9aclc_pcm_driver = {
411 .driver = {
412 .name = "txx9aclc-pcm-audio",
415 .probe = txx9aclc_soc_platform_probe,
418 module_platform_driver(txx9aclc_pcm_driver);
420 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
421 MODULE_DESCRIPTION("TXx9 ACLC Audio DMA driver");
422 MODULE_LICENSE("GPL");