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