1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * kirkwood-dma.c
4 *
5 * (c) 2010 Arnaud Patard <apatard@mandriva.com>
6 * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
7 */
8
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/io.h>
13 #include <linux/slab.h>
14 #include <linux/interrupt.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/mbus.h>
17 #include <sound/soc.h>
18 #include "kirkwood.h"
19
kirkwood_priv(struct snd_pcm_substream * subs)20 static struct kirkwood_dma_data *kirkwood_priv(struct snd_pcm_substream *subs)
21 {
22 struct snd_soc_pcm_runtime *soc_runtime = subs->private_data;
23 return snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(soc_runtime, 0));
24 }
25
26 static const struct snd_pcm_hardware kirkwood_dma_snd_hw = {
27 .info = SNDRV_PCM_INFO_INTERLEAVED |
28 SNDRV_PCM_INFO_MMAP |
29 SNDRV_PCM_INFO_MMAP_VALID |
30 SNDRV_PCM_INFO_BLOCK_TRANSFER |
31 SNDRV_PCM_INFO_PAUSE |
32 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
33 .buffer_bytes_max = KIRKWOOD_SND_MAX_BUFFER_BYTES,
34 .period_bytes_min = KIRKWOOD_SND_MIN_PERIOD_BYTES,
35 .period_bytes_max = KIRKWOOD_SND_MAX_PERIOD_BYTES,
36 .periods_min = KIRKWOOD_SND_MIN_PERIODS,
37 .periods_max = KIRKWOOD_SND_MAX_PERIODS,
38 .fifo_size = 0,
39 };
40
kirkwood_dma_irq(int irq,void * dev_id)41 static irqreturn_t kirkwood_dma_irq(int irq, void *dev_id)
42 {
43 struct kirkwood_dma_data *priv = dev_id;
44 unsigned long mask, status, cause;
45
46 mask = readl(priv->io + KIRKWOOD_INT_MASK);
47 status = readl(priv->io + KIRKWOOD_INT_CAUSE) & mask;
48
49 cause = readl(priv->io + KIRKWOOD_ERR_CAUSE);
50 if (unlikely(cause)) {
51 printk(KERN_WARNING "%s: got err interrupt 0x%lx\n",
52 __func__, cause);
53 writel(cause, priv->io + KIRKWOOD_ERR_CAUSE);
54 }
55
56 /* we've enabled only bytes interrupts ... */
57 if (status & ~(KIRKWOOD_INT_CAUSE_PLAY_BYTES | \
58 KIRKWOOD_INT_CAUSE_REC_BYTES)) {
59 printk(KERN_WARNING "%s: unexpected interrupt %lx\n",
60 __func__, status);
61 return IRQ_NONE;
62 }
63
64 /* ack int */
65 writel(status, priv->io + KIRKWOOD_INT_CAUSE);
66
67 if (status & KIRKWOOD_INT_CAUSE_PLAY_BYTES)
68 snd_pcm_period_elapsed(priv->substream_play);
69
70 if (status & KIRKWOOD_INT_CAUSE_REC_BYTES)
71 snd_pcm_period_elapsed(priv->substream_rec);
72
73 return IRQ_HANDLED;
74 }
75
76 static void
kirkwood_dma_conf_mbus_windows(void __iomem * base,int win,unsigned long dma,const struct mbus_dram_target_info * dram)77 kirkwood_dma_conf_mbus_windows(void __iomem *base, int win,
78 unsigned long dma,
79 const struct mbus_dram_target_info *dram)
80 {
81 int i;
82
83 /* First disable and clear windows */
84 writel(0, base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
85 writel(0, base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
86
87 /* try to find matching cs for current dma address */
88 for (i = 0; i < dram->num_cs; i++) {
89 const struct mbus_dram_window *cs = dram->cs + i;
90 if ((cs->base & 0xffff0000) < (dma & 0xffff0000)) {
91 writel(cs->base & 0xffff0000,
92 base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
93 writel(((cs->size - 1) & 0xffff0000) |
94 (cs->mbus_attr << 8) |
95 (dram->mbus_dram_target_id << 4) | 1,
96 base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
97 }
98 }
99 }
100
kirkwood_dma_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)101 static int kirkwood_dma_open(struct snd_soc_component *component,
102 struct snd_pcm_substream *substream)
103 {
104 int err;
105 struct snd_pcm_runtime *runtime = substream->runtime;
106 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
107 const struct mbus_dram_target_info *dram;
108 unsigned long addr;
109
110 snd_soc_set_runtime_hwparams(substream, &kirkwood_dma_snd_hw);
111
112 /* Ensure that all constraints linked to dma burst are fulfilled */
113 err = snd_pcm_hw_constraint_minmax(runtime,
114 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
115 priv->burst * 2,
116 KIRKWOOD_AUDIO_BUF_MAX-1);
117 if (err < 0)
118 return err;
119
120 err = snd_pcm_hw_constraint_step(runtime, 0,
121 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
122 priv->burst);
123 if (err < 0)
124 return err;
125
126 err = snd_pcm_hw_constraint_step(substream->runtime, 0,
127 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
128 priv->burst);
129 if (err < 0)
130 return err;
131
132 if (!priv->substream_play && !priv->substream_rec) {
133 err = request_irq(priv->irq, kirkwood_dma_irq, IRQF_SHARED,
134 "kirkwood-i2s", priv);
135 if (err)
136 return err;
137
138 /*
139 * Enable Error interrupts. We're only ack'ing them but
140 * it's useful for diagnostics
141 */
142 writel((unsigned int)-1, priv->io + KIRKWOOD_ERR_MASK);
143 }
144
145 dram = mv_mbus_dram_info();
146 addr = substream->dma_buffer.addr;
147 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
148 if (priv->substream_play)
149 return -EBUSY;
150 priv->substream_play = substream;
151 kirkwood_dma_conf_mbus_windows(priv->io,
152 KIRKWOOD_PLAYBACK_WIN, addr, dram);
153 } else {
154 if (priv->substream_rec)
155 return -EBUSY;
156 priv->substream_rec = substream;
157 kirkwood_dma_conf_mbus_windows(priv->io,
158 KIRKWOOD_RECORD_WIN, addr, dram);
159 }
160
161 return 0;
162 }
163
kirkwood_dma_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)164 static int kirkwood_dma_close(struct snd_soc_component *component,
165 struct snd_pcm_substream *substream)
166 {
167 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
168
169 if (!priv)
170 return 0;
171
172 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
173 priv->substream_play = NULL;
174 else
175 priv->substream_rec = NULL;
176
177 if (!priv->substream_play && !priv->substream_rec) {
178 writel(0, priv->io + KIRKWOOD_ERR_MASK);
179 free_irq(priv->irq, priv);
180 }
181
182 return 0;
183 }
184
kirkwood_dma_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)185 static int kirkwood_dma_hw_params(struct snd_soc_component *component,
186 struct snd_pcm_substream *substream,
187 struct snd_pcm_hw_params *params)
188 {
189 struct snd_pcm_runtime *runtime = substream->runtime;
190
191 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
192 runtime->dma_bytes = params_buffer_bytes(params);
193
194 return 0;
195 }
196
kirkwood_dma_hw_free(struct snd_soc_component * component,struct snd_pcm_substream * substream)197 static int kirkwood_dma_hw_free(struct snd_soc_component *component,
198 struct snd_pcm_substream *substream)
199 {
200 snd_pcm_set_runtime_buffer(substream, NULL);
201 return 0;
202 }
203
kirkwood_dma_prepare(struct snd_soc_component * component,struct snd_pcm_substream * substream)204 static int kirkwood_dma_prepare(struct snd_soc_component *component,
205 struct snd_pcm_substream *substream)
206 {
207 struct snd_pcm_runtime *runtime = substream->runtime;
208 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
209 unsigned long size, count;
210
211 /* compute buffer size in term of "words" as requested in specs */
212 size = frames_to_bytes(runtime, runtime->buffer_size);
213 size = (size>>2)-1;
214 count = snd_pcm_lib_period_bytes(substream);
215
216 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
217 writel(count, priv->io + KIRKWOOD_PLAY_BYTE_INT_COUNT);
218 writel(runtime->dma_addr, priv->io + KIRKWOOD_PLAY_BUF_ADDR);
219 writel(size, priv->io + KIRKWOOD_PLAY_BUF_SIZE);
220 } else {
221 writel(count, priv->io + KIRKWOOD_REC_BYTE_INT_COUNT);
222 writel(runtime->dma_addr, priv->io + KIRKWOOD_REC_BUF_ADDR);
223 writel(size, priv->io + KIRKWOOD_REC_BUF_SIZE);
224 }
225
226
227 return 0;
228 }
229
kirkwood_dma_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)230 static snd_pcm_uframes_t kirkwood_dma_pointer(
231 struct snd_soc_component *component,
232 struct snd_pcm_substream *substream)
233 {
234 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
235 snd_pcm_uframes_t count;
236
237 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
238 count = bytes_to_frames(substream->runtime,
239 readl(priv->io + KIRKWOOD_PLAY_BYTE_COUNT));
240 else
241 count = bytes_to_frames(substream->runtime,
242 readl(priv->io + KIRKWOOD_REC_BYTE_COUNT));
243
244 return count;
245 }
246
kirkwood_dma_preallocate_dma_buffer(struct snd_pcm * pcm,int stream)247 static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm *pcm,
248 int stream)
249 {
250 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
251 struct snd_dma_buffer *buf = &substream->dma_buffer;
252 size_t size = kirkwood_dma_snd_hw.buffer_bytes_max;
253
254 buf->dev.type = SNDRV_DMA_TYPE_DEV;
255 buf->dev.dev = pcm->card->dev;
256 buf->area = dma_alloc_coherent(pcm->card->dev, size,
257 &buf->addr, GFP_KERNEL);
258 if (!buf->area)
259 return -ENOMEM;
260 buf->bytes = size;
261 buf->private_data = NULL;
262
263 return 0;
264 }
265
kirkwood_dma_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)266 static int kirkwood_dma_new(struct snd_soc_component *component,
267 struct snd_soc_pcm_runtime *rtd)
268 {
269 struct snd_card *card = rtd->card->snd_card;
270 struct snd_pcm *pcm = rtd->pcm;
271 int ret;
272
273 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
274 if (ret)
275 return ret;
276
277 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
278 ret = kirkwood_dma_preallocate_dma_buffer(pcm,
279 SNDRV_PCM_STREAM_PLAYBACK);
280 if (ret)
281 return ret;
282 }
283
284 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
285 ret = kirkwood_dma_preallocate_dma_buffer(pcm,
286 SNDRV_PCM_STREAM_CAPTURE);
287 if (ret)
288 return ret;
289 }
290
291 return 0;
292 }
293
kirkwood_dma_free_dma_buffers(struct snd_soc_component * component,struct snd_pcm * pcm)294 static void kirkwood_dma_free_dma_buffers(struct snd_soc_component *component,
295 struct snd_pcm *pcm)
296 {
297 struct snd_pcm_substream *substream;
298 struct snd_dma_buffer *buf;
299 int stream;
300
301 for (stream = 0; stream < 2; stream++) {
302 substream = pcm->streams[stream].substream;
303 if (!substream)
304 continue;
305 buf = &substream->dma_buffer;
306 if (!buf->area)
307 continue;
308
309 dma_free_coherent(pcm->card->dev, buf->bytes,
310 buf->area, buf->addr);
311 buf->area = NULL;
312 }
313 }
314
315 const struct snd_soc_component_driver kirkwood_soc_component = {
316 .name = DRV_NAME,
317 .open = kirkwood_dma_open,
318 .close = kirkwood_dma_close,
319 .hw_params = kirkwood_dma_hw_params,
320 .hw_free = kirkwood_dma_hw_free,
321 .prepare = kirkwood_dma_prepare,
322 .pointer = kirkwood_dma_pointer,
323 .pcm_construct = kirkwood_dma_new,
324 .pcm_destruct = kirkwood_dma_free_dma_buffers,
325 };
326