1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PMac DACA lowlevel functions
5 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
9 #include <linux/init.h>
10 #include <linux/i2c.h>
11 #include <linux/kmod.h>
12 #include <linux/slab.h>
13 #include <sound/core.h>
17 #define DACA_I2C_ADDR 0x4d
20 #define DACA_REG_SR 0x01
21 #define DACA_REG_AVOL 0x02
22 #define DACA_REG_GCFG 0x03
24 /* maximum volume value */
25 #define DACA_VOL_MAX 0x38
29 struct pmac_keywest i2c;
30 int left_vol, right_vol;
31 unsigned int deemphasis : 1;
32 unsigned int amp_on : 1;
37 * initialize / detect DACA
39 static int daca_init_client(struct pmac_keywest *i2c)
41 unsigned short wdata = 0x00;
42 /* SR: no swap, 1bit delay, 32-48kHz */
43 /* GCFG: power amp inverted, DAC on */
44 if (i2c_smbus_write_byte_data(i2c->client, DACA_REG_SR, 0x08) < 0 ||
45 i2c_smbus_write_byte_data(i2c->client, DACA_REG_GCFG, 0x05) < 0)
47 return i2c_smbus_write_block_data(i2c->client, DACA_REG_AVOL,
48 2, (unsigned char*)&wdata);
54 static int daca_set_volume(struct pmac_daca *mix)
56 unsigned char data[2];
58 if (! mix->i2c.client)
61 if (mix->left_vol > DACA_VOL_MAX)
62 data[0] = DACA_VOL_MAX;
64 data[0] = mix->left_vol;
65 if (mix->right_vol > DACA_VOL_MAX)
66 data[1] = DACA_VOL_MAX;
68 data[1] = mix->right_vol;
69 data[1] |= mix->deemphasis ? 0x40 : 0;
70 if (i2c_smbus_write_block_data(mix->i2c.client, DACA_REG_AVOL,
72 snd_printk(KERN_ERR "failed to set volume \n");
79 /* deemphasis switch */
80 #define daca_info_deemphasis snd_ctl_boolean_mono_info
82 static int daca_get_deemphasis(struct snd_kcontrol *kcontrol,
83 struct snd_ctl_elem_value *ucontrol)
85 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
86 struct pmac_daca *mix;
87 mix = chip->mixer_data;
90 ucontrol->value.integer.value[0] = mix->deemphasis ? 1 : 0;
94 static int daca_put_deemphasis(struct snd_kcontrol *kcontrol,
95 struct snd_ctl_elem_value *ucontrol)
97 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
98 struct pmac_daca *mix;
101 mix = chip->mixer_data;
104 change = mix->deemphasis != ucontrol->value.integer.value[0];
106 mix->deemphasis = !!ucontrol->value.integer.value[0];
107 daca_set_volume(mix);
113 static int daca_info_volume(struct snd_kcontrol *kcontrol,
114 struct snd_ctl_elem_info *uinfo)
116 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
118 uinfo->value.integer.min = 0;
119 uinfo->value.integer.max = DACA_VOL_MAX;
123 static int daca_get_volume(struct snd_kcontrol *kcontrol,
124 struct snd_ctl_elem_value *ucontrol)
126 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
127 struct pmac_daca *mix;
128 mix = chip->mixer_data;
131 ucontrol->value.integer.value[0] = mix->left_vol;
132 ucontrol->value.integer.value[1] = mix->right_vol;
136 static int daca_put_volume(struct snd_kcontrol *kcontrol,
137 struct snd_ctl_elem_value *ucontrol)
139 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
140 struct pmac_daca *mix;
144 mix = chip->mixer_data;
147 vol[0] = ucontrol->value.integer.value[0];
148 vol[1] = ucontrol->value.integer.value[1];
149 if (vol[0] > DACA_VOL_MAX || vol[1] > DACA_VOL_MAX)
151 change = mix->left_vol != vol[0] ||
152 mix->right_vol != vol[1];
154 mix->left_vol = vol[0];
155 mix->right_vol = vol[1];
156 daca_set_volume(mix);
161 /* amplifier switch */
162 #define daca_info_amp daca_info_deemphasis
164 static int daca_get_amp(struct snd_kcontrol *kcontrol,
165 struct snd_ctl_elem_value *ucontrol)
167 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
168 struct pmac_daca *mix;
169 mix = chip->mixer_data;
172 ucontrol->value.integer.value[0] = mix->amp_on ? 1 : 0;
176 static int daca_put_amp(struct snd_kcontrol *kcontrol,
177 struct snd_ctl_elem_value *ucontrol)
179 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
180 struct pmac_daca *mix;
183 mix = chip->mixer_data;
186 change = mix->amp_on != ucontrol->value.integer.value[0];
188 mix->amp_on = !!ucontrol->value.integer.value[0];
189 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
190 mix->amp_on ? 0x05 : 0x04);
195 static const struct snd_kcontrol_new daca_mixers[] = {
196 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
197 .name = "Deemphasis Switch",
198 .info = daca_info_deemphasis,
199 .get = daca_get_deemphasis,
200 .put = daca_put_deemphasis
202 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
203 .name = "Master Playback Volume",
204 .info = daca_info_volume,
205 .get = daca_get_volume,
206 .put = daca_put_volume
208 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
209 .name = "Power Amplifier Switch",
210 .info = daca_info_amp,
218 static void daca_resume(struct snd_pmac *chip)
220 struct pmac_daca *mix = chip->mixer_data;
221 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_SR, 0x08);
222 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
223 mix->amp_on ? 0x05 : 0x04);
224 daca_set_volume(mix);
226 #endif /* CONFIG_PM */
229 static void daca_cleanup(struct snd_pmac *chip)
231 struct pmac_daca *mix = chip->mixer_data;
234 snd_pmac_keywest_cleanup(&mix->i2c);
236 chip->mixer_data = NULL;
240 int snd_pmac_daca_init(struct snd_pmac *chip)
243 struct pmac_daca *mix;
245 request_module("i2c-powermac");
247 mix = kzalloc(sizeof(*mix), GFP_KERNEL);
250 chip->mixer_data = mix;
251 chip->mixer_free = daca_cleanup;
252 mix->amp_on = 1; /* default on */
254 mix->i2c.addr = DACA_I2C_ADDR;
255 mix->i2c.init_client = daca_init_client;
256 mix->i2c.name = "DACA";
257 err = snd_pmac_keywest_init(&mix->i2c);
264 strcpy(chip->card->mixername, "PowerMac DACA");
266 for (i = 0; i < ARRAY_SIZE(daca_mixers); i++) {
267 err = snd_ctl_add(chip->card, snd_ctl_new1(&daca_mixers[i], chip));
273 chip->resume = daca_resume;