Merge tags 'ib-mfd-clk-gpio-regulator-rtc-v5.13', 'ib-mfd-extcon-v5.13', 'ib-mfd...
[sfrench/cifs-2.6.git] / sound / isa / gus / gusmax.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Driver for Gravis UltraSound MAX soundcard
4  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5  */
6
7 #include <linux/init.h>
8 #include <linux/err.h>
9 #include <linux/isa.h>
10 #include <linux/delay.h>
11 #include <linux/time.h>
12 #include <linux/module.h>
13 #include <asm/dma.h>
14 #include <sound/core.h>
15 #include <sound/gus.h>
16 #include <sound/wss.h>
17 #define SNDRV_LEGACY_FIND_FREE_IRQ
18 #define SNDRV_LEGACY_FIND_FREE_DMA
19 #include <sound/initval.h>
20
21 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
22 MODULE_DESCRIPTION("Gravis UltraSound MAX");
23 MODULE_LICENSE("GPL");
24
25 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
26 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
27 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
28 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* 0x220,0x230,0x240,0x250,0x260 */
29 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 2,3,5,9,11,12,15 */
30 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 1,3,5,6,7 */
31 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 1,3,5,6,7 */
32 static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
33                                 /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
34 static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
35 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
36
37 module_param_array(index, int, NULL, 0444);
38 MODULE_PARM_DESC(index, "Index value for GUS MAX soundcard.");
39 module_param_array(id, charp, NULL, 0444);
40 MODULE_PARM_DESC(id, "ID string for GUS MAX soundcard.");
41 module_param_array(enable, bool, NULL, 0444);
42 MODULE_PARM_DESC(enable, "Enable GUS MAX soundcard.");
43 module_param_hw_array(port, long, ioport, NULL, 0444);
44 MODULE_PARM_DESC(port, "Port # for GUS MAX driver.");
45 module_param_hw_array(irq, int, irq, NULL, 0444);
46 MODULE_PARM_DESC(irq, "IRQ # for GUS MAX driver.");
47 module_param_hw_array(dma1, int, dma, NULL, 0444);
48 MODULE_PARM_DESC(dma1, "DMA1 # for GUS MAX driver.");
49 module_param_hw_array(dma2, int, dma, NULL, 0444);
50 MODULE_PARM_DESC(dma2, "DMA2 # for GUS MAX driver.");
51 module_param_array(joystick_dac, int, NULL, 0444);
52 MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS MAX driver.");
53 module_param_array(channels, int, NULL, 0444);
54 MODULE_PARM_DESC(channels, "Used GF1 channels for GUS MAX driver.");
55 module_param_array(pcm_channels, int, NULL, 0444);
56 MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS MAX driver.");
57
58 struct snd_gusmax {
59         int irq;
60         struct snd_card *card;
61         struct snd_gus_card *gus;
62         struct snd_wss *wss;
63         unsigned short gus_status_reg;
64         unsigned short pcm_status_reg;
65 };
66
67 #define PFX     "gusmax: "
68
69 static int snd_gusmax_detect(struct snd_gus_card *gus)
70 {
71         unsigned char d;
72
73         snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0);   /* reset GF1 */
74         if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
75                 snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
76                 return -ENODEV;
77         }
78         udelay(160);
79         snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1);   /* release reset */
80         udelay(160);
81         if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
82                 snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
83                 return -ENODEV;
84         }
85
86         return 0;
87 }
88
89 static irqreturn_t snd_gusmax_interrupt(int irq, void *dev_id)
90 {
91         struct snd_gusmax *maxcard = dev_id;
92         int loop, max = 5;
93         int handled = 0;
94
95         do {
96                 loop = 0;
97                 if (inb(maxcard->gus_status_reg)) {
98                         handled = 1;
99                         snd_gus_interrupt(irq, maxcard->gus);
100                         loop++;
101                 }
102                 if (inb(maxcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */
103                         handled = 1;
104                         snd_wss_interrupt(irq, maxcard->wss);
105                         loop++;
106                 }
107         } while (loop && --max > 0);
108         return IRQ_RETVAL(handled);
109 }
110
111 static void snd_gusmax_init(int dev, struct snd_card *card,
112                             struct snd_gus_card *gus)
113 {
114         gus->equal_irq = 1;
115         gus->codec_flag = 1;
116         gus->joystick_dac = joystick_dac[dev];
117         /* init control register */
118         gus->max_cntrl_val = (gus->gf1.port >> 4) & 0x0f;
119         if (gus->gf1.dma1 > 3)
120                 gus->max_cntrl_val |= 0x10;
121         if (gus->gf1.dma2 > 3)
122                 gus->max_cntrl_val |= 0x20;
123         gus->max_cntrl_val |= 0x40;
124         outb(gus->max_cntrl_val, GUSP(gus, MAXCNTRLPORT));
125 }
126
127 static int snd_gusmax_mixer(struct snd_wss *chip)
128 {
129         struct snd_card *card = chip->card;
130         struct snd_ctl_elem_id id1, id2;
131         int err;
132         
133         memset(&id1, 0, sizeof(id1));
134         memset(&id2, 0, sizeof(id2));
135         id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
136         /* reassign AUXA to SYNTHESIZER */
137         strcpy(id1.name, "Aux Playback Switch");
138         strcpy(id2.name, "Synth Playback Switch");
139         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
140                 return err;
141         strcpy(id1.name, "Aux Playback Volume");
142         strcpy(id2.name, "Synth Playback Volume");
143         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
144                 return err;
145         /* reassign AUXB to CD */
146         strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
147         strcpy(id2.name, "CD Playback Switch");
148         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
149                 return err;
150         strcpy(id1.name, "Aux Playback Volume");
151         strcpy(id2.name, "CD Playback Volume");
152         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
153                 return err;
154 #if 0
155         /* reassign Mono Input to MIC */
156         if (snd_mixer_group_rename(mixer,
157                                 SNDRV_MIXER_IN_MONO, 0,
158                                 SNDRV_MIXER_IN_MIC, 0) < 0)
159                 goto __error;
160         if (snd_mixer_elem_rename(mixer,
161                                 SNDRV_MIXER_IN_MONO, 0, SNDRV_MIXER_ETYPE_INPUT,
162                                 SNDRV_MIXER_IN_MIC, 0) < 0)
163                 goto __error;
164         if (snd_mixer_elem_rename(mixer,
165                                 "Mono Capture Volume", 0, SNDRV_MIXER_ETYPE_VOLUME1,
166                                 "Mic Capture Volume", 0) < 0)
167                 goto __error;
168         if (snd_mixer_elem_rename(mixer,
169                                 "Mono Capture Switch", 0, SNDRV_MIXER_ETYPE_SWITCH1,
170                                 "Mic Capture Switch", 0) < 0)
171                 goto __error;
172 #endif
173         return 0;
174 }
175
176 static void snd_gusmax_free(struct snd_card *card)
177 {
178         struct snd_gusmax *maxcard = card->private_data;
179         
180         if (maxcard == NULL)
181                 return;
182         if (maxcard->irq >= 0)
183                 free_irq(maxcard->irq, (void *)maxcard);
184 }
185
186 static int snd_gusmax_match(struct device *pdev, unsigned int dev)
187 {
188         return enable[dev];
189 }
190
191 static int snd_gusmax_probe(struct device *pdev, unsigned int dev)
192 {
193         static const int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1};
194         static const int possible_dmas[] = {5, 6, 7, 1, 3, -1};
195         int xirq, xdma1, xdma2, err;
196         struct snd_card *card;
197         struct snd_gus_card *gus = NULL;
198         struct snd_wss *wss;
199         struct snd_gusmax *maxcard;
200
201         err = snd_card_new(pdev, index[dev], id[dev], THIS_MODULE,
202                            sizeof(struct snd_gusmax), &card);
203         if (err < 0)
204                 return err;
205         card->private_free = snd_gusmax_free;
206         maxcard = card->private_data;
207         maxcard->card = card;
208         maxcard->irq = -1;
209         
210         xirq = irq[dev];
211         if (xirq == SNDRV_AUTO_IRQ) {
212                 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
213                         snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
214                         err = -EBUSY;
215                         goto _err;
216                 }
217         }
218         xdma1 = dma1[dev];
219         if (xdma1 == SNDRV_AUTO_DMA) {
220                 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
221                         snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
222                         err = -EBUSY;
223                         goto _err;
224                 }
225         }
226         xdma2 = dma2[dev];
227         if (xdma2 == SNDRV_AUTO_DMA) {
228                 if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
229                         snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
230                         err = -EBUSY;
231                         goto _err;
232                 }
233         }
234
235         if (port[dev] != SNDRV_AUTO_PORT) {
236                 err = snd_gus_create(card,
237                                      port[dev],
238                                      -xirq, xdma1, xdma2,
239                                      0, channels[dev],
240                                      pcm_channels[dev],
241                                      0, &gus);
242         } else {
243                 static const unsigned long possible_ports[] = {
244                         0x220, 0x230, 0x240, 0x250, 0x260
245                 };
246                 int i;
247                 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
248                         err = snd_gus_create(card,
249                                              possible_ports[i],
250                                              -xirq, xdma1, xdma2,
251                                              0, channels[dev],
252                                              pcm_channels[dev],
253                                              0, &gus);
254                         if (err >= 0) {
255                                 port[dev] = possible_ports[i];
256                                 break;
257                         }
258                 }
259         }
260         if (err < 0)
261                 goto _err;
262
263         if ((err = snd_gusmax_detect(gus)) < 0)
264                 goto _err;
265
266         maxcard->gus_status_reg = gus->gf1.reg_irqstat;
267         maxcard->pcm_status_reg = gus->gf1.port + 0x10c + 2;
268         snd_gusmax_init(dev, card, gus);
269         if ((err = snd_gus_initialize(gus)) < 0)
270                 goto _err;
271
272         if (!gus->max_flag) {
273                 snd_printk(KERN_ERR PFX "GUS MAX soundcard was not detected at 0x%lx\n", gus->gf1.port);
274                 err = -ENODEV;
275                 goto _err;
276         }
277
278         if (request_irq(xirq, snd_gusmax_interrupt, 0, "GUS MAX", (void *)maxcard)) {
279                 snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq);
280                 err = -EBUSY;
281                 goto _err;
282         }
283         maxcard->irq = xirq;
284         card->sync_irq = maxcard->irq;
285
286         err = snd_wss_create(card,
287                              gus->gf1.port + 0x10c, -1, xirq,
288                              xdma2 < 0 ? xdma1 : xdma2, xdma1,
289                              WSS_HW_DETECT,
290                              WSS_HWSHARE_IRQ |
291                              WSS_HWSHARE_DMA1 |
292                              WSS_HWSHARE_DMA2,
293                              &wss);
294         if (err < 0)
295                 goto _err;
296
297         err = snd_wss_pcm(wss, 0);
298         if (err < 0)
299                 goto _err;
300
301         err = snd_wss_mixer(wss);
302         if (err < 0)
303                 goto _err;
304
305         err = snd_wss_timer(wss, 2);
306         if (err < 0)
307                 goto _err;
308
309         if (pcm_channels[dev] > 0) {
310                 if ((err = snd_gf1_pcm_new(gus, 1, 1)) < 0)
311                         goto _err;
312         }
313         err = snd_gusmax_mixer(wss);
314         if (err < 0)
315                 goto _err;
316
317         err = snd_gf1_rawmidi_new(gus, 0);
318         if (err < 0)
319                 goto _err;
320
321         sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %i, dma %i", gus->gf1.port, xirq, xdma1);
322         if (xdma2 >= 0)
323                 sprintf(card->longname + strlen(card->longname), "&%i", xdma2);
324
325         err = snd_card_register(card);
326         if (err < 0)
327                 goto _err;
328                 
329         maxcard->gus = gus;
330         maxcard->wss = wss;
331
332         dev_set_drvdata(pdev, card);
333         return 0;
334
335  _err:
336         snd_card_free(card);
337         return err;
338 }
339
340 static void snd_gusmax_remove(struct device *devptr, unsigned int dev)
341 {
342         snd_card_free(dev_get_drvdata(devptr));
343 }
344
345 #define DEV_NAME "gusmax"
346
347 static struct isa_driver snd_gusmax_driver = {
348         .match          = snd_gusmax_match,
349         .probe          = snd_gusmax_probe,
350         .remove         = snd_gusmax_remove,
351         /* FIXME: suspend/resume */
352         .driver         = {
353                 .name   = DEV_NAME
354         },
355 };
356
357 module_isa_driver(snd_gusmax_driver, SNDRV_CARDS);