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