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/*
30 *
31 */
32
33#define GF1_SINGLE(xname, xindex, shift, invert) \
34{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
35  .info = snd_gf1_info_single, \
36  .get = snd_gf1_get_single, .put = snd_gf1_put_single, \
37  .private_value = shift | (invert << 8) }
38
39static int snd_gf1_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
40{
41	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
42	uinfo->count = 1;
43	uinfo->value.integer.min = 0;
44	uinfo->value.integer.max = 1;
45	return 0;
46}
47
48static int snd_gf1_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
49{
50	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
51	int shift = kcontrol->private_value & 0xff;
52	int invert = (kcontrol->private_value >> 8) & 1;
53
54	ucontrol->value.integer.value[0] = (gus->mix_cntrl_reg >> shift) & 1;
55	if (invert)
56		ucontrol->value.integer.value[0] ^= 1;
57	return 0;
58}
59
60static int snd_gf1_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
61{
62	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
63	unsigned long flags;
64	int shift = kcontrol->private_value & 0xff;
65	int invert = (kcontrol->private_value >> 8) & 1;
66	int change;
67	unsigned char oval, nval;
68
69	nval = ucontrol->value.integer.value[0] & 1;
70	if (invert)
71		nval ^= 1;
72	nval <<= shift;
73	spin_lock_irqsave(&gus->reg_lock, flags);
74	oval = gus->mix_cntrl_reg;
75	nval = (oval & ~(1 << shift)) | nval;
76	change = nval != oval;
77	outb(gus->mix_cntrl_reg = nval, GUSP(gus, MIXCNTRLREG));
78	outb(gus->gf1.active_voice = 0, GUSP(gus, GF1PAGE));
79	spin_unlock_irqrestore(&gus->reg_lock, flags);
80	return change;
81}
82
83#define ICS_DOUBLE(xname, xindex, addr) \
84{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
85  .info = snd_ics_info_double, \
86  .get = snd_ics_get_double, .put = snd_ics_put_double, \
87  .private_value = addr }
88
89static int snd_ics_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
90{
91	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
92	uinfo->count = 2;
93	uinfo->value.integer.min = 0;
94	uinfo->value.integer.max = 127;
95	return 0;
96}
97
98static int snd_ics_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
99{
100	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
101	unsigned long flags;
102	int addr = kcontrol->private_value & 0xff;
103	unsigned char left, right;
104
105	spin_lock_irqsave(&gus->reg_lock, flags);
106	left = gus->gf1.ics_regs[addr][0];
107	right = gus->gf1.ics_regs[addr][1];
108	spin_unlock_irqrestore(&gus->reg_lock, flags);
109	ucontrol->value.integer.value[0] = left & 127;
110	ucontrol->value.integer.value[1] = right & 127;
111	return 0;
112}
113
114static int snd_ics_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
115{
116	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
117	unsigned long flags;
118	int addr = kcontrol->private_value & 0xff;
119	int change;
120	unsigned char val1, val2, oval1, oval2, tmp;
121
122	val1 = ucontrol->value.integer.value[0] & 127;
123	val2 = ucontrol->value.integer.value[1] & 127;
124	spin_lock_irqsave(&gus->reg_lock, flags);
125	oval1 = gus->gf1.ics_regs[addr][0];
126	oval2 = gus->gf1.ics_regs[addr][1];
127	change = val1 != oval1 || val2 != oval2;
128	gus->gf1.ics_regs[addr][0] = val1;
129	gus->gf1.ics_regs[addr][1] = val2;
130	if (gus->ics_flag && gus->ics_flipped &&
131	    (addr == SNDRV_ICS_GF1_DEV || addr == SNDRV_ICS_MASTER_DEV)) {
132		tmp = val1;
133		val1 = val2;
134		val2 = tmp;
135	}
136	addr <<= 3;
137	outb(addr | 0, GUSP(gus, MIXCNTRLPORT));
138	outb(1, GUSP(gus, MIXDATAPORT));
139	outb(addr | 2, GUSP(gus, MIXCNTRLPORT));
140	outb((unsigned char) val1, GUSP(gus, MIXDATAPORT));
141	outb(addr | 1, GUSP(gus, MIXCNTRLPORT));
142	outb(2, GUSP(gus, MIXDATAPORT));
143	outb(addr | 3, GUSP(gus, MIXCNTRLPORT));
144	outb((unsigned char) val2, GUSP(gus, MIXDATAPORT));
145	spin_unlock_irqrestore(&gus->reg_lock, flags);
146	return change;
147}
148
149static struct snd_kcontrol_new snd_gf1_controls[] = {
150GF1_SINGLE("Master Playback Switch", 0, 1, 1),
151GF1_SINGLE("Line Switch", 0, 0, 1),
152GF1_SINGLE("Mic Switch", 0, 2, 0)
153};
154
155static struct snd_kcontrol_new snd_ics_controls[] = {
156GF1_SINGLE("Master Playback Switch", 0, 1, 1),
157ICS_DOUBLE("Master Playback Volume", 0, SNDRV_ICS_MASTER_DEV),
158ICS_DOUBLE("Synth Playback Volume", 0, SNDRV_ICS_GF1_DEV),
159GF1_SINGLE("Line Switch", 0, 0, 1),
160ICS_DOUBLE("Line Playback Volume", 0, SNDRV_ICS_LINE_DEV),
161GF1_SINGLE("Mic Switch", 0, 2, 0),
162ICS_DOUBLE("Mic Playback Volume", 0, SNDRV_ICS_MIC_DEV),
163ICS_DOUBLE("CD Playback Volume", 0, SNDRV_ICS_CD_DEV)
164};
165
166int snd_gf1_new_mixer(struct snd_gus_card * gus)
167{
168	struct snd_card *card;
169	unsigned int idx, max;
170	int err;
171
172	snd_assert(gus != NULL, return -EINVAL);
173	card = gus->card;
174	snd_assert(card != NULL, return -EINVAL);
175
176	if (gus->ics_flag)
177		snd_component_add(card, "ICS2101");
178	if (card->mixername[0] == '\0') {
179		strcpy(card->mixername, gus->ics_flag ? "GF1,ICS2101" : "GF1");
180	} else {
181		if (gus->ics_flag)
182			strcat(card->mixername, ",ICS2101");
183		strcat(card->mixername, ",GF1");
184	}
185
186	if (!gus->ics_flag) {
187		max = gus->ess_flag ? 1 : ARRAY_SIZE(snd_gf1_controls);
188		for (idx = 0; idx < max; idx++) {
189			if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_gf1_controls[idx], gus))) < 0)
190				return err;
191		}
192	} else {
193		for (idx = 0; idx < ARRAY_SIZE(snd_ics_controls); idx++) {
194			if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ics_controls[idx], gus))) < 0)
195				return err;
196		}
197	}
198	return 0;
199}
200