ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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, caddr_t arg)
393 {
394         int retval, value, cmdf = cmd & 0xff;
395
396         opl3sa2_state_t* devc = &opl3sa2_state[dev];
397         
398         switch (cmdf) {
399                 case SOUND_MIXER_VOLUME:
400                 case SOUND_MIXER_MIC:
401                 case SOUND_MIXER_DEVMASK:
402                 case SOUND_MIXER_STEREODEVS: 
403                 case SOUND_MIXER_RECMASK:
404                 case SOUND_MIXER_RECSRC:
405                 case SOUND_MIXER_CAPS: 
406                         break;
407
408                 default:
409                         return -EINVAL;
410         }
411         
412         if (((cmd >> 8) & 0xff) != 'M')
413                 return -EINVAL;
414                 
415         retval = 0;
416         if (_SIOC_DIR (cmd) & _SIOC_WRITE) {
417                 switch (cmdf) {
418                         case SOUND_MIXER_VOLUME:
419                                 retval = get_user(value, (unsigned int *) arg);
420                                 if (retval)
421                                         break;
422                                 arg_to_vol_stereo(value, &devc->volume_l, &devc->volume_r);
423                                 opl3sa2_set_volume(devc, devc->volume_l, devc->volume_r);
424                                 value = ret_vol_stereo(devc->volume_l, devc->volume_r);
425                                 retval = put_user(value, (int *) arg);
426                                 break;
427                   
428                         case SOUND_MIXER_MIC:
429                                 retval = get_user(value, (unsigned int *) arg);
430                                 if (retval)
431                                         break;
432                                 arg_to_vol_mono(value, &devc->mic);
433                                 opl3sa2_set_mic(devc, devc->mic);
434                                 value = ret_vol_mono(devc->mic);
435                                 retval = put_user(value, (int *) arg);
436                                 break;
437
438                         default:
439                                 retval = -EINVAL;
440                 }
441         }
442         else {
443                 /*
444                  * Return parameters
445                  */
446                 switch (cmdf) {
447                         case SOUND_MIXER_DEVMASK:
448                                 retval = put_user(SOUND_MASK_VOLUME | SOUND_MASK_MIC, (int *) arg);
449                                 break;
450                   
451                         case SOUND_MIXER_STEREODEVS:
452                                 retval = put_user(SOUND_MASK_VOLUME, (int *) arg);
453                                 break;
454                   
455                         case SOUND_MIXER_RECMASK:
456                                 /* No recording devices */
457                                 retval = put_user(0, (int *) arg);
458                                 break;
459
460                         case SOUND_MIXER_CAPS:
461                                 retval = put_user(SOUND_CAP_EXCL_INPUT, (int *) arg);
462                                 break;
463
464                         case SOUND_MIXER_RECSRC:
465                                 /* No recording source */
466                                 retval = put_user(0, (int *) arg);
467                                 break;
468
469                         case SOUND_MIXER_VOLUME:
470                                 value = ret_vol_stereo(devc->volume_l, devc->volume_r);
471                                 retval = put_user(value, (int *) arg);
472                                 break;
473                           
474                         case SOUND_MIXER_MIC:
475                                 value = ret_vol_mono(devc->mic);
476                                 put_user(value, (int *) arg);
477                                 break;
478
479                         default:
480                                 retval = -EINVAL;
481                 }
482         }
483         return retval;
484 }
485 /* opl3sa2_mixer_ioctl end */
486
487
488 static int opl3sa3_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
489 {
490         int value, retval, cmdf = cmd & 0xff;
491
492         opl3sa2_state_t* devc = &opl3sa2_state[dev];
493
494         switch (cmdf) {
495         case SOUND_MIXER_BASS:
496                 value = ret_vol_stereo(devc->bass_l, devc->bass_r);
497                 retval = put_user(value, (int *) arg);
498                 break;
499                 
500         case SOUND_MIXER_TREBLE:
501                 value = ret_vol_stereo(devc->treble_l, devc->treble_r);
502                 retval = put_user(value, (int *) arg);
503                 break;
504
505         case SOUND_MIXER_DIGITAL1:
506                 value = ret_vol_stereo(devc->wide_l, devc->wide_r);
507                 retval = put_user(value, (int *) arg);
508                 break;
509
510         default:
511                 retval = -EINVAL;
512         }
513         return retval;
514 }
515 /* opl3sa3_mixer_ioctl end */
516
517
518 static struct mixer_operations opl3sa2_mixer_operations =
519 {
520         .owner  = THIS_MODULE,
521         .id     = "OPL3-SA2",
522         .name   = "Yamaha OPL3-SA2",
523         .ioctl  = opl3sa2_mixer_ioctl
524 };
525
526 static struct mixer_operations opl3sa3_mixer_operations =
527 {
528         .owner  = THIS_MODULE,
529         .id     = "OPL3-SA3",
530         .name   = "Yamaha OPL3-SA3",
531         .ioctl  = opl3sa3_mixer_ioctl
532 };
533
534 /* End of mixer-related stuff */
535
536
537 /*
538  * Component probe, attach, unload functions
539  */
540
541 static inline int __init probe_opl3sa2_mpu(struct address_info* hw_config)
542 {
543         return probe_mpu401(hw_config);
544 }
545
546
547 static inline int __init attach_opl3sa2_mpu(struct address_info* hw_config)
548 {
549         return attach_mpu401(hw_config, THIS_MODULE);
550 }
551
552
553 static inline void __exit unload_opl3sa2_mpu(struct address_info *hw_config)
554 {
555         unload_mpu401(hw_config);
556 }
557
558
559 static inline int __init probe_opl3sa2_mss(struct address_info* hw_config)
560 {
561         return probe_ms_sound(hw_config);
562 }
563
564
565 static void __init attach_opl3sa2_mss(struct address_info* hw_config)
566 {
567         int initial_mixers;
568
569         initial_mixers = num_mixers;
570         attach_ms_sound(hw_config, THIS_MODULE);        /* Slot 0 */
571         if (hw_config->slots[0] != -1) {
572                 /* Did the MSS driver install? */
573                 if(num_mixers == (initial_mixers + 1)) {
574                         /* The MSS mixer is installed, reroute mixers appropiately */
575                         AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_CD);
576                         AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_SYNTH);
577                         AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
578                 }
579                 else {
580                         printk(KERN_ERR PFX "MSS mixer not installed?\n");
581                 }
582         }
583 }
584
585
586 static inline void __exit unload_opl3sa2_mss(struct address_info* hw_config)
587 {
588         unload_ms_sound(hw_config);
589 }
590
591
592 static int __init probe_opl3sa2(struct address_info* hw_config, int card)
593 {
594         unsigned char misc;
595         unsigned char tmp;
596         unsigned char version;
597
598         /*
599          * Try and allocate our I/O port range.
600          */
601         if (!request_region(hw_config->io_base, 2, OPL3SA2_MODULE_NAME)) {
602                 printk(KERN_ERR PFX "Control I/O port %#x not free\n",
603                        hw_config->io_base);
604                 goto out_nodev;
605         }
606
607         /*
608          * Check if writing to the read-only version bits of the miscellaneous
609          * register succeeds or not (it should not).
610          */
611         opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &misc);
612         opl3sa2_write(hw_config->io_base, OPL3SA2_MISC, misc ^ 0x07);
613         opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &tmp);
614         if(tmp != misc) {
615                 printk(KERN_ERR PFX "Control I/O port %#x is not a YMF7xx chipset!\n",
616                        hw_config->io_base);
617                 goto out_region;
618         }
619
620         /*
621          * Check if the MIC register is accessible.
622          */
623         opl3sa2_read(hw_config->io_base, OPL3SA2_MIC, &tmp);
624         opl3sa2_write(hw_config->io_base, OPL3SA2_MIC, 0x8a);
625         opl3sa2_read(hw_config->io_base, OPL3SA2_MIC, &tmp);
626         if((tmp & 0x9f) != 0x8a) {
627                 printk(KERN_ERR
628                        PFX "Control I/O port %#x is not a YMF7xx chipset!\n",
629                        hw_config->io_base);
630                 goto out_region;
631         }
632         opl3sa2_write(hw_config->io_base, OPL3SA2_MIC, tmp);
633
634         /*
635          * Determine chipset type (SA2 or SA3)
636          *
637          * This is done by looking at the chipset version in the lower 3 bits
638          * of the miscellaneous register.
639          */
640         version = misc & 0x07;
641         printk(KERN_DEBUG PFX "Chipset version = %#x\n", version);
642         switch (version) {
643                 case 0:
644                         opl3sa2_state[card].chipset = CHIPSET_UNKNOWN;
645                         printk(KERN_ERR
646                                PFX "Unknown Yamaha audio controller version\n");
647                         break;
648
649                 case VERSION_YMF711:
650                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA2;
651                         printk(KERN_INFO PFX "Found OPL3-SA2 (YMF711)\n");
652                         break;
653
654                 case VERSION_YMF715:
655                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
656                         printk(KERN_INFO
657                                PFX "Found OPL3-SA3 (YMF715 or YMF719)\n");
658                         break;
659
660                 case VERSION_YMF715B:
661                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
662                         printk(KERN_INFO
663                                PFX "Found OPL3-SA3 (YMF715B or YMF719B)\n");
664                         break;
665
666                 case VERSION_YMF715E:
667                 default:
668                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
669                         printk(KERN_INFO
670                                PFX "Found OPL3-SA3 (YMF715E or YMF719E)\n");
671                         break;
672         }
673
674         if (opl3sa2_state[card].chipset != CHIPSET_UNKNOWN) {
675                 /* Generate a pretty name */
676                 opl3sa2_state[card].chipset_name = (char *)CHIPSET_TABLE[opl3sa2_state[card].chipset];
677                 return 0;
678         }
679
680 out_region:
681         release_region(hw_config->io_base, 2);
682 out_nodev:
683         return -ENODEV;
684 }
685
686
687 static void __init attach_opl3sa2(struct address_info* hw_config, int card)
688 {
689         /* Initialize IRQ configuration to IRQ-B: -, IRQ-A: WSS+MPU+OPL3 */
690         opl3sa2_write(hw_config->io_base, OPL3SA2_IRQ_CONFIG, 0x0d);
691
692         /* Initialize DMA configuration */
693         if(hw_config->dma2 == hw_config->dma) {
694                 /* Want DMA configuration DMA-B: -, DMA-A: WSS-P+WSS-R */
695                 opl3sa2_write(hw_config->io_base, OPL3SA2_DMA_CONFIG, 0x03);
696         }
697         else {
698                 /* Want DMA configuration DMA-B: WSS-R, DMA-A: WSS-P */
699                 opl3sa2_write(hw_config->io_base, OPL3SA2_DMA_CONFIG, 0x21);
700         }
701 }
702
703
704 static void __init attach_opl3sa2_mixer(struct address_info *hw_config, int card)
705 {
706         struct mixer_operations* mixer_operations;
707         opl3sa2_state_t* devc = &opl3sa2_state[card];
708
709         /* Install master mixer */
710         if (devc->chipset == CHIPSET_OPL3SA3) {
711                 mixer_operations = &opl3sa3_mixer_operations;
712         }
713         else {
714                 mixer_operations = &opl3sa2_mixer_operations;
715         }
716
717         devc->cfg_port = hw_config->io_base;
718         devc->mixer = sound_install_mixer(MIXER_DRIVER_VERSION,
719                                           mixer_operations->name,
720                                           mixer_operations,
721                                           sizeof(struct mixer_operations),
722                                           devc);
723         if(devc->mixer < 0) {
724                 printk(KERN_ERR PFX "Could not install %s master mixer\n",
725                          mixer_operations->name);
726         }
727         else {
728                         opl3sa2_mixer_reset(devc);
729
730         }
731 }
732
733
734 static void __init opl3sa2_clear_slots(struct address_info* hw_config)
735 {
736         int i;
737
738         for(i = 0; i < 6; i++) {
739                 hw_config->slots[i] = -1;
740         }
741 }
742
743
744 static void __init opl3sa2_set_ymode(struct address_info* hw_config, int ymode)
745 {
746         /*
747          * Set the Yamaha 3D enhancement mode (aka Ymersion) if asked to and
748          * it's supported.
749          *
750          * 0: Desktop (aka normal)   5-12 cm speakers
751          * 1: Notebook PC mode 1     3 cm speakers
752          * 2: Notebook PC mode 2     1.5 cm speakers
753          * 3: Hi-fi                  16-38 cm speakers
754          */
755         if(ymode >= 0 && ymode <= 3) {
756                 unsigned char sys_ctrl;
757
758                 opl3sa2_read(hw_config->io_base, OPL3SA2_SYS_CTRL, &sys_ctrl);
759                 sys_ctrl = (sys_ctrl & 0xcf) | ((ymode & 3) << 4);
760                 opl3sa2_write(hw_config->io_base, OPL3SA2_SYS_CTRL, sys_ctrl);
761         }
762         else {
763                 printk(KERN_ERR PFX "not setting ymode, it must be one of 0,1,2,3\n");
764         }
765 }
766
767
768 static void __init opl3sa2_set_loopback(struct address_info* hw_config, int loopback)
769 {
770         if(loopback >= 0 && loopback <= 1) {
771                 unsigned char misc;
772
773                 opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &misc);
774                 misc = (misc & 0xef) | ((loopback & 1) << 4);
775                 opl3sa2_write(hw_config->io_base, OPL3SA2_MISC, misc);
776         }
777         else {
778                 printk(KERN_ERR PFX "not setting loopback, it must be either 0 or 1\n");
779         }
780 }
781
782
783 static void __exit unload_opl3sa2(struct address_info* hw_config, int card)
784 {
785         /* Release control ports */
786         release_region(hw_config->io_base, 2);
787
788         /* Unload mixer */
789         if(opl3sa2_state[card].mixer >= 0)
790                 sound_unload_mixerdev(opl3sa2_state[card].mixer);
791
792 }
793
794 #ifdef CONFIG_PNP
795 struct pnp_device_id pnp_opl3sa2_list[] = {
796         {.id = "YMH0021", .driver_data = 0},
797         {.id = ""}
798 };
799
800 MODULE_DEVICE_TABLE(pnp, pnp_opl3sa2_list);
801
802 static int opl3sa2_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
803 {
804         int card = opl3sa2_cards_num;
805
806         /* we don't actually want to return an error as the user may have specified
807          * no multiple card search
808          */
809
810         if (opl3sa2_cards_num == OPL3SA2_CARDS_MAX)
811                 return 0;
812         opl3sa2_activated[card] = 1;
813
814         /* Our own config: */
815         opl3sa2_state[card].cfg.io_base = pnp_port_start(dev, 4);
816         opl3sa2_state[card].cfg.irq     = pnp_irq(dev, 0);
817         opl3sa2_state[card].cfg.dma     = pnp_dma(dev, 0);
818         opl3sa2_state[card].cfg.dma2    = pnp_dma(dev, 1);
819
820         /* The MSS config: */
821         opl3sa2_state[card].cfg_mss.io_base      = pnp_port_start(dev, 1);
822         opl3sa2_state[card].cfg_mss.irq          = pnp_irq(dev, 0);
823         opl3sa2_state[card].cfg_mss.dma          = pnp_dma(dev, 0);
824         opl3sa2_state[card].cfg_mss.dma2         = pnp_dma(dev, 1);
825         opl3sa2_state[card].cfg_mss.card_subtype = 1; /* No IRQ or DMA setup */
826
827         opl3sa2_state[card].cfg_mpu.io_base       = pnp_port_start(dev, 3);
828         opl3sa2_state[card].cfg_mpu.irq           = pnp_irq(dev, 0);
829         opl3sa2_state[card].cfg_mpu.dma           = -1;
830         opl3sa2_state[card].cfg_mpu.dma2          = -1;
831         opl3sa2_state[card].cfg_mpu.always_detect = 1; /* It's there, so use shared IRQs */
832
833         /* Call me paranoid: */
834         opl3sa2_clear_slots(&opl3sa2_state[card].cfg);
835         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mss);
836         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mpu);
837
838         opl3sa2_state[card].pdev = dev;
839         opl3sa2_cards_num++;
840
841         return 0;
842 }
843
844 static struct pnp_driver opl3sa2_driver = {
845         .name           = "opl3sa2",
846         .id_table       = pnp_opl3sa2_list,
847         .probe          = opl3sa2_pnp_probe,
848 };
849
850 #endif /* CONFIG_PNP */
851
852 /* End of component functions */
853
854 #ifdef CONFIG_PM
855 static spinlock_t opl3sa2_lock = SPIN_LOCK_UNLOCKED;
856
857 /* Power Management support functions */
858 static int opl3sa2_suspend(struct pm_dev *pdev, unsigned int pm_mode)
859 {
860         unsigned long flags;
861         opl3sa2_state_t *p;
862
863         if (!pdev)
864                 return -EINVAL;
865
866         spin_lock_irqsave(&opl3sa2_lock,flags);
867
868         p = (opl3sa2_state_t *) pdev->data;
869         switch (pm_mode) {
870         case 1:
871                 pm_mode = OPL3SA2_PM_MODE1;
872                 break;
873         case 2:
874                 pm_mode = OPL3SA2_PM_MODE2;
875                 break;
876         case 3:
877                 pm_mode = OPL3SA2_PM_MODE3;
878                 break;
879         default:
880                 /* we don't know howto handle this... */
881                 spin_unlock_irqrestore(&opl3sa2_lock, flags);
882                 return -EBUSY;
883         }
884
885         p->in_suspend = 1;
886
887         /* its supposed to automute before suspending, so we won't bother */
888         opl3sa2_write(p->cfg_port, OPL3SA2_PM, pm_mode);
889         /* wait a while for the clock oscillator to stabilise */
890         mdelay(10);
891
892         spin_unlock_irqrestore(&opl3sa2_lock,flags);
893         return 0;
894 }
895
896 static int opl3sa2_resume(struct pm_dev *pdev)
897 {
898         unsigned long flags;
899         opl3sa2_state_t *p;
900
901         if (!pdev)
902                 return -EINVAL;
903
904         p = (opl3sa2_state_t *) pdev->data;
905         spin_lock_irqsave(&opl3sa2_lock,flags);
906
907         /* I don't think this is necessary */
908         opl3sa2_write(p->cfg_port, OPL3SA2_PM, OPL3SA2_PM_MODE0);
909         opl3sa2_mixer_restore(p);
910         p->in_suspend = 0;
911
912         spin_unlock_irqrestore(&opl3sa2_lock,flags);
913         return 0;
914 }
915
916 static int opl3sa2_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data)
917 {
918         unsigned long mode = (unsigned  long)data;
919
920         switch (rqst) {
921                 case PM_SUSPEND:
922                         return opl3sa2_suspend(pdev, mode);
923
924                 case PM_RESUME:
925                         return opl3sa2_resume(pdev);
926         }
927         return 0;
928 }
929 #endif /* CONFIG_PM */
930
931 /*
932  * Install OPL3-SA2 based card(s).
933  *
934  * Need to have ad1848 and mpu401 loaded ready.
935  */
936 static int __init init_opl3sa2(void)
937 {
938         int card, max;
939
940         /* Sanitize isapnp and multiple settings */
941         isapnp = isapnp != 0 ? 1 : 0;
942         multiple = multiple != 0 ? 1 : 0;
943
944         max = (multiple && isapnp) ? OPL3SA2_CARDS_MAX : 1;
945
946 #ifdef CONFIG_PNP
947         if (isapnp){
948                 pnp_register_driver(&opl3sa2_driver);
949                 if(!opl3sa2_cards_num){
950                         printk(KERN_INFO PFX "No PnP cards found\n");
951                         isapnp = 0;
952                 }
953                 max = opl3sa2_cards_num;
954         }
955 #endif
956
957         for (card = 0; card < max; card++) {
958                 /* If a user wants an I/O then assume they meant it */
959                 
960                 if (!isapnp) {
961                         if (io == -1 || irq == -1 || dma == -1 ||
962                             dma2 == -1 || mss_io == -1) {
963                                 printk(KERN_ERR
964                                        PFX "io, mss_io, irq, dma, and dma2 must be set\n");
965                                 return -EINVAL;
966                         }
967                         opl3sa2_cards_num++;
968
969                         /*
970                          * Our own config:
971                          * (NOTE: IRQ and DMA aren't used, so they're set to
972                          *  give pretty output from conf_printf. :)
973                          */
974                         opl3sa2_state[card].cfg.io_base = io;
975                         opl3sa2_state[card].cfg.irq     = irq;
976                         opl3sa2_state[card].cfg.dma     = dma;
977                         opl3sa2_state[card].cfg.dma2    = dma2;
978         
979                         /* The MSS config: */
980                         opl3sa2_state[card].cfg_mss.io_base      = mss_io;
981                         opl3sa2_state[card].cfg_mss.irq          = irq;
982                         opl3sa2_state[card].cfg_mss.dma          = dma;
983                         opl3sa2_state[card].cfg_mss.dma2         = dma2;
984                         opl3sa2_state[card].cfg_mss.card_subtype = 1; /* No IRQ or DMA setup */
985
986                         opl3sa2_state[card].cfg_mpu.io_base       = mpu_io;
987                         opl3sa2_state[card].cfg_mpu.irq           = irq;
988                         opl3sa2_state[card].cfg_mpu.dma           = -1;
989                         opl3sa2_state[card].cfg_mpu.always_detect = 1; /* Use shared IRQs */
990
991                         /* Call me paranoid: */
992                         opl3sa2_clear_slots(&opl3sa2_state[card].cfg);
993                         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mss);
994                         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mpu);
995                 }
996
997                 if (probe_opl3sa2(&opl3sa2_state[card].cfg, card))
998                         return -ENODEV;
999
1000
1001                 if (!probe_opl3sa2_mss(&opl3sa2_state[card].cfg_mss)) {
1002                         /*
1003                          * If one or more cards are already registered, don't
1004                          * return an error but print a warning.  Note, this
1005                          * should never really happen unless the hardware or
1006                          * ISA PnP screwed up.
1007                          */
1008                         release_region(opl3sa2_state[card].cfg.io_base, 2);
1009
1010                         if (opl3sa2_cards_num) {
1011                                 printk(KERN_WARNING
1012                                        PFX "There was a problem probing one "
1013                                        " of the ISA PNP cards, continuing\n");
1014                                 opl3sa2_cards_num--;
1015                                 continue;
1016                         } else
1017                                 return -ENODEV;
1018                 }
1019
1020                 attach_opl3sa2(&opl3sa2_state[card].cfg, card);
1021                 conf_printf(opl3sa2_state[card].chipset_name, &opl3sa2_state[card].cfg);
1022                 attach_opl3sa2_mixer(&opl3sa2_state[card].cfg, card);
1023                 attach_opl3sa2_mss(&opl3sa2_state[card].cfg_mss);
1024
1025                 /* ewww =) */
1026                 opl3sa2_state[card].card = card;
1027 #ifdef CONFIG_PM
1028                 /* register our power management capabilities */
1029                 opl3sa2_state[card].pmdev = pm_register(PM_ISA_DEV, card, opl3sa2_pm_callback);
1030                 if (opl3sa2_state[card].pmdev)
1031                         opl3sa2_state[card].pmdev->data = &opl3sa2_state[card];
1032 #endif /* CONFIG_PM */
1033
1034                 /*
1035                  * Set the Yamaha 3D enhancement mode (aka Ymersion) if asked to and
1036                  * it's supported.
1037                  */
1038                 if (ymode != -1) {
1039                         if (opl3sa2_state[card].chipset == CHIPSET_OPL3SA2) {
1040                                 printk(KERN_ERR
1041                                        PFX "ymode not supported on OPL3-SA2\n");
1042                         }
1043                         else {
1044                                 opl3sa2_set_ymode(&opl3sa2_state[card].cfg, ymode);
1045                         }
1046                 }
1047
1048
1049                 /* Set A/D input to Mono loopback if asked to. */
1050                 if (loopback != -1) {
1051                         opl3sa2_set_loopback(&opl3sa2_state[card].cfg, loopback);
1052                 }
1053                 
1054                 /* Attach MPU if we've been asked to do so, failure isn't fatal */
1055                 if (opl3sa2_state[card].cfg_mpu.io_base != -1) {
1056                         if (probe_opl3sa2_mpu(&opl3sa2_state[card].cfg_mpu)) {
1057                                 if (attach_opl3sa2_mpu(&opl3sa2_state[card].cfg_mpu)) {
1058                                         printk(KERN_ERR PFX "failed to attach MPU401\n");
1059                                         opl3sa2_state[card].cfg_mpu.slots[1] = -1;
1060                                 }
1061                         }
1062                 }
1063         }
1064
1065         if (isapnp) {
1066                 printk(KERN_NOTICE PFX "%d PnP card(s) found.\n", opl3sa2_cards_num);
1067         }
1068
1069         return 0;
1070 }
1071
1072
1073 /*
1074  * Uninstall OPL3-SA2 based card(s).
1075  */
1076 static void __exit cleanup_opl3sa2(void)
1077 {
1078         int card;
1079
1080         for(card = 0; card < opl3sa2_cards_num; card++) {
1081 #ifdef CONFIG_PM
1082                 if (opl3sa2_state[card].pmdev)
1083                         pm_unregister(opl3sa2_state[card].pmdev);
1084 #endif
1085                 if (opl3sa2_state[card].cfg_mpu.slots[1] != -1) {
1086                         unload_opl3sa2_mpu(&opl3sa2_state[card].cfg_mpu);
1087                 }
1088                 unload_opl3sa2_mss(&opl3sa2_state[card].cfg_mss);
1089                 unload_opl3sa2(&opl3sa2_state[card].cfg, card);
1090 #ifdef CONFIG_PNP
1091                 pnp_unregister_driver(&opl3sa2_driver);
1092 #endif
1093         }
1094 }
1095
1096 module_init(init_opl3sa2);
1097 module_exit(cleanup_opl3sa2);
1098
1099 #ifndef MODULE
1100 static int __init setup_opl3sa2(char *str)
1101 {
1102         /* io, irq, dma, dma2,... */
1103 #ifdef CONFIG_PNP
1104         int ints[11];
1105 #else
1106         int ints[9];
1107 #endif
1108         str = get_options(str, ARRAY_SIZE(ints), ints);
1109         
1110         io       = ints[1];
1111         irq      = ints[2];
1112         dma      = ints[3];
1113         dma2     = ints[4];
1114         mss_io   = ints[5];
1115         mpu_io   = ints[6];
1116         ymode    = ints[7];
1117         loopback = ints[8];
1118 #ifdef CONFIG_PNP
1119         isapnp   = ints[9];
1120         multiple = ints[10];
1121 #endif
1122         return 1;
1123 }
1124
1125 __setup("opl3sa2=", setup_opl3sa2);
1126 #endif