Home | History | Annotate | Line # | Download | only in isa
ym.c revision 1.14.4.4
      1 /*	$NetBSD: ym.c,v 1.14.4.4 2002/03/27 10:18:04 he Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999-2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by ITOH Yasufumi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1998 Constantine Sapuntzakis. All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. The name of the author may not be used to endorse or promote products
     51  *    derived from this software without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     54  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     55  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     56  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     57  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     58  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     59  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     60  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     61  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     62  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     63  */
     64 
     65 /*
     66  *  Original code from OpenBSD.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: ym.c,v 1.14.4.4 2002/03/27 10:18:04 he Exp $");
     71 
     72 #include "mpu_ym.h"
     73 #include "opt_ym.h"
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/errno.h>
     78 #include <sys/device.h>
     79 #include <sys/fcntl.h>
     80 #include <sys/kernel.h>
     81 #include <sys/proc.h>
     82 
     83 #include <machine/cpu.h>
     84 #include <machine/intr.h>
     85 #include <machine/bus.h>
     86 
     87 #include <sys/audioio.h>
     88 #include <dev/audio_if.h>
     89 
     90 #include <dev/isa/isavar.h>
     91 #include <dev/isa/isadmavar.h>
     92 
     93 #include <dev/ic/ad1848reg.h>
     94 #include <dev/isa/ad1848var.h>
     95 #include <dev/ic/opl3sa3reg.h>
     96 #include <dev/isa/wssreg.h>
     97 #if NMPU_YM > 0
     98 #include <dev/ic/mpuvar.h>
     99 #endif
    100 #include <dev/isa/ymvar.h>
    101 #include <dev/isa/sbreg.h>
    102 
    103 #ifndef spllowersoftclock
    104  #error "We depend on the new semantics of splsoftclock(9)."
    105 #endif
    106 
    107 /* Power management mode. */
    108 #ifndef YM_POWER_MODE
    109 #define YM_POWER_MODE		YM_POWER_POWERSAVE
    110 #endif
    111 
    112 /* Time in second before power down the chip. */
    113 #ifndef YM_POWER_OFF_SEC
    114 #define YM_POWER_OFF_SEC	5
    115 #endif
    116 
    117 /* Default mixer settings. */
    118 #ifndef YM_VOL_MASTER
    119 #define YM_VOL_MASTER		220
    120 #endif
    121 
    122 #ifndef YM_VOL_DAC
    123 #define YM_VOL_DAC		224
    124 #endif
    125 
    126 #ifndef YM_VOL_OPL3
    127 #define YM_VOL_OPL3		184
    128 #endif
    129 
    130 /*
    131  * The equalizer is ``flat'' if the 3D Enhance is turned off,
    132  * but you can set other default values.
    133  */
    134 #ifndef YM_ENHANCE_TREBLE
    135 #define YM_ENHANCE_TREBLE	0
    136 #endif
    137 #ifndef YM_ENHANCE_BASS
    138 #define YM_ENHANCE_BASS		0
    139 #endif
    140 
    141 #ifdef __i386__		/* XXX */
    142 # include "joy.h"
    143 #else
    144 # define NJOY	0
    145 #endif
    146 
    147 #ifdef AUDIO_DEBUG
    148 #define DPRINTF(x)	if (ymdebug) printf x
    149 int	ymdebug = 0;
    150 #else
    151 #define DPRINTF(x)
    152 #endif
    153 #define DVNAME(softc)	((softc)->sc_ad1848.sc_ad1848.sc_dev.dv_xname)
    154 
    155 int	ym_getdev __P((void *, struct audio_device *));
    156 int	ym_mixer_set_port __P((void *, mixer_ctrl_t *));
    157 int	ym_mixer_get_port __P((void *, mixer_ctrl_t *));
    158 int	ym_query_devinfo __P((void *, mixer_devinfo_t *));
    159 int	ym_intr __P((void *));
    160 #ifndef AUDIO_NO_POWER_CTL
    161 static void ym_save_codec_regs __P((struct ym_softc *));
    162 static void ym_restore_codec_regs __P((struct ym_softc *));
    163 void	ym_power_hook __P((int, void *));
    164 int	ym_codec_power_ctl __P((void *, int));
    165 static void ym_chip_powerdown __P((struct ym_softc *));
    166 static void ym_chip_powerup __P((struct ym_softc *, int));
    167 void ym_powerdown_blocks __P((void *));
    168 void ym_power_ctl __P((struct ym_softc *, int, int));
    169 #endif
    170 
    171 static void ym_init __P((struct ym_softc *));
    172 static void ym_mute __P((struct ym_softc *, int, int));
    173 static void ym_set_master_gain __P((struct ym_softc *, struct ad1848_volume*));
    174 static void ym_hvol_to_master_gain __P((struct ym_softc *));
    175 static void ym_set_mic_gain __P((struct ym_softc *, int));
    176 static void ym_set_3d __P((struct ym_softc *, mixer_ctrl_t *,
    177 	struct ad1848_volume *, int));
    178 
    179 
    180 struct audio_hw_if ym_hw_if = {
    181 	ad1848_isa_open,
    182 	ad1848_isa_close,
    183 	NULL,
    184 	ad1848_query_encoding,
    185 	ad1848_set_params,
    186 	ad1848_round_blocksize,
    187 	ad1848_commit_settings,
    188 	NULL,
    189 	NULL,
    190 	NULL,
    191 	NULL,
    192 	ad1848_isa_halt_output,
    193 	ad1848_isa_halt_input,
    194 	NULL,
    195 	ym_getdev,
    196 	NULL,
    197 	ym_mixer_set_port,
    198 	ym_mixer_get_port,
    199 	ym_query_devinfo,
    200 	ad1848_isa_malloc,
    201 	ad1848_isa_free,
    202 	ad1848_isa_round_buffersize,
    203 	ad1848_isa_mappage,
    204 	ad1848_isa_get_props,
    205 	ad1848_isa_trigger_output,
    206 	ad1848_isa_trigger_input,
    207 };
    208 
    209 static __inline int ym_read __P((struct ym_softc *, int));
    210 static __inline void ym_write __P((struct ym_softc *, int, int));
    211 
    212 void
    213 ym_attach(sc)
    214 	struct ym_softc *sc;
    215 {
    216 	struct ad1848_softc *ac = &sc->sc_ad1848.sc_ad1848;
    217 	static struct ad1848_volume vol_master = {YM_VOL_MASTER, YM_VOL_MASTER};
    218 	static struct ad1848_volume vol_dac    = {YM_VOL_DAC,    YM_VOL_DAC};
    219 	static struct ad1848_volume vol_opl3   = {YM_VOL_OPL3,   YM_VOL_OPL3};
    220 #if YM_ENHANCE_TREBLE || YM_ENHANCE_BASS
    221 	mixer_ctrl_t mctl;
    222 #endif
    223 	struct audio_attach_args arg;
    224 
    225 	callout_init(&sc->sc_powerdown_ch);
    226 
    227 	/* Mute the output to reduce noise during initialization. */
    228 	ym_mute(sc, SA3_VOL_L, 1);
    229 	ym_mute(sc, SA3_VOL_R, 1);
    230 
    231 	sc->sc_version = ym_read(sc, SA3_MISC) & SA3_MISC_VER;
    232 	ac->chip_name = YM_IS_SA3(sc) ? "OPL3-SA3" : "OPL3-SA2";
    233 
    234 	sc->sc_ad1848.sc_ih = isa_intr_establish(sc->sc_ic, sc->ym_irq,
    235 						 IST_EDGE, IPL_AUDIO,
    236 						 ym_intr, sc);
    237 
    238 #ifndef AUDIO_NO_POWER_CTL
    239 	sc->sc_ad1848.powerctl = ym_codec_power_ctl;
    240 	sc->sc_ad1848.powerarg = sc;
    241 #endif
    242 	ad1848_isa_attach(&sc->sc_ad1848);
    243 	printf("\n");
    244 	ac->parent = sc;
    245 
    246 	/* Establish chip in well known mode */
    247 	ym_set_master_gain(sc, &vol_master);
    248 	ym_set_mic_gain(sc, 0);
    249 	sc->master_mute = 0;
    250 
    251 	sc->mic_mute = 1;
    252 	ym_mute(sc, SA3_MIC_VOL, sc->mic_mute);
    253 
    254 	/* Override ad1848 settings. */
    255 	ad1848_set_channel_gain(ac, AD1848_DAC_CHANNEL, &vol_dac);
    256 	ad1848_set_channel_gain(ac, AD1848_AUX2_CHANNEL, &vol_opl3);
    257 
    258 	/*
    259 	 * Mute all external sources.  If you change this, you must
    260 	 * also change the initial value of sc->sc_external_sources
    261 	 * (currently 0 --- no external source is active).
    262 	 */
    263 	ad1848_mute_channel(ac, AD1848_AUX1_CHANNEL, MUTE_ALL);	/* CD */
    264 	ad1848_mute_channel(ac, AD1848_LINE_CHANNEL, MUTE_ALL);	/* line */
    265 	ac->mute[AD1848_AUX1_CHANNEL] = MUTE_ALL;
    266 	ac->mute[AD1848_LINE_CHANNEL] = MUTE_ALL;
    267 	/* speaker is muted by default */
    268 
    269 	/* We use only one IRQ (IRQ-A). */
    270 	ym_write(sc, SA3_IRQ_CONF, SA3_IRQ_CONF_MPU_A | SA3_IRQ_CONF_WSS_A);
    271 	ym_write(sc, SA3_HVOL_INTR_CNF, SA3_HVOL_INTR_CNF_A);
    272 
    273 	/* audio at ym attachment */
    274 	sc->sc_audiodev = audio_attach_mi(&ym_hw_if, ac, &ac->sc_dev);
    275 
    276 	/* opl at ym attachment */
    277 	if (sc->sc_opl_ioh) {
    278 		arg.type = AUDIODEV_TYPE_OPL;
    279 		arg.hwif = 0;
    280 		arg.hdl = 0;
    281 		(void)config_found(&ac->sc_dev, &arg, audioprint);
    282 	}
    283 
    284 #if NMPU_YM > 0
    285 	/* mpu at ym attachment */
    286 	if (sc->sc_mpu_ioh) {
    287 		arg.type = AUDIODEV_TYPE_MPU;
    288 		arg.hwif = 0;
    289 		arg.hdl = 0;
    290 		sc->sc_mpudev = config_found(&ac->sc_dev, &arg, audioprint);
    291 	}
    292 #endif
    293 
    294 	/* This must be AFTER the attachment of sub-devices. */
    295 	ym_init(sc);
    296 
    297 #ifndef AUDIO_NO_POWER_CTL
    298 	/*
    299 	 * Initialize power control.
    300 	 */
    301 	sc->sc_pow_mode = YM_POWER_MODE;
    302 	sc->sc_pow_timeout = YM_POWER_OFF_SEC;
    303 
    304 	sc->sc_on_blocks = sc->sc_turning_off =
    305 		YM_POWER_CODEC_P | YM_POWER_CODEC_R |
    306 		YM_POWER_OPL3 | YM_POWER_MPU401 | YM_POWER_3D |
    307 		YM_POWER_CODEC_DA | YM_POWER_CODEC_AD | YM_POWER_OPL3_DA;
    308 #if NJOY > 0
    309 	sc->sc_on_blocks |= YM_POWER_JOYSTICK;	/* prevents chip powerdown */
    310 #endif
    311 	ym_powerdown_blocks(sc);
    312 
    313 	powerhook_establish(ym_power_hook, sc);
    314 
    315 	if (sc->sc_on_blocks /* & YM_POWER_ACTIVE */)
    316 #endif
    317 	{
    318 		/* Unmute the output now if the chip is on. */
    319 		ym_mute(sc, SA3_VOL_L, sc->master_mute);
    320 		ym_mute(sc, SA3_VOL_R, sc->master_mute);
    321 	}
    322 
    323 #if YM_ENHANCE_TREBLE || YM_ENHANCE_BASS
    324 	/* Set tone control to the default position. */
    325 	mctl.un.value.num_channels = 1;
    326 #if YM_ENHANCE_TREBLE
    327 	mctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = YM_ENHANCE_TREBLE;
    328 	mctl.dev = YM_MASTER_TREBLE;
    329 	ym_mixer_set_port(sc, &mctl);
    330 #endif
    331 #if YM_ENHANCE_BASS
    332 	mctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = YM_ENHANCE_BASS;
    333 	mctl.dev = YM_MASTER_BASS;
    334 	ym_mixer_set_port(sc, &mctl);
    335 #endif
    336 #endif
    337 }
    338 
    339 static __inline int
    340 ym_read(sc, reg)
    341 	struct ym_softc *sc;
    342 	int reg;
    343 {
    344 	bus_space_write_1(sc->sc_iot, sc->sc_controlioh,
    345 				SA3_CTL_INDEX, (reg & 0xff));
    346 	return (bus_space_read_1(sc->sc_iot, sc->sc_controlioh, SA3_CTL_DATA));
    347 }
    348 
    349 static __inline void
    350 ym_write(sc, reg, data)
    351 	struct ym_softc *sc;
    352 	int reg;
    353 	int data;
    354 {
    355 	bus_space_write_1(sc->sc_iot, sc->sc_controlioh,
    356 				SA3_CTL_INDEX, (reg & 0xff));
    357 	bus_space_write_1(sc->sc_iot, sc->sc_controlioh,
    358 				SA3_CTL_DATA, (data & 0xff));
    359 }
    360 
    361 static void
    362 ym_init(sc)
    363 	struct ym_softc *sc;
    364 {
    365 	u_int8_t dpd, apd;
    366 
    367 	/* Mute SoundBlaster output if possible. */
    368 	if (sc->sc_sb_ioh) {
    369 		bus_space_write_1(sc->sc_iot, sc->sc_sb_ioh, SBP_MIXER_ADDR,
    370 				  SBP_MASTER_VOL);
    371 		bus_space_write_1(sc->sc_iot, sc->sc_sb_ioh, SBP_MIXER_DATA,
    372 				  0x00);
    373 	}
    374 
    375 	if (!YM_IS_SA3(sc)) {
    376 		/* OPL3-SA2 */
    377 		ym_write(sc, SA3_PWR_MNG, SA2_PWR_MNG_CLKO |
    378 		    (sc->sc_opl_ioh == 0 ? SA2_PWR_MNG_FMPS : 0));
    379 		return;
    380 	}
    381 
    382 	/* OPL3-SA3 */
    383 	/* Figure out which part can be power down. */
    384 	dpd = SA3_DPWRDWN_SB		/* we never use SB */
    385 #if NMPU_YM > 0
    386 		| (sc->sc_mpu_ioh ? 0 : SA3_DPWRDWN_MPU)
    387 #else
    388 		| SA3_DPWRDWN_MPU
    389 #endif
    390 #if NJOY == 0
    391 		| SA3_DPWRDWN_JOY
    392 #endif
    393 		| SA3_DPWRDWN_PNP	/* ISA Plug and Play is done */
    394 		/*
    395 		 * The master clock is for external wavetable synthesizer
    396 		 * OPL4-ML (YMF704) or OPL4-ML2 (YMF721),
    397 		 * and is currently unused.
    398 		 */
    399 		| SA3_DPWRDWN_MCLKO;
    400 
    401 	apd = SA3_APWRDWN_SBDAC;	/* we never use SB */
    402 
    403 	/* Power down OPL3 if not attached. */
    404 	if (sc->sc_opl_ioh == 0) {
    405 		dpd |= SA3_DPWRDWN_FM;
    406 		apd |= SA3_APWRDWN_FMDAC;
    407 	}
    408 	/* CODEC is always attached. */
    409 
    410 	/* Power down unused digital parts. */
    411 	ym_write(sc, SA3_DPWRDWN, dpd);
    412 
    413 	/* Power down unused analog parts. */
    414 	ym_write(sc, SA3_APWRDWN, apd);
    415 }
    416 
    417 
    418 int
    419 ym_getdev(addr, retp)
    420 	void *addr;
    421 	struct audio_device *retp;
    422 {
    423 	struct ym_softc *sc = addr;
    424 	struct ad1848_softc *ac = &sc->sc_ad1848.sc_ad1848;
    425 
    426 	strcpy(retp->name, ac->chip_name);
    427 	sprintf(retp->version, "%d", sc->sc_version);
    428 	strcpy(retp->config, "ym");
    429 
    430 	return 0;
    431 }
    432 
    433 
    434 static ad1848_devmap_t mappings[] = {
    435 	{ YM_DAC_LVL, AD1848_KIND_LVL, AD1848_DAC_CHANNEL },
    436 	{ YM_MIDI_LVL, AD1848_KIND_LVL, AD1848_AUX2_CHANNEL },
    437 	{ YM_CD_LVL, AD1848_KIND_LVL, AD1848_AUX1_CHANNEL },
    438 	{ YM_LINE_LVL, AD1848_KIND_LVL, AD1848_LINE_CHANNEL },
    439 	{ YM_SPEAKER_LVL, AD1848_KIND_LVL, AD1848_MONO_CHANNEL },
    440 	{ YM_MONITOR_LVL, AD1848_KIND_LVL, AD1848_MONITOR_CHANNEL },
    441 	{ YM_DAC_MUTE, AD1848_KIND_MUTE, AD1848_DAC_CHANNEL },
    442 	{ YM_MIDI_MUTE, AD1848_KIND_MUTE, AD1848_AUX2_CHANNEL },
    443 	{ YM_CD_MUTE, AD1848_KIND_MUTE, AD1848_AUX1_CHANNEL },
    444 	{ YM_LINE_MUTE, AD1848_KIND_MUTE, AD1848_LINE_CHANNEL },
    445 	{ YM_SPEAKER_MUTE, AD1848_KIND_MUTE, AD1848_MONO_CHANNEL },
    446 	{ YM_MONITOR_MUTE, AD1848_KIND_MUTE, AD1848_MONITOR_CHANNEL },
    447 	{ YM_REC_LVL, AD1848_KIND_RECORDGAIN, -1 },
    448 	{ YM_RECORD_SOURCE, AD1848_KIND_RECORDSOURCE, -1}
    449 };
    450 
    451 #define NUMMAP	(sizeof(mappings) / sizeof(mappings[0]))
    452 
    453 
    454 static void
    455 ym_mute(sc, left_reg, mute)
    456 	struct ym_softc *sc;
    457 	int left_reg;
    458 	int mute;
    459 {
    460 	u_int8_t reg;
    461 
    462 	reg = ym_read(sc, left_reg);
    463 	if (mute)
    464 		ym_write(sc, left_reg, reg | 0x80);
    465 	else
    466 		ym_write(sc, left_reg, reg & ~0x80);
    467 }
    468 
    469 
    470 static void
    471 ym_set_master_gain(sc, vol)
    472 	struct ym_softc *sc;
    473 	struct ad1848_volume *vol;
    474 {
    475 	u_int atten;
    476 
    477 	sc->master_gain = *vol;
    478 
    479 	atten = ((AUDIO_MAX_GAIN - vol->left) * (SA3_VOL_MV + 1)) /
    480 		(AUDIO_MAX_GAIN + 1);
    481 
    482 	ym_write(sc, SA3_VOL_L, (ym_read(sc, SA3_VOL_L) & ~SA3_VOL_MV) | atten);
    483 
    484 	atten = ((AUDIO_MAX_GAIN - vol->right) * (SA3_VOL_MV + 1)) /
    485 		(AUDIO_MAX_GAIN + 1);
    486 
    487 	ym_write(sc, SA3_VOL_R, (ym_read(sc, SA3_VOL_R) & ~SA3_VOL_MV) | atten);
    488 }
    489 
    490 /*
    491  * Read current setting of master volume from hardware
    492  * and update the software value if changed.
    493  * [SA3] This function clears hardware volume interrupt.
    494  */
    495 static void
    496 ym_hvol_to_master_gain(sc)
    497 	struct ym_softc *sc;
    498 {
    499 	u_int prevval, val;
    500 	int changed = 0;
    501 
    502 	val = SA3_VOL_MV & ~ym_read(sc, SA3_VOL_L);
    503 	prevval = (sc->master_gain.left * (SA3_VOL_MV + 1)) /
    504 	    (AUDIO_MAX_GAIN + 1);
    505 	if (val != prevval) {
    506 		sc->master_gain.left =
    507 		    val * ((AUDIO_MAX_GAIN + 1) / (SA3_VOL_MV + 1));
    508 		changed = 1;
    509 	}
    510 
    511 	val = SA3_VOL_MV & ~ym_read(sc, SA3_VOL_R);
    512 	prevval = (sc->master_gain.right * (SA3_VOL_MV + 1)) /
    513 	    (AUDIO_MAX_GAIN + 1);
    514 	if (val != prevval) {
    515 		sc->master_gain.right =
    516 		    val * ((AUDIO_MAX_GAIN + 1) / (SA3_VOL_MV + 1));
    517 		changed = 1;
    518 	}
    519 
    520 #if 0	/* XXX NOT YET */
    521 	/* Notify the change to async processes. */
    522 	if (changed && sc->sc_audiodev)
    523 		mixer_signal(sc->sc_audiodev);
    524 #endif
    525 }
    526 
    527 static void
    528 ym_set_mic_gain(sc, vol)
    529 	struct ym_softc *sc;
    530 	int vol;
    531 {
    532 	u_int atten;
    533 
    534 	sc->mic_gain = vol;
    535 
    536 	atten = ((AUDIO_MAX_GAIN - vol) * (SA3_MIC_MCV + 1)) /
    537 		(AUDIO_MAX_GAIN + 1);
    538 
    539 	ym_write(sc, SA3_MIC_VOL,
    540 		 (ym_read(sc, SA3_MIC_VOL) & ~SA3_MIC_MCV) | atten);
    541 }
    542 
    543 static void
    544 ym_set_3d(sc, cp, val, reg)
    545 	struct ym_softc *sc;
    546 	mixer_ctrl_t *cp;
    547 	struct ad1848_volume *val;
    548 	int reg;
    549 {
    550 	u_int8_t e;
    551 
    552 	ad1848_to_vol(cp, val);
    553 
    554 	e = (val->left * (SA3_3D_BITS + 1) + (SA3_3D_BITS + 1) / 2) /
    555 		(AUDIO_MAX_GAIN + 1) << SA3_3D_LSHIFT |
    556 	    (val->right * (SA3_3D_BITS + 1) + (SA3_3D_BITS + 1) / 2) /
    557 		(AUDIO_MAX_GAIN + 1) << SA3_3D_RSHIFT;
    558 
    559 #ifndef AUDIO_NO_POWER_CTL
    560 	/* turn wide stereo on if necessary */
    561 	if (e)
    562 		ym_power_ctl(sc, YM_POWER_3D, 1);
    563 #endif
    564 
    565 	ym_write(sc, reg, e);
    566 
    567 #ifndef AUDIO_NO_POWER_CTL
    568 	/* turn wide stereo off if necessary */
    569 	if (YM_EQ_OFF(&sc->sc_treble) && YM_EQ_OFF(&sc->sc_bass) &&
    570 	    YM_EQ_OFF(&sc->sc_wide))
    571 		ym_power_ctl(sc, YM_POWER_3D, 0);
    572 #endif
    573 }
    574 
    575 int
    576 ym_mixer_set_port(addr, cp)
    577 	void *addr;
    578 	mixer_ctrl_t *cp;
    579 {
    580 	struct ad1848_softc *ac = addr;
    581 	struct ym_softc *sc = ac->parent;
    582 	struct ad1848_volume vol;
    583 	int error = 0;
    584 	u_int8_t extsources;
    585 
    586 	DPRINTF(("%s: ym_mixer_set_port: dev 0x%x, type 0x%x, 0x%x (%d; %d, %d)\n",
    587 		DVNAME(sc), cp->dev, cp->type, cp->un.ord,
    588 		cp->un.value.num_channels, cp->un.value.level[0],
    589 		cp->un.value.level[1]));
    590 
    591 	/* SA2 doesn't have equalizer */
    592 	if (!YM_IS_SA3(sc) && YM_MIXER_SA3_ONLY(cp->dev))
    593 		return ENXIO;
    594 
    595 #ifndef AUDIO_NO_POWER_CTL
    596 	/* Power-up chip */
    597 	ym_power_ctl(sc, YM_POWER_CODEC_CTL, 1);
    598 #endif
    599 
    600 	switch (cp->dev) {
    601 	case YM_OUTPUT_LVL:
    602 		ad1848_to_vol(cp, &vol);
    603 		ym_set_master_gain(sc, &vol);
    604 		goto out;
    605 
    606 	case YM_OUTPUT_MUTE:
    607 		sc->master_mute = (cp->un.ord != 0);
    608 		ym_mute(sc, SA3_VOL_L, sc->master_mute);
    609 		ym_mute(sc, SA3_VOL_R, sc->master_mute);
    610 		goto out;
    611 
    612 	case YM_MIC_LVL:
    613 		if (cp->un.value.num_channels != 1)
    614 			error = EINVAL;
    615 		else
    616 			ym_set_mic_gain(sc,
    617 				cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
    618 		goto out;
    619 
    620 	case YM_MASTER_EQMODE:
    621 		sc->sc_eqmode = cp->un.ord & SA3_SYS_CTL_YMODE;
    622 		ym_write(sc, SA3_SYS_CTL, (ym_read(sc, SA3_SYS_CTL) &
    623 					   ~SA3_SYS_CTL_YMODE) | sc->sc_eqmode);
    624 		goto out;
    625 
    626 	case YM_MASTER_TREBLE:
    627 		ym_set_3d(sc, cp, &sc->sc_treble, SA3_3D_TREBLE);
    628 		goto out;
    629 
    630 	case YM_MASTER_BASS:
    631 		ym_set_3d(sc, cp, &sc->sc_bass, SA3_3D_BASS);
    632 		goto out;
    633 
    634 	case YM_MASTER_WIDE:
    635 		ym_set_3d(sc, cp, &sc->sc_wide, SA3_3D_WIDE);
    636 		goto out;
    637 
    638 #ifndef AUDIO_NO_POWER_CTL
    639 	case YM_PWR_MODE:
    640 		if ((unsigned) cp->un.ord > YM_POWER_NOSAVE)
    641 			error = EINVAL;
    642 		else
    643 			sc->sc_pow_mode = cp->un.ord;
    644 		goto out;
    645 
    646 	case YM_PWR_TIMEOUT:
    647 		if (cp->un.value.num_channels != 1)
    648 			error = EINVAL;
    649 		else
    650 			sc->sc_pow_timeout =
    651 				cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    652 		goto out;
    653 
    654 	/*
    655 	 * Needs power-up to hear external sources.
    656 	 */
    657 	case YM_CD_MUTE:
    658 	case YM_LINE_MUTE:
    659 	case YM_SPEAKER_MUTE:
    660 	case YM_MIC_MUTE:
    661 		extsources = YM_MIXER_TO_XS(cp->dev);
    662 		if (cp->un.ord) {
    663 			if ((sc->sc_external_sources &= ~extsources) == 0) {
    664 				/*
    665 				 * All the external sources are muted
    666 				 *  --- no need to keep the chip on.
    667 				 */
    668 				ym_power_ctl(sc, YM_POWER_EXT_SRC, 0);
    669 				DPRINTF(("%s: ym_mixer_set_port: off for ext\n",
    670 					DVNAME(sc)));
    671 			}
    672 		} else {
    673 			/* mute off - power-up the chip */
    674 			sc->sc_external_sources |= extsources;
    675 			ym_power_ctl(sc, YM_POWER_EXT_SRC, 1);
    676 			DPRINTF(("%s: ym_mixer_set_port: on for ext\n",
    677 				DVNAME(sc)));
    678 		}
    679 		break;	/* fall to ad1848_mixer_set_port() */
    680 
    681 	/*
    682 	 * Power on/off the playback part for monitoring.
    683 	 */
    684 	case YM_MONITOR_MUTE:
    685 		if ((ac->open_mode & (FREAD | FWRITE)) == FREAD)
    686 			ym_power_ctl(sc, YM_POWER_CODEC_P | YM_POWER_CODEC_DA,
    687 					cp->un.ord == 0);
    688 		break;	/* fall to ad1848_mixer_set_port() */
    689 #endif
    690 	}
    691 
    692 	error = ad1848_mixer_set_port(ac, mappings, NUMMAP, cp);
    693 
    694 	if (error != ENXIO)
    695 		goto out;
    696 
    697 	error = 0;
    698 
    699 	switch (cp->dev) {
    700 	case YM_MIC_MUTE:
    701 		sc->mic_mute = (cp->un.ord != 0);
    702 		ym_mute(sc, SA3_MIC_VOL, sc->mic_mute);
    703 		break;
    704 
    705 	default:
    706 		error = ENXIO;
    707 		break;
    708 	}
    709 
    710 out:
    711 #ifndef AUDIO_NO_POWER_CTL
    712 	/* Power-down chip */
    713 	ym_power_ctl(sc, YM_POWER_CODEC_CTL, 0);
    714 #endif
    715 
    716 	return (error);
    717 }
    718 
    719 int
    720 ym_mixer_get_port(addr, cp)
    721 	void *addr;
    722 	mixer_ctrl_t *cp;
    723 {
    724 	struct ad1848_softc *ac = addr;
    725 	struct ym_softc *sc = ac->parent;
    726 	int error;
    727 
    728 	/* SA2 doesn't have equalizer */
    729 	if (!YM_IS_SA3(sc) && YM_MIXER_SA3_ONLY(cp->dev))
    730 		return ENXIO;
    731 
    732 	switch (cp->dev) {
    733 	case YM_OUTPUT_LVL:
    734 		if (!YM_IS_SA3(sc)) {
    735 			/*
    736 			 * SA2 doesn't have hardware volume interrupt.
    737 			 * Read current value and update every time.
    738 			 */
    739 #ifndef AUDIO_NO_POWER_CTL
    740 			/* Power-up chip */
    741 			ym_power_ctl(sc, YM_POWER_CODEC_CTL, 1);
    742 #endif
    743 			ym_hvol_to_master_gain(sc);
    744 #ifndef AUDIO_NO_POWER_CTL
    745 			/* Power-down chip */
    746 			ym_power_ctl(sc, YM_POWER_CODEC_CTL, 0);
    747 #endif
    748 		}
    749 		ad1848_from_vol(cp, &sc->master_gain);
    750 		return 0;
    751 
    752 	case YM_OUTPUT_MUTE:
    753 		cp->un.ord = sc->master_mute;
    754 		return 0;
    755 
    756 	case YM_MIC_LVL:
    757 		if (cp->un.value.num_channels != 1)
    758 			return EINVAL;
    759 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->mic_gain;
    760 		return 0;
    761 
    762 	case YM_MASTER_EQMODE:
    763 		cp->un.ord = sc->sc_eqmode;
    764 		return 0;
    765 
    766 	case YM_MASTER_TREBLE:
    767 		ad1848_from_vol(cp, &sc->sc_treble);
    768 		return 0;
    769 
    770 	case YM_MASTER_BASS:
    771 		ad1848_from_vol(cp, &sc->sc_bass);
    772 		return 0;
    773 
    774 	case YM_MASTER_WIDE:
    775 		ad1848_from_vol(cp, &sc->sc_wide);
    776 		return 0;
    777 
    778 #ifndef AUDIO_NO_POWER_CTL
    779 	case YM_PWR_MODE:
    780 		cp->un.ord = sc->sc_pow_mode;
    781 		return 0;
    782 
    783 	case YM_PWR_TIMEOUT:
    784 		if (cp->un.value.num_channels != 1)
    785 			return EINVAL;
    786 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_pow_timeout;
    787 		return 0;
    788 #endif
    789 	}
    790 
    791 	error = ad1848_mixer_get_port(ac, mappings, NUMMAP, cp);
    792 
    793 	if (error != ENXIO)
    794 		return (error);
    795 
    796 	error = 0;
    797 
    798 	switch (cp->dev) {
    799 	case YM_MIC_MUTE:
    800 		cp->un.ord = sc->mic_mute;
    801 		break;
    802 
    803 	default:
    804 		error = ENXIO;
    805 		break;
    806 	}
    807 
    808 	return(error);
    809 }
    810 
    811 static char *mixer_classes[] = {
    812 	AudioCinputs, AudioCrecord, AudioCoutputs, AudioCmonitor,
    813 #ifndef AUDIO_NO_POWER_CTL
    814 	AudioCpower,
    815 #endif
    816 	AudioCequalization
    817 };
    818 
    819 int
    820 ym_query_devinfo(addr, dip)
    821 	void *addr;
    822 	mixer_devinfo_t *dip;
    823 {
    824 	static char *mixer_port_names[] = {
    825 		AudioNdac, AudioNmidi, AudioNcd, AudioNline, AudioNspeaker,
    826 		AudioNmicrophone, AudioNmonitor
    827 	};
    828 	struct ad1848_softc *ac = addr;
    829 	struct ym_softc *sc = ac->parent;
    830 
    831 	/* SA2 doesn't have equalizer */
    832 	if (!YM_IS_SA3(sc) && YM_MIXER_SA3_ONLY(dip->index))
    833 		return ENXIO;
    834 
    835 	dip->next = dip->prev = AUDIO_MIXER_LAST;
    836 
    837 	switch(dip->index) {
    838 	case YM_INPUT_CLASS:
    839 	case YM_OUTPUT_CLASS:
    840 	case YM_MONITOR_CLASS:
    841 	case YM_RECORD_CLASS:
    842 #ifndef AUDIO_NO_POWER_CTL
    843 	case YM_PWR_CLASS:
    844 #endif
    845 	case YM_EQ_CLASS:
    846 		dip->type = AUDIO_MIXER_CLASS;
    847 		dip->mixer_class = dip->index;
    848 		strcpy(dip->label.name,
    849 		       mixer_classes[dip->index - YM_INPUT_CLASS]);
    850 		break;
    851 
    852 	case YM_DAC_LVL:
    853 	case YM_MIDI_LVL:
    854 	case YM_CD_LVL:
    855 	case YM_LINE_LVL:
    856 	case YM_SPEAKER_LVL:
    857 	case YM_MIC_LVL:
    858 	case YM_MONITOR_LVL:
    859 		dip->type = AUDIO_MIXER_VALUE;
    860 		if (dip->index == YM_MONITOR_LVL)
    861 			dip->mixer_class = YM_MONITOR_CLASS;
    862 		else
    863 			dip->mixer_class = YM_INPUT_CLASS;
    864 
    865 		dip->next = dip->index + 7;
    866 
    867 		strcpy(dip->label.name,
    868 		       mixer_port_names[dip->index - YM_DAC_LVL]);
    869 
    870 		if (dip->index == YM_SPEAKER_LVL ||
    871 		    dip->index == YM_MIC_LVL)
    872 			dip->un.v.num_channels = 1;
    873 		else
    874 			dip->un.v.num_channels = 2;
    875 
    876 		strcpy(dip->un.v.units.name, AudioNvolume);
    877 		break;
    878 
    879 	case YM_DAC_MUTE:
    880 	case YM_MIDI_MUTE:
    881 	case YM_CD_MUTE:
    882 	case YM_LINE_MUTE:
    883 	case YM_SPEAKER_MUTE:
    884 	case YM_MIC_MUTE:
    885 	case YM_MONITOR_MUTE:
    886 		if (dip->index == YM_MONITOR_MUTE)
    887 			dip->mixer_class = YM_MONITOR_CLASS;
    888 		else
    889 			dip->mixer_class = YM_INPUT_CLASS;
    890 		dip->type = AUDIO_MIXER_ENUM;
    891 		dip->prev = dip->index - 7;
    892 	mute:
    893 		strcpy(dip->label.name, AudioNmute);
    894 		dip->un.e.num_mem = 2;
    895 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
    896 		dip->un.e.member[0].ord = 0;
    897 		strcpy(dip->un.e.member[1].label.name, AudioNon);
    898 		dip->un.e.member[1].ord = 1;
    899 		break;
    900 
    901 
    902 	case YM_OUTPUT_LVL:
    903 		dip->type = AUDIO_MIXER_VALUE;
    904 		dip->mixer_class = YM_OUTPUT_CLASS;
    905 		dip->next = YM_OUTPUT_MUTE;
    906 		strcpy(dip->label.name, AudioNmaster);
    907 		dip->un.v.num_channels = 2;
    908 		strcpy(dip->un.v.units.name, AudioNvolume);
    909 		break;
    910 
    911 	case YM_OUTPUT_MUTE:
    912 		dip->mixer_class = YM_OUTPUT_CLASS;
    913 		dip->type = AUDIO_MIXER_ENUM;
    914 		dip->prev = YM_OUTPUT_LVL;
    915 		goto mute;
    916 
    917 
    918 	case YM_REC_LVL:	/* record level */
    919 		dip->type = AUDIO_MIXER_VALUE;
    920 		dip->mixer_class = YM_RECORD_CLASS;
    921 		dip->next = YM_RECORD_SOURCE;
    922 		strcpy(dip->label.name, AudioNrecord);
    923 		dip->un.v.num_channels = 2;
    924 		strcpy(dip->un.v.units.name, AudioNvolume);
    925 		break;
    926 
    927 	case YM_RECORD_SOURCE:
    928 		dip->mixer_class = YM_RECORD_CLASS;
    929 		dip->type = AUDIO_MIXER_ENUM;
    930 		dip->prev = YM_REC_LVL;
    931 		strcpy(dip->label.name, AudioNsource);
    932 		dip->un.e.num_mem = 4;
    933 		strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
    934 		dip->un.e.member[0].ord = MIC_IN_PORT;
    935 		strcpy(dip->un.e.member[1].label.name, AudioNline);
    936 		dip->un.e.member[1].ord = LINE_IN_PORT;
    937 		strcpy(dip->un.e.member[2].label.name, AudioNdac);
    938 		dip->un.e.member[2].ord = DAC_IN_PORT;
    939 		strcpy(dip->un.e.member[3].label.name, AudioNcd);
    940 		dip->un.e.member[3].ord = AUX1_IN_PORT;
    941 		break;
    942 
    943 
    944 	case YM_MASTER_EQMODE:
    945 		dip->type = AUDIO_MIXER_ENUM;
    946 		dip->mixer_class = YM_EQ_CLASS;
    947 		strcpy(dip->label.name, AudioNmode);
    948 		strcpy(dip->un.v.units.name, AudioNmode);
    949 		dip->un.e.num_mem = 4;
    950 		strcpy(dip->un.e.member[0].label.name, AudioNdesktop);
    951 		dip->un.e.member[0].ord = SA3_SYS_CTL_YMODE0;
    952 		strcpy(dip->un.e.member[1].label.name, AudioNlaptop);
    953 		dip->un.e.member[1].ord = SA3_SYS_CTL_YMODE1;
    954 		strcpy(dip->un.e.member[2].label.name, AudioNsubnote);
    955 		dip->un.e.member[2].ord = SA3_SYS_CTL_YMODE2;
    956 		strcpy(dip->un.e.member[3].label.name, AudioNhifi);
    957 		dip->un.e.member[3].ord = SA3_SYS_CTL_YMODE3;
    958 		break;
    959 
    960 	case YM_MASTER_TREBLE:
    961 		dip->type = AUDIO_MIXER_VALUE;
    962 		dip->mixer_class = YM_EQ_CLASS;
    963 		strcpy(dip->label.name, AudioNtreble);
    964 		dip->un.v.num_channels = 2;
    965 		strcpy(dip->un.v.units.name, AudioNtreble);
    966 		break;
    967 
    968 	case YM_MASTER_BASS:
    969 		dip->type = AUDIO_MIXER_VALUE;
    970 		dip->mixer_class = YM_EQ_CLASS;
    971 		strcpy(dip->label.name, AudioNbass);
    972 		dip->un.v.num_channels = 2;
    973 		strcpy(dip->un.v.units.name, AudioNbass);
    974 		break;
    975 
    976 	case YM_MASTER_WIDE:
    977 		dip->type = AUDIO_MIXER_VALUE;
    978 		dip->mixer_class = YM_EQ_CLASS;
    979 		strcpy(dip->label.name, AudioNsurround);
    980 		dip->un.v.num_channels = 2;
    981 		strcpy(dip->un.v.units.name, AudioNsurround);
    982 		break;
    983 
    984 
    985 #ifndef AUDIO_NO_POWER_CTL
    986 	case YM_PWR_MODE:
    987 		dip->type = AUDIO_MIXER_ENUM;
    988 		dip->mixer_class = YM_PWR_CLASS;
    989 		dip->next = YM_PWR_TIMEOUT;
    990 		strcpy(dip->label.name, AudioNsave);
    991 		dip->un.e.num_mem = 3;
    992 		strcpy(dip->un.e.member[0].label.name, AudioNpowerdown);
    993 		dip->un.e.member[0].ord = YM_POWER_POWERDOWN;
    994 		strcpy(dip->un.e.member[1].label.name, AudioNpowersave);
    995 		dip->un.e.member[1].ord = YM_POWER_POWERSAVE;
    996 		strcpy(dip->un.e.member[2].label.name, AudioNnosave);
    997 		dip->un.e.member[2].ord = YM_POWER_NOSAVE;
    998 		break;
    999 
   1000 	case YM_PWR_TIMEOUT:
   1001 		dip->type = AUDIO_MIXER_VALUE;
   1002 		dip->mixer_class = YM_PWR_CLASS;
   1003 		dip->prev = YM_PWR_MODE;
   1004 		strcpy(dip->label.name, AudioNtimeout);
   1005 		dip->un.v.num_channels = 1;
   1006 		strcpy(dip->un.v.units.name, AudioNtimeout);
   1007 		break;
   1008 #endif /* not AUDIO_NO_POWER_CTL */
   1009 
   1010 	default:
   1011 		return ENXIO;
   1012 		/*NOTREACHED*/
   1013 	}
   1014 
   1015 	return 0;
   1016 }
   1017 
   1018 int
   1019 ym_intr(arg)
   1020 	void *arg;
   1021 {
   1022 	struct ym_softc *sc = arg;
   1023 	u_int8_t ist;
   1024 	int processed;
   1025 
   1026 	/* OPL3 timer is currently unused. */
   1027 	if (((ist = ym_read(sc, SA3_IRQA_STAT)) &
   1028 	     ~(SA3_IRQ_STAT_SB|SA3_IRQ_STAT_OPL3)) == 0) {
   1029 		DPRINTF(("%s: ym_intr: spurious interrupt\n", DVNAME(sc)));
   1030 		return 0;
   1031 	}
   1032 
   1033 	/* Process pending interrupts. */
   1034 	do {
   1035 		processed = 0;
   1036 		/*
   1037 		 * CODEC interrupts.
   1038 		 */
   1039 		if (ist & (SA3_IRQ_STAT_TI|SA3_IRQ_STAT_CI|SA3_IRQ_STAT_PI)) {
   1040 			ad1848_isa_intr(&sc->sc_ad1848);
   1041 			processed = 1;
   1042 		}
   1043 #if NMPU_YM > 0
   1044 		/*
   1045 		 * MPU401 interrupt.
   1046 		 */
   1047 		if (ist & SA3_IRQ_STAT_MPU) {
   1048 			mpu_intr(sc->sc_mpudev);
   1049 			processed = 1;
   1050 		}
   1051 #endif
   1052 		/*
   1053 		 * Hardware volume interrupt (SA3 only).
   1054 		 * Recalculate master volume from the hardware setting.
   1055 		 */
   1056 		if ((ist & SA3_IRQ_STAT_MV) && YM_IS_SA3(sc)) {
   1057 			ym_hvol_to_master_gain(sc);
   1058 			processed = 1;
   1059 		}
   1060 	} while (processed && (ist = ym_read(sc, SA3_IRQA_STAT)));
   1061 
   1062 	return 1;
   1063 }
   1064 
   1065 
   1066 #ifndef AUDIO_NO_POWER_CTL
   1067 static void
   1068 ym_save_codec_regs(sc)
   1069 	struct ym_softc *sc;
   1070 {
   1071 	struct ad1848_softc *ac = &sc->sc_ad1848.sc_ad1848;
   1072 	int i;
   1073 
   1074 	DPRINTF(("%s: ym_save_codec_regs\n", DVNAME(sc)));
   1075 
   1076 	for (i = 0; i <= 0x1f; i++)
   1077 		sc->sc_codec_scan[i] = ad_read(ac, i);
   1078 }
   1079 
   1080 static void
   1081 ym_restore_codec_regs(sc)
   1082 	struct ym_softc *sc;
   1083 {
   1084 	struct ad1848_softc *ac = &sc->sc_ad1848.sc_ad1848;
   1085 	int i, t;
   1086 
   1087 	DPRINTF(("%s: ym_restore_codec_regs\n", DVNAME(sc)));
   1088 
   1089 	for (i = 0; i <= 0x1f; i++) {
   1090 		/*
   1091 		 * Wait til the chip becomes ready.
   1092 		 * This is required after suspend/resume.
   1093 		 */
   1094 		for (t = 0;
   1095 		    t < 100000 && ADREAD(ac, AD1848_IADDR) & SP_IN_INIT; t++)
   1096 			;
   1097 #ifdef AUDIO_DEBUG
   1098 		if (t)
   1099 			DPRINTF(("%s: ym_restore_codec_regs: reg %d, t %d\n",
   1100 				 DVNAME(sc), i, t));
   1101 #endif
   1102 		ad_write(ac, i, sc->sc_codec_scan[i]);
   1103 	}
   1104 }
   1105 
   1106 /*
   1107  * Save and restore the state on suspending / resumning.
   1108  *
   1109  * XXX This is not complete.
   1110  * Currently only the parameters, such as output gain, are restored.
   1111  * DMA state should also be restored.  FIXME.
   1112  */
   1113 void
   1114 ym_power_hook(why, v)
   1115 	int why;
   1116 	void *v;
   1117 {
   1118 	struct ym_softc *sc = v;
   1119 	int i, max;
   1120 	int s;
   1121 
   1122 	DPRINTF(("%s: ym_power_hook: why = %d\n", DVNAME(sc), why));
   1123 
   1124 	s = splaudio();
   1125 
   1126 	switch (why) {
   1127 	case PWR_SUSPEND:
   1128 	case PWR_STANDBY:
   1129 		/*
   1130 		 * suspending...
   1131 		 */
   1132 		callout_stop(&sc->sc_powerdown_ch);
   1133 		if (sc->sc_turning_off)
   1134 			ym_powerdown_blocks(sc);
   1135 
   1136 		/*
   1137 		 * Save CODEC registers.
   1138 		 * Note that the registers read incorrect
   1139 		 * if the CODEC part is in power-down mode.
   1140 		 */
   1141 		if (sc->sc_on_blocks & YM_POWER_CODEC_DIGITAL)
   1142 			ym_save_codec_regs(sc);
   1143 
   1144 		/*
   1145 		 * Save OPL3-SA3 control registers and power-down the chip.
   1146 		 * Note that the registers read incorrect
   1147 		 * if the chip is in global power-down mode.
   1148 		 */
   1149 		sc->sc_sa3_scan[SA3_PWR_MNG] = ym_read(sc, SA3_PWR_MNG);
   1150 		if (sc->sc_on_blocks)
   1151 			ym_chip_powerdown(sc);
   1152 		break;
   1153 
   1154 	case PWR_RESUME:
   1155 		/*
   1156 		 * resuming...
   1157 		 */
   1158 		ym_chip_powerup(sc, 1);
   1159 		ym_init(sc);		/* power-on CODEC */
   1160 
   1161 		/* Restore control registers. */
   1162 		max = YM_IS_SA3(sc)? YM_SAVE_REG_MAX_SA3 : YM_SAVE_REG_MAX_SA2;
   1163 		for (i = SA3_PWR_MNG + 1; i <= max; i++) {
   1164 			if (i == SA3_SB_SCAN || i == SA3_SB_SCAN_DATA ||
   1165 			    i == SA3_DPWRDWN)
   1166 				continue;
   1167 			ym_write(sc, i, sc->sc_sa3_scan[i]);
   1168 		}
   1169 
   1170 		/* Restore CODEC registers (including mixer). */
   1171 		ym_restore_codec_regs(sc);
   1172 
   1173 		/* Restore global/digital power-down state. */
   1174 		ym_write(sc, SA3_PWR_MNG, sc->sc_sa3_scan[SA3_PWR_MNG]);
   1175 		if (YM_IS_SA3(sc))
   1176 			ym_write(sc, SA3_DPWRDWN, sc->sc_sa3_scan[SA3_DPWRDWN]);
   1177 		break;
   1178 	case PWR_SOFTSUSPEND:
   1179 	case PWR_SOFTSTANDBY:
   1180 	case PWR_SOFTRESUME:
   1181 		break;
   1182 	}
   1183 	splx(s);
   1184 }
   1185 
   1186 int
   1187 ym_codec_power_ctl(arg, flags)
   1188 	void *arg;
   1189 	int flags;
   1190 {
   1191 	struct ym_softc *sc = arg;
   1192 	struct ad1848_softc *ac = &sc->sc_ad1848.sc_ad1848;
   1193 	int parts;
   1194 
   1195 	DPRINTF(("%s: ym_codec_power_ctl: flags = 0x%x\n", DVNAME(sc), flags));
   1196 
   1197 	if (flags != 0) {
   1198 		parts = 0;
   1199 		if (flags & FREAD) {
   1200 			parts |= YM_POWER_CODEC_R | YM_POWER_CODEC_AD;
   1201 			if (ac->mute[AD1848_MONITOR_CHANNEL] == 0)
   1202 				parts |= YM_POWER_CODEC_P | YM_POWER_CODEC_DA;
   1203 		}
   1204 		if (flags & FWRITE)
   1205 			parts |= YM_POWER_CODEC_P | YM_POWER_CODEC_DA;
   1206 	} else
   1207 		parts = YM_POWER_CODEC_P | YM_POWER_CODEC_R |
   1208 			YM_POWER_CODEC_DA | YM_POWER_CODEC_AD;
   1209 
   1210 	ym_power_ctl(sc, parts, flags);
   1211 
   1212 	return 0;
   1213 }
   1214 
   1215 /*
   1216  * Enter Power Save mode or Global Power Down mode.
   1217  * Total dissipation becomes 5mA and 10uA (typ.) respective.
   1218  *
   1219  * This must be called at splaudio().
   1220  */
   1221 static void
   1222 ym_chip_powerdown(sc)
   1223 	struct ym_softc *sc;
   1224 {
   1225 	int i, max;
   1226 
   1227 	DPRINTF(("%s: ym_chip_powerdown\n", DVNAME(sc)));
   1228 
   1229 	max = YM_IS_SA3(sc) ? YM_SAVE_REG_MAX_SA3 : YM_SAVE_REG_MAX_SA2;
   1230 
   1231 	/* Save control registers. */
   1232 	for (i = SA3_PWR_MNG + 1; i <= max; i++) {
   1233 		if (i == SA3_SB_SCAN || i == SA3_SB_SCAN_DATA)
   1234 			continue;
   1235 		sc->sc_sa3_scan[i] = ym_read(sc, i);
   1236 	}
   1237 	ym_write(sc, SA3_PWR_MNG,
   1238 		 (sc->sc_pow_mode == YM_POWER_POWERDOWN ?
   1239 			SA3_PWR_MNG_PDN : SA3_PWR_MNG_PSV) | SA3_PWR_MNG_PDX);
   1240 }
   1241 
   1242 /*
   1243  * Power up from Power Save / Global Power Down Mode.
   1244  *
   1245  * We assume no ym interrupt shall occur, since the chip is
   1246  * in power-down mode (or should be blocked by splaudio()).
   1247  */
   1248 static void
   1249 ym_chip_powerup(sc, nosleep)
   1250 	struct ym_softc *sc;
   1251 	int nosleep;
   1252 {
   1253 	int wchan;
   1254 	u_int8_t pw;
   1255 
   1256 	DPRINTF(("%s: ym_chip_powerup\n", DVNAME(sc)));
   1257 
   1258 	pw = ym_read(sc, SA3_PWR_MNG);
   1259 
   1260 	if ((pw & (SA3_PWR_MNG_PSV | SA3_PWR_MNG_PDN | SA3_PWR_MNG_PDX)) == 0)
   1261 		return;		/* already on */
   1262 
   1263 	pw &= ~SA3_PWR_MNG_PDX;
   1264 	ym_write(sc, SA3_PWR_MNG, pw);
   1265 
   1266 	/* wait 100 ms */
   1267 	if (nosleep)
   1268 		delay(100000);
   1269 	else
   1270 		tsleep(&wchan, PWAIT, "ym_pu1", hz / 10);
   1271 
   1272 	pw &= ~(SA3_PWR_MNG_PSV | SA3_PWR_MNG_PDN);
   1273 	ym_write(sc, SA3_PWR_MNG, pw);
   1274 
   1275 	/* wait 70 ms */
   1276 	if (nosleep)
   1277 		delay(70000);
   1278 	else
   1279 		tsleep(&wchan, PWAIT, "ym_pu2", hz / 14);
   1280 
   1281 	/* The chip is muted automatically --- unmute it now. */
   1282 	ym_mute(sc, SA3_VOL_L, sc->master_mute);
   1283 	ym_mute(sc, SA3_VOL_R, sc->master_mute);
   1284 }
   1285 
   1286 /* callout handler for power-down */
   1287 void
   1288 ym_powerdown_blocks(arg)
   1289 	void *arg;
   1290 {
   1291 	struct ym_softc *sc = arg;
   1292 	u_int16_t parts;
   1293 	u_int16_t on_blocks = sc->sc_on_blocks;
   1294 	u_int8_t sv;
   1295 	int s;
   1296 
   1297 	DPRINTF(("%s: ym_powerdown_blocks: turning_off 0x%x\n",
   1298 		DVNAME(sc), sc->sc_turning_off));
   1299 
   1300 	s = splaudio();
   1301 
   1302 	on_blocks = sc->sc_on_blocks;
   1303 
   1304 	/* Be sure not to change the state of the chip.  Save it first. */
   1305 	sv =  bus_space_read_1(sc->sc_iot, sc->sc_controlioh, SA3_CTL_INDEX);
   1306 
   1307 	parts = sc->sc_turning_off;
   1308 
   1309 	if (on_blocks & ~parts & YM_POWER_CODEC_CTL)
   1310 		parts &= ~(YM_POWER_CODEC_P | YM_POWER_CODEC_R);
   1311 	if (parts & YM_POWER_CODEC_CTL) {
   1312 		if ((on_blocks & YM_POWER_CODEC_P) == 0)
   1313 			parts |= YM_POWER_CODEC_P;
   1314 		if ((on_blocks & YM_POWER_CODEC_R) == 0)
   1315 			parts |= YM_POWER_CODEC_R;
   1316 	}
   1317 	parts &= ~YM_POWER_CODEC_PSEUDO;
   1318 
   1319 	/* If CODEC is being off, save the state. */
   1320 	if ((sc->sc_on_blocks & YM_POWER_CODEC_DIGITAL) &&
   1321 	    (sc->sc_on_blocks & ~sc->sc_turning_off &
   1322 				YM_POWER_CODEC_DIGITAL) == 0)
   1323 		ym_save_codec_regs(sc);
   1324 
   1325 	if (YM_IS_SA3(sc)) {
   1326 		/* OPL3-SA3 */
   1327 		ym_write(sc, SA3_DPWRDWN,
   1328 		    ym_read(sc, SA3_DPWRDWN) | (u_int8_t) parts);
   1329 		ym_write(sc, SA3_APWRDWN,
   1330 		    ym_read(sc, SA3_APWRDWN) | (parts >> 8));
   1331 	} else {
   1332 		/* OPL3-SA2 (only OPL3 can be off partially) */
   1333 		if (parts & YM_POWER_OPL3)
   1334 			ym_write(sc, SA3_PWR_MNG,
   1335 			    ym_read(sc, SA3_PWR_MNG) | SA2_PWR_MNG_FMPS);
   1336 	}
   1337 
   1338 	if (((sc->sc_on_blocks &= ~sc->sc_turning_off) & YM_POWER_ACTIVE) == 0)
   1339 		ym_chip_powerdown(sc);
   1340 
   1341 	sc->sc_turning_off = 0;
   1342 
   1343 	/* Restore the state of the chip. */
   1344 	bus_space_write_1(sc->sc_iot, sc->sc_controlioh, SA3_CTL_INDEX, sv);
   1345 
   1346 	splx(s);
   1347 }
   1348 
   1349 /*
   1350  * Power control entry point.
   1351  */
   1352 void
   1353 ym_power_ctl(sc, parts, onoff)
   1354 	struct ym_softc *sc;
   1355 	int parts, onoff;
   1356 {
   1357 	int s;
   1358 	int need_restore_codec;
   1359 
   1360 	DPRINTF(("%s: ym_power_ctl: parts = 0x%x, %s\n",
   1361 		DVNAME(sc), parts, onoff ? "on" : "off"));
   1362 
   1363 #ifdef DIAGNOSTIC
   1364 	if (curproc == NULL)
   1365 		panic("ym_power_ctl: no curproc");
   1366 #endif
   1367 	/* This function may sleep --- needs locking. */
   1368 	while (sc->sc_in_power_ctl & YM_POWER_CTL_INUSE) {
   1369 		sc->sc_in_power_ctl |= YM_POWER_CTL_WANTED;
   1370 		DPRINTF(("%s: ym_power_ctl: sleeping\n", DVNAME(sc)));
   1371 		tsleep(&sc->sc_in_power_ctl, PWAIT, "ym_pc", 0);
   1372 		DPRINTF(("%s: ym_power_ctl: awaken\n", DVNAME(sc)));
   1373 	}
   1374 	sc->sc_in_power_ctl |= YM_POWER_CTL_INUSE;
   1375 
   1376 	/* Defeat softclock interrupts. */
   1377 	s = splsoftclock();
   1378 
   1379 	/* If ON requested to parts which are scheduled to OFF, cancel it. */
   1380 	if (onoff && sc->sc_turning_off && (sc->sc_turning_off &= ~parts) == 0)
   1381 		callout_stop(&sc->sc_powerdown_ch);
   1382 
   1383 	if (!onoff && sc->sc_turning_off)
   1384 		parts &= ~sc->sc_turning_off;
   1385 
   1386 	/* Discard bits which are currently {on,off}. */
   1387 	parts &= onoff ? ~sc->sc_on_blocks : sc->sc_on_blocks;
   1388 
   1389 	/* Cancel previous timeout if needed. */
   1390 	if (parts != 0 && sc->sc_turning_off)
   1391 		callout_stop(&sc->sc_powerdown_ch);
   1392 
   1393 	(void) splx(s);
   1394 
   1395 	if (parts == 0)
   1396 		goto unlock;		/* no work to do */
   1397 
   1398 	if (onoff) {
   1399 		/* Turning on is done immediately. */
   1400 
   1401 		/* If the chip is off, turn it on. */
   1402 		if ((sc->sc_on_blocks & YM_POWER_ACTIVE) == 0)
   1403 			ym_chip_powerup(sc, 0);
   1404 
   1405 		need_restore_codec = (parts & YM_POWER_CODEC_DIGITAL) &&
   1406 		    (sc->sc_on_blocks & YM_POWER_CODEC_DIGITAL) == 0;
   1407 
   1408 		sc->sc_on_blocks |= parts;
   1409 		if (parts & YM_POWER_CODEC_CTL)
   1410 			parts |= YM_POWER_CODEC_P | YM_POWER_CODEC_R;
   1411 
   1412 		s = splaudio();
   1413 
   1414 		if (YM_IS_SA3(sc)) {
   1415 			/* OPL3-SA3 */
   1416 			ym_write(sc, SA3_DPWRDWN,
   1417 			    ym_read(sc, SA3_DPWRDWN) & (u_int8_t)~parts);
   1418 			ym_write(sc, SA3_APWRDWN,
   1419 			    ym_read(sc, SA3_APWRDWN) & ~(parts >> 8));
   1420 		} else {
   1421 			/* OPL3-SA2 (only OPL3 can be off partially) */
   1422 			if (parts & YM_POWER_OPL3)
   1423 				ym_write(sc, SA3_PWR_MNG,
   1424 				    ym_read(sc, SA3_PWR_MNG)
   1425 					& ~SA2_PWR_MNG_FMPS);
   1426 		}
   1427 		if (need_restore_codec)
   1428 			ym_restore_codec_regs(sc);
   1429 
   1430 		(void) splx(s);
   1431 	} else {
   1432 		/* Turning off is delayed. */
   1433 		sc->sc_turning_off |= parts;
   1434 	}
   1435 
   1436 	/* Schedule turning off. */
   1437 	if (sc->sc_pow_mode != YM_POWER_NOSAVE && sc->sc_turning_off)
   1438 		callout_reset(&sc->sc_powerdown_ch, hz * sc->sc_pow_timeout,
   1439 		    ym_powerdown_blocks, sc);
   1440 
   1441 unlock:
   1442 	if (sc->sc_in_power_ctl & YM_POWER_CTL_WANTED)
   1443 		wakeup(&sc->sc_in_power_ctl);
   1444 	sc->sc_in_power_ctl = 0;
   1445 }
   1446 #endif /* not AUDIO_NO_POWER_CTL */
   1447