2 * C-Media CMI8788 driver - mixer code
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/mutex.h>
21 #include <sound/ac97_codec.h>
22 #include <sound/asoundef.h>
23 #include <sound/control.h>
24 #include <sound/tlv.h>
28 static int dac_volume_info(struct snd_kcontrol *ctl,
29 struct snd_ctl_elem_info *info)
31 struct oxygen *chip = ctl->private_data;
33 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
34 info->count = chip->model->dac_channels;
35 info->value.integer.min = chip->model->dac_volume_min;
36 info->value.integer.max = chip->model->dac_volume_max;
40 static int dac_volume_get(struct snd_kcontrol *ctl,
41 struct snd_ctl_elem_value *value)
43 struct oxygen *chip = ctl->private_data;
46 mutex_lock(&chip->mutex);
47 for (i = 0; i < chip->model->dac_channels; ++i)
48 value->value.integer.value[i] = chip->dac_volume[i];
49 mutex_unlock(&chip->mutex);
53 static int dac_volume_put(struct snd_kcontrol *ctl,
54 struct snd_ctl_elem_value *value)
56 struct oxygen *chip = ctl->private_data;
61 mutex_lock(&chip->mutex);
62 for (i = 0; i < chip->model->dac_channels; ++i)
63 if (value->value.integer.value[i] != chip->dac_volume[i]) {
64 chip->dac_volume[i] = value->value.integer.value[i];
68 chip->model->update_dac_volume(chip);
69 mutex_unlock(&chip->mutex);
73 static int dac_mute_get(struct snd_kcontrol *ctl,
74 struct snd_ctl_elem_value *value)
76 struct oxygen *chip = ctl->private_data;
78 mutex_lock(&chip->mutex);
79 value->value.integer.value[0] = !chip->dac_mute;
80 mutex_unlock(&chip->mutex);
84 static int dac_mute_put(struct snd_kcontrol *ctl,
85 struct snd_ctl_elem_value *value)
87 struct oxygen *chip = ctl->private_data;
90 mutex_lock(&chip->mutex);
91 changed = !value->value.integer.value[0] != chip->dac_mute;
93 chip->dac_mute = !value->value.integer.value[0];
94 chip->model->update_dac_mute(chip);
96 mutex_unlock(&chip->mutex);
100 static int upmix_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
102 static const char *const names[3] = {
103 "Front", "Front+Surround", "Front+Surround+Back"
105 struct oxygen *chip = ctl->private_data;
106 unsigned int count = 2 + (chip->model->dac_channels == 8);
108 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
110 info->value.enumerated.items = count;
111 if (info->value.enumerated.item >= count)
112 info->value.enumerated.item = count - 1;
113 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
117 static int upmix_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
119 struct oxygen *chip = ctl->private_data;
121 mutex_lock(&chip->mutex);
122 value->value.enumerated.item[0] = chip->dac_routing;
123 mutex_unlock(&chip->mutex);
127 void oxygen_update_dac_routing(struct oxygen *chip)
129 /* DAC 0: front, DAC 1: surround, DAC 2: center/LFE, DAC 3: back */
130 static const unsigned int reg_values[3] = {
131 /* stereo -> front */
132 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
133 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
134 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
135 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
136 /* stereo -> front+surround */
137 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
138 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
139 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
140 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
141 /* stereo -> front+surround+back */
142 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
143 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
144 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
145 (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
148 unsigned int reg_value;
150 channels = oxygen_read8(chip, OXYGEN_PLAY_CHANNELS) &
151 OXYGEN_PLAY_CHANNELS_MASK;
152 if (channels == OXYGEN_PLAY_CHANNELS_2)
153 reg_value = reg_values[chip->dac_routing];
154 else if (channels == OXYGEN_PLAY_CHANNELS_8)
155 /* in 7.1 mode, "rear" channels go to the "back" jack */
156 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
157 (3 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
158 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
159 (1 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
161 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
162 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
163 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
164 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
165 oxygen_write16_masked(chip, OXYGEN_PLAY_ROUTING, reg_value,
166 OXYGEN_PLAY_DAC0_SOURCE_MASK |
167 OXYGEN_PLAY_DAC1_SOURCE_MASK |
168 OXYGEN_PLAY_DAC2_SOURCE_MASK |
169 OXYGEN_PLAY_DAC3_SOURCE_MASK);
172 static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
174 struct oxygen *chip = ctl->private_data;
175 unsigned int count = 2 + (chip->model->dac_channels == 8);
178 mutex_lock(&chip->mutex);
179 changed = value->value.enumerated.item[0] != chip->dac_routing;
181 chip->dac_routing = min(value->value.enumerated.item[0],
183 spin_lock_irq(&chip->reg_lock);
184 oxygen_update_dac_routing(chip);
185 spin_unlock_irq(&chip->reg_lock);
187 mutex_unlock(&chip->mutex);
191 static int spdif_switch_get(struct snd_kcontrol *ctl,
192 struct snd_ctl_elem_value *value)
194 struct oxygen *chip = ctl->private_data;
196 mutex_lock(&chip->mutex);
197 value->value.integer.value[0] = chip->spdif_playback_enable;
198 mutex_unlock(&chip->mutex);
202 static unsigned int oxygen_spdif_rate(unsigned int oxygen_rate)
204 switch (oxygen_rate) {
205 case OXYGEN_RATE_32000:
206 return IEC958_AES3_CON_FS_32000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
207 case OXYGEN_RATE_44100:
208 return IEC958_AES3_CON_FS_44100 << OXYGEN_SPDIF_CS_RATE_SHIFT;
209 default: /* OXYGEN_RATE_48000 */
210 return IEC958_AES3_CON_FS_48000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
211 case OXYGEN_RATE_64000:
212 return 0xb << OXYGEN_SPDIF_CS_RATE_SHIFT;
213 case OXYGEN_RATE_88200:
214 return 0x8 << OXYGEN_SPDIF_CS_RATE_SHIFT;
215 case OXYGEN_RATE_96000:
216 return 0xa << OXYGEN_SPDIF_CS_RATE_SHIFT;
217 case OXYGEN_RATE_176400:
218 return 0xc << OXYGEN_SPDIF_CS_RATE_SHIFT;
219 case OXYGEN_RATE_192000:
220 return 0xe << OXYGEN_SPDIF_CS_RATE_SHIFT;
224 void oxygen_update_spdif_source(struct oxygen *chip)
226 u32 old_control, new_control;
227 u16 old_routing, new_routing;
228 unsigned int oxygen_rate;
230 old_control = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
231 old_routing = oxygen_read16(chip, OXYGEN_PLAY_ROUTING);
232 if (chip->pcm_active & (1 << PCM_SPDIF)) {
233 new_control = old_control | OXYGEN_SPDIF_OUT_ENABLE;
234 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
235 | OXYGEN_PLAY_SPDIF_SPDIF;
236 oxygen_rate = (old_control >> OXYGEN_SPDIF_OUT_RATE_SHIFT)
237 & OXYGEN_I2S_RATE_MASK;
238 /* S/PDIF rate was already set by the caller */
239 } else if ((chip->pcm_active & (1 << PCM_MULTICH)) &&
240 chip->spdif_playback_enable) {
241 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
242 | OXYGEN_PLAY_SPDIF_MULTICH_01;
243 oxygen_rate = oxygen_read16(chip, OXYGEN_I2S_MULTICH_FORMAT)
244 & OXYGEN_I2S_RATE_MASK;
245 new_control = (old_control & ~OXYGEN_SPDIF_OUT_RATE_MASK) |
246 (oxygen_rate << OXYGEN_SPDIF_OUT_RATE_SHIFT) |
247 OXYGEN_SPDIF_OUT_ENABLE;
249 new_control = old_control & ~OXYGEN_SPDIF_OUT_ENABLE;
250 new_routing = old_routing;
251 oxygen_rate = OXYGEN_RATE_44100;
253 if (old_routing != new_routing) {
254 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL,
255 new_control & ~OXYGEN_SPDIF_OUT_ENABLE);
256 oxygen_write16(chip, OXYGEN_PLAY_ROUTING, new_routing);
258 if (new_control & OXYGEN_SPDIF_OUT_ENABLE)
259 oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS,
260 oxygen_spdif_rate(oxygen_rate) |
261 ((chip->pcm_active & (1 << PCM_SPDIF)) ?
262 chip->spdif_pcm_bits : chip->spdif_bits));
263 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, new_control);
266 static int spdif_switch_put(struct snd_kcontrol *ctl,
267 struct snd_ctl_elem_value *value)
269 struct oxygen *chip = ctl->private_data;
272 mutex_lock(&chip->mutex);
273 changed = value->value.integer.value[0] != chip->spdif_playback_enable;
275 chip->spdif_playback_enable = !!value->value.integer.value[0];
276 spin_lock_irq(&chip->reg_lock);
277 oxygen_update_spdif_source(chip);
278 spin_unlock_irq(&chip->reg_lock);
280 mutex_unlock(&chip->mutex);
284 static int spdif_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
286 info->type = SNDRV_CTL_ELEM_TYPE_IEC958;
291 static void oxygen_to_iec958(u32 bits, struct snd_ctl_elem_value *value)
293 value->value.iec958.status[0] =
294 bits & (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
295 OXYGEN_SPDIF_PREEMPHASIS);
296 value->value.iec958.status[1] = /* category and original */
297 bits >> OXYGEN_SPDIF_CATEGORY_SHIFT;
300 static u32 iec958_to_oxygen(struct snd_ctl_elem_value *value)
304 bits = value->value.iec958.status[0] &
305 (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
306 OXYGEN_SPDIF_PREEMPHASIS);
307 bits |= value->value.iec958.status[1] << OXYGEN_SPDIF_CATEGORY_SHIFT;
308 if (bits & OXYGEN_SPDIF_NONAUDIO)
309 bits |= OXYGEN_SPDIF_V;
313 static inline void write_spdif_bits(struct oxygen *chip, u32 bits)
315 oxygen_write32_masked(chip, OXYGEN_SPDIF_OUTPUT_BITS, bits,
316 OXYGEN_SPDIF_NONAUDIO |
318 OXYGEN_SPDIF_PREEMPHASIS |
319 OXYGEN_SPDIF_CATEGORY_MASK |
320 OXYGEN_SPDIF_ORIGINAL |
324 static int spdif_default_get(struct snd_kcontrol *ctl,
325 struct snd_ctl_elem_value *value)
327 struct oxygen *chip = ctl->private_data;
329 mutex_lock(&chip->mutex);
330 oxygen_to_iec958(chip->spdif_bits, value);
331 mutex_unlock(&chip->mutex);
335 static int spdif_default_put(struct snd_kcontrol *ctl,
336 struct snd_ctl_elem_value *value)
338 struct oxygen *chip = ctl->private_data;
342 new_bits = iec958_to_oxygen(value);
343 mutex_lock(&chip->mutex);
344 changed = new_bits != chip->spdif_bits;
346 chip->spdif_bits = new_bits;
347 if (!(chip->pcm_active & (1 << PCM_SPDIF)))
348 write_spdif_bits(chip, new_bits);
350 mutex_unlock(&chip->mutex);
354 static int spdif_mask_get(struct snd_kcontrol *ctl,
355 struct snd_ctl_elem_value *value)
357 value->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
358 IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS;
359 value->value.iec958.status[1] =
360 IEC958_AES1_CON_CATEGORY | IEC958_AES1_CON_ORIGINAL;
364 static int spdif_pcm_get(struct snd_kcontrol *ctl,
365 struct snd_ctl_elem_value *value)
367 struct oxygen *chip = ctl->private_data;
369 mutex_lock(&chip->mutex);
370 oxygen_to_iec958(chip->spdif_pcm_bits, value);
371 mutex_unlock(&chip->mutex);
375 static int spdif_pcm_put(struct snd_kcontrol *ctl,
376 struct snd_ctl_elem_value *value)
378 struct oxygen *chip = ctl->private_data;
382 new_bits = iec958_to_oxygen(value);
383 mutex_lock(&chip->mutex);
384 changed = new_bits != chip->spdif_pcm_bits;
386 chip->spdif_pcm_bits = new_bits;
387 if (chip->pcm_active & (1 << PCM_SPDIF))
388 write_spdif_bits(chip, new_bits);
390 mutex_unlock(&chip->mutex);
394 static int spdif_input_mask_get(struct snd_kcontrol *ctl,
395 struct snd_ctl_elem_value *value)
397 value->value.iec958.status[0] = 0xff;
398 value->value.iec958.status[1] = 0xff;
399 value->value.iec958.status[2] = 0xff;
400 value->value.iec958.status[3] = 0xff;
404 static int spdif_input_default_get(struct snd_kcontrol *ctl,
405 struct snd_ctl_elem_value *value)
407 struct oxygen *chip = ctl->private_data;
410 bits = oxygen_read32(chip, OXYGEN_SPDIF_INPUT_BITS);
411 value->value.iec958.status[0] = bits;
412 value->value.iec958.status[1] = bits >> 8;
413 value->value.iec958.status[2] = bits >> 16;
414 value->value.iec958.status[3] = bits >> 24;
418 static int spdif_loopback_get(struct snd_kcontrol *ctl,
419 struct snd_ctl_elem_value *value)
421 struct oxygen *chip = ctl->private_data;
423 value->value.integer.value[0] =
424 !!(oxygen_read32(chip, OXYGEN_SPDIF_CONTROL)
425 & OXYGEN_SPDIF_LOOPBACK);
429 static int spdif_loopback_put(struct snd_kcontrol *ctl,
430 struct snd_ctl_elem_value *value)
432 struct oxygen *chip = ctl->private_data;
436 spin_lock_irq(&chip->reg_lock);
437 oldreg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
438 if (value->value.integer.value[0])
439 newreg = oldreg | OXYGEN_SPDIF_LOOPBACK;
441 newreg = oldreg & ~OXYGEN_SPDIF_LOOPBACK;
442 changed = newreg != oldreg;
444 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, newreg);
445 spin_unlock_irq(&chip->reg_lock);
449 static int monitor_volume_info(struct snd_kcontrol *ctl,
450 struct snd_ctl_elem_info *info)
452 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
454 info->value.integer.min = 0;
455 info->value.integer.max = 1;
459 static int monitor_get(struct snd_kcontrol *ctl,
460 struct snd_ctl_elem_value *value)
462 struct oxygen *chip = ctl->private_data;
463 u8 bit = ctl->private_value;
464 int invert = ctl->private_value & (1 << 8);
466 value->value.integer.value[0] =
467 !!invert ^ !!(oxygen_read8(chip, OXYGEN_ADC_MONITOR) & bit);
471 static int monitor_put(struct snd_kcontrol *ctl,
472 struct snd_ctl_elem_value *value)
474 struct oxygen *chip = ctl->private_data;
475 u8 bit = ctl->private_value;
476 int invert = ctl->private_value & (1 << 8);
480 spin_lock_irq(&chip->reg_lock);
481 oldreg = oxygen_read8(chip, OXYGEN_ADC_MONITOR);
482 if ((!!value->value.integer.value[0] ^ !!invert) != 0)
483 newreg = oldreg | bit;
485 newreg = oldreg & ~bit;
486 changed = newreg != oldreg;
488 oxygen_write8(chip, OXYGEN_ADC_MONITOR, newreg);
489 spin_unlock_irq(&chip->reg_lock);
493 static int ac97_switch_get(struct snd_kcontrol *ctl,
494 struct snd_ctl_elem_value *value)
496 struct oxygen *chip = ctl->private_data;
497 unsigned int codec = (ctl->private_value >> 24) & 1;
498 unsigned int index = ctl->private_value & 0xff;
499 unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
500 int invert = ctl->private_value & (1 << 16);
503 mutex_lock(&chip->mutex);
504 reg = oxygen_read_ac97(chip, codec, index);
505 mutex_unlock(&chip->mutex);
506 if (!(reg & (1 << bitnr)) ^ !invert)
507 value->value.integer.value[0] = 1;
509 value->value.integer.value[0] = 0;
513 static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
515 unsigned int priv_idx = chip->controls[control]->private_value & 0xff;
518 value = oxygen_read_ac97(chip, 0, priv_idx);
519 if (!(value & 0x8000)) {
520 oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000);
521 if (chip->model->ac97_switch)
522 chip->model->ac97_switch(chip, priv_idx, 0x8000);
523 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
524 &chip->controls[control]->id);
528 static int ac97_switch_put(struct snd_kcontrol *ctl,
529 struct snd_ctl_elem_value *value)
531 struct oxygen *chip = ctl->private_data;
532 unsigned int codec = (ctl->private_value >> 24) & 1;
533 unsigned int index = ctl->private_value & 0xff;
534 unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
535 int invert = ctl->private_value & (1 << 16);
539 mutex_lock(&chip->mutex);
540 oldreg = oxygen_read_ac97(chip, codec, index);
542 if (!value->value.integer.value[0] ^ !invert)
543 newreg |= 1 << bitnr;
545 newreg &= ~(1 << bitnr);
546 change = newreg != oldreg;
548 oxygen_write_ac97(chip, codec, index, newreg);
549 if (codec == 0 && chip->model->ac97_switch)
550 chip->model->ac97_switch(chip, index, newreg & 0x8000);
551 if (index == AC97_LINE) {
552 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
554 CM9780_GPO0 : 0, CM9780_GPO0);
555 if (!(newreg & 0x8000)) {
556 mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
557 mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
558 mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
560 } else if ((index == AC97_MIC || index == AC97_CD ||
561 index == AC97_VIDEO || index == AC97_AUX) &&
562 bitnr == 15 && !(newreg & 0x8000)) {
563 mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
564 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
565 CM9780_GPO0, CM9780_GPO0);
568 mutex_unlock(&chip->mutex);
572 static int ac97_volume_info(struct snd_kcontrol *ctl,
573 struct snd_ctl_elem_info *info)
575 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
577 info->value.integer.min = 0;
578 info->value.integer.max = 0x1f;
582 static int ac97_volume_get(struct snd_kcontrol *ctl,
583 struct snd_ctl_elem_value *value)
585 struct oxygen *chip = ctl->private_data;
586 unsigned int codec = (ctl->private_value >> 24) & 1;
587 unsigned int index = ctl->private_value & 0xff;
590 mutex_lock(&chip->mutex);
591 reg = oxygen_read_ac97(chip, codec, index);
592 mutex_unlock(&chip->mutex);
593 value->value.integer.value[0] = 31 - (reg & 0x1f);
594 value->value.integer.value[1] = 31 - ((reg >> 8) & 0x1f);
598 static int ac97_volume_put(struct snd_kcontrol *ctl,
599 struct snd_ctl_elem_value *value)
601 struct oxygen *chip = ctl->private_data;
602 unsigned int codec = (ctl->private_value >> 24) & 1;
603 unsigned int index = ctl->private_value & 0xff;
607 mutex_lock(&chip->mutex);
608 oldreg = oxygen_read_ac97(chip, codec, index);
610 newreg = (newreg & ~0x1f) |
611 (31 - (value->value.integer.value[0] & 0x1f));
612 newreg = (newreg & ~0x1f00) |
613 ((31 - (value->value.integer.value[0] & 0x1f)) << 8);
614 change = newreg != oldreg;
616 oxygen_write_ac97(chip, codec, index, newreg);
617 mutex_unlock(&chip->mutex);
621 static int ac97_fp_rec_volume_info(struct snd_kcontrol *ctl,
622 struct snd_ctl_elem_info *info)
624 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
626 info->value.integer.min = 0;
627 info->value.integer.max = 7;
631 static int ac97_fp_rec_volume_get(struct snd_kcontrol *ctl,
632 struct snd_ctl_elem_value *value)
634 struct oxygen *chip = ctl->private_data;
637 mutex_lock(&chip->mutex);
638 reg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
639 mutex_unlock(&chip->mutex);
640 value->value.integer.value[0] = reg & 7;
641 value->value.integer.value[1] = (reg >> 8) & 7;
645 static int ac97_fp_rec_volume_put(struct snd_kcontrol *ctl,
646 struct snd_ctl_elem_value *value)
648 struct oxygen *chip = ctl->private_data;
652 mutex_lock(&chip->mutex);
653 oldreg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
654 newreg = oldreg & ~0x0707;
655 newreg = newreg | (value->value.integer.value[0] & 7);
656 newreg = newreg | ((value->value.integer.value[0] & 7) << 8);
657 change = newreg != oldreg;
659 oxygen_write_ac97(chip, 1, AC97_REC_GAIN, newreg);
660 mutex_unlock(&chip->mutex);
664 #define AC97_SWITCH(xname, codec, index, bitnr, invert) { \
665 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
667 .info = snd_ctl_boolean_mono_info, \
668 .get = ac97_switch_get, \
669 .put = ac97_switch_put, \
670 .private_value = ((codec) << 24) | ((invert) << 16) | \
671 ((bitnr) << 8) | (index), \
673 #define AC97_VOLUME(xname, codec, index) { \
674 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
676 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
677 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
678 .info = ac97_volume_info, \
679 .get = ac97_volume_get, \
680 .put = ac97_volume_put, \
681 .tlv = { .p = ac97_db_scale, }, \
682 .private_value = ((codec) << 24) | (index), \
685 static DECLARE_TLV_DB_SCALE(monitor_db_scale, -1000, 1000, 0);
686 static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0);
687 static DECLARE_TLV_DB_SCALE(ac97_rec_db_scale, 0, 150, 0);
689 static const struct snd_kcontrol_new controls[] = {
691 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
692 .name = "Master Playback Volume",
693 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
694 .info = dac_volume_info,
695 .get = dac_volume_get,
696 .put = dac_volume_put,
699 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
700 .name = "Master Playback Switch",
701 .info = snd_ctl_boolean_mono_info,
706 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
707 .name = "Stereo Upmixing",
713 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
714 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
715 .info = snd_ctl_boolean_mono_info,
716 .get = spdif_switch_get,
717 .put = spdif_switch_put,
720 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
722 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
724 .get = spdif_default_get,
725 .put = spdif_default_put,
728 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
730 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
731 .access = SNDRV_CTL_ELEM_ACCESS_READ,
733 .get = spdif_mask_get,
736 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
738 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
739 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
740 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
742 .get = spdif_pcm_get,
743 .put = spdif_pcm_put,
747 static const struct snd_kcontrol_new spdif_input_controls[] = {
749 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
751 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK),
752 .access = SNDRV_CTL_ELEM_ACCESS_READ,
754 .get = spdif_input_mask_get,
757 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
759 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
760 .access = SNDRV_CTL_ELEM_ACCESS_READ,
762 .get = spdif_input_default_get,
765 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
766 .name = SNDRV_CTL_NAME_IEC958("Loopback ", NONE, SWITCH),
767 .info = snd_ctl_boolean_mono_info,
768 .get = spdif_loopback_get,
769 .put = spdif_loopback_put,
773 static const struct {
774 unsigned int pcm_dev;
775 struct snd_kcontrol_new controls[2];
776 } monitor_controls[] = {
778 .pcm_dev = CAPTURE_0_FROM_I2S_1,
781 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
782 .name = "Analog Input Monitor Switch",
783 .info = snd_ctl_boolean_mono_info,
786 .private_value = OXYGEN_ADC_MONITOR_A,
789 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
790 .name = "Analog Input Monitor Volume",
791 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
792 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
793 .info = monitor_volume_info,
796 .private_value = OXYGEN_ADC_MONITOR_A_HALF_VOL
798 .tlv = { .p = monitor_db_scale, },
803 .pcm_dev = CAPTURE_0_FROM_I2S_2,
806 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
807 .name = "Analog Input Monitor Switch",
808 .info = snd_ctl_boolean_mono_info,
811 .private_value = OXYGEN_ADC_MONITOR_B,
814 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
815 .name = "Analog Input Monitor Volume",
816 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
817 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
818 .info = monitor_volume_info,
821 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
823 .tlv = { .p = monitor_db_scale, },
828 .pcm_dev = CAPTURE_2_FROM_I2S_2,
831 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
832 .name = "Analog Input Monitor Switch",
834 .info = snd_ctl_boolean_mono_info,
837 .private_value = OXYGEN_ADC_MONITOR_B,
840 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
841 .name = "Analog Input Monitor Volume",
843 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
844 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
845 .info = monitor_volume_info,
848 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
850 .tlv = { .p = monitor_db_scale, },
855 .pcm_dev = CAPTURE_1_FROM_SPDIF,
858 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
859 .name = "Digital Input Monitor Switch",
860 .info = snd_ctl_boolean_mono_info,
863 .private_value = OXYGEN_ADC_MONITOR_C,
866 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
867 .name = "Digital Input Monitor Volume",
868 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
869 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
870 .info = monitor_volume_info,
873 .private_value = OXYGEN_ADC_MONITOR_C_HALF_VOL
875 .tlv = { .p = monitor_db_scale, },
881 static const struct snd_kcontrol_new ac97_controls[] = {
882 AC97_VOLUME("Mic Capture Volume", 0, AC97_MIC),
883 AC97_SWITCH("Mic Capture Switch", 0, AC97_MIC, 15, 1),
884 AC97_SWITCH("Mic Boost (+20dB)", 0, AC97_MIC, 6, 0),
885 AC97_SWITCH("Line Capture Switch", 0, AC97_LINE, 15, 1),
886 AC97_VOLUME("CD Capture Volume", 0, AC97_CD),
887 AC97_SWITCH("CD Capture Switch", 0, AC97_CD, 15, 1),
888 AC97_VOLUME("Aux Capture Volume", 0, AC97_AUX),
889 AC97_SWITCH("Aux Capture Switch", 0, AC97_AUX, 15, 1),
892 static const struct snd_kcontrol_new ac97_fp_controls[] = {
893 AC97_VOLUME("Front Panel Playback Volume", 1, AC97_HEADPHONE),
894 AC97_SWITCH("Front Panel Playback Switch", 1, AC97_HEADPHONE, 15, 1),
896 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
897 .name = "Front Panel Capture Volume",
898 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
899 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
900 .info = ac97_fp_rec_volume_info,
901 .get = ac97_fp_rec_volume_get,
902 .put = ac97_fp_rec_volume_put,
903 .tlv = { .p = ac97_rec_db_scale, },
905 AC97_SWITCH("Front Panel Capture Switch", 1, AC97_REC_GAIN, 15, 1),
908 static void oxygen_any_ctl_free(struct snd_kcontrol *ctl)
910 struct oxygen *chip = ctl->private_data;
913 /* I'm too lazy to write a function for each control :-) */
914 for (i = 0; i < ARRAY_SIZE(chip->controls); ++i)
915 chip->controls[i] = NULL;
918 static int add_controls(struct oxygen *chip,
919 const struct snd_kcontrol_new controls[],
922 static const char *const known_ctl_names[CONTROL_COUNT] = {
923 [CONTROL_SPDIF_PCM] =
924 SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
925 [CONTROL_SPDIF_INPUT_BITS] =
926 SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
927 [CONTROL_MIC_CAPTURE_SWITCH] = "Mic Capture Switch",
928 [CONTROL_LINE_CAPTURE_SWITCH] = "Line Capture Switch",
929 [CONTROL_CD_CAPTURE_SWITCH] = "CD Capture Switch",
930 [CONTROL_AUX_CAPTURE_SWITCH] = "Aux Capture Switch",
933 struct snd_kcontrol_new template;
934 struct snd_kcontrol *ctl;
937 for (i = 0; i < count; ++i) {
938 template = controls[i];
939 if (chip->model->control_filter) {
940 err = chip->model->control_filter(&template);
946 if (!strcmp(template.name, "Master Playback Volume") &&
947 chip->model->dac_tlv) {
948 template.tlv.p = chip->model->dac_tlv;
949 template.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
951 ctl = snd_ctl_new1(&template, chip);
954 err = snd_ctl_add(chip->card, ctl);
957 for (j = 0; j < CONTROL_COUNT; ++j)
958 if (!strcmp(ctl->id.name, known_ctl_names[j])) {
959 chip->controls[j] = ctl;
960 ctl->private_free = oxygen_any_ctl_free;
966 int oxygen_mixer_init(struct oxygen *chip)
971 err = add_controls(chip, controls, ARRAY_SIZE(controls));
974 if (chip->model->pcm_dev_cfg & CAPTURE_1_FROM_SPDIF) {
975 err = add_controls(chip, spdif_input_controls,
976 ARRAY_SIZE(spdif_input_controls));
980 for (i = 0; i < ARRAY_SIZE(monitor_controls); ++i) {
981 if (!(chip->model->pcm_dev_cfg & monitor_controls[i].pcm_dev))
983 err = add_controls(chip, monitor_controls[i].controls,
984 ARRAY_SIZE(monitor_controls[i].controls));
988 if (chip->has_ac97_0) {
989 err = add_controls(chip, ac97_controls,
990 ARRAY_SIZE(ac97_controls));
994 if (chip->has_ac97_1) {
995 err = add_controls(chip, ac97_fp_controls,
996 ARRAY_SIZE(ac97_fp_controls));
1000 return chip->model->mixer_init ? chip->model->mixer_init(chip) : 0;