patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / oss / opl3sa2.c
1 /*
2  * sound/opl3sa2.c
3  *
4  * A low level driver for Yamaha OPL3-SA2 and SA3 cards.
5  * NOTE: All traces of the name OPL3-SAx have now (December 2000) been
6  *       removed from the driver code, as an email exchange with Yamaha
7  *       provided the information that the YMF-719 is indeed just a
8  *       re-badged 715.
9  *
10  * Copyright 1998-2001 Scott Murray <scott@spiteful.org>
11  *
12  * Originally based on the CS4232 driver (in cs4232.c) by Hannu Savolainen
13  * and others.  Now incorporates code/ideas from pss.c, also by Hannu
14  * Savolainen.  Both of those files are distributed with the following
15  * license:
16  *
17  * "Copyright (C) by Hannu Savolainen 1993-1997
18  *
19  *  OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
20  *  Version 2 (June 1991). See the "COPYING" file distributed with this software
21  *  for more info."
22  *
23  * As such, in accordance with the above license, this file, opl3sa2.c, is
24  * distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2 (June 1991).
25  * See the "COPYING" file distributed with this software for more information.
26  *
27  * Change History
28  * --------------
29  * Scott Murray            Original driver (Jun 14, 1998)
30  * Paul J.Y. Lahaie        Changed probing / attach code order
31  * Scott Murray            Added mixer support (Dec 03, 1998)
32  * Scott Murray            Changed detection code to be more forgiving,
33  *                         added force option as last resort,
34  *                         fixed ioctl return values. (Dec 30, 1998)
35  * Scott Murray            Simpler detection code should work all the time now
36  *                         (with thanks to Ben Hutchings for the heuristic),
37  *                         removed now unnecessary force option. (Jan 5, 1999)
38  * Christoph Hellwig       Adapted to module_init/module_exit (Mar 4, 2000)
39  * Scott Murray            Reworked SA2 versus SA3 mixer code, updated chipset
40  *                         version detection code (again!). (Dec 5, 2000)
41  * Scott Murray            Adjusted master volume mixer scaling. (Dec 6, 2000)
42  * Scott Murray            Based on a patch by Joel Yliluoma (aka Bisqwit),
43  *                         integrated wide mixer and adjusted mic, bass, treble
44  *                         scaling. (Dec 6, 2000)
45  * Scott Murray            Based on a patch by Peter Englmaier, integrated
46  *                         ymode and loopback options. (Dec 6, 2000)
47  * Scott Murray            Inspired by a patch by Peter Englmaier, and based on
48  *                         what ALSA does, added initialization code for the
49  *                         default DMA and IRQ settings. (Dec 6, 2000)
50  * Scott Murray            Added some more checks to the card detection code,
51  *                         based on what ALSA does. (Dec 12, 2000)
52  * Scott Murray            Inspired by similar patches from John Fremlin,
53  *                         Jim Radford, Mike Rolig, and Ingmar Steen, added 2.4
54  *                         ISA PnP API support, mainly based on bits from
55  *                         sb_card.c and awe_wave.c. (Dec 12, 2000)
56  * Scott Murray            Some small cleanups to the init code output.
57  *                         (Jan 7, 2001)
58  * Zwane Mwaikambo         Added PM support. (Dec 4 2001)
59  *
60  * Adam Belay              Converted driver to new PnP Layer (Oct 12, 2002)
61  * Zwane Mwaikambo         Code, data structure cleanups. (Feb 15 2002)
62  * Zwane Mwaikambo         Free resources during auxiliary device probe
63  *                         failures (Apr 29 2002)
64  *   
65  */
66
67 #include <linux/config.h>
68 #include <linux/pnp.h>
69 #include <linux/init.h>
70 #include <linux/module.h>
71 #include <linux/delay.h>
72 #include <linux/pm.h>
73 #include "sound_config.h"
74
75 #include "ad1848.h"
76 #include "mpu401.h"
77
78 #define OPL3SA2_MODULE_NAME     "opl3sa2"
79 #define PFX                     OPL3SA2_MODULE_NAME ": "
80
81 /* Useful control port indexes: */
82 #define OPL3SA2_PM           0x01
83 #define OPL3SA2_SYS_CTRL     0x02
84 #define OPL3SA2_IRQ_CONFIG   0x03
85 #define OPL3SA2_DMA_CONFIG   0x06
86 #define OPL3SA2_MASTER_LEFT  0x07
87 #define OPL3SA2_MASTER_RIGHT 0x08
88 #define OPL3SA2_MIC          0x09
89 #define OPL3SA2_MISC         0x0A
90
91 #define OPL3SA3_WIDE         0x14
92 #define OPL3SA3_BASS         0x15
93 #define OPL3SA3_TREBLE       0x16
94
95 /* Useful constants: */
96 #define DEFAULT_VOLUME 50
97 #define DEFAULT_MIC    50
98 #define DEFAULT_TIMBRE 0
99
100 /* Power saving modes */
101 #define OPL3SA2_PM_MODE0        0x00
102 #define OPL3SA2_PM_MODE1        0x04    /* PSV */
103 #define OPL3SA2_PM_MODE2        0x05    /* PSV | PDX */
104 #define OPL3SA2_PM_MODE3        0x27    /* ADOWN | PSV | PDN | PDX */
105
106
107 /* For checking against what the card returns: */
108 #define VERSION_UNKNOWN 0
109 #define VERSION_YMF711  1
110 #define VERSION_YMF715  2
111 #define VERSION_YMF715B 3
112 #define VERSION_YMF715E 4
113 /* also assuming that anything > 4 but <= 7 is a 715E */
114
115 /* Chipset type constants for use below */
116 #define CHIPSET_UNKNOWN -1
117 #define CHIPSET_OPL3SA2 0
118 #define CHIPSET_OPL3SA3 1
119 static const char *CHIPSET_TABLE[] = {"OPL3-SA2", "OPL3-SA3"};
120
121 #ifdef CONFIG_PNP
122 #define OPL3SA2_CARDS_MAX 4
123 #else
124 #define OPL3SA2_CARDS_MAX 1
125 #endif
126
127 /* This should be pretty obvious */
128 static int opl3sa2_cards_num;
129
130 typedef struct {
131         /* device resources */
132         unsigned short cfg_port;
133         struct address_info cfg;
134         struct address_info cfg_mss;
135         struct address_info cfg_mpu;
136 #ifdef CONFIG_PNP
137         /* PnP Stuff */
138         struct pnp_dev* pdev;
139         int activated;                  /* Whether said devices have been activated */
140 #endif
141 #ifdef CONFIG_PM
142         unsigned int    in_suspend;
143         struct pm_dev   *pmdev;
144 #endif
145         unsigned int    card;
146         int             chipset;        /* What's my version(s)? */
147         char            *chipset_name;
148
149         /* mixer data */
150         int             mixer;
151         unsigned int    volume_l;
152         unsigned int    volume_r;
153         unsigned int    mic;
154         unsigned int    bass_l;
155         unsigned int    bass_r;
156         unsigned int    treble_l;
157         unsigned int    treble_r;
158         unsigned int    wide_l;
159         unsigned int    wide_r;
160 } opl3sa2_state_t;
161 static opl3sa2_state_t opl3sa2_state[OPL3SA2_CARDS_MAX];
162
163         
164
165 /* Our parameters */
166 static int __initdata io        = -1;
167 static int __initdata mss_io    = -1;
168 static int __initdata mpu_io    = -1;
169 static int __initdata irq       = -1;
170 static int __initdata dma       = -1;
171 static int __initdata dma2      = -1;
172 static int __initdata ymode     = -1;
173 static int __initdata loopback  = -1;
174
175 #ifdef CONFIG_PNP
176 /* PnP specific parameters */
177 static int __initdata isapnp = 1;
178 static int __initdata multiple = 1;
179
180 /* PnP devices */
181 struct pnp_dev* opl3sa2_dev[OPL3SA2_CARDS_MAX];
182
183 /* Whether said devices have been activated */
184 static int opl3sa2_activated[OPL3SA2_CARDS_MAX];
185 #else
186 static int __initdata isapnp; /* = 0 */
187 static int __initdata multiple; /* = 0 */
188 #endif
189
190 MODULE_DESCRIPTION("Module for OPL3-SA2 and SA3 sound cards (uses AD1848 MSS driver).");
191 MODULE_AUTHOR("Scott Murray <scott@spiteful.org>");
192 MODULE_LICENSE("GPL");
193
194
195 MODULE_PARM(io, "i");
196 MODULE_PARM_DESC(io, "Set I/O base of OPL3-SA2 or SA3 card (usually 0x370.  Address must be even and must be from 0x100 to 0xFFE)");
197
198 MODULE_PARM(mss_io, "i");
199 MODULE_PARM_DESC(mss_io, "Set MSS (audio) I/O base (0x530, 0xE80, or other. Address must end in 0 or 4 and must be from 0x530 to 0xF48)");
200
201 MODULE_PARM(mpu_io, "i");
202 MODULE_PARM_DESC(mpu_io, "Set MIDI I/O base (0x330 or other. Address must be even and must be from 0x300 to 0x334)");
203
204 MODULE_PARM(irq, "i");
205 MODULE_PARM_DESC(mss_irq, "Set MSS (audio) IRQ (5, 7, 9, 10, 11, 12)");
206
207 MODULE_PARM(dma, "i");
208 MODULE_PARM_DESC(dma, "Set MSS (audio) first DMA channel (0, 1, 3)");
209
210 MODULE_PARM(dma2, "i");
211 MODULE_PARM_DESC(dma2, "Set MSS (audio) second DMA channel (0, 1, 3)");
212
213 MODULE_PARM(ymode, "i");
214 MODULE_PARM_DESC(ymode, "Set Yamaha 3D enhancement mode (0 = Desktop/Normal, 1 = Notebook PC (1), 2 = Notebook PC (2), 3 = Hi-Fi)");
215
216 MODULE_PARM(loopback, "i");
217 MODULE_PARM_DESC(loopback, "Set A/D input source. Useful for echo cancellation (0 = Mic Rch (default), 1 = Mono output loopback)");
218
219 #ifdef CONFIG_PNP
220 MODULE_PARM(isapnp, "i");
221 MODULE_PARM_DESC(isapnp, "When set to 0, ISA PnP support will be disabled");
222
223 MODULE_PARM(multiple, "i");
224 MODULE_PARM_DESC(multiple, "When set to 0, will not search for multiple cards");
225 #endif
226
227
228 /*
229  * Standard read and write functions
230 */
231
232 static inline void opl3sa2_write(unsigned short port,
233                                  unsigned char  index,
234                                  unsigned char  data)
235 {
236         outb_p(index, port);
237         outb(data, port + 1);
238 }
239
240
241 static inline void opl3sa2_read(unsigned short port,
242                                 unsigned char  index,
243                                 unsigned char* data)
244 {
245         outb_p(index, port);
246         *data = inb(port + 1);
247 }
248
249
250 /*
251  * All of the mixer functions...
252  */
253
254 static void opl3sa2_set_volume(opl3sa2_state_t* devc, int left, int right)
255 {
256         static unsigned char scale[101] = {
257                 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e,
258                 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0c,
259                 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b,
260                 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09,
261                 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x08,
262                 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
263                 0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
264                 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
265                 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01,
266                 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
267                 0x00
268         };
269         unsigned char vol;
270
271         vol = scale[left];
272
273         /* If level is zero, turn on mute */
274         if(!left)
275                 vol |= 0x80;
276
277         opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_LEFT, vol);
278
279         vol = scale[right];
280
281         /* If level is zero, turn on mute */
282         if(!right)
283                 vol |= 0x80;
284
285         opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_RIGHT, vol);
286 }
287
288
289 static void opl3sa2_set_mic(opl3sa2_state_t* devc, int level)
290 {
291         unsigned char vol = 0x1F;
292
293         if((level >= 0) && (level <= 100))
294                 vol = 0x1F - (unsigned char) (32 * level / 101);
295
296         /* If level is zero, turn on mute */
297         if(!level)
298                 vol |= 0x80;
299
300         opl3sa2_write(devc->cfg_port, OPL3SA2_MIC, vol);
301 }
302
303
304 static void opl3sa3_set_bass(opl3sa2_state_t* devc, int left, int right)
305 {
306         unsigned char bass;
307
308         bass = left ? ((unsigned char) (8 * left / 101)) : 0; 
309         bass |= (right ? ((unsigned char) (8 * right / 101)) : 0) << 4;
310
311         opl3sa2_write(devc->cfg_port, OPL3SA3_BASS, bass);
312 }
313
314
315 static void opl3sa3_set_treble(opl3sa2_state_t* devc, int left, int right)
316 {       
317         unsigned char treble;
318
319         treble = left ? ((unsigned char) (8 * left / 101)) : 0; 
320         treble |= (right ? ((unsigned char) (8 * right / 101)) : 0) << 4;
321
322         opl3sa2_write(devc->cfg_port, OPL3SA3_TREBLE, treble);
323 }
324
325
326
327
328 static void opl3sa2_mixer_reset(opl3sa2_state_t* devc)
329 {
330         if (devc) {
331                 opl3sa2_set_volume(devc, DEFAULT_VOLUME, DEFAULT_VOLUME);
332                 devc->volume_l = devc->volume_r = DEFAULT_VOLUME;
333
334                 opl3sa2_set_mic(devc, DEFAULT_MIC);
335                 devc->mic = DEFAULT_MIC;
336
337                 if (devc->chipset == CHIPSET_OPL3SA3) {
338                         opl3sa3_set_bass(devc, DEFAULT_TIMBRE, DEFAULT_TIMBRE);
339                         devc->bass_l = devc->bass_r = DEFAULT_TIMBRE;
340                         opl3sa3_set_treble(devc, DEFAULT_TIMBRE, DEFAULT_TIMBRE);
341                         devc->treble_l = devc->treble_r = DEFAULT_TIMBRE;
342                 }
343         }
344 }
345
346 /* Currently only used for power management */
347 #ifdef CONFIG_PM
348 static void opl3sa2_mixer_restore(opl3sa2_state_t* devc)
349 {
350         if (devc) {
351                 opl3sa2_set_volume(devc, devc->volume_l, devc->volume_r);
352                 opl3sa2_set_mic(devc, devc->mic);
353
354                 if (devc->chipset == CHIPSET_OPL3SA3) {
355                         opl3sa3_set_bass(devc, devc->bass_l, devc->bass_r);
356                         opl3sa3_set_treble(devc, devc->treble_l, devc->treble_r);
357                 }
358         }
359 }
360 #endif
361
362 static inline void arg_to_vol_mono(unsigned int vol, int* value)
363 {
364         int left;
365         
366         left = vol & 0x00ff;
367         if (left > 100)
368                 left = 100;
369         *value = left;
370 }
371
372
373 static inline void arg_to_vol_stereo(unsigned int vol, int* aleft, int* aright)
374 {
375         arg_to_vol_mono(vol, aleft);
376         arg_to_vol_mono(vol >> 8, aright);
377 }
378
379
380 static inline int ret_vol_mono(int vol)
381 {
382         return ((vol << 8) | vol);
383 }
384
385
386 static inline int ret_vol_stereo(int left, int right)
387 {
388         return ((right << 8) | left);
389 }
390
391
392 static int opl3sa2_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
393 {
394         int retval, value, cmdf = cmd & 0xff;
395         int __user *p = (int __user *)arg;
396
397         opl3sa2_state_t* devc = &opl3sa2_state[dev];
398         
399         switch (cmdf) {
400                 case SOUND_MIXER_VOLUME:
401                 case SOUND_MIXER_MIC:
402                 case SOUND_MIXER_DEVMASK:
403                 case SOUND_MIXER_STEREODEVS: 
404                 case SOUND_MIXER_RECMASK:
405                 case SOUND_MIXER_RECSRC:
406                 case SOUND_MIXER_CAPS: 
407                         break;
408
409                 default:
410                         return -EINVAL;
411         }
412         
413         if (((cmd >> 8) & 0xff) != 'M')
414                 return -EINVAL;
415                 
416         retval = 0;
417         if (_SIOC_DIR (cmd) & _SIOC_WRITE) {
418                 switch (cmdf) {
419                         case SOUND_MIXER_VOLUME:
420                                 retval = get_user(value, (unsigned __user *) arg);
421                                 if (retval)
422                                         break;
423                                 arg_to_vol_stereo(value, &devc->volume_l, &devc->volume_r);
424                                 opl3sa2_set_volume(devc, devc->volume_l, devc->volume_r);
425                                 value = ret_vol_stereo(devc->volume_l, devc->volume_r);
426                                 retval = put_user(value, p);
427                                 break;
428                   
429                         case SOUND_MIXER_MIC:
430                                 retval = get_user(value, (unsigned __user *) arg);
431                                 if (retval)
432                                         break;
433                                 arg_to_vol_mono(value, &devc->mic);
434                                 opl3sa2_set_mic(devc, devc->mic);
435                                 value = ret_vol_mono(devc->mic);
436                                 retval = put_user(value, p);
437                                 break;
438
439                         default:
440                                 retval = -EINVAL;
441                 }
442         }
443         else {
444                 /*
445                  * Return parameters
446                  */
447                 switch (cmdf) {
448                         case SOUND_MIXER_DEVMASK:
449                                 retval = put_user(SOUND_MASK_VOLUME | SOUND_MASK_MIC, p);
450                                 break;
451                   
452                         case SOUND_MIXER_STEREODEVS:
453                                 retval = put_user(SOUND_MASK_VOLUME, p);
454                                 break;
455                   
456                         case SOUND_MIXER_RECMASK:
457                                 /* No recording devices */
458                                 retval = put_user(0, p);
459                                 break;
460
461                         case SOUND_MIXER_CAPS:
462                                 retval = put_user(SOUND_CAP_EXCL_INPUT, p);
463                                 break;
464
465                         case SOUND_MIXER_RECSRC:
466                                 /* No recording source */
467                                 retval = put_user(0, p);
468                                 break;
469
470                         case SOUND_MIXER_VOLUME:
471                                 value = ret_vol_stereo(devc->volume_l, devc->volume_r);
472                                 retval = put_user(value, p);
473                                 break;
474                           
475                         case SOUND_MIXER_MIC:
476                                 value = ret_vol_mono(devc->mic);
477                                 put_user(value, p);
478                                 break;
479
480                         default:
481                                 retval = -EINVAL;
482                 }
483         }
484         return retval;
485 }
486 /* opl3sa2_mixer_ioctl end */
487
488
489 static int opl3sa3_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
490 {
491         int value, retval, cmdf = cmd & 0xff;
492
493         opl3sa2_state_t* devc = &opl3sa2_state[dev];
494
495         switch (cmdf) {
496         case SOUND_MIXER_BASS:
497                 value = ret_vol_stereo(devc->bass_l, devc->bass_r);
498                 retval = put_user(value, (int __user *) arg);
499                 break;
500                 
501         case SOUND_MIXER_TREBLE:
502                 value = ret_vol_stereo(devc->treble_l, devc->treble_r);
503                 retval = put_user(value, (int __user *) arg);
504                 break;
505
506         case SOUND_MIXER_DIGITAL1:
507                 value = ret_vol_stereo(devc->wide_l, devc->wide_r);
508                 retval = put_user(value, (int __user *) arg);
509                 break;
510
511         default:
512                 retval = -EINVAL;
513         }
514         return retval;
515 }
516 /* opl3sa3_mixer_ioctl end */
517
518
519 static struct mixer_operations opl3sa2_mixer_operations =
520 {
521         .owner  = THIS_MODULE,
522         .id     = "OPL3-SA2",
523         .name   = "Yamaha OPL3-SA2",
524         .ioctl  = opl3sa2_mixer_ioctl
525 };
526
527 static struct mixer_operations opl3sa3_mixer_operations =
528 {
529         .owner  = THIS_MODULE,
530         .id     = "OPL3-SA3",
531         .name   = "Yamaha OPL3-SA3",
532         .ioctl  = opl3sa3_mixer_ioctl
533 };
534
535 /* End of mixer-related stuff */
536
537
538 /*
539  * Component probe, attach, unload functions
540  */
541
542 static inline int __init probe_opl3sa2_mpu(struct address_info* hw_config)
543 {
544         return probe_mpu401(hw_config);
545 }
546
547
548 static inline int __init attach_opl3sa2_mpu(struct address_info* hw_config)
549 {
550         return attach_mpu401(hw_config, THIS_MODULE);
551 }
552
553
554 static inline void __exit unload_opl3sa2_mpu(struct address_info *hw_config)
555 {
556         unload_mpu401(hw_config);
557 }
558
559
560 static inline int __init probe_opl3sa2_mss(struct address_info* hw_config)
561 {
562         return probe_ms_sound(hw_config);
563 }
564
565
566 static void __init attach_opl3sa2_mss(struct address_info* hw_config)
567 {
568         int initial_mixers;
569
570         initial_mixers = num_mixers;
571         attach_ms_sound(hw_config, THIS_MODULE);        /* Slot 0 */
572         if (hw_config->slots[0] != -1) {
573                 /* Did the MSS driver install? */
574                 if(num_mixers == (initial_mixers + 1)) {
575                         /* The MSS mixer is installed, reroute mixers appropiately */
576                         AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_CD);
577                         AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_SYNTH);
578                         AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
579                 }
580                 else {
581                         printk(KERN_ERR PFX "MSS mixer not installed?\n");
582                 }
583         }
584 }
585
586
587 static inline void __exit unload_opl3sa2_mss(struct address_info* hw_config)
588 {
589         unload_ms_sound(hw_config);
590 }
591
592
593 static int __init probe_opl3sa2(struct address_info* hw_config, int card)
594 {
595         unsigned char misc;
596         unsigned char tmp;
597         unsigned char version;
598
599         /*
600          * Try and allocate our I/O port range.
601          */
602         if (!request_region(hw_config->io_base, 2, OPL3SA2_MODULE_NAME)) {
603                 printk(KERN_ERR PFX "Control I/O port %#x not free\n",
604                        hw_config->io_base);
605                 goto out_nodev;
606         }
607
608         /*
609          * Check if writing to the read-only version bits of the miscellaneous
610          * register succeeds or not (it should not).
611          */
612         opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &misc);
613         opl3sa2_write(hw_config->io_base, OPL3SA2_MISC, misc ^ 0x07);
614         opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &tmp);
615         if(tmp != misc) {
616                 printk(KERN_ERR PFX "Control I/O port %#x is not a YMF7xx chipset!\n",
617                        hw_config->io_base);
618                 goto out_region;
619         }
620
621         /*
622          * Check if the MIC register is accessible.
623          */
624         opl3sa2_read(hw_config->io_base, OPL3SA2_MIC, &tmp);
625         opl3sa2_write(hw_config->io_base, OPL3SA2_MIC, 0x8a);
626         opl3sa2_read(hw_config->io_base, OPL3SA2_MIC, &tmp);
627         if((tmp & 0x9f) != 0x8a) {
628                 printk(KERN_ERR
629                        PFX "Control I/O port %#x is not a YMF7xx chipset!\n",
630                        hw_config->io_base);
631                 goto out_region;
632         }
633         opl3sa2_write(hw_config->io_base, OPL3SA2_MIC, tmp);
634
635         /*
636          * Determine chipset type (SA2 or SA3)
637          *
638          * This is done by looking at the chipset version in the lower 3 bits
639          * of the miscellaneous register.
640          */
641         version = misc & 0x07;
642         printk(KERN_DEBUG PFX "Chipset version = %#x\n", version);
643         switch (version) {
644                 case 0:
645                         opl3sa2_state[card].chipset = CHIPSET_UNKNOWN;
646                         printk(KERN_ERR
647                                PFX "Unknown Yamaha audio controller version\n");
648                         break;
649
650                 case VERSION_YMF711:
651                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA2;
652                         printk(KERN_INFO PFX "Found OPL3-SA2 (YMF711)\n");
653                         break;
654
655                 case VERSION_YMF715:
656                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
657                         printk(KERN_INFO
658                                PFX "Found OPL3-SA3 (YMF715 or YMF719)\n");
659                         break;
660
661                 case VERSION_YMF715B:
662                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
663                         printk(KERN_INFO
664                                PFX "Found OPL3-SA3 (YMF715B or YMF719B)\n");
665                         break;
666
667                 case VERSION_YMF715E:
668                 default:
669                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
670                         printk(KERN_INFO
671                                PFX "Found OPL3-SA3 (YMF715E or YMF719E)\n");
672                         break;
673         }
674
675         if (opl3sa2_state[card].chipset != CHIPSET_UNKNOWN) {
676                 /* Generate a pretty name */
677                 opl3sa2_state[card].chipset_name = (char *)CHIPSET_TABLE[opl3sa2_state[card].chipset];
678                 return 0;
679         }
680
681 out_region:
682         release_region(hw_config->io_base, 2);
683 out_nodev:
684         return -ENODEV;
685 }
686
687
688 static void __init attach_opl3sa2(struct address_info* hw_config, int card)
689 {
690         /* Initialize IRQ configuration to IRQ-B: -, IRQ-A: WSS+MPU+OPL3 */
691         opl3sa2_write(hw_config->io_base, OPL3SA2_IRQ_CONFIG, 0x0d);
692
693         /* Initialize DMA configuration */
694         if(hw_config->dma2 == hw_config->dma) {
695                 /* Want DMA configuration DMA-B: -, DMA-A: WSS-P+WSS-R */
696                 opl3sa2_write(hw_config->io_base, OPL3SA2_DMA_CONFIG, 0x03);
697         }
698         else {
699                 /* Want DMA configuration DMA-B: WSS-R, DMA-A: WSS-P */
700                 opl3sa2_write(hw_config->io_base, OPL3SA2_DMA_CONFIG, 0x21);
701         }
702 }
703
704
705 static void __init attach_opl3sa2_mixer(struct address_info *hw_config, int card)
706 {
707         struct mixer_operations* mixer_operations;
708         opl3sa2_state_t* devc = &opl3sa2_state[card];
709
710         /* Install master mixer */
711         if (devc->chipset == CHIPSET_OPL3SA3) {
712                 mixer_operations = &opl3sa3_mixer_operations;
713         }
714         else {
715                 mixer_operations = &opl3sa2_mixer_operations;
716         }
717
718         devc->cfg_port = hw_config->io_base;
719         devc->mixer = sound_install_mixer(MIXER_DRIVER_VERSION,
720                                           mixer_operations->name,
721                                           mixer_operations,
722                                           sizeof(struct mixer_operations),
723                                           devc);
724         if(devc->mixer < 0) {
725                 printk(KERN_ERR PFX "Could not install %s master mixer\n",
726                          mixer_operations->name);
727         }
728         else {
729                         opl3sa2_mixer_reset(devc);
730
731         }
732 }
733
734
735 static void __init opl3sa2_clear_slots(struct address_info* hw_config)
736 {
737         int i;
738
739         for(i = 0; i < 6; i++) {
740                 hw_config->slots[i] = -1;
741         }
742 }
743
744
745 static void __init opl3sa2_set_ymode(struct address_info* hw_config, int ymode)
746 {
747         /*
748          * Set the Yamaha 3D enhancement mode (aka Ymersion) if asked to and
749          * it's supported.
750          *
751          * 0: Desktop (aka normal)   5-12 cm speakers
752          * 1: Notebook PC mode 1     3 cm speakers
753          * 2: Notebook PC mode 2     1.5 cm speakers
754          * 3: Hi-fi                  16-38 cm speakers
755          */
756         if(ymode >= 0 && ymode <= 3) {
757                 unsigned char sys_ctrl;
758
759                 opl3sa2_read(hw_config->io_base, OPL3SA2_SYS_CTRL, &sys_ctrl);
760                 sys_ctrl = (sys_ctrl & 0xcf) | ((ymode & 3) << 4);
761                 opl3sa2_write(hw_config->io_base, OPL3SA2_SYS_CTRL, sys_ctrl);
762         }
763         else {
764                 printk(KERN_ERR PFX "not setting ymode, it must be one of 0,1,2,3\n");
765         }
766 }
767
768
769 static void __init opl3sa2_set_loopback(struct address_info* hw_config, int loopback)
770 {
771         if(loopback >= 0 && loopback <= 1) {
772                 unsigned char misc;
773
774                 opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &misc);
775                 misc = (misc & 0xef) | ((loopback & 1) << 4);
776                 opl3sa2_write(hw_config->io_base, OPL3SA2_MISC, misc);
777         }
778         else {
779                 printk(KERN_ERR PFX "not setting loopback, it must be either 0 or 1\n");
780         }
781 }
782
783
784 static void __exit unload_opl3sa2(struct address_info* hw_config, int card)
785 {
786         /* Release control ports */
787         release_region(hw_config->io_base, 2);
788
789         /* Unload mixer */
790         if(opl3sa2_state[card].mixer >= 0)
791                 sound_unload_mixerdev(opl3sa2_state[card].mixer);
792
793 }
794
795 #ifdef CONFIG_PNP
796 struct pnp_device_id pnp_opl3sa2_list[] = {
797         {.id = "YMH0021", .driver_data = 0},
798         {.id = ""}
799 };
800
801 MODULE_DEVICE_TABLE(pnp, pnp_opl3sa2_list);
802
803 static int opl3sa2_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
804 {
805         int card = opl3sa2_cards_num;
806
807         /* we don't actually want to return an error as the user may have specified
808          * no multiple card search
809          */
810
811         if (opl3sa2_cards_num == OPL3SA2_CARDS_MAX)
812                 return 0;
813         opl3sa2_activated[card] = 1;
814
815         /* Our own config: */
816         opl3sa2_state[card].cfg.io_base = pnp_port_start(dev, 4);
817         opl3sa2_state[card].cfg.irq     = pnp_irq(dev, 0);
818         opl3sa2_state[card].cfg.dma     = pnp_dma(dev, 0);
819         opl3sa2_state[card].cfg.dma2    = pnp_dma(dev, 1);
820
821         /* The MSS config: */
822         opl3sa2_state[card].cfg_mss.io_base      = pnp_port_start(dev, 1);
823         opl3sa2_state[card].cfg_mss.irq          = pnp_irq(dev, 0);
824         opl3sa2_state[card].cfg_mss.dma          = pnp_dma(dev, 0);
825         opl3sa2_state[card].cfg_mss.dma2         = pnp_dma(dev, 1);
826         opl3sa2_state[card].cfg_mss.card_subtype = 1; /* No IRQ or DMA setup */
827
828         opl3sa2_state[card].cfg_mpu.io_base       = pnp_port_start(dev, 3);
829         opl3sa2_state[card].cfg_mpu.irq           = pnp_irq(dev, 0);
830         opl3sa2_state[card].cfg_mpu.dma           = -1;
831         opl3sa2_state[card].cfg_mpu.dma2          = -1;
832         opl3sa2_state[card].cfg_mpu.always_detect = 1; /* It's there, so use shared IRQs */
833
834         /* Call me paranoid: */
835         opl3sa2_clear_slots(&opl3sa2_state[card].cfg);
836         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mss);
837         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mpu);
838
839         opl3sa2_state[card].pdev = dev;
840         opl3sa2_cards_num++;
841
842         return 0;
843 }
844
845 static struct pnp_driver opl3sa2_driver = {
846         .name           = "opl3sa2",
847         .id_table       = pnp_opl3sa2_list,
848         .probe          = opl3sa2_pnp_probe,
849 };
850
851 #endif /* CONFIG_PNP */
852
853 /* End of component functions */
854
855 #ifdef CONFIG_PM
856 static spinlock_t opl3sa2_lock = SPIN_LOCK_UNLOCKED;
857
858 /* Power Management support functions */
859 static int opl3sa2_suspend(struct pm_dev *pdev, unsigned int pm_mode)
860 {
861         unsigned long flags;
862         opl3sa2_state_t *p;
863
864         if (!pdev)
865                 return -EINVAL;
866
867         spin_lock_irqsave(&opl3sa2_lock,flags);
868
869         p = (opl3sa2_state_t *) pdev->data;
870         switch (pm_mode) {
871         case 1:
872                 pm_mode = OPL3SA2_PM_MODE1;
873                 break;
874         case 2:
875                 pm_mode = OPL3SA2_PM_MODE2;
876                 break;
877         case 3:
878                 pm_mode = OPL3SA2_PM_MODE3;
879                 break;
880         default:
881                 /* we don't know howto handle this... */
882                 spin_unlock_irqrestore(&opl3sa2_lock, flags);
883                 return -EBUSY;
884         }
885
886         p->in_suspend = 1;
887
888         /* its supposed to automute before suspending, so we won't bother */
889         opl3sa2_write(p->cfg_port, OPL3SA2_PM, pm_mode);
890         /* wait a while for the clock oscillator to stabilise */
891         mdelay(10);
892
893         spin_unlock_irqrestore(&opl3sa2_lock,flags);
894         return 0;
895 }
896
897 static int opl3sa2_resume(struct pm_dev *pdev)
898 {
899         unsigned long flags;
900         opl3sa2_state_t *p;
901
902         if (!pdev)
903                 return -EINVAL;
904
905         p = (opl3sa2_state_t *) pdev->data;
906         spin_lock_irqsave(&opl3sa2_lock,flags);
907
908         /* I don't think this is necessary */
909         opl3sa2_write(p->cfg_port, OPL3SA2_PM, OPL3SA2_PM_MODE0);
910         opl3sa2_mixer_restore(p);
911         p->in_suspend = 0;
912
913         spin_unlock_irqrestore(&opl3sa2_lock,flags);
914         return 0;
915 }
916
917 static int opl3sa2_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data)
918 {
919         unsigned long mode = (unsigned  long)data;
920
921         switch (rqst) {
922                 case PM_SUSPEND:
923                         return opl3sa2_suspend(pdev, mode);
924
925                 case PM_RESUME:
926                         return opl3sa2_resume(pdev);
927         }
928         return 0;
929 }
930 #endif /* CONFIG_PM */
931
932 /*
933  * Install OPL3-SA2 based card(s).
934  *
935  * Need to have ad1848 and mpu401 loaded ready.
936  */
937 static int __init init_opl3sa2(void)
938 {
939         int card, max;
940
941         /* Sanitize isapnp and multiple settings */
942         isapnp = isapnp != 0 ? 1 : 0;
943         multiple = multiple != 0 ? 1 : 0;
944
945         max = (multiple && isapnp) ? OPL3SA2_CARDS_MAX : 1;
946
947 #ifdef CONFIG_PNP
948         if (isapnp){
949                 pnp_register_driver(&opl3sa2_driver);
950                 if(!opl3sa2_cards_num){
951                         printk(KERN_INFO PFX "No PnP cards found\n");
952                         isapnp = 0;
953                 }
954                 max = opl3sa2_cards_num;
955         }
956 #endif
957
958         for (card = 0; card < max; card++) {
959                 /* If a user wants an I/O then assume they meant it */
960                 
961                 if (!isapnp) {
962                         if (io == -1 || irq == -1 || dma == -1 ||
963                             dma2 == -1 || mss_io == -1) {
964                                 printk(KERN_ERR
965                                        PFX "io, mss_io, irq, dma, and dma2 must be set\n");
966                                 return -EINVAL;
967                         }
968                         opl3sa2_cards_num++;
969
970                         /*
971                          * Our own config:
972                          * (NOTE: IRQ and DMA aren't used, so they're set to
973                          *  give pretty output from conf_printf. :)
974                          */
975                         opl3sa2_state[card].cfg.io_base = io;
976                         opl3sa2_state[card].cfg.irq     = irq;
977                         opl3sa2_state[card].cfg.dma     = dma;
978                         opl3sa2_state[card].cfg.dma2    = dma2;
979         
980                         /* The MSS config: */
981                         opl3sa2_state[card].cfg_mss.io_base      = mss_io;
982                         opl3sa2_state[card].cfg_mss.irq          = irq;
983                         opl3sa2_state[card].cfg_mss.dma          = dma;
984                         opl3sa2_state[card].cfg_mss.dma2         = dma2;
985                         opl3sa2_state[card].cfg_mss.card_subtype = 1; /* No IRQ or DMA setup */
986
987                         opl3sa2_state[card].cfg_mpu.io_base       = mpu_io;
988                         opl3sa2_state[card].cfg_mpu.irq           = irq;
989                         opl3sa2_state[card].cfg_mpu.dma           = -1;
990                         opl3sa2_state[card].cfg_mpu.always_detect = 1; /* Use shared IRQs */
991
992                         /* Call me paranoid: */
993                         opl3sa2_clear_slots(&opl3sa2_state[card].cfg);
994                         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mss);
995                         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mpu);
996                 }
997
998                 if (probe_opl3sa2(&opl3sa2_state[card].cfg, card))
999                         return -ENODEV;
1000
1001
1002                 if (!probe_opl3sa2_mss(&opl3sa2_state[card].cfg_mss)) {
1003                         /*
1004                          * If one or more cards are already registered, don't
1005                          * return an error but print a warning.  Note, this
1006                          * should never really happen unless the hardware or
1007                          * ISA PnP screwed up.
1008                          */
1009                         release_region(opl3sa2_state[card].cfg.io_base, 2);
1010
1011                         if (opl3sa2_cards_num) {
1012                                 printk(KERN_WARNING
1013                                        PFX "There was a problem probing one "
1014                                        " of the ISA PNP cards, continuing\n");
1015                                 opl3sa2_cards_num--;
1016                                 continue;
1017                         } else
1018                                 return -ENODEV;
1019                 }
1020
1021                 attach_opl3sa2(&opl3sa2_state[card].cfg, card);
1022                 conf_printf(opl3sa2_state[card].chipset_name, &opl3sa2_state[card].cfg);
1023                 attach_opl3sa2_mixer(&opl3sa2_state[card].cfg, card);
1024                 attach_opl3sa2_mss(&opl3sa2_state[card].cfg_mss);
1025
1026                 /* ewww =) */
1027                 opl3sa2_state[card].card = card;
1028 #ifdef CONFIG_PM
1029                 /* register our power management capabilities */
1030                 opl3sa2_state[card].pmdev = pm_register(PM_ISA_DEV, card, opl3sa2_pm_callback);
1031                 if (opl3sa2_state[card].pmdev)
1032                         opl3sa2_state[card].pmdev->data = &opl3sa2_state[card];
1033 #endif /* CONFIG_PM */
1034
1035                 /*
1036                  * Set the Yamaha 3D enhancement mode (aka Ymersion) if asked to and
1037                  * it's supported.
1038                  */
1039                 if (ymode != -1) {
1040                         if (opl3sa2_state[card].chipset == CHIPSET_OPL3SA2) {
1041                                 printk(KERN_ERR
1042                                        PFX "ymode not supported on OPL3-SA2\n");
1043                         }
1044                         else {
1045                                 opl3sa2_set_ymode(&opl3sa2_state[card].cfg, ymode);
1046                         }
1047                 }
1048
1049
1050                 /* Set A/D input to Mono loopback if asked to. */
1051                 if (loopback != -1) {
1052                         opl3sa2_set_loopback(&opl3sa2_state[card].cfg, loopback);
1053                 }
1054                 
1055                 /* Attach MPU if we've been asked to do so, failure isn't fatal */
1056                 if (opl3sa2_state[card].cfg_mpu.io_base != -1) {
1057                         if (probe_opl3sa2_mpu(&opl3sa2_state[card].cfg_mpu)) {
1058                                 if (attach_opl3sa2_mpu(&opl3sa2_state[card].cfg_mpu)) {
1059                                         printk(KERN_ERR PFX "failed to attach MPU401\n");
1060                                         opl3sa2_state[card].cfg_mpu.slots[1] = -1;
1061                                 }
1062                         }
1063                 }
1064         }
1065
1066         if (isapnp) {
1067                 printk(KERN_NOTICE PFX "%d PnP card(s) found.\n", opl3sa2_cards_num);
1068         }
1069
1070         return 0;
1071 }
1072
1073
1074 /*
1075  * Uninstall OPL3-SA2 based card(s).
1076  */
1077 static void __exit cleanup_opl3sa2(void)
1078 {
1079         int card;
1080
1081         for(card = 0; card < opl3sa2_cards_num; card++) {
1082 #ifdef CONFIG_PM
1083                 if (opl3sa2_state[card].pmdev)
1084                         pm_unregister(opl3sa2_state[card].pmdev);
1085 #endif
1086                 if (opl3sa2_state[card].cfg_mpu.slots[1] != -1) {
1087                         unload_opl3sa2_mpu(&opl3sa2_state[card].cfg_mpu);
1088                 }
1089                 unload_opl3sa2_mss(&opl3sa2_state[card].cfg_mss);
1090                 unload_opl3sa2(&opl3sa2_state[card].cfg, card);
1091 #ifdef CONFIG_PNP
1092                 pnp_unregister_driver(&opl3sa2_driver);
1093 #endif
1094         }
1095 }
1096
1097 module_init(init_opl3sa2);
1098 module_exit(cleanup_opl3sa2);
1099
1100 #ifndef MODULE
1101 static int __init setup_opl3sa2(char *str)
1102 {
1103         /* io, irq, dma, dma2,... */
1104 #ifdef CONFIG_PNP
1105         int ints[11];
1106 #else
1107         int ints[9];
1108 #endif
1109         str = get_options(str, ARRAY_SIZE(ints), ints);
1110         
1111         io       = ints[1];
1112         irq      = ints[2];
1113         dma      = ints[3];
1114         dma2     = ints[4];
1115         mss_io   = ints[5];
1116         mpu_io   = ints[6];
1117         ymode    = ints[7];
1118         loopback = ints[8];
1119 #ifdef CONFIG_PNP
1120         isapnp   = ints[9];
1121         multiple = ints[10];
1122 #endif
1123         return 1;
1124 }
1125
1126 __setup("opl3sa2=", setup_opl3sa2);
1127 #endif