blob: 700a18561a9405bc314770545e6694f8611feb18 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
apatard@mandriva.comf9b95982010-05-31 13:49:14 +02002/*
3 * kirkwood-dma.c
4 *
5 * (c) 2010 Arnaud Patard <apatard@mandriva.com>
Arnaud Patard (Rtp)69737892010-09-09 14:10:33 +02006 * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
apatard@mandriva.comf9b95982010-05-31 13:49:14 +02007 */
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>
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020018#include "kirkwood.h"
19
Russell Kinga7d09422013-08-04 20:22:03 +010020static struct kirkwood_dma_data *kirkwood_priv(struct snd_pcm_substream *subs)
21{
22 struct snd_soc_pcm_runtime *soc_runtime = subs->private_data;
Kuninori Morimotof8447052020-03-23 14:19:14 +090023 return snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(soc_runtime, 0));
Russell Kinga7d09422013-08-04 20:22:03 +010024}
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020025
Bhumika Goyal636d7e282017-08-17 15:46:10 +053026static const struct snd_pcm_hardware kirkwood_dma_snd_hw = {
Russell King920ec4e2014-06-26 15:23:20 +010027 .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,
Russell Kinge4065f32013-08-04 20:28:04 +010033 .buffer_bytes_max = KIRKWOOD_SND_MAX_BUFFER_BYTES,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020034 .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
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020041static irqreturn_t kirkwood_dma_irq(int irq, void *dev_id)
42{
Russell Kinga7d09422013-08-04 20:22:03 +010043 struct kirkwood_dma_data *priv = dev_id;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020044 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);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020054 }
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)
Russell Kinga7d09422013-08-04 20:22:03 +010068 snd_pcm_period_elapsed(priv->substream_play);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020069
70 if (status & KIRKWOOD_INT_CAUSE_REC_BYTES)
Russell Kinga7d09422013-08-04 20:22:03 +010071 snd_pcm_period_elapsed(priv->substream_rec);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020072
73 return IRQ_HANDLED;
74}
75
Andrew Lunn63a93322011-12-07 21:48:07 +010076static void
77kirkwood_dma_conf_mbus_windows(void __iomem *base, int win,
78 unsigned long dma,
79 const struct mbus_dram_target_info *dram)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020080{
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++) {
Andrew Lunn63a93322011-12-07 21:48:07 +010089 const struct mbus_dram_window *cs = dram->cs + i;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +020090 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
Kuninori Morimoto851f5ab2019-10-02 14:34:10 +0900101static int kirkwood_dma_open(struct snd_soc_component *component,
102 struct snd_pcm_substream *substream)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200103{
104 int err;
105 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kinga7d09422013-08-04 20:22:03 +0100106 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200107
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200108 snd_soc_set_runtime_hwparams(substream, &kirkwood_dma_snd_hw);
109
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300110 /* Ensure that all constraints linked to dma burst are fulfilled */
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200111 err = snd_pcm_hw_constraint_minmax(runtime,
112 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
113 priv->burst * 2,
114 KIRKWOOD_AUDIO_BUF_MAX-1);
115 if (err < 0)
116 return err;
117
118 err = snd_pcm_hw_constraint_step(runtime, 0,
119 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
120 priv->burst);
121 if (err < 0)
122 return err;
123
124 err = snd_pcm_hw_constraint_step(substream->runtime, 0,
125 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
126 priv->burst);
127 if (err < 0)
128 return err;
129
Russell Kinga7d09422013-08-04 20:22:03 +0100130 if (!priv->substream_play && !priv->substream_rec) {
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200131 err = request_irq(priv->irq, kirkwood_dma_irq, IRQF_SHARED,
Russell Kinga7d09422013-08-04 20:22:03 +0100132 "kirkwood-i2s", priv);
133 if (err)
Russell King175fc922019-10-23 16:46:59 +0100134 return err;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200135
136 /*
137 * Enable Error interrupts. We're only ack'ing them but
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300138 * it's useful for diagnostics
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200139 */
Vladimir Murzine2c99172013-09-29 16:00:13 +0200140 writel((unsigned int)-1, priv->io + KIRKWOOD_ERR_MASK);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200141 }
142
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200143 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Jean-Francois Moinec4a42912015-07-21 18:32:58 +0200144 if (priv->substream_play)
145 return -EBUSY;
Russell Kinga7d09422013-08-04 20:22:03 +0100146 priv->substream_play = substream;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200147 } else {
Jean-Francois Moinec4a42912015-07-21 18:32:58 +0200148 if (priv->substream_rec)
149 return -EBUSY;
Russell Kinga7d09422013-08-04 20:22:03 +0100150 priv->substream_rec = substream;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200151 }
152
153 return 0;
154}
155
Kuninori Morimoto851f5ab2019-10-02 14:34:10 +0900156static int kirkwood_dma_close(struct snd_soc_component *component,
157 struct snd_pcm_substream *substream)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200158{
Russell Kinga7d09422013-08-04 20:22:03 +0100159 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200160
Russell Kinga7d09422013-08-04 20:22:03 +0100161 if (!priv)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200162 return 0;
163
164 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Russell Kinga7d09422013-08-04 20:22:03 +0100165 priv->substream_play = NULL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200166 else
Russell Kinga7d09422013-08-04 20:22:03 +0100167 priv->substream_rec = NULL;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200168
Russell Kinga7d09422013-08-04 20:22:03 +0100169 if (!priv->substream_play && !priv->substream_rec) {
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200170 writel(0, priv->io + KIRKWOOD_ERR_MASK);
Russell Kinga7d09422013-08-04 20:22:03 +0100171 free_irq(priv->irq, priv);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200172 }
173
174 return 0;
175}
176
Takashi Iwaibb6a40f2021-07-28 13:23:53 +0200177static int kirkwood_dma_hw_params(struct snd_soc_component *component,
178 struct snd_pcm_substream *substream,
179 struct snd_pcm_hw_params *params)
180{
181 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
182 const struct mbus_dram_target_info *dram = mv_mbus_dram_info();
183 unsigned long addr = substream->runtime->dma_addr;
184
185 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
186 kirkwood_dma_conf_mbus_windows(priv->io,
187 KIRKWOOD_PLAYBACK_WIN, addr, dram);
188 else
189 kirkwood_dma_conf_mbus_windows(priv->io,
190 KIRKWOOD_RECORD_WIN, addr, dram);
191 return 0;
192}
193
Kuninori Morimoto851f5ab2019-10-02 14:34:10 +0900194static int kirkwood_dma_prepare(struct snd_soc_component *component,
195 struct snd_pcm_substream *substream)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200196{
197 struct snd_pcm_runtime *runtime = substream->runtime;
Russell Kinga7d09422013-08-04 20:22:03 +0100198 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200199 unsigned long size, count;
200
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200201 /* compute buffer size in term of "words" as requested in specs */
202 size = frames_to_bytes(runtime, runtime->buffer_size);
203 size = (size>>2)-1;
204 count = snd_pcm_lib_period_bytes(substream);
205
206 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
207 writel(count, priv->io + KIRKWOOD_PLAY_BYTE_INT_COUNT);
208 writel(runtime->dma_addr, priv->io + KIRKWOOD_PLAY_BUF_ADDR);
209 writel(size, priv->io + KIRKWOOD_PLAY_BUF_SIZE);
210 } else {
211 writel(count, priv->io + KIRKWOOD_REC_BYTE_INT_COUNT);
212 writel(runtime->dma_addr, priv->io + KIRKWOOD_REC_BUF_ADDR);
213 writel(size, priv->io + KIRKWOOD_REC_BUF_SIZE);
214 }
215
216
217 return 0;
218}
219
Kuninori Morimoto851f5ab2019-10-02 14:34:10 +0900220static snd_pcm_uframes_t kirkwood_dma_pointer(
221 struct snd_soc_component *component,
222 struct snd_pcm_substream *substream)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200223{
Russell Kinga7d09422013-08-04 20:22:03 +0100224 struct kirkwood_dma_data *priv = kirkwood_priv(substream);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200225 snd_pcm_uframes_t count;
226
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200227 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
228 count = bytes_to_frames(substream->runtime,
229 readl(priv->io + KIRKWOOD_PLAY_BYTE_COUNT));
230 else
231 count = bytes_to_frames(substream->runtime,
232 readl(priv->io + KIRKWOOD_REC_BYTE_COUNT));
233
234 return count;
235}
236
Kuninori Morimoto851f5ab2019-10-02 14:34:10 +0900237static int kirkwood_dma_new(struct snd_soc_component *component,
238 struct snd_soc_pcm_runtime *rtd)
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200239{
Lars-Peter Clausenb3c0ae72021-01-06 14:36:50 +0100240 size_t size = kirkwood_dma_snd_hw.buffer_bytes_max;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100241 struct snd_card *card = rtd->card->snd_card;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200242 int ret;
243
Russell Kingc9bd5e62013-06-27 12:53:37 +0100244 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
245 if (ret)
246 return ret;
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200247
Lars-Peter Clausenb3c0ae72021-01-06 14:36:50 +0100248 snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
249 card->dev, size, size);
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200250
251 return 0;
252}
253
Kuninori Morimotof98fc0f2018-01-29 02:48:03 +0000254const struct snd_soc_component_driver kirkwood_soc_component = {
255 .name = DRV_NAME,
Kuninori Morimoto851f5ab2019-10-02 14:34:10 +0900256 .open = kirkwood_dma_open,
257 .close = kirkwood_dma_close,
Takashi Iwaibb6a40f2021-07-28 13:23:53 +0200258 .hw_params = kirkwood_dma_hw_params,
Kuninori Morimoto851f5ab2019-10-02 14:34:10 +0900259 .prepare = kirkwood_dma_prepare,
260 .pointer = kirkwood_dma_pointer,
261 .pcm_construct = kirkwood_dma_new,
apatard@mandriva.comf9b95982010-05-31 13:49:14 +0200262};