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