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