1 /* $NetBSD: ics2101.c,v 1.15 2008/04/28 20:23:52 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Ken Hornstein and John Kohl. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: ics2101.c,v 1.15 2008/04/28 20:23:52 martin Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/errno.h> 38 #include <sys/ioctl.h> 39 #include <sys/syslog.h> 40 #include <sys/device.h> 41 #include <sys/proc.h> 42 #include <sys/buf.h> 43 44 #include <sys/cpu.h> 45 46 #include <sys/audioio.h> 47 #include <dev/audio_if.h> 48 49 #include <dev/isa/isavar.h> 50 #include <dev/isa/isadmavar.h> 51 52 #include <dev/ic/ics2101reg.h> 53 #include <dev/isa/ics2101var.h> 54 55 56 #define ICS_VALUE 0x01 57 #define ICS_MUTE 0x02 58 #define ICS_MUTE_MUTED 0x04 59 60 /* convert from [AUDIO_MIN_GAIN,AUDIO_MAX_GAIN] (0,255) to 61 [ICSMIX_MAX_ATTN,ICSMIX_MIN_ATTN] (0,127) */ 62 63 #define cvt_value(val) ((val) >> 1) 64 65 static void ics2101_mix_doit(struct ics2101_softc *, u_int, u_int, u_int, 66 u_int); 67 /* 68 * Program one channel of the ICS mixer 69 */ 70 71 72 static void 73 ics2101_mix_doit(sc, chan, side, value, flags) 74 struct ics2101_softc *sc; 75 u_int chan, side, value, flags; 76 { 77 bus_space_tag_t iot = sc->sc_iot; 78 unsigned char flip_left[6] = {0x01, 0x01, 0x01, 0x02, 0x01, 0x02}; 79 unsigned char flip_right[6] = {0x02, 0x02, 0x02, 0x01, 0x02, 0x01}; 80 unsigned char ctrl_addr; 81 unsigned char attn_addr; 82 unsigned char normal; 83 int s; 84 85 if (/* chan < ICSMIX_CHAN_0 || */ chan > ICSMIX_CHAN_5) 86 return; 87 if (side != ICSMIX_LEFT && side != ICSMIX_RIGHT) 88 return; 89 90 if (flags & ICS_MUTE) { 91 value = cvt_value(sc->sc_setting[chan][side]); 92 sc->sc_mute[chan][side] = flags & ICS_MUTE_MUTED; 93 } else if (flags & ICS_VALUE) { 94 sc->sc_setting[chan][side] = value; 95 value = cvt_value(value); 96 if (value > ICSMIX_MIN_ATTN) 97 value = ICSMIX_MIN_ATTN; 98 } else 99 return; 100 101 ctrl_addr = chan << 3; 102 attn_addr = chan << 3; 103 104 if (side == ICSMIX_LEFT) { 105 ctrl_addr |= ICSMIX_CTRL_LEFT; 106 attn_addr |= ICSMIX_ATTN_LEFT; 107 if (sc->sc_mute[chan][side]) 108 normal = 0x0; 109 else if (sc->sc_flags & ICS_FLIP) 110 normal = flip_left[chan]; 111 else 112 normal = 0x01; 113 } else { 114 ctrl_addr |= ICSMIX_CTRL_RIGHT; 115 attn_addr |= ICSMIX_ATTN_RIGHT; 116 if (sc->sc_mute[chan][side]) 117 normal = 0x0; 118 else if (sc->sc_flags & ICS_FLIP) 119 normal = flip_right[chan]; 120 else 121 normal = 0x02; 122 } 123 124 s = splaudio(); 125 126 bus_space_write_1(iot, sc->sc_selio_ioh, sc->sc_selio, ctrl_addr); 127 bus_space_write_1(iot, sc->sc_dataio_ioh, sc->sc_dataio, normal); 128 129 bus_space_write_1(iot, sc->sc_selio_ioh, sc->sc_selio, attn_addr); 130 bus_space_write_1(iot, sc->sc_dataio_ioh, sc->sc_dataio, (unsigned char) value); 131 132 splx(s); 133 } 134 135 void 136 ics2101_mix_mute(sc, chan, side, domute) 137 struct ics2101_softc *sc; 138 unsigned int chan, side, domute; 139 { 140 ics2101_mix_doit(sc, chan, side, 0, 141 domute ? ICS_MUTE|ICS_MUTE_MUTED : ICS_MUTE); 142 } 143 144 void 145 ics2101_mix_attenuate(sc, chan, side, value) 146 struct ics2101_softc *sc; 147 unsigned int chan, side, value; 148 { 149 ics2101_mix_doit(sc, chan, side, value, ICS_VALUE); 150 } 151