ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / isa / gus / gus_mixer.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  Routines for control of ICS 2101 chip and "mixer" in GF1 chip
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <sound/driver.h>
23 #include <linux/time.h>
24 #include <linux/wait.h>
25 #include <sound/core.h>
26 #include <sound/control.h>
27 #include <sound/gus.h>
28
29 #define chip_t snd_gus_card_t
30
31 /*
32  *
33  */
34
35 #define GF1_SINGLE(xname, xindex, shift, invert) \
36 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
37   .info = snd_gf1_info_single, \
38   .get = snd_gf1_get_single, .put = snd_gf1_put_single, \
39   .private_value = shift | (invert << 8) }
40
41 static int snd_gf1_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
42 {
43         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
44         uinfo->count = 1;
45         uinfo->value.integer.min = 0;
46         uinfo->value.integer.max = 1;
47         return 0;
48 }
49
50 static int snd_gf1_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
51 {
52         snd_gus_card_t *gus = snd_kcontrol_chip(kcontrol);
53         int shift = kcontrol->private_value & 0xff;
54         int invert = (kcontrol->private_value >> 8) & 1;
55         
56         ucontrol->value.integer.value[0] = (gus->mix_cntrl_reg >> shift) & 1;
57         if (invert)
58                 ucontrol->value.integer.value[0] ^= 1;
59         return 0;
60 }
61
62 static int snd_gf1_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
63 {
64         snd_gus_card_t *gus = snd_kcontrol_chip(kcontrol);
65         unsigned long flags;
66         int shift = kcontrol->private_value & 0xff;
67         int invert = (kcontrol->private_value >> 8) & 1;
68         int change;
69         unsigned char oval, nval;
70         
71         nval = ucontrol->value.integer.value[0] & 1;
72         if (invert)
73                 nval ^= 1;
74         nval <<= shift;
75         spin_lock_irqsave(&gus->reg_lock, flags);
76         oval = gus->mix_cntrl_reg;
77         nval = (oval & ~(1 << shift)) | nval;
78         change = nval != oval;
79         outb(gus->mix_cntrl_reg = nval, GUSP(gus, MIXCNTRLREG));
80         outb(gus->gf1.active_voice = 0, GUSP(gus, GF1PAGE));
81         spin_unlock_irqrestore(&gus->reg_lock, flags);
82         return change;
83 }
84
85 #define ICS_DOUBLE(xname, xindex, addr) \
86 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
87   .info = snd_ics_info_double, \
88   .get = snd_ics_get_double, .put = snd_ics_put_double, \
89   .private_value = addr }
90
91 static int snd_ics_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
92 {
93         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
94         uinfo->count = 2;
95         uinfo->value.integer.min = 0;
96         uinfo->value.integer.max = 127;
97         return 0;
98 }
99
100 static int snd_ics_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
101 {
102         snd_gus_card_t *gus = snd_kcontrol_chip(kcontrol);
103         unsigned long flags;
104         int addr = kcontrol->private_value & 0xff;
105         unsigned char left, right;
106         
107         spin_lock_irqsave(&gus->reg_lock, flags);
108         left = gus->gf1.ics_regs[addr][0];
109         right = gus->gf1.ics_regs[addr][1];
110         spin_unlock_irqrestore(&gus->reg_lock, flags);
111         ucontrol->value.integer.value[0] = left & 127;
112         ucontrol->value.integer.value[1] = right & 127;
113         return 0;
114 }
115
116 static int snd_ics_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
117 {
118         snd_gus_card_t *gus = snd_kcontrol_chip(kcontrol);
119         unsigned long flags;
120         int addr = kcontrol->private_value & 0xff;
121         int change;
122         unsigned char val1, val2, oval1, oval2, tmp;
123         
124         val1 = ucontrol->value.integer.value[0] & 127;
125         val2 = ucontrol->value.integer.value[1] & 127;
126         spin_lock_irqsave(&gus->reg_lock, flags);
127         oval1 = gus->gf1.ics_regs[addr][0];
128         oval2 = gus->gf1.ics_regs[addr][1];
129         change = val1 != oval1 || val2 != oval2;
130         gus->gf1.ics_regs[addr][0] = val1;
131         gus->gf1.ics_regs[addr][1] = val2;
132         if (gus->ics_flag && gus->ics_flipped &&
133             (addr == SNDRV_ICS_GF1_DEV || addr == SNDRV_ICS_MASTER_DEV)) {
134                 tmp = val1;
135                 val1 = val2;
136                 val2 = tmp;
137         }
138         addr <<= 3;
139         outb(addr | 0, GUSP(gus, MIXCNTRLPORT));
140         outb(1, GUSP(gus, MIXDATAPORT));
141         outb(addr | 2, GUSP(gus, MIXCNTRLPORT));
142         outb((unsigned char) val1, GUSP(gus, MIXDATAPORT));
143         outb(addr | 1, GUSP(gus, MIXCNTRLPORT));
144         outb(2, GUSP(gus, MIXDATAPORT));
145         outb(addr | 3, GUSP(gus, MIXCNTRLPORT));
146         outb((unsigned char) val2, GUSP(gus, MIXDATAPORT));
147         spin_unlock_irqrestore(&gus->reg_lock, flags);
148         return change;
149 }
150
151 #define GF1_CONTROLS (sizeof(snd_gf1_controls)/sizeof(snd_kcontrol_new_t))
152
153 static snd_kcontrol_new_t snd_gf1_controls[] = {
154 GF1_SINGLE("Master Playback Switch", 0, 1, 1),
155 GF1_SINGLE("Line Switch", 0, 0, 1),
156 GF1_SINGLE("Mic Switch", 0, 2, 0)
157 };
158
159 #define ICS_CONTROLS (sizeof(snd_ics_controls)/sizeof(snd_kcontrol_new_t))
160
161 static snd_kcontrol_new_t snd_ics_controls[] = {
162 GF1_SINGLE("Master Playback Switch", 0, 1, 1),
163 ICS_DOUBLE("Master Playback Volume", 0, SNDRV_ICS_MASTER_DEV),
164 ICS_DOUBLE("Synth Playback Volume", 0, SNDRV_ICS_GF1_DEV),
165 GF1_SINGLE("Line Switch", 0, 0, 1),
166 ICS_DOUBLE("Line Playback Volume", 0, SNDRV_ICS_LINE_DEV),
167 GF1_SINGLE("Mic Switch", 0, 2, 0),
168 ICS_DOUBLE("Mic Playback Volume", 0, SNDRV_ICS_MIC_DEV),
169 ICS_DOUBLE("CD Playback Volume", 0, SNDRV_ICS_CD_DEV)
170 };
171
172 int snd_gf1_new_mixer(snd_gus_card_t * gus)
173 {
174         snd_card_t *card;
175         unsigned int idx, max;
176         int err;
177
178         snd_assert(gus != NULL, return -EINVAL);
179         card = gus->card;
180         snd_assert(card != NULL, return -EINVAL);
181
182         if (gus->ics_flag)
183                 snd_component_add(card, "ICS2101");
184         if (card->mixername[0] == '\0') {
185                 strcpy(card->mixername, gus->ics_flag ? "GF1,ICS2101" : "GF1");
186         } else {
187                 if (gus->ics_flag)
188                         strcat(card->mixername, ",ICS2101");
189                 strcat(card->mixername, ",GF1");
190         }
191
192         if (!gus->ics_flag) {
193                 max = gus->ess_flag ? 1 : GF1_CONTROLS;
194                 for (idx = 0; idx < max; idx++) {
195                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_gf1_controls[idx], gus))) < 0)
196                                 return err;
197                 }
198         } else {
199                 for (idx = 0; idx < ICS_CONTROLS; idx++) {
200                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ics_controls[idx], gus))) < 0)
201                                 return err;
202                 }
203         }
204         return 0;
205 }