2 * PMac DACA lowlevel functions
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
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.
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.
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
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/i2c.h>
25 #include <linux/i2c-dev.h>
26 #include <linux/kmod.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
32 #define DACA_I2C_ADDR 0x4d
35 #define DACA_REG_SR 0x01
36 #define DACA_REG_AVOL 0x02
37 #define DACA_REG_GCFG 0x03
39 /* maximum volume value */
40 #define DACA_VOL_MAX 0x38
44 struct pmac_keywest i2c;
45 int left_vol, right_vol;
46 unsigned int deemphasis : 1;
47 unsigned int amp_on : 1;
52 * initialize / detect DACA
54 static int daca_init_client(struct pmac_keywest *i2c)
56 unsigned short wdata = 0x00;
57 /* SR: no swap, 1bit delay, 32-48kHz */
58 /* GCFG: power amp inverted, DAC on */
59 if (i2c_smbus_write_byte_data(i2c->client, DACA_REG_SR, 0x08) < 0 ||
60 i2c_smbus_write_byte_data(i2c->client, DACA_REG_GCFG, 0x05) < 0)
62 return i2c_smbus_write_block_data(i2c->client, DACA_REG_AVOL,
63 2, (unsigned char*)&wdata);
69 static int daca_set_volume(struct pmac_daca *mix)
71 unsigned char data[2];
73 if (! mix->i2c.client)
76 if (mix->left_vol > DACA_VOL_MAX)
77 data[0] = DACA_VOL_MAX;
79 data[0] = mix->left_vol;
80 if (mix->right_vol > DACA_VOL_MAX)
81 data[1] = DACA_VOL_MAX;
83 data[1] = mix->right_vol;
84 data[1] |= mix->deemphasis ? 0x40 : 0;
85 if (i2c_smbus_write_block_data(mix->i2c.client, DACA_REG_AVOL,
87 snd_printk("failed to set volume \n");
94 /* deemphasis switch */
95 static int daca_info_deemphasis(struct snd_kcontrol *kcontrol,
96 struct snd_ctl_elem_info *uinfo)
98 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
100 uinfo->value.integer.min = 0;
101 uinfo->value.integer.max = 1;
105 static int daca_get_deemphasis(struct snd_kcontrol *kcontrol,
106 struct snd_ctl_elem_value *ucontrol)
108 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
109 struct pmac_daca *mix;
110 if (! (mix = chip->mixer_data))
112 ucontrol->value.integer.value[0] = mix->deemphasis ? 1 : 0;
116 static int daca_put_deemphasis(struct snd_kcontrol *kcontrol,
117 struct snd_ctl_elem_value *ucontrol)
119 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
120 struct pmac_daca *mix;
123 if (! (mix = chip->mixer_data))
125 change = mix->deemphasis != ucontrol->value.integer.value[0];
127 mix->deemphasis = ucontrol->value.integer.value[0];
128 daca_set_volume(mix);
134 static int daca_info_volume(struct snd_kcontrol *kcontrol,
135 struct snd_ctl_elem_info *uinfo)
137 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
139 uinfo->value.integer.min = 0;
140 uinfo->value.integer.max = DACA_VOL_MAX;
144 static int daca_get_volume(struct snd_kcontrol *kcontrol,
145 struct snd_ctl_elem_value *ucontrol)
147 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
148 struct pmac_daca *mix;
149 if (! (mix = chip->mixer_data))
151 ucontrol->value.integer.value[0] = mix->left_vol;
152 ucontrol->value.integer.value[1] = mix->right_vol;
156 static int daca_put_volume(struct snd_kcontrol *kcontrol,
157 struct snd_ctl_elem_value *ucontrol)
159 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
160 struct pmac_daca *mix;
163 if (! (mix = chip->mixer_data))
165 change = mix->left_vol != ucontrol->value.integer.value[0] ||
166 mix->right_vol != ucontrol->value.integer.value[1];
168 mix->left_vol = ucontrol->value.integer.value[0];
169 mix->right_vol = ucontrol->value.integer.value[1];
170 daca_set_volume(mix);
175 /* amplifier switch */
176 #define daca_info_amp daca_info_deemphasis
178 static int daca_get_amp(struct snd_kcontrol *kcontrol,
179 struct snd_ctl_elem_value *ucontrol)
181 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
182 struct pmac_daca *mix;
183 if (! (mix = chip->mixer_data))
185 ucontrol->value.integer.value[0] = mix->amp_on ? 1 : 0;
189 static int daca_put_amp(struct snd_kcontrol *kcontrol,
190 struct snd_ctl_elem_value *ucontrol)
192 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
193 struct pmac_daca *mix;
196 if (! (mix = chip->mixer_data))
198 change = mix->amp_on != ucontrol->value.integer.value[0];
200 mix->amp_on = ucontrol->value.integer.value[0];
201 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
202 mix->amp_on ? 0x05 : 0x04);
207 static struct snd_kcontrol_new daca_mixers[] = {
208 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
209 .name = "Deemphasis Switch",
210 .info = daca_info_deemphasis,
211 .get = daca_get_deemphasis,
212 .put = daca_put_deemphasis
214 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
215 .name = "Master Playback Volume",
216 .info = daca_info_volume,
217 .get = daca_get_volume,
218 .put = daca_put_volume
220 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
221 .name = "Power Amplifier Switch",
222 .info = daca_info_amp,
230 static void daca_resume(struct snd_pmac *chip)
232 struct pmac_daca *mix = chip->mixer_data;
233 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_SR, 0x08);
234 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
235 mix->amp_on ? 0x05 : 0x04);
236 daca_set_volume(mix);
238 #endif /* CONFIG_PM */
241 static void daca_cleanup(struct snd_pmac *chip)
243 struct pmac_daca *mix = chip->mixer_data;
246 snd_pmac_keywest_cleanup(&mix->i2c);
248 chip->mixer_data = NULL;
252 int __init snd_pmac_daca_init(struct snd_pmac *chip)
255 struct pmac_daca *mix;
258 if (current->fs->root)
259 request_module("i2c-keywest");
260 #endif /* CONFIG_KMOD */
262 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
265 memset(mix, 0, sizeof(*mix));
266 chip->mixer_data = mix;
267 chip->mixer_free = daca_cleanup;
268 mix->amp_on = 1; /* default on */
270 mix->i2c.addr = DACA_I2C_ADDR;
271 mix->i2c.init_client = daca_init_client;
272 mix->i2c.name = "DACA";
273 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
279 strcpy(chip->card->mixername, "PowerMac DACA");
281 for (i = 0; i < ARRAY_SIZE(daca_mixers); i++) {
282 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&daca_mixers[i], chip))) < 0)
287 chip->resume = daca_resume;