109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b3c70c9eSManuel Lauss /*
3b3c70c9eSManuel Lauss * Au1000/Au1500/Au1100 Audio DMA support.
4b3c70c9eSManuel Lauss *
5b3c70c9eSManuel Lauss * (c) 2011 Manuel Lauss <manuel.lauss@googlemail.com>
6b3c70c9eSManuel Lauss *
7b3c70c9eSManuel Lauss * copied almost verbatim from the old ALSA driver, written by
8b3c70c9eSManuel Lauss * Charles Eidsness <charles@cooper-street.com>
9b3c70c9eSManuel Lauss */
10b3c70c9eSManuel Lauss
11b3c70c9eSManuel Lauss #include <linux/module.h>
12b3c70c9eSManuel Lauss #include <linux/init.h>
13b3c70c9eSManuel Lauss #include <linux/platform_device.h>
14b3c70c9eSManuel Lauss #include <linux/slab.h>
15b3c70c9eSManuel Lauss #include <linux/dma-mapping.h>
16b3c70c9eSManuel Lauss #include <sound/core.h>
17b3c70c9eSManuel Lauss #include <sound/pcm.h>
18b3c70c9eSManuel Lauss #include <sound/pcm_params.h>
19b3c70c9eSManuel Lauss #include <sound/soc.h>
20b3c70c9eSManuel Lauss #include <asm/mach-au1x00/au1000.h>
21b3c70c9eSManuel Lauss #include <asm/mach-au1x00/au1000_dma.h>
22b3c70c9eSManuel Lauss
23b3c70c9eSManuel Lauss #include "psc.h"
24b3c70c9eSManuel Lauss
25781a9fccSKuninori Morimoto #define DRV_NAME "au1x_dma"
26781a9fccSKuninori Morimoto
27b3c70c9eSManuel Lauss struct pcm_period {
28b3c70c9eSManuel Lauss u32 start;
29b3c70c9eSManuel Lauss u32 relative_end; /* relative to start of buffer */
30b3c70c9eSManuel Lauss struct pcm_period *next;
31b3c70c9eSManuel Lauss };
32b3c70c9eSManuel Lauss
33b3c70c9eSManuel Lauss struct audio_stream {
34b3c70c9eSManuel Lauss struct snd_pcm_substream *substream;
35b3c70c9eSManuel Lauss int dma;
36b3c70c9eSManuel Lauss struct pcm_period *buffer;
37b3c70c9eSManuel Lauss unsigned int period_size;
38b3c70c9eSManuel Lauss unsigned int periods;
39b3c70c9eSManuel Lauss };
40b3c70c9eSManuel Lauss
41b3c70c9eSManuel Lauss struct alchemy_pcm_ctx {
42b3c70c9eSManuel Lauss struct audio_stream stream[2]; /* playback & capture */
43b3c70c9eSManuel Lauss };
44b3c70c9eSManuel Lauss
au1000_release_dma_link(struct audio_stream * stream)45b3c70c9eSManuel Lauss static void au1000_release_dma_link(struct audio_stream *stream)
46b3c70c9eSManuel Lauss {
47b3c70c9eSManuel Lauss struct pcm_period *pointer;
48b3c70c9eSManuel Lauss struct pcm_period *pointer_next;
49b3c70c9eSManuel Lauss
50b3c70c9eSManuel Lauss stream->period_size = 0;
51b3c70c9eSManuel Lauss stream->periods = 0;
52b3c70c9eSManuel Lauss pointer = stream->buffer;
53b3c70c9eSManuel Lauss if (!pointer)
54b3c70c9eSManuel Lauss return;
55b3c70c9eSManuel Lauss do {
56b3c70c9eSManuel Lauss pointer_next = pointer->next;
57b3c70c9eSManuel Lauss kfree(pointer);
58b3c70c9eSManuel Lauss pointer = pointer_next;
59b3c70c9eSManuel Lauss } while (pointer != stream->buffer);
60b3c70c9eSManuel Lauss stream->buffer = NULL;
61b3c70c9eSManuel Lauss }
62b3c70c9eSManuel Lauss
au1000_setup_dma_link(struct audio_stream * stream,unsigned int period_bytes,unsigned int periods)63b3c70c9eSManuel Lauss static int au1000_setup_dma_link(struct audio_stream *stream,
64b3c70c9eSManuel Lauss unsigned int period_bytes,
65b3c70c9eSManuel Lauss unsigned int periods)
66b3c70c9eSManuel Lauss {
67b3c70c9eSManuel Lauss struct snd_pcm_substream *substream = stream->substream;
68b3c70c9eSManuel Lauss struct snd_pcm_runtime *runtime = substream->runtime;
69b3c70c9eSManuel Lauss struct pcm_period *pointer;
70b3c70c9eSManuel Lauss unsigned long dma_start;
71b3c70c9eSManuel Lauss int i;
72b3c70c9eSManuel Lauss
73b3c70c9eSManuel Lauss dma_start = virt_to_phys(runtime->dma_area);
74b3c70c9eSManuel Lauss
75b3c70c9eSManuel Lauss if (stream->period_size == period_bytes &&
76b3c70c9eSManuel Lauss stream->periods == periods)
77b3c70c9eSManuel Lauss return 0; /* not changed */
78b3c70c9eSManuel Lauss
79b3c70c9eSManuel Lauss au1000_release_dma_link(stream);
80b3c70c9eSManuel Lauss
81b3c70c9eSManuel Lauss stream->period_size = period_bytes;
82b3c70c9eSManuel Lauss stream->periods = periods;
83b3c70c9eSManuel Lauss
84b3c70c9eSManuel Lauss stream->buffer = kmalloc(sizeof(struct pcm_period), GFP_KERNEL);
85b3c70c9eSManuel Lauss if (!stream->buffer)
86b3c70c9eSManuel Lauss return -ENOMEM;
87b3c70c9eSManuel Lauss pointer = stream->buffer;
88b3c70c9eSManuel Lauss for (i = 0; i < periods; i++) {
89b3c70c9eSManuel Lauss pointer->start = (u32)(dma_start + (i * period_bytes));
90b3c70c9eSManuel Lauss pointer->relative_end = (u32) (((i+1) * period_bytes) - 0x1);
91b3c70c9eSManuel Lauss if (i < periods - 1) {
92b3c70c9eSManuel Lauss pointer->next = kmalloc(sizeof(struct pcm_period),
93b3c70c9eSManuel Lauss GFP_KERNEL);
94b3c70c9eSManuel Lauss if (!pointer->next) {
95b3c70c9eSManuel Lauss au1000_release_dma_link(stream);
96b3c70c9eSManuel Lauss return -ENOMEM;
97b3c70c9eSManuel Lauss }
98b3c70c9eSManuel Lauss pointer = pointer->next;
99b3c70c9eSManuel Lauss }
100b3c70c9eSManuel Lauss }
101b3c70c9eSManuel Lauss pointer->next = stream->buffer;
102b3c70c9eSManuel Lauss return 0;
103b3c70c9eSManuel Lauss }
104b3c70c9eSManuel Lauss
au1000_dma_stop(struct audio_stream * stream)105b3c70c9eSManuel Lauss static void au1000_dma_stop(struct audio_stream *stream)
106b3c70c9eSManuel Lauss {
107b3c70c9eSManuel Lauss if (stream->buffer)
108b3c70c9eSManuel Lauss disable_dma(stream->dma);
109b3c70c9eSManuel Lauss }
110b3c70c9eSManuel Lauss
au1000_dma_start(struct audio_stream * stream)111b3c70c9eSManuel Lauss static void au1000_dma_start(struct audio_stream *stream)
112b3c70c9eSManuel Lauss {
113b3c70c9eSManuel Lauss if (!stream->buffer)
114b3c70c9eSManuel Lauss return;
115b3c70c9eSManuel Lauss
116b3c70c9eSManuel Lauss init_dma(stream->dma);
117b3c70c9eSManuel Lauss if (get_dma_active_buffer(stream->dma) == 0) {
118b3c70c9eSManuel Lauss clear_dma_done0(stream->dma);
119b3c70c9eSManuel Lauss set_dma_addr0(stream->dma, stream->buffer->start);
120b3c70c9eSManuel Lauss set_dma_count0(stream->dma, stream->period_size >> 1);
121b3c70c9eSManuel Lauss set_dma_addr1(stream->dma, stream->buffer->next->start);
122b3c70c9eSManuel Lauss set_dma_count1(stream->dma, stream->period_size >> 1);
123b3c70c9eSManuel Lauss } else {
124b3c70c9eSManuel Lauss clear_dma_done1(stream->dma);
125b3c70c9eSManuel Lauss set_dma_addr1(stream->dma, stream->buffer->start);
126b3c70c9eSManuel Lauss set_dma_count1(stream->dma, stream->period_size >> 1);
127b3c70c9eSManuel Lauss set_dma_addr0(stream->dma, stream->buffer->next->start);
128b3c70c9eSManuel Lauss set_dma_count0(stream->dma, stream->period_size >> 1);
129b3c70c9eSManuel Lauss }
130b3c70c9eSManuel Lauss enable_dma_buffers(stream->dma);
131b3c70c9eSManuel Lauss start_dma(stream->dma);
132b3c70c9eSManuel Lauss }
133b3c70c9eSManuel Lauss
au1000_dma_interrupt(int irq,void * ptr)134b3c70c9eSManuel Lauss static irqreturn_t au1000_dma_interrupt(int irq, void *ptr)
135b3c70c9eSManuel Lauss {
136b3c70c9eSManuel Lauss struct audio_stream *stream = (struct audio_stream *)ptr;
137b3c70c9eSManuel Lauss struct snd_pcm_substream *substream = stream->substream;
138b3c70c9eSManuel Lauss
139b3c70c9eSManuel Lauss switch (get_dma_buffer_done(stream->dma)) {
140b3c70c9eSManuel Lauss case DMA_D0:
141b3c70c9eSManuel Lauss stream->buffer = stream->buffer->next;
142b3c70c9eSManuel Lauss clear_dma_done0(stream->dma);
143b3c70c9eSManuel Lauss set_dma_addr0(stream->dma, stream->buffer->next->start);
144b3c70c9eSManuel Lauss set_dma_count0(stream->dma, stream->period_size >> 1);
145b3c70c9eSManuel Lauss enable_dma_buffer0(stream->dma);
146b3c70c9eSManuel Lauss break;
147b3c70c9eSManuel Lauss case DMA_D1:
148b3c70c9eSManuel Lauss stream->buffer = stream->buffer->next;
149b3c70c9eSManuel Lauss clear_dma_done1(stream->dma);
150b3c70c9eSManuel Lauss set_dma_addr1(stream->dma, stream->buffer->next->start);
151b3c70c9eSManuel Lauss set_dma_count1(stream->dma, stream->period_size >> 1);
152b3c70c9eSManuel Lauss enable_dma_buffer1(stream->dma);
153b3c70c9eSManuel Lauss break;
154b3c70c9eSManuel Lauss case (DMA_D0 | DMA_D1):
155b3c70c9eSManuel Lauss pr_debug("DMA %d missed interrupt.\n", stream->dma);
156b3c70c9eSManuel Lauss au1000_dma_stop(stream);
157b3c70c9eSManuel Lauss au1000_dma_start(stream);
158b3c70c9eSManuel Lauss break;
159b3c70c9eSManuel Lauss case (~DMA_D0 & ~DMA_D1):
160b3c70c9eSManuel Lauss pr_debug("DMA %d empty irq.\n", stream->dma);
161b3c70c9eSManuel Lauss }
162b3c70c9eSManuel Lauss snd_pcm_period_elapsed(substream);
163b3c70c9eSManuel Lauss return IRQ_HANDLED;
164b3c70c9eSManuel Lauss }
165b3c70c9eSManuel Lauss
166b3c70c9eSManuel Lauss static const struct snd_pcm_hardware alchemy_pcm_hardware = {
167b3c70c9eSManuel Lauss .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
168b3c70c9eSManuel Lauss SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BATCH,
169b3c70c9eSManuel Lauss .period_bytes_min = 1024,
170b3c70c9eSManuel Lauss .period_bytes_max = 16 * 1024 - 1,
171b3c70c9eSManuel Lauss .periods_min = 4,
172b3c70c9eSManuel Lauss .periods_max = 255,
173b3c70c9eSManuel Lauss .buffer_bytes_max = 128 * 1024,
174b3c70c9eSManuel Lauss .fifo_size = 16,
175b3c70c9eSManuel Lauss };
176b3c70c9eSManuel Lauss
ss_to_ctx(struct snd_pcm_substream * ss,struct snd_soc_component * component)177297bdfd4SKuninori Morimoto static inline struct alchemy_pcm_ctx *ss_to_ctx(struct snd_pcm_substream *ss,
178297bdfd4SKuninori Morimoto struct snd_soc_component *component)
179b3c70c9eSManuel Lauss {
180781a9fccSKuninori Morimoto return snd_soc_component_get_drvdata(component);
181b3c70c9eSManuel Lauss }
182b3c70c9eSManuel Lauss
ss_to_as(struct snd_pcm_substream * ss,struct snd_soc_component * component)183297bdfd4SKuninori Morimoto static inline struct audio_stream *ss_to_as(struct snd_pcm_substream *ss,
184297bdfd4SKuninori Morimoto struct snd_soc_component *component)
185b3c70c9eSManuel Lauss {
186297bdfd4SKuninori Morimoto struct alchemy_pcm_ctx *ctx = ss_to_ctx(ss, component);
187b3c70c9eSManuel Lauss return &(ctx->stream[ss->stream]);
188b3c70c9eSManuel Lauss }
189b3c70c9eSManuel Lauss
alchemy_pcm_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)190297bdfd4SKuninori Morimoto static int alchemy_pcm_open(struct snd_soc_component *component,
191297bdfd4SKuninori Morimoto struct snd_pcm_substream *substream)
192b3c70c9eSManuel Lauss {
193297bdfd4SKuninori Morimoto struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream, component);
194*2162d453SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
195b3c70c9eSManuel Lauss int *dmaids, s = substream->stream;
196b3c70c9eSManuel Lauss char *name;
197b3c70c9eSManuel Lauss
198*2162d453SKuninori Morimoto dmaids = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);
199b3c70c9eSManuel Lauss if (!dmaids)
200b3c70c9eSManuel Lauss return -ENODEV; /* whoa, has ordering changed? */
201b3c70c9eSManuel Lauss
202b3c70c9eSManuel Lauss /* DMA setup */
203b3c70c9eSManuel Lauss name = (s == SNDRV_PCM_STREAM_PLAYBACK) ? "audio-tx" : "audio-rx";
204b3c70c9eSManuel Lauss ctx->stream[s].dma = request_au1000_dma(dmaids[s], name,
20588e24c3aSYong Zhang au1000_dma_interrupt, 0,
206b3c70c9eSManuel Lauss &ctx->stream[s]);
207b3c70c9eSManuel Lauss set_dma_mode(ctx->stream[s].dma,
208b3c70c9eSManuel Lauss get_dma_mode(ctx->stream[s].dma) & ~DMA_NC);
209b3c70c9eSManuel Lauss
210b3c70c9eSManuel Lauss ctx->stream[s].substream = substream;
211b3c70c9eSManuel Lauss ctx->stream[s].buffer = NULL;
212b3c70c9eSManuel Lauss snd_soc_set_runtime_hwparams(substream, &alchemy_pcm_hardware);
213b3c70c9eSManuel Lauss
214b3c70c9eSManuel Lauss return 0;
215b3c70c9eSManuel Lauss }
216b3c70c9eSManuel Lauss
alchemy_pcm_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)217297bdfd4SKuninori Morimoto static int alchemy_pcm_close(struct snd_soc_component *component,
218297bdfd4SKuninori Morimoto struct snd_pcm_substream *substream)
219b3c70c9eSManuel Lauss {
220297bdfd4SKuninori Morimoto struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream, component);
221b3c70c9eSManuel Lauss int stype = substream->stream;
222b3c70c9eSManuel Lauss
223b3c70c9eSManuel Lauss ctx->stream[stype].substream = NULL;
224b3c70c9eSManuel Lauss free_au1000_dma(ctx->stream[stype].dma);
225b3c70c9eSManuel Lauss
226b3c70c9eSManuel Lauss return 0;
227b3c70c9eSManuel Lauss }
228b3c70c9eSManuel Lauss
alchemy_pcm_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)229297bdfd4SKuninori Morimoto static int alchemy_pcm_hw_params(struct snd_soc_component *component,
230297bdfd4SKuninori Morimoto struct snd_pcm_substream *substream,
231b3c70c9eSManuel Lauss struct snd_pcm_hw_params *hw_params)
232b3c70c9eSManuel Lauss {
233297bdfd4SKuninori Morimoto struct audio_stream *stream = ss_to_as(substream, component);
234b3c70c9eSManuel Lauss
235fe9912acSTakashi Iwai return au1000_setup_dma_link(stream,
236b3c70c9eSManuel Lauss params_period_bytes(hw_params),
237b3c70c9eSManuel Lauss params_periods(hw_params));
238b3c70c9eSManuel Lauss }
239b3c70c9eSManuel Lauss
alchemy_pcm_hw_free(struct snd_soc_component * component,struct snd_pcm_substream * substream)240297bdfd4SKuninori Morimoto static int alchemy_pcm_hw_free(struct snd_soc_component *component,
241297bdfd4SKuninori Morimoto struct snd_pcm_substream *substream)
242b3c70c9eSManuel Lauss {
243297bdfd4SKuninori Morimoto struct audio_stream *stream = ss_to_as(substream, component);
244b3c70c9eSManuel Lauss au1000_release_dma_link(stream);
245fe9912acSTakashi Iwai return 0;
246b3c70c9eSManuel Lauss }
247b3c70c9eSManuel Lauss
alchemy_pcm_trigger(struct snd_soc_component * component,struct snd_pcm_substream * substream,int cmd)248297bdfd4SKuninori Morimoto static int alchemy_pcm_trigger(struct snd_soc_component *component,
249297bdfd4SKuninori Morimoto struct snd_pcm_substream *substream, int cmd)
250b3c70c9eSManuel Lauss {
251297bdfd4SKuninori Morimoto struct audio_stream *stream = ss_to_as(substream, component);
252b3c70c9eSManuel Lauss int err = 0;
253b3c70c9eSManuel Lauss
254b3c70c9eSManuel Lauss switch (cmd) {
255b3c70c9eSManuel Lauss case SNDRV_PCM_TRIGGER_START:
256b3c70c9eSManuel Lauss au1000_dma_start(stream);
257b3c70c9eSManuel Lauss break;
258b3c70c9eSManuel Lauss case SNDRV_PCM_TRIGGER_STOP:
259b3c70c9eSManuel Lauss au1000_dma_stop(stream);
260b3c70c9eSManuel Lauss break;
261b3c70c9eSManuel Lauss default:
262b3c70c9eSManuel Lauss err = -EINVAL;
263b3c70c9eSManuel Lauss break;
264b3c70c9eSManuel Lauss }
265b3c70c9eSManuel Lauss return err;
266b3c70c9eSManuel Lauss }
267b3c70c9eSManuel Lauss
alchemy_pcm_pointer(struct snd_soc_component * component,struct snd_pcm_substream * ss)268297bdfd4SKuninori Morimoto static snd_pcm_uframes_t alchemy_pcm_pointer(struct snd_soc_component *component,
269297bdfd4SKuninori Morimoto struct snd_pcm_substream *ss)
270b3c70c9eSManuel Lauss {
271297bdfd4SKuninori Morimoto struct audio_stream *stream = ss_to_as(ss, component);
272b3c70c9eSManuel Lauss long location;
273b3c70c9eSManuel Lauss
274b3c70c9eSManuel Lauss location = get_dma_residue(stream->dma);
275b3c70c9eSManuel Lauss location = stream->buffer->relative_end - location;
276b3c70c9eSManuel Lauss if (location == -1)
277b3c70c9eSManuel Lauss location = 0;
278b3c70c9eSManuel Lauss return bytes_to_frames(ss->runtime, location);
279b3c70c9eSManuel Lauss }
280b3c70c9eSManuel Lauss
alchemy_pcm_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)281297bdfd4SKuninori Morimoto static int alchemy_pcm_new(struct snd_soc_component *component,
282297bdfd4SKuninori Morimoto struct snd_soc_pcm_runtime *rtd)
283b3c70c9eSManuel Lauss {
284b3c70c9eSManuel Lauss struct snd_pcm *pcm = rtd->pcm;
285b3c70c9eSManuel Lauss
286fe9912acSTakashi Iwai snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
2871a486032STakashi Iwai NULL, 65536, (4096 * 1024) - 1);
288b3c70c9eSManuel Lauss
289b3c70c9eSManuel Lauss return 0;
290b3c70c9eSManuel Lauss }
291b3c70c9eSManuel Lauss
292781a9fccSKuninori Morimoto static struct snd_soc_component_driver alchemy_pcm_soc_component = {
293781a9fccSKuninori Morimoto .name = DRV_NAME,
294297bdfd4SKuninori Morimoto .open = alchemy_pcm_open,
295297bdfd4SKuninori Morimoto .close = alchemy_pcm_close,
296297bdfd4SKuninori Morimoto .hw_params = alchemy_pcm_hw_params,
297297bdfd4SKuninori Morimoto .hw_free = alchemy_pcm_hw_free,
298297bdfd4SKuninori Morimoto .trigger = alchemy_pcm_trigger,
299297bdfd4SKuninori Morimoto .pointer = alchemy_pcm_pointer,
300297bdfd4SKuninori Morimoto .pcm_construct = alchemy_pcm_new,
301b3c70c9eSManuel Lauss };
302b3c70c9eSManuel Lauss
alchemy_pcm_drvprobe(struct platform_device * pdev)3035c658be0SBill Pemberton static int alchemy_pcm_drvprobe(struct platform_device *pdev)
304b3c70c9eSManuel Lauss {
305b3c70c9eSManuel Lauss struct alchemy_pcm_ctx *ctx;
306b3c70c9eSManuel Lauss
30746c3a02cSJulia Lawall ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
308b3c70c9eSManuel Lauss if (!ctx)
309b3c70c9eSManuel Lauss return -ENOMEM;
310b3c70c9eSManuel Lauss
311b3c70c9eSManuel Lauss platform_set_drvdata(pdev, ctx);
312b3c70c9eSManuel Lauss
313781a9fccSKuninori Morimoto return devm_snd_soc_register_component(&pdev->dev,
314781a9fccSKuninori Morimoto &alchemy_pcm_soc_component, NULL, 0);
315b3c70c9eSManuel Lauss }
316b3c70c9eSManuel Lauss
317b3c70c9eSManuel Lauss static struct platform_driver alchemy_pcmdma_driver = {
318b3c70c9eSManuel Lauss .driver = {
319b3c70c9eSManuel Lauss .name = "alchemy-pcm-dma",
320b3c70c9eSManuel Lauss },
321b3c70c9eSManuel Lauss .probe = alchemy_pcm_drvprobe,
322b3c70c9eSManuel Lauss };
323b3c70c9eSManuel Lauss
3248a124f9cSAxel Lin module_platform_driver(alchemy_pcmdma_driver);
325b3c70c9eSManuel Lauss
326b3c70c9eSManuel Lauss MODULE_LICENSE("GPL");
327b3c70c9eSManuel Lauss MODULE_DESCRIPTION("Au1000/Au1500/Au1100 Audio DMA driver");
328b3c70c9eSManuel Lauss MODULE_AUTHOR("Manuel Lauss");
329