1 // SPDX-License-Identifier: GPL-2.0
3 * Sound driver for Nintendo 64.
5 * Copyright 2021 Lauri Kasanen
8 #include <linux/dma-mapping.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
12 #include <linux/log2.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/spinlock.h>
17 #include <sound/control.h>
18 #include <sound/core.h>
19 #include <sound/initval.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
23 MODULE_AUTHOR("Lauri Kasanen <cand@gmx.com>");
24 MODULE_DESCRIPTION("N64 Audio");
25 MODULE_LICENSE("GPL");
27 #define AI_NTSC_DACRATE 48681812
28 #define AI_STATUS_BUSY (1 << 30)
29 #define AI_STATUS_FULL (1 << 31)
33 #define AI_CONTROL_REG 2
34 #define AI_STATUS_REG 3
36 #define AI_BITCLOCK_REG 5
41 #define MI_INTR_AI 0x04
43 #define MI_MASK_CLR_AI 0x0010
44 #define MI_MASK_SET_AI 0x0020
48 u32 __iomem *ai_reg_base;
49 u32 __iomem *mi_reg_base;
52 dma_addr_t ring_base_dma;
54 struct snd_card *card;
57 struct snd_pcm_substream *substream;
65 static void n64audio_write_reg(struct n64audio *priv, const u8 reg, const u32 value)
67 writel(value, priv->ai_reg_base + reg);
70 static void n64mi_write_reg(struct n64audio *priv, const u8 reg, const u32 value)
72 writel(value, priv->mi_reg_base + reg);
75 static u32 n64mi_read_reg(struct n64audio *priv, const u8 reg)
77 return readl(priv->mi_reg_base + reg);
80 static void n64audio_push(struct n64audio *priv)
82 struct snd_pcm_runtime *runtime = priv->chan.substream->runtime;
86 spin_lock_irqsave(&priv->chan.lock, flags);
88 count = priv->chan.writesize;
90 memcpy(priv->ring_base + priv->chan.nextpos,
91 runtime->dma_area + priv->chan.nextpos, count);
94 * The hw registers are double-buffered, and the IRQ fires essentially
95 * one period behind. The core only allows one period's distance, so we
96 * keep a private DMA buffer to afford two.
98 n64audio_write_reg(priv, AI_ADDR_REG, priv->ring_base_dma + priv->chan.nextpos);
100 n64audio_write_reg(priv, AI_LEN_REG, count);
102 priv->chan.nextpos += count;
103 priv->chan.nextpos %= priv->chan.bufsize;
105 runtime->delay = runtime->period_size;
107 spin_unlock_irqrestore(&priv->chan.lock, flags);
110 static irqreturn_t n64audio_isr(int irq, void *dev_id)
112 struct n64audio *priv = dev_id;
113 const u32 intrs = n64mi_read_reg(priv, MI_INTR_REG);
117 if (!(intrs & MI_INTR_AI))
120 n64audio_write_reg(priv, AI_STATUS_REG, 1);
122 if (priv->chan.substream && snd_pcm_running(priv->chan.substream)) {
123 spin_lock_irqsave(&priv->chan.lock, flags);
125 priv->chan.pos = priv->chan.nextpos;
127 spin_unlock_irqrestore(&priv->chan.lock, flags);
129 snd_pcm_period_elapsed(priv->chan.substream);
130 if (priv->chan.substream && snd_pcm_running(priv->chan.substream))
137 static const struct snd_pcm_hardware n64audio_pcm_hw = {
138 .info = (SNDRV_PCM_INFO_MMAP |
139 SNDRV_PCM_INFO_MMAP_VALID |
140 SNDRV_PCM_INFO_INTERLEAVED |
141 SNDRV_PCM_INFO_BLOCK_TRANSFER),
142 .formats = SNDRV_PCM_FMTBIT_S16_BE,
143 .rates = SNDRV_PCM_RATE_8000_48000,
148 .buffer_bytes_max = 32768,
149 .period_bytes_min = 1024,
150 .period_bytes_max = 32768,
152 // 3 periods lets the double-buffering hw read one buffer behind safely
156 static int hw_rule_period_size(struct snd_pcm_hw_params *params,
157 struct snd_pcm_hw_rule *rule)
159 struct snd_interval *c = hw_param_interval(params,
160 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
164 * The DMA unit has errata on (start + len) & 0x3fff == 0x2000.
165 * This constraint makes sure that the period size is not a power of two,
166 * which combined with dma_alloc_coherent aligning the buffer to the largest
167 * PoT <= size guarantees it won't be hit.
170 if (is_power_of_2(c->min)) {
174 if (is_power_of_2(c->max)) {
178 if (snd_interval_checkempty(c)) {
186 static int n64audio_pcm_open(struct snd_pcm_substream *substream)
188 struct snd_pcm_runtime *runtime = substream->runtime;
191 runtime->hw = n64audio_pcm_hw;
192 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
196 err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
200 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
201 hw_rule_period_size, NULL, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
208 static int n64audio_pcm_prepare(struct snd_pcm_substream *substream)
210 struct snd_pcm_runtime *runtime = substream->runtime;
211 struct n64audio *priv = substream->pcm->private_data;
214 rate = ((2 * AI_NTSC_DACRATE / runtime->rate) + 1) / 2 - 1;
216 n64audio_write_reg(priv, AI_RATE_REG, rate);
221 n64audio_write_reg(priv, AI_BITCLOCK_REG, rate - 1);
223 spin_lock_irq(&priv->chan.lock);
225 /* Setup the pseudo-dma transfer pointers. */
227 priv->chan.nextpos = 0;
228 priv->chan.substream = substream;
229 priv->chan.writesize = snd_pcm_lib_period_bytes(substream);
230 priv->chan.bufsize = snd_pcm_lib_buffer_bytes(substream);
232 spin_unlock_irq(&priv->chan.lock);
236 static int n64audio_pcm_trigger(struct snd_pcm_substream *substream,
239 struct n64audio *priv = substream->pcm->private_data;
242 case SNDRV_PCM_TRIGGER_START:
243 n64audio_push(substream->pcm->private_data);
244 n64audio_write_reg(priv, AI_CONTROL_REG, 1);
245 n64mi_write_reg(priv, MI_MASK_REG, MI_MASK_SET_AI);
247 case SNDRV_PCM_TRIGGER_STOP:
248 n64audio_write_reg(priv, AI_CONTROL_REG, 0);
249 n64mi_write_reg(priv, MI_MASK_REG, MI_MASK_CLR_AI);
257 static snd_pcm_uframes_t n64audio_pcm_pointer(struct snd_pcm_substream *substream)
259 struct n64audio *priv = substream->pcm->private_data;
261 return bytes_to_frames(substream->runtime,
265 static int n64audio_pcm_close(struct snd_pcm_substream *substream)
267 struct n64audio *priv = substream->pcm->private_data;
269 priv->chan.substream = NULL;
274 static const struct snd_pcm_ops n64audio_pcm_ops = {
275 .open = n64audio_pcm_open,
276 .prepare = n64audio_pcm_prepare,
277 .trigger = n64audio_pcm_trigger,
278 .pointer = n64audio_pcm_pointer,
279 .close = n64audio_pcm_close,
283 * The target device is embedded and RAM-constrained. We save RAM
284 * by initializing in __init code that gets dropped late in boot.
285 * For the same reason there is no module or unloading support.
287 static int __init n64audio_probe(struct platform_device *pdev)
289 struct snd_card *card;
291 struct n64audio *priv;
292 struct resource *res;
295 err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1,
297 THIS_MODULE, sizeof(*priv), &card);
301 priv = card->private_data;
303 spin_lock_init(&priv->chan.lock);
307 priv->ring_base = dma_alloc_coherent(card->dev, 32 * 1024, &priv->ring_base_dma,
309 if (!priv->ring_base) {
314 priv->mi_reg_base = devm_platform_ioremap_resource(pdev, 0);
315 if (IS_ERR(priv->mi_reg_base)) {
316 err = PTR_ERR(priv->mi_reg_base);
320 priv->ai_reg_base = devm_platform_ioremap_resource(pdev, 1);
321 if (IS_ERR(priv->ai_reg_base)) {
322 err = PTR_ERR(priv->ai_reg_base);
326 err = snd_pcm_new(card, "N64 Audio", 0, 1, 0, &pcm);
330 pcm->private_data = priv;
331 strcpy(pcm->name, "N64 Audio");
333 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &n64audio_pcm_ops);
334 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, card->dev, 0, 0);
336 strcpy(card->driver, "N64 Audio");
337 strcpy(card->shortname, "N64 Audio");
338 strcpy(card->longname, "N64 Audio");
340 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
345 if (devm_request_irq(&pdev->dev, res->start, n64audio_isr,
346 IRQF_SHARED, "N64 Audio", priv)) {
351 err = snd_card_register(card);
358 dma_free_coherent(card->dev, 32 * 1024, priv->ring_base, priv->ring_base_dma);
365 static struct platform_driver n64audio_driver = {
371 static int __init n64audio_init(void)
373 return platform_driver_probe(&n64audio_driver, n64audio_probe);
376 module_init(n64audio_init);