[ALSA] gus - Use platform_device
[linux-2.6.git] / sound / isa / gus / interwave.c
1 /*
2  *  Driver for AMD InterWave soundcard
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  *   1999/07/22         Erik Inge Bolso <knan@mo.himolde.no>
21  *                      * mixer group handlers
22  *
23  */
24
25 #include <sound/driver.h>
26 #include <linux/init.h>
27 #include <linux/err.h>
28 #include <linux/platform_device.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/pnp.h>
32 #include <linux/moduleparam.h>
33 #include <asm/dma.h>
34 #include <sound/core.h>
35 #include <sound/gus.h>
36 #include <sound/cs4231.h>
37 #ifdef SNDRV_STB
38 #include <sound/tea6330t.h>
39 #endif
40 #define SNDRV_LEGACY_FIND_FREE_IRQ
41 #define SNDRV_LEGACY_FIND_FREE_DMA
42 #include <sound/initval.h>
43
44 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
45 MODULE_LICENSE("GPL");
46 #ifndef SNDRV_STB
47 MODULE_DESCRIPTION("AMD InterWave");
48 MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound Plug & Play},"
49                 "{STB,SoundRage32},"
50                 "{MED,MED3210},"
51                 "{Dynasonix,Dynasonix Pro},"
52                 "{Panasonic,PCA761AW}}");
53 #else
54 MODULE_DESCRIPTION("AMD InterWave STB with TEA6330T");
55 MODULE_SUPPORTED_DEVICE("{{AMD,InterWave STB with TEA6330T}}");
56 #endif
57
58 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
59 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
60 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
61 #ifdef CONFIG_PNP
62 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
63 #endif
64 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* 0x210,0x220,0x230,0x240,0x250,0x260 */
65 #ifdef SNDRV_STB
66 static long port_tc[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* 0x350,0x360,0x370,0x380 */
67 #endif
68 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 2,3,5,9,11,12,15 */
69 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3,5,6,7 */
70 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3,5,6,7 */
71 static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
72                                 /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
73 static int midi[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
74 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
75 static int effect[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
76
77 #ifdef SNDRV_STB
78 #define PFX "interwave-stb: "
79 #define INTERWAVE_DRIVER        "snd_interwave_stb"
80 #define INTERWAVE_PNP_DRIVER    "interwave-stb"
81 #else
82 #define PFX "interwave: "
83 #define INTERWAVE_DRIVER        "snd_interwave"
84 #define INTERWAVE_PNP_DRIVER    "interwave"
85 #endif
86
87 module_param_array(index, int, NULL, 0444);
88 MODULE_PARM_DESC(index, "Index value for InterWave soundcard.");
89 module_param_array(id, charp, NULL, 0444);
90 MODULE_PARM_DESC(id, "ID string for InterWave soundcard.");
91 module_param_array(enable, bool, NULL, 0444);
92 MODULE_PARM_DESC(enable, "Enable InterWave soundcard.");
93 #ifdef CONFIG_PNP
94 module_param_array(isapnp, bool, NULL, 0444);
95 MODULE_PARM_DESC(isapnp, "ISA PnP detection for specified soundcard.");
96 #endif
97 module_param_array(port, long, NULL, 0444);
98 MODULE_PARM_DESC(port, "Port # for InterWave driver.");
99 #ifdef SNDRV_STB
100 module_param_array(port_tc, long, NULL, 0444);
101 MODULE_PARM_DESC(port_tc, "Tone control (TEA6330T - i2c bus) port # for InterWave driver.");
102 #endif
103 module_param_array(irq, int, NULL, 0444);
104 MODULE_PARM_DESC(irq, "IRQ # for InterWave driver.");
105 module_param_array(dma1, int, NULL, 0444);
106 MODULE_PARM_DESC(dma1, "DMA1 # for InterWave driver.");
107 module_param_array(dma2, int, NULL, 0444);
108 MODULE_PARM_DESC(dma2, "DMA2 # for InterWave driver.");
109 module_param_array(joystick_dac, int, NULL, 0444);
110 MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for InterWave driver.");
111 module_param_array(midi, int, NULL, 0444);
112 MODULE_PARM_DESC(midi, "MIDI UART enable for InterWave driver.");
113 module_param_array(pcm_channels, int, NULL, 0444);
114 MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for InterWave driver.");
115 module_param_array(effect, int, NULL, 0444);
116 MODULE_PARM_DESC(effect, "Effects enable for InterWave driver.");
117
118 struct snd_interwave {
119         int irq;
120         struct snd_card *card;
121         struct snd_gus_card *gus;
122         struct snd_cs4231 *cs4231;
123 #ifdef SNDRV_STB
124         struct resource *i2c_res;
125 #endif
126         unsigned short gus_status_reg;
127         unsigned short pcm_status_reg;
128 #ifdef CONFIG_PNP
129         struct pnp_dev *dev;
130 #ifdef SNDRV_STB
131         struct pnp_dev *devtc;
132 #endif
133 #endif
134 };
135
136
137 #ifdef CONFIG_PNP
138
139 static struct pnp_card_device_id snd_interwave_pnpids[] = {
140 #ifndef SNDRV_STB
141         /* Gravis UltraSound Plug & Play */
142         { .id = "GRV0001", .devs = { { .id = "GRV0000" } } },
143         /* STB SoundRage32 */
144         { .id = "STB011a", .devs = { { .id = "STB0010" } } },
145         /* MED3210 */
146         { .id = "DXP3201", .devs = { { .id = "DXP0010" } } },
147         /* Dynasonic Pro */
148         /* This device also have CDC1117:DynaSonix Pro Audio Effects Processor */
149         { .id = "CDC1111", .devs = { { .id = "CDC1112" } } },
150         /* Panasonic PCA761AW Audio Card */
151         { .id = "ADV55ff", .devs = { { .id = "ADV0010" } } },
152         /* InterWave STB without TEA6330T */
153         { .id = "ADV550a", .devs = { { .id = "ADV0010" } } },
154 #else
155         /* InterWave STB with TEA6330T */
156         { .id = "ADV550a", .devs = { { .id = "ADV0010" }, { .id = "ADV0015" } } },
157 #endif
158         { .id = "" }
159 };
160
161 MODULE_DEVICE_TABLE(pnp_card, snd_interwave_pnpids);
162
163 #endif /* CONFIG_PNP */
164
165
166 #ifdef SNDRV_STB
167 static void snd_interwave_i2c_setlines(struct snd_i2c_bus *bus, int ctrl, int data)
168 {
169         unsigned long port = bus->private_value;
170
171 #if 0
172         printk("i2c_setlines - 0x%lx <- %i,%i\n", port, ctrl, data);
173 #endif
174         outb((data << 1) | ctrl, port);
175         udelay(10);
176 }
177
178 static int snd_interwave_i2c_getclockline(struct snd_i2c_bus *bus)
179 {
180         unsigned long port = bus->private_value;
181         unsigned char res;
182
183         res = inb(port) & 1;
184 #if 0
185         printk("i2c_getclockline - 0x%lx -> %i\n", port, res);
186 #endif
187         return res;
188 }
189
190 static int snd_interwave_i2c_getdataline(struct snd_i2c_bus *bus, int ack)
191 {
192         unsigned long port = bus->private_value;
193         unsigned char res;
194
195         if (ack)
196                 udelay(10);
197         res = (inb(port) & 2) >> 1;
198 #if 0
199         printk("i2c_getdataline - 0x%lx -> %i\n", port, res);
200 #endif
201         return res;
202 }
203
204 static struct snd_i2c_bit_ops snd_interwave_i2c_bit_ops = {
205         .setlines = snd_interwave_i2c_setlines,
206         .getclock = snd_interwave_i2c_getclockline,
207         .getdata  = snd_interwave_i2c_getdataline,
208 };
209
210 static int __devinit snd_interwave_detect_stb(struct snd_interwave *iwcard,
211                                               struct snd_gus_card * gus, int dev,
212                                               struct snd_i2c_bus **rbus)
213 {
214         unsigned long port;
215         struct snd_i2c_bus *bus;
216         struct snd_card *card = iwcard->card;
217         char name[32];
218         int err;
219
220         *rbus = NULL;
221         port = port_tc[dev];
222         if (port == SNDRV_AUTO_PORT) {
223                 port = 0x350;
224                 if (gus->gf1.port == 0x250) {
225                         port = 0x360;
226                 }
227                 while (port <= 0x380) {
228                         if ((iwcard->i2c_res = request_region(port, 1, "InterWave (I2C bus)")) != NULL)
229                                 break;
230                         port += 0x10;
231                 }
232         } else {
233                 iwcard->i2c_res = request_region(port, 1, "InterWave (I2C bus)");
234         }
235         if (iwcard->i2c_res == NULL) {
236                 snd_printk(KERN_ERR "interwave: can't grab i2c bus port\n");
237                 return -ENODEV;
238         }
239
240         sprintf(name, "InterWave-%i", card->number);
241         if ((err = snd_i2c_bus_create(card, name, NULL, &bus)) < 0)
242                 return err;
243         bus->private_value = port;
244         bus->hw_ops.bit = &snd_interwave_i2c_bit_ops;
245         if ((err = snd_tea6330t_detect(bus, 0)) < 0)
246                 return err;
247         *rbus = bus;
248         return 0;
249 }
250 #endif
251
252 static int __devinit snd_interwave_detect(struct snd_interwave *iwcard,
253                                           struct snd_gus_card * gus,
254                                           int dev
255 #ifdef SNDRV_STB
256                                           , struct snd_i2c_bus **rbus
257 #endif
258                                           )
259 {
260         unsigned long flags;
261         unsigned char rev1, rev2;
262         int d;
263
264         snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0);   /* reset GF1 */
265         if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
266                 snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
267                 return -ENODEV;
268         }
269         udelay(160);
270         snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1);   /* release reset */
271         udelay(160);
272         if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
273                 snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
274                 return -ENODEV;
275         }
276         spin_lock_irqsave(&gus->reg_lock, flags);
277         rev1 = snd_gf1_look8(gus, SNDRV_GF1_GB_VERSION_NUMBER);
278         snd_gf1_write8(gus, SNDRV_GF1_GB_VERSION_NUMBER, ~rev1);
279         rev2 = snd_gf1_look8(gus, SNDRV_GF1_GB_VERSION_NUMBER);
280         snd_gf1_write8(gus, SNDRV_GF1_GB_VERSION_NUMBER, rev1);
281         spin_unlock_irqrestore(&gus->reg_lock, flags);
282         snd_printdd("[0x%lx] InterWave check - rev1=0x%x, rev2=0x%x\n", gus->gf1.port, rev1, rev2);
283         if ((rev1 & 0xf0) == (rev2 & 0xf0) &&
284             (rev1 & 0x0f) != (rev2 & 0x0f)) {
285                 snd_printdd("[0x%lx] InterWave check - passed\n", gus->gf1.port);
286                 gus->interwave = 1;
287                 strcpy(gus->card->shortname, "AMD InterWave");
288                 gus->revision = rev1 >> 4;
289 #ifndef SNDRV_STB
290                 return 0;       /* ok.. We have an InterWave board */
291 #else
292                 return snd_interwave_detect_stb(iwcard, gus, dev, rbus);
293 #endif
294         }
295         snd_printdd("[0x%lx] InterWave check - failed\n", gus->gf1.port);
296         return -ENODEV;
297 }
298
299 static irqreturn_t snd_interwave_interrupt(int irq, void *dev_id, struct pt_regs *regs)
300 {
301         struct snd_interwave *iwcard = (struct snd_interwave *) dev_id;
302         int loop, max = 5;
303         int handled = 0;
304
305         do {
306                 loop = 0;
307                 if (inb(iwcard->gus_status_reg)) {
308                         handled = 1;
309                         snd_gus_interrupt(irq, iwcard->gus, regs);
310                         loop++;
311                 }
312                 if (inb(iwcard->pcm_status_reg) & 0x01) {       /* IRQ bit is set? */
313                         handled = 1;
314                         snd_cs4231_interrupt(irq, iwcard->cs4231, regs);
315                         loop++;
316                 }
317         } while (loop && --max > 0);
318         return IRQ_RETVAL(handled);
319 }
320
321 static void __devinit snd_interwave_reset(struct snd_gus_card * gus)
322 {
323         snd_gf1_write8(gus, SNDRV_GF1_GB_RESET, 0x00);
324         udelay(160);
325         snd_gf1_write8(gus, SNDRV_GF1_GB_RESET, 0x01);
326         udelay(160);
327 }
328
329 static void __devinit snd_interwave_bank_sizes(struct snd_gus_card * gus, int *sizes)
330 {
331         unsigned int idx;
332         unsigned int local;
333         unsigned char d;
334
335         for (idx = 0; idx < 4; idx++) {
336                 sizes[idx] = 0;
337                 d = 0x55;
338                 for (local = idx << 22;
339                      local < (idx << 22) + 0x400000;
340                      local += 0x40000, d++) {
341                         snd_gf1_poke(gus, local, d);
342                         snd_gf1_poke(gus, local + 1, d + 1);
343 #if 0
344                         printk("d = 0x%x, local = 0x%x, local + 1 = 0x%x, idx << 22 = 0x%x\n",
345                                d,
346                                snd_gf1_peek(gus, local),
347                                snd_gf1_peek(gus, local + 1),
348                                snd_gf1_peek(gus, idx << 22));
349 #endif
350                         if (snd_gf1_peek(gus, local) != d ||
351                             snd_gf1_peek(gus, local + 1) != d + 1 ||
352                             snd_gf1_peek(gus, idx << 22) != 0x55)
353                                 break;
354                         sizes[idx]++;
355                 }
356         }
357 #if 0
358         printk("sizes: %i %i %i %i\n", sizes[0], sizes[1], sizes[2], sizes[3]);
359 #endif
360 }
361
362 struct rom_hdr {
363         /* 000 */ unsigned char iwave[8];
364         /* 008 */ unsigned char rom_hdr_revision;
365         /* 009 */ unsigned char series_number;
366         /* 010 */ unsigned char series_name[16];
367         /* 026 */ unsigned char date[10];
368         /* 036 */ unsigned short vendor_revision_major;
369         /* 038 */ unsigned short vendor_revision_minor;
370         /* 040 */ unsigned int rom_size;
371         /* 044 */ unsigned char copyright[128];
372         /* 172 */ unsigned char vendor_name[64];
373         /* 236 */ unsigned char rom_description[128];
374         /* 364 */ unsigned char pad[147];
375         /* 511 */ unsigned char csum;
376 };
377
378 static void __devinit snd_interwave_detect_memory(struct snd_gus_card * gus)
379 {
380         static unsigned int lmc[13] =
381         {
382                 0x00000001, 0x00000101, 0x01010101, 0x00000401,
383                 0x04040401, 0x00040101, 0x04040101, 0x00000004,
384                 0x00000404, 0x04040404, 0x00000010, 0x00001010,
385                 0x10101010
386         };
387
388         int bank_pos, pages;
389         unsigned int i, lmct;
390         int psizes[4];
391         unsigned char iwave[8];
392         unsigned char csum;
393
394         snd_interwave_reset(gus);
395         snd_gf1_write8(gus, SNDRV_GF1_GB_GLOBAL_MODE, snd_gf1_read8(gus, SNDRV_GF1_GB_GLOBAL_MODE) | 0x01);             /* enhanced mode */
396         snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01); /* DRAM I/O cycles selected */
397         snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xff10) | 0x004c);
398         /* ok.. simple test of memory size */
399         pages = 0;
400         snd_gf1_poke(gus, 0, 0x55);
401         snd_gf1_poke(gus, 1, 0xaa);
402 #if 1
403         if (snd_gf1_peek(gus, 0) == 0x55 && snd_gf1_peek(gus, 1) == 0xaa)
404 #else
405         if (0)                  /* ok.. for testing of 0k RAM */
406 #endif
407         {
408                 snd_interwave_bank_sizes(gus, psizes);
409                 lmct = (psizes[3] << 24) | (psizes[2] << 16) |
410                     (psizes[1] << 8) | psizes[0];
411 #if 0
412                 printk("lmct = 0x%08x\n", lmct);
413 #endif
414                 for (i = 0; i < ARRAY_SIZE(lmc); i++)
415                         if (lmct == lmc[i]) {
416 #if 0
417                                 printk("found !!! %i\n", i);
418 #endif
419                                 snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xfff0) | i);
420                                 snd_interwave_bank_sizes(gus, psizes);
421                                 break;
422                         }
423                 if (i >= ARRAY_SIZE(lmc) && !gus->gf1.enh_mode)
424                          snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xfff0) | 2);
425                 for (i = 0; i < 4; i++) {
426                         gus->gf1.mem_alloc.banks_8[i].address =
427                             gus->gf1.mem_alloc.banks_16[i].address = i << 22;
428                         gus->gf1.mem_alloc.banks_8[i].size =
429                             gus->gf1.mem_alloc.banks_16[i].size = psizes[i] << 18;
430                         pages += psizes[i];
431                 }
432         }
433         pages <<= 18;
434         gus->gf1.memory = pages;
435
436         snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x03); /* select ROM */
437         snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xff1f) | (4 << 5));
438         gus->gf1.rom_banks = 0;
439         gus->gf1.rom_memory = 0;
440         for (bank_pos = 0; bank_pos < 16L * 1024L * 1024L; bank_pos += 4L * 1024L * 1024L) {
441                 for (i = 0; i < 8; ++i)
442                         iwave[i] = snd_gf1_peek(gus, bank_pos + i);
443 #ifdef CONFIG_SND_DEBUG_ROM
444                 printk(KERN_DEBUG "ROM at 0x%06x = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", bank_pos,
445                        iwave[0], iwave[1], iwave[2], iwave[3],
446                        iwave[4], iwave[5], iwave[6], iwave[7]);
447 #endif
448                 if (strncmp(iwave, "INTRWAVE", 8))
449                         continue;       /* first check */
450                 csum = 0;
451                 for (i = 0; i < sizeof(struct rom_hdr); i++)
452                         csum += snd_gf1_peek(gus, bank_pos + i);
453 #ifdef CONFIG_SND_DEBUG_ROM
454                 printk(KERN_DEBUG "ROM checksum = 0x%x (computed)\n", csum);
455 #endif
456                 if (csum != 0)
457                         continue;       /* not valid rom */
458                 gus->gf1.rom_banks++;
459                 gus->gf1.rom_present |= 1 << (bank_pos >> 22);
460                 gus->gf1.rom_memory = snd_gf1_peek(gus, bank_pos + 40) |
461                                      (snd_gf1_peek(gus, bank_pos + 41) << 8) |
462                                      (snd_gf1_peek(gus, bank_pos + 42) << 16) |
463                                      (snd_gf1_peek(gus, bank_pos + 43) << 24);
464         }
465 #if 0
466         if (gus->gf1.rom_memory > 0) {
467                 if (gus->gf1.rom_banks == 1 && gus->gf1.rom_present == 8)
468                         gus->card->type = SNDRV_CARD_TYPE_IW_DYNASONIC;
469         }
470 #endif
471         snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x00); /* select RAM */
472
473         if (!gus->gf1.enh_mode)
474                 snd_interwave_reset(gus);
475 }
476
477 static void __devinit snd_interwave_init(int dev, struct snd_gus_card * gus)
478 {
479         unsigned long flags;
480
481         /* ok.. some InterWave specific initialization */
482         spin_lock_irqsave(&gus->reg_lock, flags);
483         snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, 0x00);
484         snd_gf1_write8(gus, SNDRV_GF1_GB_COMPATIBILITY, 0x1f);
485         snd_gf1_write8(gus, SNDRV_GF1_GB_DECODE_CONTROL, 0x49);
486         snd_gf1_write8(gus, SNDRV_GF1_GB_VERSION_NUMBER, 0x11);
487         snd_gf1_write8(gus, SNDRV_GF1_GB_MPU401_CONTROL_A, 0x00);
488         snd_gf1_write8(gus, SNDRV_GF1_GB_MPU401_CONTROL_B, 0x30);
489         snd_gf1_write8(gus, SNDRV_GF1_GB_EMULATION_IRQ, 0x00);
490         spin_unlock_irqrestore(&gus->reg_lock, flags);
491         gus->equal_irq = 1;
492         gus->codec_flag = 1;
493         gus->interwave = 1;
494         gus->max_flag = 1;
495         gus->joystick_dac = joystick_dac[dev];
496
497 }
498
499 static struct snd_kcontrol_new snd_interwave_controls[] = {
500 CS4231_DOUBLE("Master Playback Switch", 0, CS4231_LINE_LEFT_OUTPUT, CS4231_LINE_RIGHT_OUTPUT, 7, 7, 1, 1),
501 CS4231_DOUBLE("Master Playback Volume", 0, CS4231_LINE_LEFT_OUTPUT, CS4231_LINE_RIGHT_OUTPUT, 0, 0, 31, 1),
502 CS4231_DOUBLE("Mic Playback Switch", 0, CS4231_LEFT_MIC_INPUT, CS4231_RIGHT_MIC_INPUT, 7, 7, 1, 1),
503 CS4231_DOUBLE("Mic Playback Volume", 0, CS4231_LEFT_MIC_INPUT, CS4231_RIGHT_MIC_INPUT, 0, 0, 31, 1)
504 };
505
506 static int __devinit snd_interwave_mixer(struct snd_cs4231 *chip)
507 {
508         struct snd_card *card = chip->card;
509         struct snd_ctl_elem_id id1, id2;
510         unsigned int idx;
511         int err;
512
513         memset(&id1, 0, sizeof(id1));
514         memset(&id2, 0, sizeof(id2));
515         id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
516 #if 0
517         /* remove mono microphone controls */
518         strcpy(id1.name, "Mic Playback Switch");
519         if ((err = snd_ctl_remove_id(card, &id1)) < 0)
520                 return err;
521         strcpy(id1.name, "Mic Playback Volume");
522         if ((err = snd_ctl_remove_id(card, &id1)) < 0)
523                 return err;
524 #endif
525         /* add new master and mic controls */
526         for (idx = 0; idx < ARRAY_SIZE(snd_interwave_controls); idx++)
527                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_interwave_controls[idx], chip))) < 0)
528                         return err;
529         snd_cs4231_out(chip, CS4231_LINE_LEFT_OUTPUT, 0x9f);
530         snd_cs4231_out(chip, CS4231_LINE_RIGHT_OUTPUT, 0x9f);
531         snd_cs4231_out(chip, CS4231_LEFT_MIC_INPUT, 0x9f);
532         snd_cs4231_out(chip, CS4231_RIGHT_MIC_INPUT, 0x9f);
533         /* reassign AUXA to SYNTHESIZER */
534         strcpy(id1.name, "Aux Playback Switch");
535         strcpy(id2.name, "Synth Playback Switch");
536         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
537                 return err;
538         strcpy(id1.name, "Aux Playback Volume");
539         strcpy(id2.name, "Synth Playback Volume");
540         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
541                 return err;
542         /* reassign AUXB to CD */
543         strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
544         strcpy(id2.name, "CD Playback Switch");
545         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
546                 return err;
547         strcpy(id1.name, "Aux Playback Volume");
548         strcpy(id2.name, "CD Playback Volume");
549         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
550                 return err;
551         return 0;
552 }
553
554 #ifdef CONFIG_PNP
555
556 static int __devinit snd_interwave_pnp(int dev, struct snd_interwave *iwcard,
557                                        struct pnp_card_link *card,
558                                        const struct pnp_card_device_id *id)
559 {
560         struct pnp_dev *pdev;
561         struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
562         int err;
563
564         iwcard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
565         if (iwcard->dev == NULL) {
566                 kfree(cfg);
567                 return -EBUSY;
568         }
569 #ifdef SNDRV_STB
570         iwcard->devtc = pnp_request_card_device(card, id->devs[1].id, NULL);
571         if (iwcard->devtc == NULL) {
572                 kfree(cfg);
573                 return -EBUSY;
574         }
575 #endif
576         /* Synth & Codec initialization */
577         pdev = iwcard->dev;
578         pnp_init_resource_table(cfg);
579         if (port[dev] != SNDRV_AUTO_PORT) {
580                 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
581                 pnp_resource_change(&cfg->port_resource[1], port[dev] + 0x100, 12);
582                 pnp_resource_change(&cfg->port_resource[2], port[dev] + 0x10c, 4);
583         }
584         if (dma1[dev] != SNDRV_AUTO_DMA)
585                 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
586         if (dma2[dev] != SNDRV_AUTO_DMA)
587                 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
588         if (dma2[dev] < 0)
589                 pnp_resource_change(&cfg->dma_resource[1], 4, 1);
590         if (irq[dev] != SNDRV_AUTO_IRQ)
591                 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
592         if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
593                 snd_printk(KERN_ERR "InterWave - Synth - the requested resources are invalid, using auto config\n");
594         err = pnp_activate_dev(pdev);
595         if (err < 0) {
596                 kfree(cfg);
597                 snd_printk(KERN_ERR "InterWave PnP configure failure (out of resources?)\n");
598                 return err;
599         }
600         if (pnp_port_start(pdev, 0) + 0x100 != pnp_port_start(pdev, 1) ||
601             pnp_port_start(pdev, 0) + 0x10c != pnp_port_start(pdev, 2)) {
602                 kfree(cfg);
603                 snd_printk(KERN_ERR "PnP configure failure (wrong ports)\n");
604                 return -ENOENT;
605         }
606         port[dev] = pnp_port_start(pdev, 0);
607         dma1[dev] = pnp_dma(pdev, 0);
608         if (dma2[dev] >= 0)
609                 dma2[dev] = pnp_dma(pdev, 1);
610         irq[dev] = pnp_irq(pdev, 0);
611         snd_printdd("isapnp IW: sb port=0x%lx, gf1 port=0x%lx, codec port=0x%lx\n",
612                                 pnp_port_start(pdev, 0),
613                                 pnp_port_start(pdev, 1),
614                                 pnp_port_start(pdev, 2));
615         snd_printdd("isapnp IW: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
616 #ifdef SNDRV_STB
617         /* Tone Control initialization */
618         pdev = iwcard->devtc;
619         pnp_init_resource_table(cfg);
620         if (port_tc[dev] != SNDRV_AUTO_PORT)
621                 pnp_resource_change(&cfg->port_resource[0], port_tc[dev], 1);
622         if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
623                 snd_printk(KERN_ERR "InterWave - ToneControl - the requested resources are invalid, using auto config\n");
624         err = pnp_activate_dev(pdev);
625         if (err < 0) {
626                 kfree(cfg);
627                 snd_printk(KERN_ERR "InterWave ToneControl PnP configure failure (out of resources?)\n");
628                 return err;
629         }
630         port_tc[dev] = pnp_port_start(pdev, 0);
631         snd_printdd("isapnp IW: tone control port=0x%lx\n", port_tc[dev]);
632 #endif
633         kfree(cfg);
634         return 0;
635 }
636 #endif /* CONFIG_PNP */
637
638 static void snd_interwave_free(struct snd_card *card)
639 {
640         struct snd_interwave *iwcard = card->private_data;
641
642         if (iwcard == NULL)
643                 return;
644 #ifdef SNDRV_STB
645         release_and_free_resource(iwcard->i2c_res);
646 #endif
647         if (iwcard->irq >= 0)
648                 free_irq(iwcard->irq, (void *)iwcard);
649 }
650
651 static struct snd_card *snd_interwave_card_new(int dev)
652 {
653         struct snd_card *card;
654         struct snd_interwave *iwcard;
655
656         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
657                             sizeof(struct snd_interwave));
658         if (card == NULL)
659                 return NULL;
660         iwcard = card->private_data;
661         iwcard->card = card;
662         iwcard->irq = -1;
663         card->private_free = snd_interwave_free;
664         return card;
665 }
666
667 static int __devinit snd_interwave_probe(struct snd_card *card, int dev)
668 {
669         int xirq, xdma1, xdma2;
670         struct snd_interwave *iwcard = card->private_data;
671         struct snd_cs4231 *cs4231;
672         struct snd_gus_card *gus;
673 #ifdef SNDRV_STB
674         struct snd_i2c_bus *i2c_bus;
675 #endif
676         struct snd_pcm *pcm;
677         char *str;
678         int err;
679
680         xirq = irq[dev];
681         xdma1 = dma1[dev];
682         xdma2 = dma2[dev];
683
684         if ((err = snd_gus_create(card,
685                                   port[dev],
686                                   -xirq, xdma1, xdma2,
687                                   0, 32,
688                                   pcm_channels[dev], effect[dev], &gus)) < 0)
689                 return err;
690
691         if ((err = snd_interwave_detect(iwcard, gus, dev
692 #ifdef SNDRV_STB
693             , &i2c_bus
694 #endif
695             )) < 0)
696                 return err;
697
698         iwcard->gus_status_reg = gus->gf1.reg_irqstat;
699         iwcard->pcm_status_reg = gus->gf1.port + 0x10c + 2;
700
701         snd_interwave_init(dev, gus);
702         snd_interwave_detect_memory(gus);
703         if ((err = snd_gus_initialize(gus)) < 0)
704                 return err;
705
706         if (request_irq(xirq, snd_interwave_interrupt, SA_INTERRUPT,
707                         "InterWave", iwcard)) {
708                 snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq);
709                 return -EBUSY;
710         }
711         iwcard->irq = xirq;
712
713         if ((err = snd_cs4231_create(card,
714                                      gus->gf1.port + 0x10c, -1, xirq,
715                                      xdma2 < 0 ? xdma1 : xdma2, xdma1,
716                                      CS4231_HW_INTERWAVE,
717                                      CS4231_HWSHARE_IRQ |
718                                      CS4231_HWSHARE_DMA1 |
719                                      CS4231_HWSHARE_DMA2,
720                                      &cs4231)) < 0)
721                 return err;
722
723         if ((err = snd_cs4231_pcm(cs4231, 0, &pcm)) < 0)
724                 return err;
725
726         sprintf(pcm->name + strlen(pcm->name), " rev %c", gus->revision + 'A');
727         strcat(pcm->name, " (codec)");
728
729         if ((err = snd_cs4231_timer(cs4231, 2, NULL)) < 0)
730                 return err;
731
732         if ((err = snd_cs4231_mixer(cs4231)) < 0)
733                 return err;
734
735         if (pcm_channels[dev] > 0) {
736                 if ((err = snd_gf1_pcm_new(gus, 1, 1, NULL)) < 0)
737                         return err;
738         }
739         if ((err = snd_interwave_mixer(cs4231)) < 0)
740                 return err;
741
742 #ifdef SNDRV_STB
743         {
744                 struct snd_ctl_elem_id id1, id2;
745                 memset(&id1, 0, sizeof(id1));
746                 memset(&id2, 0, sizeof(id2));
747                 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
748                 strcpy(id1.name, "Master Playback Switch");
749                 strcpy(id2.name, id1.name);
750                 id2.index = 1;
751                 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
752                         return err;
753                 strcpy(id1.name, "Master Playback Volume");
754                 strcpy(id2.name, id1.name);
755                 if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
756                         return err;
757                 if ((err = snd_tea6330t_update_mixer(card, i2c_bus, 0, 1)) < 0)
758                         return err;
759         }
760 #endif
761
762         gus->uart_enable = midi[dev];
763         if ((err = snd_gf1_rawmidi_new(gus, 0, NULL)) < 0)
764                 return err;
765
766 #ifndef SNDRV_STB
767         str = "AMD InterWave";
768         if (gus->gf1.rom_banks == 1 && gus->gf1.rom_present == 8)
769                 str = "Dynasonic 3-D";
770 #else
771         str = "InterWave STB";
772 #endif
773         strcpy(card->driver, str);
774         strcpy(card->shortname, str);
775         sprintf(card->longname, "%s at 0x%lx, irq %i, dma %d",
776                 str,
777                 gus->gf1.port,
778                 xirq,
779                 xdma1);
780         if (xdma2 >= 0)
781                 sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
782
783         if ((err = snd_card_register(card)) < 0)
784                 return err;
785         
786         iwcard->cs4231 = cs4231;
787         iwcard->gus = gus;
788         return 0;
789 }
790
791 static int __init snd_interwave_nonpnp_probe1(int dev, struct platform_device *devptr)
792 {
793         struct snd_card *card;
794         int err;
795
796         card = snd_interwave_card_new(dev);
797         if (! card)
798                 return -ENOMEM;
799
800         snd_card_set_dev(card, &devptr->dev);
801         if ((err = snd_interwave_probe(card, dev)) < 0) {
802                 snd_card_free(card);
803                 return err;
804         }
805         platform_set_drvdata(devptr, card);
806         return 0;
807 }
808
809 static int __init snd_interwave_nonpnp_probe(struct platform_device *pdev)
810 {
811         int dev = pdev->id;
812         int err;
813         static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1};
814         static int possible_dmas[] = {0, 1, 3, 5, 6, 7, -1};
815
816         if (irq[dev] == SNDRV_AUTO_IRQ) {
817                 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
818                         snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
819                         return -EBUSY;
820                 }
821         }
822         if (dma1[dev] == SNDRV_AUTO_DMA) {
823                 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
824                         snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
825                         return -EBUSY;
826                 }
827         }
828         if (dma2[dev] == SNDRV_AUTO_DMA) {
829                 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
830                         snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
831                         return -EBUSY;
832                 }
833         }
834
835         if (port[dev] != SNDRV_AUTO_PORT)
836                 return snd_interwave_nonpnp_probe1(dev, pdev);
837         else {
838                 static long possible_ports[] = {0x210, 0x220, 0x230, 0x240, 0x250, 0x260};
839                 int i;
840                 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
841                         port[dev] = possible_ports[i];
842                         err = snd_interwave_nonpnp_probe1(dev, pdev);
843                         if (! err)
844                                 return 0;
845                 }
846                 return err;
847         }
848 }
849
850 static int __devexit snd_interwave_nonpnp_remove(struct platform_device *devptr)
851 {
852         snd_card_free(platform_get_drvdata(devptr));
853         platform_set_drvdata(devptr, NULL);
854         return 0;
855 }
856
857 static struct platform_driver snd_interwave_driver = {
858         .probe          = snd_interwave_nonpnp_probe,
859         .remove         = __devexit_p(snd_interwave_nonpnp_remove),
860         /* FIXME: suspend,resume */
861         .driver         = {
862                 .name   = INTERWAVE_DRIVER
863         },
864 };
865
866 #ifdef CONFIG_PNP
867
868 static int __devinit snd_interwave_pnp_detect(struct pnp_card_link *pcard,
869                                               const struct pnp_card_device_id *pid)
870 {
871         static int dev;
872         struct snd_card *card;
873         int res;
874
875         for ( ; dev < SNDRV_CARDS; dev++) {
876                 if (enable[dev] && isapnp[dev])
877                         break;
878         }
879         if (dev >= SNDRV_CARDS)
880                 return -ENODEV;
881                                 
882         card = snd_interwave_card_new(dev);
883         if (! card)
884                 return -ENOMEM;
885
886         if ((res = snd_interwave_pnp(dev, card->private_data, pcard, pid)) < 0) {
887                 snd_card_free(card);
888                 return res;
889         }
890         snd_card_set_dev(card, &pcard->card->dev);
891         if ((res = snd_interwave_probe(card, dev)) < 0) {
892                 snd_card_free(card);
893                 return res;
894         }
895         pnp_set_card_drvdata(pcard, card);
896         dev++;
897         return 0;
898 }
899
900 static void __devexit snd_interwave_pnp_remove(struct pnp_card_link * pcard)
901 {
902         snd_card_free(pnp_get_card_drvdata(pcard));
903         pnp_set_card_drvdata(pcard, NULL);
904 }
905
906 static struct pnp_card_driver interwave_pnpc_driver = {
907         .flags = PNP_DRIVER_RES_DISABLE,
908         .name = INTERWAVE_PNP_DRIVER,
909         .id_table = snd_interwave_pnpids,
910         .probe = snd_interwave_pnp_detect,
911         .remove = __devexit_p(snd_interwave_pnp_remove),
912         /* FIXME: suspend,resume */
913 };
914
915 #endif /* CONFIG_PNP */
916
917 static int __init alsa_card_interwave_init(void)
918 {
919         int i, err, cards = 0;
920
921         if ((err = platform_driver_register(&snd_interwave_driver)) < 0)
922                 return err;
923
924         for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
925                 struct platform_device *device;
926 #ifdef CONFIG_PNP
927                 if (isapnp[i])
928                         continue;
929 #endif
930                 device = platform_device_register_simple(INTERWAVE_DRIVER,
931                                                          i, NULL, 0);
932                 if (IS_ERR(device)) {
933                         err = PTR_ERR(device);
934                         platform_driver_unregister(&snd_interwave_driver);
935                         return err;
936                 }
937                 cards++;
938         }
939
940         /* ISA PnP cards */
941         i = pnp_register_card_driver(&interwave_pnpc_driver);
942         if (i > 0)
943                 cards += i;
944
945         if (!cards) {
946                 pnp_unregister_card_driver(&interwave_pnpc_driver);
947                 platform_driver_unregister(&snd_interwave_driver);
948 #ifdef MODULE
949                 printk(KERN_ERR "InterWave soundcard not found or device busy\n");
950 #endif
951                 return -ENODEV;
952         }
953         return 0;
954 }
955
956 static void __exit alsa_card_interwave_exit(void)
957 {
958         pnp_unregister_card_driver(&interwave_pnpc_driver);
959         platform_driver_unregister(&snd_interwave_driver);
960 }
961
962 module_init(alsa_card_interwave_init)
963 module_exit(alsa_card_interwave_exit)