2 * Mixer controls for the Xonar DG/DGX
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Copyright (c) Roman Volkov <v1ron@mail.ru>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2.
10 * This driver is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this driver; if not, see <http://www.gnu.org/licenses/>.
19 #include <linux/pci.h>
20 #include <linux/delay.h>
21 #include <sound/control.h>
22 #include <sound/core.h>
23 #include <sound/info.h>
24 #include <sound/pcm.h>
25 #include <sound/tlv.h>
30 /* analog output select */
32 static int output_select_apply(struct oxygen *chip)
34 struct dg *data = chip->model_data;
36 data->cs4245_shadow[CS4245_SIGNAL_SEL] &= ~CS4245_A_OUT_SEL_MASK;
37 if (data->output_sel == PLAYBACK_DST_HP) {
38 /* mute FP (aux output) amplifier, switch rear jack to CS4245 */
39 oxygen_set_bits8(chip, OXYGEN_GPIO_DATA, GPIO_HP_REAR);
40 } else if (data->output_sel == PLAYBACK_DST_HP_FP) {
42 * Unmute FP amplifier, switch rear jack to CS4361;
43 * I2S channels 2,3,4 should be inactive.
45 oxygen_clear_bits8(chip, OXYGEN_GPIO_DATA, GPIO_HP_REAR);
46 data->cs4245_shadow[CS4245_SIGNAL_SEL] |= CS4245_A_OUT_SEL_DAC;
49 * 2.0, 4.0, 5.1: switch to CS4361, mute FP amp.,
50 * and change playback routing.
52 oxygen_clear_bits8(chip, OXYGEN_GPIO_DATA, GPIO_HP_REAR);
54 return cs4245_write_spi(chip, CS4245_SIGNAL_SEL);
57 static int output_select_info(struct snd_kcontrol *ctl,
58 struct snd_ctl_elem_info *info)
60 static const char *const names[3] = {
62 "Stereo Headphones FP",
66 return snd_ctl_enum_info(info, 1, 3, names);
69 static int output_select_get(struct snd_kcontrol *ctl,
70 struct snd_ctl_elem_value *value)
72 struct oxygen *chip = ctl->private_data;
73 struct dg *data = chip->model_data;
75 mutex_lock(&chip->mutex);
76 value->value.enumerated.item[0] = data->output_sel;
77 mutex_unlock(&chip->mutex);
81 static int output_select_put(struct snd_kcontrol *ctl,
82 struct snd_ctl_elem_value *value)
84 struct oxygen *chip = ctl->private_data;
85 struct dg *data = chip->model_data;
86 unsigned int new = value->value.enumerated.item[0];
90 mutex_lock(&chip->mutex);
91 if (data->output_sel != new) {
92 data->output_sel = new;
93 ret = output_select_apply(chip);
94 changed = ret >= 0 ? 1 : ret;
95 oxygen_update_dac_routing(chip);
97 mutex_unlock(&chip->mutex);
102 /* CS4245 Headphone Channels A&B Volume Control */
104 static int hp_stereo_volume_info(struct snd_kcontrol *ctl,
105 struct snd_ctl_elem_info *info)
107 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
109 info->value.integer.min = 0;
110 info->value.integer.max = 255;
114 static int hp_stereo_volume_get(struct snd_kcontrol *ctl,
115 struct snd_ctl_elem_value *val)
117 struct oxygen *chip = ctl->private_data;
118 struct dg *data = chip->model_data;
121 mutex_lock(&chip->mutex);
122 tmp = (~data->cs4245_shadow[CS4245_DAC_A_CTRL]) & 255;
123 val->value.integer.value[0] = tmp;
124 tmp = (~data->cs4245_shadow[CS4245_DAC_B_CTRL]) & 255;
125 val->value.integer.value[1] = tmp;
126 mutex_unlock(&chip->mutex);
130 static int hp_stereo_volume_put(struct snd_kcontrol *ctl,
131 struct snd_ctl_elem_value *val)
133 struct oxygen *chip = ctl->private_data;
134 struct dg *data = chip->model_data;
137 long new1 = val->value.integer.value[0];
138 long new2 = val->value.integer.value[1];
140 if ((new1 > 255) || (new1 < 0) || (new2 > 255) || (new2 < 0))
143 mutex_lock(&chip->mutex);
144 if ((data->cs4245_shadow[CS4245_DAC_A_CTRL] != ~new1) ||
145 (data->cs4245_shadow[CS4245_DAC_B_CTRL] != ~new2)) {
146 data->cs4245_shadow[CS4245_DAC_A_CTRL] = ~new1;
147 data->cs4245_shadow[CS4245_DAC_B_CTRL] = ~new2;
148 ret = cs4245_write_spi(chip, CS4245_DAC_A_CTRL);
150 ret = cs4245_write_spi(chip, CS4245_DAC_B_CTRL);
151 changed = ret >= 0 ? 1 : ret;
153 mutex_unlock(&chip->mutex);
160 static int hp_mute_get(struct snd_kcontrol *ctl,
161 struct snd_ctl_elem_value *val)
163 struct oxygen *chip = ctl->private_data;
164 struct dg *data = chip->model_data;
166 mutex_lock(&chip->mutex);
167 val->value.integer.value[0] =
168 !(data->cs4245_shadow[CS4245_DAC_CTRL_1] & CS4245_MUTE_DAC);
169 mutex_unlock(&chip->mutex);
173 static int hp_mute_put(struct snd_kcontrol *ctl,
174 struct snd_ctl_elem_value *val)
176 struct oxygen *chip = ctl->private_data;
177 struct dg *data = chip->model_data;
181 if (val->value.integer.value[0] > 1)
183 mutex_lock(&chip->mutex);
184 data->cs4245_shadow[CS4245_DAC_CTRL_1] &= ~CS4245_MUTE_DAC;
185 data->cs4245_shadow[CS4245_DAC_CTRL_1] |=
186 (~val->value.integer.value[0] << 2) & CS4245_MUTE_DAC;
187 ret = cs4245_write_spi(chip, CS4245_DAC_CTRL_1);
188 changed = ret >= 0 ? 1 : ret;
189 mutex_unlock(&chip->mutex);
193 /* capture volume for all sources */
195 static int input_volume_apply(struct oxygen *chip, char left, char right)
197 struct dg *data = chip->model_data;
200 data->cs4245_shadow[CS4245_PGA_A_CTRL] = left;
201 data->cs4245_shadow[CS4245_PGA_B_CTRL] = right;
202 ret = cs4245_write_spi(chip, CS4245_PGA_A_CTRL);
205 return cs4245_write_spi(chip, CS4245_PGA_B_CTRL);
208 static int input_vol_info(struct snd_kcontrol *ctl,
209 struct snd_ctl_elem_info *info)
211 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
213 info->value.integer.min = 2 * -12;
214 info->value.integer.max = 2 * 12;
218 static int input_vol_get(struct snd_kcontrol *ctl,
219 struct snd_ctl_elem_value *value)
221 struct oxygen *chip = ctl->private_data;
222 struct dg *data = chip->model_data;
223 unsigned int idx = ctl->private_value;
225 mutex_lock(&chip->mutex);
226 value->value.integer.value[0] = data->input_vol[idx][0];
227 value->value.integer.value[1] = data->input_vol[idx][1];
228 mutex_unlock(&chip->mutex);
232 static int input_vol_put(struct snd_kcontrol *ctl,
233 struct snd_ctl_elem_value *value)
235 struct oxygen *chip = ctl->private_data;
236 struct dg *data = chip->model_data;
237 unsigned int idx = ctl->private_value;
241 if (value->value.integer.value[0] < 2 * -12 ||
242 value->value.integer.value[0] > 2 * 12 ||
243 value->value.integer.value[1] < 2 * -12 ||
244 value->value.integer.value[1] > 2 * 12)
246 mutex_lock(&chip->mutex);
247 changed = data->input_vol[idx][0] != value->value.integer.value[0] ||
248 data->input_vol[idx][1] != value->value.integer.value[1];
250 data->input_vol[idx][0] = value->value.integer.value[0];
251 data->input_vol[idx][1] = value->value.integer.value[1];
252 if (idx == data->input_sel) {
253 ret = input_volume_apply(chip,
254 data->input_vol[idx][0],
255 data->input_vol[idx][1]);
257 changed = ret >= 0 ? 1 : ret;
259 mutex_unlock(&chip->mutex);
263 static int input_sel_info(struct snd_kcontrol *ctl,
264 struct snd_ctl_elem_info *info)
266 static const char *const names[4] = {
267 "Mic", "Aux", "Front Mic", "Line"
270 return snd_ctl_enum_info(info, 1, 4, names);
273 static int input_sel_get(struct snd_kcontrol *ctl,
274 struct snd_ctl_elem_value *value)
276 struct oxygen *chip = ctl->private_data;
277 struct dg *data = chip->model_data;
279 mutex_lock(&chip->mutex);
280 value->value.enumerated.item[0] = data->input_sel;
281 mutex_unlock(&chip->mutex);
285 static int input_sel_put(struct snd_kcontrol *ctl,
286 struct snd_ctl_elem_value *value)
288 static const u8 sel_values[4] = {
294 struct oxygen *chip = ctl->private_data;
295 struct dg *data = chip->model_data;
298 if (value->value.enumerated.item[0] > 3)
301 mutex_lock(&chip->mutex);
302 changed = value->value.enumerated.item[0] != data->input_sel;
304 data->input_sel = value->value.enumerated.item[0];
306 cs4245_write(chip, CS4245_ANALOG_IN,
307 (data->cs4245_shadow[CS4245_ANALOG_IN] &
309 sel_values[data->input_sel]);
311 cs4245_write_cached(chip, CS4245_PGA_A_CTRL,
312 data->input_vol[data->input_sel][0]);
313 cs4245_write_cached(chip, CS4245_PGA_B_CTRL,
314 data->input_vol[data->input_sel][1]);
316 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
317 data->input_sel ? 0 : GPIO_INPUT_ROUTE,
320 mutex_unlock(&chip->mutex);
324 static int hpf_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
326 static const char *const names[2] = { "Active", "Frozen" };
328 return snd_ctl_enum_info(info, 1, 2, names);
331 static int hpf_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
333 struct oxygen *chip = ctl->private_data;
334 struct dg *data = chip->model_data;
336 value->value.enumerated.item[0] =
337 !!(data->cs4245_shadow[CS4245_ADC_CTRL] & CS4245_HPF_FREEZE);
341 static int hpf_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
343 struct oxygen *chip = ctl->private_data;
344 struct dg *data = chip->model_data;
348 mutex_lock(&chip->mutex);
349 reg = data->cs4245_shadow[CS4245_ADC_CTRL] & ~CS4245_HPF_FREEZE;
350 if (value->value.enumerated.item[0])
351 reg |= CS4245_HPF_FREEZE;
352 changed = reg != data->cs4245_shadow[CS4245_ADC_CTRL];
354 cs4245_write(chip, CS4245_ADC_CTRL, reg);
355 mutex_unlock(&chip->mutex);
359 #define INPUT_VOLUME(xname, index) { \
360 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
362 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
363 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
364 .info = input_vol_info, \
365 .get = input_vol_get, \
366 .put = input_vol_put, \
367 .tlv = { .p = pga_db_scale }, \
368 .private_value = index, \
370 static const DECLARE_TLV_DB_MINMAX(hp_db_scale, -12550, 0);
371 static const DECLARE_TLV_DB_MINMAX(pga_db_scale, -1200, 1200);
372 static const struct snd_kcontrol_new dg_controls[] = {
374 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
375 .name = "Analog Output Playback Enum",
376 .info = output_select_info,
377 .get = output_select_get,
378 .put = output_select_put,
381 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
382 .name = "Headphone Playback Volume",
383 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
384 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
385 .info = hp_stereo_volume_info,
386 .get = hp_stereo_volume_get,
387 .put = hp_stereo_volume_put,
388 .tlv = { .p = hp_db_scale, },
391 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
392 .name = "Headphone Playback Switch",
393 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
394 .info = snd_ctl_boolean_mono_info,
398 INPUT_VOLUME("Mic Capture Volume", 0),
399 INPUT_VOLUME("Aux Capture Volume", 1),
400 INPUT_VOLUME("Front Mic Capture Volume", 2),
401 INPUT_VOLUME("Line Capture Volume", 3),
403 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
404 .name = "Capture Source",
405 .info = input_sel_info,
406 .get = input_sel_get,
407 .put = input_sel_put,
410 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
411 .name = "ADC High-pass Filter Capture Enum",
418 static int dg_control_filter(struct snd_kcontrol_new *template)
420 if (!strncmp(template->name, "Master Playback ", 16))
425 static int dg_mixer_init(struct oxygen *chip)
430 for (i = 0; i < ARRAY_SIZE(dg_controls); ++i) {
431 err = snd_ctl_add(chip->card,
432 snd_ctl_new1(&dg_controls[i], chip));
439 struct oxygen_model model_xonar_dg = {
440 .longname = "C-Media Oxygen HD Audio",
443 .control_filter = dg_control_filter,
444 .mixer_init = dg_mixer_init,
445 .cleanup = dg_cleanup,
446 .suspend = dg_suspend,
448 .set_dac_params = set_cs4245_dac_params,
449 .set_adc_params = set_cs4245_adc_params,
450 .adjust_dac_routing = adjust_dg_dac_routing,
451 .dump_registers = dump_cs4245_registers,
452 .model_data_size = sizeof(struct dg),
453 .device_config = PLAYBACK_0_TO_I2S |
454 PLAYBACK_1_TO_SPDIF |
455 CAPTURE_0_FROM_I2S_1 |
456 CAPTURE_1_FROM_SPDIF,
457 .dac_channels_pcm = 6,
458 .dac_channels_mixer = 0,
459 .function_flags = OXYGEN_FUNCTION_SPI,
460 .dac_mclks = OXYGEN_MCLKS(256, 128, 128),
461 .adc_mclks = OXYGEN_MCLKS(256, 128, 128),
462 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
463 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,