Merge tag 'soc-ep93xx-dt-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-block.git] / sound / isa / gus / gus_main.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Routines for Gravis UltraSound soundcards
c1017a4c 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
5 */
6
1da177e4
LT
7#include <linux/init.h>
8#include <linux/interrupt.h>
9#include <linux/delay.h>
10#include <linux/slab.h>
11#include <linux/ioport.h>
da155d5b 12#include <linux/module.h>
1da177e4
LT
13#include <sound/core.h>
14#include <sound/gus.h>
15#include <sound/control.h>
16
17#include <asm/dma.h>
18
c1017a4c 19MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
1da177e4
LT
20MODULE_DESCRIPTION("Routines for Gravis UltraSound soundcards");
21MODULE_LICENSE("GPL");
22
5e2da206 23static int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches);
1da177e4 24
5e2da206 25int snd_gus_use_inc(struct snd_gus_card * gus)
1da177e4
LT
26{
27 if (!try_module_get(gus->card->module))
28 return 0;
29 return 1;
30}
31
5e2da206 32void snd_gus_use_dec(struct snd_gus_card * gus)
1da177e4
LT
33{
34 module_put(gus->card->module);
35}
36
5e2da206 37static int snd_gus_joystick_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4
LT
38{
39 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
40 uinfo->count = 1;
41 uinfo->value.integer.min = 0;
42 uinfo->value.integer.max = 31;
43 return 0;
44}
45
5e2da206 46static int snd_gus_joystick_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 47{
5e2da206 48 struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
1da177e4
LT
49
50 ucontrol->value.integer.value[0] = gus->joystick_dac & 31;
51 return 0;
52}
53
5e2da206 54static int snd_gus_joystick_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 55{
5e2da206 56 struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
1da177e4
LT
57 unsigned long flags;
58 int change;
59 unsigned char nval;
60
61 nval = ucontrol->value.integer.value[0] & 31;
62 spin_lock_irqsave(&gus->reg_lock, flags);
63 change = gus->joystick_dac != nval;
64 gus->joystick_dac = nval;
65 snd_gf1_write8(gus, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL, gus->joystick_dac);
66 spin_unlock_irqrestore(&gus->reg_lock, flags);
67 return change;
68}
69
3a84d6c9 70static const struct snd_kcontrol_new snd_gus_joystick_control = {
1da177e4
LT
71 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
72 .name = "Joystick Speed",
73 .info = snd_gus_joystick_info,
74 .get = snd_gus_joystick_get,
75 .put = snd_gus_joystick_put
76};
77
5e2da206 78static void snd_gus_init_control(struct snd_gus_card *gus)
1da177e4 79{
1dacca7f
GKH
80 if (!gus->ace_flag)
81 snd_ctl_add(gus->card, snd_ctl_new1(&snd_gus_joystick_control, gus));
1da177e4
LT
82}
83
84/*
85 *
86 */
87
5e2da206 88static int snd_gus_free(struct snd_gus_card *gus)
1da177e4 89{
9d2e19e3
TI
90 if (gus->gf1.res_port2 == NULL)
91 goto __hw_end;
92 snd_gf1_stop(gus);
93 snd_gus_init_dma_irq(gus, 0);
94 __hw_end:
95 release_and_free_resource(gus->gf1.res_port1);
96 release_and_free_resource(gus->gf1.res_port2);
97 if (gus->gf1.irq >= 0)
98 free_irq(gus->gf1.irq, (void *) gus);
99 if (gus->gf1.dma1 >= 0) {
100 disable_dma(gus->gf1.dma1);
101 free_dma(gus->gf1.dma1);
1da177e4 102 }
9d2e19e3
TI
103 if (!gus->equal_dma && gus->gf1.dma2 >= 0) {
104 disable_dma(gus->gf1.dma2);
105 free_dma(gus->gf1.dma2);
106 }
107 kfree(gus);
1da177e4
LT
108 return 0;
109}
110
5e2da206 111static int snd_gus_dev_free(struct snd_device *device)
1da177e4 112{
5e2da206 113 struct snd_gus_card *gus = device->device_data;
1da177e4
LT
114 return snd_gus_free(gus);
115}
116
5e2da206 117int snd_gus_create(struct snd_card *card,
1da177e4
LT
118 unsigned long port,
119 int irq, int dma1, int dma2,
120 int timer_dev,
121 int voices,
122 int pcm_channels,
123 int effect,
5e2da206 124 struct snd_gus_card **rgus)
1da177e4 125{
5e2da206 126 struct snd_gus_card *gus;
1da177e4 127 int err;
99f664df 128 static const struct snd_device_ops ops = {
1da177e4
LT
129 .dev_free = snd_gus_dev_free,
130 };
131
132 *rgus = NULL;
9d2e19e3 133 gus = kzalloc(sizeof(*gus), GFP_KERNEL);
1da177e4
LT
134 if (gus == NULL)
135 return -ENOMEM;
c2cbdbb1
IM
136 spin_lock_init(&gus->reg_lock);
137 spin_lock_init(&gus->voice_alloc);
138 spin_lock_init(&gus->active_voice_lock);
139 spin_lock_init(&gus->event_lock);
140 spin_lock_init(&gus->dma_lock);
141 spin_lock_init(&gus->pcm_volume_level_lock);
142 spin_lock_init(&gus->uart_cmd_lock);
143 mutex_init(&gus->dma_mutex);
1da177e4
LT
144 gus->gf1.irq = -1;
145 gus->gf1.dma1 = -1;
146 gus->gf1.dma2 = -1;
147 gus->card = card;
148 gus->gf1.port = port;
149 /* fill register variables for speedup */
150 gus->gf1.reg_page = GUSP(gus, GF1PAGE);
151 gus->gf1.reg_regsel = GUSP(gus, GF1REGSEL);
152 gus->gf1.reg_data8 = GUSP(gus, GF1DATAHIGH);
153 gus->gf1.reg_data16 = GUSP(gus, GF1DATALOW);
154 gus->gf1.reg_irqstat = GUSP(gus, IRQSTAT);
155 gus->gf1.reg_dram = GUSP(gus, DRAM);
156 gus->gf1.reg_timerctrl = GUSP(gus, TIMERCNTRL);
157 gus->gf1.reg_timerdata = GUSP(gus, TIMERDATA);
158 /* allocate resources */
9d2e19e3 159 gus->gf1.res_port1 = request_region(port, 16, "GUS GF1 (Adlib/SB)");
310efd3a 160 if (!gus->gf1.res_port1) {
a6676811 161 dev_err(card->dev, "gus: can't grab SB port 0x%lx\n", port);
9d2e19e3 162 snd_gus_free(gus);
1da177e4
LT
163 return -EBUSY;
164 }
9d2e19e3 165 gus->gf1.res_port2 = request_region(port + 0x100, 12, "GUS GF1 (Synth)");
310efd3a 166 if (!gus->gf1.res_port2) {
a6676811 167 dev_err(card->dev, "gus: can't grab synth port 0x%lx\n", port + 0x100);
9d2e19e3 168 snd_gus_free(gus);
1da177e4
LT
169 return -EBUSY;
170 }
9d2e19e3 171 if (irq >= 0 && request_irq(irq, snd_gus_interrupt, 0, "GUS GF1", (void *) gus)) {
a6676811 172 dev_err(card->dev, "gus: can't grab irq %d\n", irq);
9d2e19e3 173 snd_gus_free(gus);
1da177e4
LT
174 return -EBUSY;
175 }
176 gus->gf1.irq = irq;
914da689 177 card->sync_irq = irq;
9d2e19e3 178 if (request_dma(dma1, "GUS - 1")) {
a6676811 179 dev_err(card->dev, "gus: can't grab DMA1 %d\n", dma1);
9d2e19e3 180 snd_gus_free(gus);
1da177e4
LT
181 return -EBUSY;
182 }
183 gus->gf1.dma1 = dma1;
184 if (dma2 >= 0 && dma1 != dma2) {
9d2e19e3 185 if (request_dma(dma2, "GUS - 2")) {
a6676811 186 dev_err(card->dev, "gus: can't grab DMA2 %d\n", dma2);
9d2e19e3 187 snd_gus_free(gus);
1da177e4
LT
188 return -EBUSY;
189 }
190 gus->gf1.dma2 = dma2;
191 } else {
192 gus->gf1.dma2 = gus->gf1.dma1;
193 gus->equal_dma = 1;
194 }
195 gus->timer_dev = timer_dev;
196 if (voices < 14)
197 voices = 14;
198 if (voices > 32)
199 voices = 32;
200 if (pcm_channels < 0)
201 pcm_channels = 0;
202 if (pcm_channels > 8)
203 pcm_channels = 8;
204 pcm_channels++;
205 pcm_channels &= ~1;
206 gus->gf1.effect = effect ? 1 : 0;
207 gus->gf1.active_voices = voices;
208 gus->gf1.pcm_channels = pcm_channels;
209 gus->gf1.volume_ramp = 25;
210 gus->gf1.smooth_pan = 1;
310efd3a 211 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, gus, &ops);
9d2e19e3
TI
212 if (err < 0) {
213 snd_gus_free(gus);
1da177e4 214 return err;
9d2e19e3 215 }
1da177e4
LT
216 *rgus = gus;
217 return 0;
218}
219
220/*
221 * Memory detection routine for plain GF1 soundcards
222 */
223
5e2da206 224static int snd_gus_detect_memory(struct snd_gus_card * gus)
1da177e4
LT
225{
226 int l, idx, local;
227 unsigned char d;
228
229 snd_gf1_poke(gus, 0L, 0xaa);
230 snd_gf1_poke(gus, 1L, 0x55);
231 if (snd_gf1_peek(gus, 0L) != 0xaa || snd_gf1_peek(gus, 1L) != 0x55) {
a6676811
TI
232 dev_err(gus->card->dev,
233 "plain GF1 card at 0x%lx without onboard DRAM?\n",
234 gus->gf1.port);
1da177e4
LT
235 return -ENOMEM;
236 }
237 for (idx = 1, d = 0xab; idx < 4; idx++, d++) {
238 local = idx << 18;
239 snd_gf1_poke(gus, local, d);
240 snd_gf1_poke(gus, local + 1, d + 1);
241 if (snd_gf1_peek(gus, local) != d ||
242 snd_gf1_peek(gus, local + 1) != d + 1 ||
243 snd_gf1_peek(gus, 0L) != 0xaa)
244 break;
245 }
246#if 1
247 gus->gf1.memory = idx << 18;
248#else
249 gus->gf1.memory = 256 * 1024;
250#endif
251 for (l = 0, local = gus->gf1.memory; l < 4; l++, local -= 256 * 1024) {
252 gus->gf1.mem_alloc.banks_8[l].address =
253 gus->gf1.mem_alloc.banks_8[l].size = 0;
254 gus->gf1.mem_alloc.banks_16[l].address = l << 18;
255 gus->gf1.mem_alloc.banks_16[l].size = local > 0 ? 256 * 1024 : 0;
256 }
257 gus->gf1.mem_alloc.banks_8[0].size = gus->gf1.memory;
258 return 0; /* some memory were detected */
259}
260
5e2da206 261static int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches)
1da177e4 262{
5e2da206 263 struct snd_card *card;
1da177e4
LT
264 unsigned long flags;
265 int irq, dma1, dma2;
15a1af95 266 static const unsigned char irqs[16] =
1da177e4 267 {0, 0, 1, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7};
15a1af95 268 static const unsigned char dmas[8] =
1da177e4
LT
269 {6, 1, 0, 2, 0, 3, 4, 5};
270
622207dc
TI
271 if (snd_BUG_ON(!gus))
272 return -EINVAL;
1da177e4 273 card = gus->card;
622207dc
TI
274 if (snd_BUG_ON(!card))
275 return -EINVAL;
1da177e4
LT
276
277 gus->mix_cntrl_reg &= 0xf8;
278 gus->mix_cntrl_reg |= 0x01; /* disable MIC, LINE IN, enable LINE OUT */
279 if (gus->codec_flag || gus->ess_flag) {
280 gus->mix_cntrl_reg &= ~1; /* enable LINE IN */
281 gus->mix_cntrl_reg |= 4; /* enable MIC */
282 }
283 dma1 = gus->gf1.dma1;
377a4f7e 284 dma1 = abs(dma1);
1da177e4
LT
285 dma1 = dmas[dma1 & 7];
286 dma2 = gus->gf1.dma2;
377a4f7e 287 dma2 = abs(dma2);
1da177e4 288 dma2 = dmas[dma2 & 7];
1da177e4
LT
289 dma1 |= gus->equal_dma ? 0x40 : (dma2 << 3);
290
291 if ((dma1 & 7) == 0 || (dma2 & 7) == 0) {
a6676811 292 dev_err(gus->card->dev, "Error! DMA isn't defined.\n");
1da177e4
LT
293 return -EINVAL;
294 }
295 irq = gus->gf1.irq;
377a4f7e 296 irq = abs(irq);
1da177e4
LT
297 irq = irqs[irq & 0x0f];
298 if (irq == 0) {
a6676811 299 dev_err(gus->card->dev, "Error! IRQ isn't defined.\n");
1da177e4
LT
300 return -EINVAL;
301 }
302 irq |= 0x40;
303#if 0
304 card->mixer.mix_ctrl_reg |= 0x10;
305#endif
306
307 spin_lock_irqsave(&gus->reg_lock, flags);
308 outb(5, GUSP(gus, REGCNTRLS));
309 outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
310 outb(0x00, GUSP(gus, IRQDMACNTRLREG));
311 outb(0, GUSP(gus, REGCNTRLS));
312 spin_unlock_irqrestore(&gus->reg_lock, flags);
313
314 udelay(100);
315
316 spin_lock_irqsave(&gus->reg_lock, flags);
317 outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
318 outb(dma1, GUSP(gus, IRQDMACNTRLREG));
319 if (latches) {
320 outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
321 outb(irq, GUSP(gus, IRQDMACNTRLREG));
322 }
323 spin_unlock_irqrestore(&gus->reg_lock, flags);
324
325 udelay(100);
326
327 spin_lock_irqsave(&gus->reg_lock, flags);
328 outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
329 outb(dma1, GUSP(gus, IRQDMACNTRLREG));
330 if (latches) {
331 outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
332 outb(irq, GUSP(gus, IRQDMACNTRLREG));
333 }
334 spin_unlock_irqrestore(&gus->reg_lock, flags);
335
336 snd_gf1_delay(gus);
337
338 if (latches)
339 gus->mix_cntrl_reg |= 0x08; /* enable latches */
340 else
341 gus->mix_cntrl_reg &= ~0x08; /* disable latches */
342 spin_lock_irqsave(&gus->reg_lock, flags);
343 outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
344 outb(0, GUSP(gus, GF1PAGE));
345 spin_unlock_irqrestore(&gus->reg_lock, flags);
346
347 return 0;
348}
349
5e2da206 350static int snd_gus_check_version(struct snd_gus_card * gus)
1da177e4
LT
351{
352 unsigned long flags;
353 unsigned char val, rev;
5e2da206 354 struct snd_card *card;
1da177e4
LT
355
356 card = gus->card;
357 spin_lock_irqsave(&gus->reg_lock, flags);
358 outb(0x20, GUSP(gus, REGCNTRLS));
359 val = inb(GUSP(gus, REGCNTRLS));
360 rev = inb(GUSP(gus, BOARDVERSION));
361 spin_unlock_irqrestore(&gus->reg_lock, flags);
a6676811 362 dev_dbg(card->dev, "GF1 [0x%lx] init - val = 0x%x, rev = 0x%x\n", gus->gf1.port, val, rev);
1da177e4
LT
363 strcpy(card->driver, "GUS");
364 strcpy(card->longname, "Gravis UltraSound Classic (2.4)");
365 if ((val != 255 && (val & 0x06)) || (rev >= 5 && rev != 255)) {
366 if (rev >= 5 && rev <= 9) {
367 gus->ics_flag = 1;
368 if (rev == 5)
369 gus->ics_flipped = 1;
370 card->longname[27] = '3';
371 card->longname[29] = rev == 5 ? '5' : '7';
372 }
373 if (rev >= 10 && rev != 255) {
374 if (rev >= 10 && rev <= 11) {
375 strcpy(card->driver, "GUS MAX");
376 strcpy(card->longname, "Gravis UltraSound MAX");
377 gus->max_flag = 1;
378 } else if (rev == 0x30) {
379 strcpy(card->driver, "GUS ACE");
380 strcpy(card->longname, "Gravis UltraSound Ace");
381 gus->ace_flag = 1;
382 } else if (rev == 0x50) {
383 strcpy(card->driver, "GUS Extreme");
384 strcpy(card->longname, "Gravis UltraSound Extreme");
385 gus->ess_flag = 1;
386 } else {
a6676811
TI
387 dev_err(card->dev,
388 "unknown GF1 revision number at 0x%lx - 0x%x (0x%x)\n",
389 gus->gf1.port, rev, val);
390 dev_err(card->dev,
391 " please - report to <perex@perex.cz>\n");
1da177e4
LT
392 }
393 }
394 }
bac59054 395 strscpy(card->shortname, card->longname, sizeof(card->shortname));
1da177e4
LT
396 gus->uart_enable = 1; /* standard GUSes doesn't have midi uart trouble */
397 snd_gus_init_control(gus);
398 return 0;
399}
400
5e2da206 401int snd_gus_initialize(struct snd_gus_card *gus)
1da177e4
LT
402{
403 int err;
404
405 if (!gus->interwave) {
310efd3a
TI
406 err = snd_gus_check_version(gus);
407 if (err < 0) {
a6676811 408 dev_err(gus->card->dev, "version check failed\n");
1da177e4
LT
409 return err;
410 }
310efd3a
TI
411 err = snd_gus_detect_memory(gus);
412 if (err < 0)
1da177e4
LT
413 return err;
414 }
310efd3a
TI
415 err = snd_gus_init_dma_irq(gus, 1);
416 if (err < 0)
1da177e4 417 return err;
1da177e4
LT
418 snd_gf1_start(gus);
419 gus->initialized = 1;
420 return 0;
421}
422
423 /* gus_io.c */
424EXPORT_SYMBOL(snd_gf1_delay);
425EXPORT_SYMBOL(snd_gf1_write8);
426EXPORT_SYMBOL(snd_gf1_look8);
427EXPORT_SYMBOL(snd_gf1_write16);
428EXPORT_SYMBOL(snd_gf1_look16);
429EXPORT_SYMBOL(snd_gf1_i_write8);
430EXPORT_SYMBOL(snd_gf1_i_look8);
1da177e4
LT
431EXPORT_SYMBOL(snd_gf1_i_look16);
432EXPORT_SYMBOL(snd_gf1_dram_addr);
433EXPORT_SYMBOL(snd_gf1_write_addr);
434EXPORT_SYMBOL(snd_gf1_poke);
435EXPORT_SYMBOL(snd_gf1_peek);
436 /* gus_reset.c */
437EXPORT_SYMBOL(snd_gf1_alloc_voice);
438EXPORT_SYMBOL(snd_gf1_free_voice);
439EXPORT_SYMBOL(snd_gf1_ctrl_stop);
440EXPORT_SYMBOL(snd_gf1_stop_voice);
1da177e4
LT
441 /* gus_mixer.c */
442EXPORT_SYMBOL(snd_gf1_new_mixer);
443 /* gus_pcm.c */
444EXPORT_SYMBOL(snd_gf1_pcm_new);
445 /* gus.c */
446EXPORT_SYMBOL(snd_gus_use_inc);
447EXPORT_SYMBOL(snd_gus_use_dec);
448EXPORT_SYMBOL(snd_gus_create);
449EXPORT_SYMBOL(snd_gus_initialize);
450 /* gus_irq.c */
451EXPORT_SYMBOL(snd_gus_interrupt);
452 /* gus_uart.c */
453EXPORT_SYMBOL(snd_gf1_rawmidi_new);
454 /* gus_dram.c */
455EXPORT_SYMBOL(snd_gus_dram_write);
456EXPORT_SYMBOL(snd_gus_dram_read);
457 /* gus_volume.c */
458EXPORT_SYMBOL(snd_gf1_lvol_to_gvol_raw);
459EXPORT_SYMBOL(snd_gf1_translate_freq);
460 /* gus_mem.c */
461EXPORT_SYMBOL(snd_gf1_mem_alloc);
462EXPORT_SYMBOL(snd_gf1_mem_xfree);
463EXPORT_SYMBOL(snd_gf1_mem_free);
464EXPORT_SYMBOL(snd_gf1_mem_lock);