GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / drivers / vx / vx_mixer.c
blob5d7895f2e5c5427e894a99f747036b65a584dfa9
1 /*
2 * Driver for Digigram VX soundcards
4 * Common mixer part
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/core.h>
24 #include <sound/control.h>
25 #include <sound/tlv.h>
26 #include <sound/vx_core.h>
27 #include "vx_cmd.h"
31 * write a codec data (24bit)
33 static void vx_write_codec_reg(struct vx_core *chip, int codec, unsigned int data)
35 unsigned long flags;
37 if (snd_BUG_ON(!chip->ops->write_codec))
38 return;
40 if (chip->chip_status & VX_STAT_IS_STALE)
41 return;
43 spin_lock_irqsave(&chip->lock, flags);
44 chip->ops->write_codec(chip, codec, data);
45 spin_unlock_irqrestore(&chip->lock, flags);
49 * Data type used to access the Codec
51 union vx_codec_data {
52 u32 l;
53 #ifdef SNDRV_BIG_ENDIAN
54 struct w {
55 u16 h;
56 u16 l;
57 } w;
58 struct b {
59 u8 hh;
60 u8 mh;
61 u8 ml;
62 u8 ll;
63 } b;
64 #else /* LITTLE_ENDIAN */
65 struct w {
66 u16 l;
67 u16 h;
68 } w;
69 struct b {
70 u8 ll;
71 u8 ml;
72 u8 mh;
73 u8 hh;
74 } b;
75 #endif
78 #define SET_CDC_DATA_SEL(di,s) ((di).b.mh = (u8) (s))
79 #define SET_CDC_DATA_REG(di,r) ((di).b.ml = (u8) (r))
80 #define SET_CDC_DATA_VAL(di,d) ((di).b.ll = (u8) (d))
81 #define SET_CDC_DATA_INIT(di) ((di).l = 0L, SET_CDC_DATA_SEL(di,XX_CODEC_SELECTOR))
84 * set up codec register and write the value
85 * @codec: the codec id, 0 or 1
86 * @reg: register index
87 * @val: data value
89 static void vx_set_codec_reg(struct vx_core *chip, int codec, int reg, int val)
91 union vx_codec_data data;
92 /* DAC control register */
93 SET_CDC_DATA_INIT(data);
94 SET_CDC_DATA_REG(data, reg);
95 SET_CDC_DATA_VAL(data, val);
96 vx_write_codec_reg(chip, codec, data.l);
101 * vx_set_analog_output_level - set the output attenuation level
102 * @codec: the output codec, 0 or 1. (1 for VXP440 only)
103 * @left: left output level, 0 = mute
104 * @right: right output level
106 static void vx_set_analog_output_level(struct vx_core *chip, int codec, int left, int right)
108 left = chip->hw->output_level_max - left;
109 right = chip->hw->output_level_max - right;
111 if (chip->ops->akm_write) {
112 chip->ops->akm_write(chip, XX_CODEC_LEVEL_LEFT_REGISTER, left);
113 chip->ops->akm_write(chip, XX_CODEC_LEVEL_RIGHT_REGISTER, right);
114 } else {
115 /* convert to attenuation level: 0 = 0dB (max), 0xe3 = -113.5 dB (min) */
116 vx_set_codec_reg(chip, codec, XX_CODEC_LEVEL_LEFT_REGISTER, left);
117 vx_set_codec_reg(chip, codec, XX_CODEC_LEVEL_RIGHT_REGISTER, right);
123 * vx_toggle_dac_mute - mute/unmute DAC
124 * @mute: 0 = unmute, 1 = mute
127 #define DAC_ATTEN_MIN 0x08
128 #define DAC_ATTEN_MAX 0x38
130 void vx_toggle_dac_mute(struct vx_core *chip, int mute)
132 unsigned int i;
133 for (i = 0; i < chip->hw->num_codecs; i++) {
134 if (chip->ops->akm_write)
135 chip->ops->akm_write(chip, XX_CODEC_DAC_CONTROL_REGISTER, mute);
136 else
137 vx_set_codec_reg(chip, i, XX_CODEC_DAC_CONTROL_REGISTER,
138 mute ? DAC_ATTEN_MAX : DAC_ATTEN_MIN);
143 * vx_reset_codec - reset and initialize the codecs
145 void vx_reset_codec(struct vx_core *chip, int cold_reset)
147 unsigned int i;
148 int port = chip->type >= VX_TYPE_VXPOCKET ? 0x75 : 0x65;
150 chip->ops->reset_codec(chip);
152 /* AKM codecs should be initialized in reset_codec callback */
153 if (! chip->ops->akm_write) {
154 /* initialize old codecs */
155 for (i = 0; i < chip->hw->num_codecs; i++) {
156 /* DAC control register (change level when zero crossing + mute) */
157 vx_set_codec_reg(chip, i, XX_CODEC_DAC_CONTROL_REGISTER, DAC_ATTEN_MAX);
158 /* ADC control register */
159 vx_set_codec_reg(chip, i, XX_CODEC_ADC_CONTROL_REGISTER, 0x00);
160 /* Port mode register */
161 vx_set_codec_reg(chip, i, XX_CODEC_PORT_MODE_REGISTER, port);
162 /* Clock control register */
163 vx_set_codec_reg(chip, i, XX_CODEC_CLOCK_CONTROL_REGISTER, 0x00);
167 /* mute analog output */
168 for (i = 0; i < chip->hw->num_codecs; i++) {
169 chip->output_level[i][0] = 0;
170 chip->output_level[i][1] = 0;
171 vx_set_analog_output_level(chip, i, 0, 0);
176 * change the audio input source
177 * @src: the target source (VX_AUDIO_SRC_XXX)
179 static void vx_change_audio_source(struct vx_core *chip, int src)
181 unsigned long flags;
183 if (chip->chip_status & VX_STAT_IS_STALE)
184 return;
186 spin_lock_irqsave(&chip->lock, flags);
187 chip->ops->change_audio_source(chip, src);
188 spin_unlock_irqrestore(&chip->lock, flags);
193 * change the audio source if necessary and possible
194 * returns 1 if the source is actually changed.
196 int vx_sync_audio_source(struct vx_core *chip)
198 if (chip->audio_source_target == chip->audio_source ||
199 chip->pcm_running)
200 return 0;
201 vx_change_audio_source(chip, chip->audio_source_target);
202 chip->audio_source = chip->audio_source_target;
203 return 1;
208 * audio level, mute, monitoring
210 struct vx_audio_level {
211 unsigned int has_level: 1;
212 unsigned int has_monitor_level: 1;
213 unsigned int has_mute: 1;
214 unsigned int has_monitor_mute: 1;
215 unsigned int mute;
216 unsigned int monitor_mute;
217 short level;
218 short monitor_level;
221 static int vx_adjust_audio_level(struct vx_core *chip, int audio, int capture,
222 struct vx_audio_level *info)
224 struct vx_rmh rmh;
226 if (chip->chip_status & VX_STAT_IS_STALE)
227 return -EBUSY;
229 vx_init_rmh(&rmh, CMD_AUDIO_LEVEL_ADJUST);
230 if (capture)
231 rmh.Cmd[0] |= COMMAND_RECORD_MASK;
232 /* Add Audio IO mask */
233 rmh.Cmd[1] = 1 << audio;
234 rmh.Cmd[2] = 0;
235 if (info->has_level) {
236 rmh.Cmd[0] |= VALID_AUDIO_IO_DIGITAL_LEVEL;
237 rmh.Cmd[2] |= info->level;
239 if (info->has_monitor_level) {
240 rmh.Cmd[0] |= VALID_AUDIO_IO_MONITORING_LEVEL;
241 rmh.Cmd[2] |= ((unsigned int)info->monitor_level << 10);
243 if (info->has_mute) {
244 rmh.Cmd[0] |= VALID_AUDIO_IO_MUTE_LEVEL;
245 if (info->mute)
246 rmh.Cmd[2] |= AUDIO_IO_HAS_MUTE_LEVEL;
248 if (info->has_monitor_mute) {
249 /* validate flag for M2 at least to unmute it */
250 rmh.Cmd[0] |= VALID_AUDIO_IO_MUTE_MONITORING_1 | VALID_AUDIO_IO_MUTE_MONITORING_2;
251 if (info->monitor_mute)
252 rmh.Cmd[2] |= AUDIO_IO_HAS_MUTE_MONITORING_1;
255 return vx_send_msg(chip, &rmh);
261 * set the monitoring level and mute state of the given audio
262 * no more static, because must be called from vx_pcm to demute monitoring
264 int vx_set_monitor_level(struct vx_core *chip, int audio, int level, int active)
266 struct vx_audio_level info;
268 memset(&info, 0, sizeof(info));
269 info.has_monitor_level = 1;
270 info.monitor_level = level;
271 info.has_monitor_mute = 1;
272 info.monitor_mute = !active;
273 chip->audio_monitor[audio] = level;
274 chip->audio_monitor_active[audio] = active;
275 return vx_adjust_audio_level(chip, audio, 0, &info); /* playback only */
280 * set the mute status of the given audio
282 static int vx_set_audio_switch(struct vx_core *chip, int audio, int active)
284 struct vx_audio_level info;
286 memset(&info, 0, sizeof(info));
287 info.has_mute = 1;
288 info.mute = !active;
289 chip->audio_active[audio] = active;
290 return vx_adjust_audio_level(chip, audio, 0, &info); /* playback only */
294 * set the mute status of the given audio
296 static int vx_set_audio_gain(struct vx_core *chip, int audio, int capture, int level)
298 struct vx_audio_level info;
300 memset(&info, 0, sizeof(info));
301 info.has_level = 1;
302 info.level = level;
303 chip->audio_gain[capture][audio] = level;
304 return vx_adjust_audio_level(chip, audio, capture, &info);
308 * reset all audio levels
310 static void vx_reset_audio_levels(struct vx_core *chip)
312 unsigned int i, c;
313 struct vx_audio_level info;
315 memset(chip->audio_gain, 0, sizeof(chip->audio_gain));
316 memset(chip->audio_active, 0, sizeof(chip->audio_active));
317 memset(chip->audio_monitor, 0, sizeof(chip->audio_monitor));
318 memset(chip->audio_monitor_active, 0, sizeof(chip->audio_monitor_active));
320 for (c = 0; c < 2; c++) {
321 for (i = 0; i < chip->hw->num_ins * 2; i++) {
322 memset(&info, 0, sizeof(info));
323 if (c == 0) {
324 info.has_monitor_level = 1;
325 info.has_mute = 1;
326 info.has_monitor_mute = 1;
328 info.has_level = 1;
329 info.level = CVAL_0DB; /* default: 0dB */
330 vx_adjust_audio_level(chip, i, c, &info);
331 chip->audio_gain[c][i] = CVAL_0DB;
332 chip->audio_monitor[i] = CVAL_0DB;
339 * VU, peak meter record
342 #define VU_METER_CHANNELS 2
344 struct vx_vu_meter {
345 int saturated;
346 int vu_level;
347 int peak_level;
351 * get the VU and peak meter values
352 * @audio: the audio index
353 * @capture: 0 = playback, 1 = capture operation
354 * @info: the array of vx_vu_meter records (size = 2).
356 static int vx_get_audio_vu_meter(struct vx_core *chip, int audio, int capture, struct vx_vu_meter *info)
358 struct vx_rmh rmh;
359 int i, err;
361 if (chip->chip_status & VX_STAT_IS_STALE)
362 return -EBUSY;
364 vx_init_rmh(&rmh, CMD_AUDIO_VU_PIC_METER);
365 rmh.LgStat += 2 * VU_METER_CHANNELS;
366 if (capture)
367 rmh.Cmd[0] |= COMMAND_RECORD_MASK;
369 /* Add Audio IO mask */
370 rmh.Cmd[1] = 0;
371 for (i = 0; i < VU_METER_CHANNELS; i++)
372 rmh.Cmd[1] |= 1 << (audio + i);
373 err = vx_send_msg(chip, &rmh);
374 if (err < 0)
375 return err;
376 /* Read response */
377 for (i = 0; i < 2 * VU_METER_CHANNELS; i +=2) {
378 info->saturated = (rmh.Stat[0] & (1 << (audio + i))) ? 1 : 0;
379 info->vu_level = rmh.Stat[i + 1];
380 info->peak_level = rmh.Stat[i + 2];
381 info++;
383 return 0;
388 * control API entries
392 * output level control
394 static int vx_output_level_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
396 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
397 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
398 uinfo->count = 2;
399 uinfo->value.integer.min = 0;
400 uinfo->value.integer.max = chip->hw->output_level_max;
401 return 0;
404 static int vx_output_level_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
406 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
407 int codec = kcontrol->id.index;
408 mutex_lock(&chip->mixer_mutex);
409 ucontrol->value.integer.value[0] = chip->output_level[codec][0];
410 ucontrol->value.integer.value[1] = chip->output_level[codec][1];
411 mutex_unlock(&chip->mixer_mutex);
412 return 0;
415 static int vx_output_level_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
417 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
418 int codec = kcontrol->id.index;
419 unsigned int val[2], vmax;
421 vmax = chip->hw->output_level_max;
422 val[0] = ucontrol->value.integer.value[0];
423 val[1] = ucontrol->value.integer.value[1];
424 if (val[0] > vmax || val[1] > vmax)
425 return -EINVAL;
426 mutex_lock(&chip->mixer_mutex);
427 if (val[0] != chip->output_level[codec][0] ||
428 val[1] != chip->output_level[codec][1]) {
429 vx_set_analog_output_level(chip, codec, val[0], val[1]);
430 chip->output_level[codec][0] = val[0];
431 chip->output_level[codec][1] = val[1];
432 mutex_unlock(&chip->mixer_mutex);
433 return 1;
435 mutex_unlock(&chip->mixer_mutex);
436 return 0;
439 static struct snd_kcontrol_new vx_control_output_level = {
440 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
441 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
442 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
443 .name = "Master Playback Volume",
444 .info = vx_output_level_info,
445 .get = vx_output_level_get,
446 .put = vx_output_level_put,
447 /* tlv will be filled later */
451 * audio source select
453 static int vx_audio_src_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
455 static char *texts_mic[3] = {
456 "Digital", "Line", "Mic"
458 static char *texts_vx2[2] = {
459 "Digital", "Analog"
461 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
463 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
464 uinfo->count = 1;
465 if (chip->type >= VX_TYPE_VXPOCKET) {
466 uinfo->value.enumerated.items = 3;
467 if (uinfo->value.enumerated.item > 2)
468 uinfo->value.enumerated.item = 2;
469 strcpy(uinfo->value.enumerated.name,
470 texts_mic[uinfo->value.enumerated.item]);
471 } else {
472 uinfo->value.enumerated.items = 2;
473 if (uinfo->value.enumerated.item > 1)
474 uinfo->value.enumerated.item = 1;
475 strcpy(uinfo->value.enumerated.name,
476 texts_vx2[uinfo->value.enumerated.item]);
478 return 0;
481 static int vx_audio_src_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
483 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
484 ucontrol->value.enumerated.item[0] = chip->audio_source_target;
485 return 0;
488 static int vx_audio_src_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
490 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
492 if (chip->type >= VX_TYPE_VXPOCKET) {
493 if (ucontrol->value.enumerated.item[0] > 2)
494 return -EINVAL;
495 } else {
496 if (ucontrol->value.enumerated.item[0] > 1)
497 return -EINVAL;
499 mutex_lock(&chip->mixer_mutex);
500 if (chip->audio_source_target != ucontrol->value.enumerated.item[0]) {
501 chip->audio_source_target = ucontrol->value.enumerated.item[0];
502 vx_sync_audio_source(chip);
503 mutex_unlock(&chip->mixer_mutex);
504 return 1;
506 mutex_unlock(&chip->mixer_mutex);
507 return 0;
510 static struct snd_kcontrol_new vx_control_audio_src = {
511 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
512 .name = "Capture Source",
513 .info = vx_audio_src_info,
514 .get = vx_audio_src_get,
515 .put = vx_audio_src_put,
519 * clock mode selection
521 static int vx_clock_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
523 static char *texts[3] = {
524 "Auto", "Internal", "External"
527 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
528 uinfo->count = 1;
529 uinfo->value.enumerated.items = 3;
530 if (uinfo->value.enumerated.item > 2)
531 uinfo->value.enumerated.item = 2;
532 strcpy(uinfo->value.enumerated.name,
533 texts[uinfo->value.enumerated.item]);
534 return 0;
537 static int vx_clock_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
539 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
540 ucontrol->value.enumerated.item[0] = chip->clock_mode;
541 return 0;
544 static int vx_clock_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
546 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
548 if (ucontrol->value.enumerated.item[0] > 2)
549 return -EINVAL;
550 mutex_lock(&chip->mixer_mutex);
551 if (chip->clock_mode != ucontrol->value.enumerated.item[0]) {
552 chip->clock_mode = ucontrol->value.enumerated.item[0];
553 vx_set_clock(chip, chip->freq);
554 mutex_unlock(&chip->mixer_mutex);
555 return 1;
557 mutex_unlock(&chip->mixer_mutex);
558 return 0;
561 static struct snd_kcontrol_new vx_control_clock_mode = {
562 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
563 .name = "Clock Mode",
564 .info = vx_clock_mode_info,
565 .get = vx_clock_mode_get,
566 .put = vx_clock_mode_put,
570 * Audio Gain
572 static int vx_audio_gain_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
574 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
575 uinfo->count = 2;
576 uinfo->value.integer.min = 0;
577 uinfo->value.integer.max = CVAL_MAX;
578 return 0;
581 static int vx_audio_gain_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
583 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
584 int audio = kcontrol->private_value & 0xff;
585 int capture = (kcontrol->private_value >> 8) & 1;
587 mutex_lock(&chip->mixer_mutex);
588 ucontrol->value.integer.value[0] = chip->audio_gain[capture][audio];
589 ucontrol->value.integer.value[1] = chip->audio_gain[capture][audio+1];
590 mutex_unlock(&chip->mixer_mutex);
591 return 0;
594 static int vx_audio_gain_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
596 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
597 int audio = kcontrol->private_value & 0xff;
598 int capture = (kcontrol->private_value >> 8) & 1;
599 unsigned int val[2];
601 val[0] = ucontrol->value.integer.value[0];
602 val[1] = ucontrol->value.integer.value[1];
603 if (val[0] > CVAL_MAX || val[1] > CVAL_MAX)
604 return -EINVAL;
605 mutex_lock(&chip->mixer_mutex);
606 if (val[0] != chip->audio_gain[capture][audio] ||
607 val[1] != chip->audio_gain[capture][audio+1]) {
608 vx_set_audio_gain(chip, audio, capture, val[0]);
609 vx_set_audio_gain(chip, audio+1, capture, val[1]);
610 mutex_unlock(&chip->mixer_mutex);
611 return 1;
613 mutex_unlock(&chip->mixer_mutex);
614 return 0;
617 static int vx_audio_monitor_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
619 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
620 int audio = kcontrol->private_value & 0xff;
622 mutex_lock(&chip->mixer_mutex);
623 ucontrol->value.integer.value[0] = chip->audio_monitor[audio];
624 ucontrol->value.integer.value[1] = chip->audio_monitor[audio+1];
625 mutex_unlock(&chip->mixer_mutex);
626 return 0;
629 static int vx_audio_monitor_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
631 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
632 int audio = kcontrol->private_value & 0xff;
633 unsigned int val[2];
635 val[0] = ucontrol->value.integer.value[0];
636 val[1] = ucontrol->value.integer.value[1];
637 if (val[0] > CVAL_MAX || val[1] > CVAL_MAX)
638 return -EINVAL;
640 mutex_lock(&chip->mixer_mutex);
641 if (val[0] != chip->audio_monitor[audio] ||
642 val[1] != chip->audio_monitor[audio+1]) {
643 vx_set_monitor_level(chip, audio, val[0],
644 chip->audio_monitor_active[audio]);
645 vx_set_monitor_level(chip, audio+1, val[1],
646 chip->audio_monitor_active[audio+1]);
647 mutex_unlock(&chip->mixer_mutex);
648 return 1;
650 mutex_unlock(&chip->mixer_mutex);
651 return 0;
654 #define vx_audio_sw_info snd_ctl_boolean_stereo_info
656 static int vx_audio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
658 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
659 int audio = kcontrol->private_value & 0xff;
661 mutex_lock(&chip->mixer_mutex);
662 ucontrol->value.integer.value[0] = chip->audio_active[audio];
663 ucontrol->value.integer.value[1] = chip->audio_active[audio+1];
664 mutex_unlock(&chip->mixer_mutex);
665 return 0;
668 static int vx_audio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
670 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
671 int audio = kcontrol->private_value & 0xff;
673 mutex_lock(&chip->mixer_mutex);
674 if (ucontrol->value.integer.value[0] != chip->audio_active[audio] ||
675 ucontrol->value.integer.value[1] != chip->audio_active[audio+1]) {
676 vx_set_audio_switch(chip, audio,
677 !!ucontrol->value.integer.value[0]);
678 vx_set_audio_switch(chip, audio+1,
679 !!ucontrol->value.integer.value[1]);
680 mutex_unlock(&chip->mixer_mutex);
681 return 1;
683 mutex_unlock(&chip->mixer_mutex);
684 return 0;
687 static int vx_monitor_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
689 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
690 int audio = kcontrol->private_value & 0xff;
692 mutex_lock(&chip->mixer_mutex);
693 ucontrol->value.integer.value[0] = chip->audio_monitor_active[audio];
694 ucontrol->value.integer.value[1] = chip->audio_monitor_active[audio+1];
695 mutex_unlock(&chip->mixer_mutex);
696 return 0;
699 static int vx_monitor_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
701 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
702 int audio = kcontrol->private_value & 0xff;
704 mutex_lock(&chip->mixer_mutex);
705 if (ucontrol->value.integer.value[0] != chip->audio_monitor_active[audio] ||
706 ucontrol->value.integer.value[1] != chip->audio_monitor_active[audio+1]) {
707 vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
708 !!ucontrol->value.integer.value[0]);
709 vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
710 !!ucontrol->value.integer.value[1]);
711 mutex_unlock(&chip->mixer_mutex);
712 return 1;
714 mutex_unlock(&chip->mixer_mutex);
715 return 0;
718 static const DECLARE_TLV_DB_SCALE(db_scale_audio_gain, -10975, 25, 0);
720 static struct snd_kcontrol_new vx_control_audio_gain = {
721 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
722 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
723 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
724 /* name will be filled later */
725 .info = vx_audio_gain_info,
726 .get = vx_audio_gain_get,
727 .put = vx_audio_gain_put,
728 .tlv = { .p = db_scale_audio_gain },
730 static struct snd_kcontrol_new vx_control_output_switch = {
731 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
732 .name = "PCM Playback Switch",
733 .info = vx_audio_sw_info,
734 .get = vx_audio_sw_get,
735 .put = vx_audio_sw_put
737 static struct snd_kcontrol_new vx_control_monitor_gain = {
738 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
739 .name = "Monitoring Volume",
740 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
741 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
742 .info = vx_audio_gain_info, /* shared */
743 .get = vx_audio_monitor_get,
744 .put = vx_audio_monitor_put,
745 .tlv = { .p = db_scale_audio_gain },
747 static struct snd_kcontrol_new vx_control_monitor_switch = {
748 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
749 .name = "Monitoring Switch",
750 .info = vx_audio_sw_info, /* shared */
751 .get = vx_monitor_sw_get,
752 .put = vx_monitor_sw_put
757 * IEC958 status bits
759 static int vx_iec958_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
761 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
762 uinfo->count = 1;
763 return 0;
766 static int vx_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
768 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
770 mutex_lock(&chip->mixer_mutex);
771 ucontrol->value.iec958.status[0] = (chip->uer_bits >> 0) & 0xff;
772 ucontrol->value.iec958.status[1] = (chip->uer_bits >> 8) & 0xff;
773 ucontrol->value.iec958.status[2] = (chip->uer_bits >> 16) & 0xff;
774 ucontrol->value.iec958.status[3] = (chip->uer_bits >> 24) & 0xff;
775 mutex_unlock(&chip->mixer_mutex);
776 return 0;
779 static int vx_iec958_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
781 ucontrol->value.iec958.status[0] = 0xff;
782 ucontrol->value.iec958.status[1] = 0xff;
783 ucontrol->value.iec958.status[2] = 0xff;
784 ucontrol->value.iec958.status[3] = 0xff;
785 return 0;
788 static int vx_iec958_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
790 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
791 unsigned int val;
793 val = (ucontrol->value.iec958.status[0] << 0) |
794 (ucontrol->value.iec958.status[1] << 8) |
795 (ucontrol->value.iec958.status[2] << 16) |
796 (ucontrol->value.iec958.status[3] << 24);
797 mutex_lock(&chip->mixer_mutex);
798 if (chip->uer_bits != val) {
799 chip->uer_bits = val;
800 vx_set_iec958_status(chip, val);
801 mutex_unlock(&chip->mixer_mutex);
802 return 1;
804 mutex_unlock(&chip->mixer_mutex);
805 return 0;
808 static struct snd_kcontrol_new vx_control_iec958_mask = {
809 .access = SNDRV_CTL_ELEM_ACCESS_READ,
810 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
811 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
812 .info = vx_iec958_info, /* shared */
813 .get = vx_iec958_mask_get,
816 static struct snd_kcontrol_new vx_control_iec958 = {
817 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
818 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
819 .info = vx_iec958_info,
820 .get = vx_iec958_get,
821 .put = vx_iec958_put
826 * VU meter
829 #define METER_MAX 0xff
830 #define METER_SHIFT 16
832 static int vx_vu_meter_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
834 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
835 uinfo->count = 2;
836 uinfo->value.integer.min = 0;
837 uinfo->value.integer.max = METER_MAX;
838 return 0;
841 static int vx_vu_meter_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
843 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
844 struct vx_vu_meter meter[2];
845 int audio = kcontrol->private_value & 0xff;
846 int capture = (kcontrol->private_value >> 8) & 1;
848 vx_get_audio_vu_meter(chip, audio, capture, meter);
849 ucontrol->value.integer.value[0] = meter[0].vu_level >> METER_SHIFT;
850 ucontrol->value.integer.value[1] = meter[1].vu_level >> METER_SHIFT;
851 return 0;
854 static int vx_peak_meter_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
856 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
857 struct vx_vu_meter meter[2];
858 int audio = kcontrol->private_value & 0xff;
859 int capture = (kcontrol->private_value >> 8) & 1;
861 vx_get_audio_vu_meter(chip, audio, capture, meter);
862 ucontrol->value.integer.value[0] = meter[0].peak_level >> METER_SHIFT;
863 ucontrol->value.integer.value[1] = meter[1].peak_level >> METER_SHIFT;
864 return 0;
867 #define vx_saturation_info snd_ctl_boolean_stereo_info
869 static int vx_saturation_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
871 struct vx_core *chip = snd_kcontrol_chip(kcontrol);
872 struct vx_vu_meter meter[2];
873 int audio = kcontrol->private_value & 0xff;
875 vx_get_audio_vu_meter(chip, audio, 1, meter); /* capture only */
876 ucontrol->value.integer.value[0] = meter[0].saturated;
877 ucontrol->value.integer.value[1] = meter[1].saturated;
878 return 0;
881 static struct snd_kcontrol_new vx_control_vu_meter = {
882 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
883 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
884 /* name will be filled later */
885 .info = vx_vu_meter_info,
886 .get = vx_vu_meter_get,
889 static struct snd_kcontrol_new vx_control_peak_meter = {
890 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
891 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
892 /* name will be filled later */
893 .info = vx_vu_meter_info, /* shared */
894 .get = vx_peak_meter_get,
897 static struct snd_kcontrol_new vx_control_saturation = {
898 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
899 .name = "Input Saturation",
900 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
901 .info = vx_saturation_info,
902 .get = vx_saturation_get,
911 int snd_vx_mixer_new(struct vx_core *chip)
913 unsigned int i, c;
914 int err;
915 struct snd_kcontrol_new temp;
916 struct snd_card *card = chip->card;
917 char name[32];
919 strcpy(card->mixername, card->driver);
921 /* output level controls */
922 for (i = 0; i < chip->hw->num_outs; i++) {
923 temp = vx_control_output_level;
924 temp.index = i;
925 temp.tlv.p = chip->hw->output_level_db_scale;
926 if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
927 return err;
930 /* PCM volumes, switches, monitoring */
931 for (i = 0; i < chip->hw->num_outs; i++) {
932 int val = i * 2;
933 temp = vx_control_audio_gain;
934 temp.index = i;
935 temp.name = "PCM Playback Volume";
936 temp.private_value = val;
937 if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
938 return err;
939 temp = vx_control_output_switch;
940 temp.index = i;
941 temp.private_value = val;
942 if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
943 return err;
944 temp = vx_control_monitor_gain;
945 temp.index = i;
946 temp.private_value = val;
947 if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
948 return err;
949 temp = vx_control_monitor_switch;
950 temp.index = i;
951 temp.private_value = val;
952 if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
953 return err;
955 for (i = 0; i < chip->hw->num_outs; i++) {
956 temp = vx_control_audio_gain;
957 temp.index = i;
958 temp.name = "PCM Capture Volume";
959 temp.private_value = (i * 2) | (1 << 8);
960 if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
961 return err;
964 /* Audio source */
965 if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_audio_src, chip))) < 0)
966 return err;
967 /* clock mode */
968 if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_clock_mode, chip))) < 0)
969 return err;
970 /* IEC958 controls */
971 if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_iec958_mask, chip))) < 0)
972 return err;
973 if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_iec958, chip))) < 0)
974 return err;
975 /* VU, peak, saturation meters */
976 for (c = 0; c < 2; c++) {
977 static char *dir[2] = { "Output", "Input" };
978 for (i = 0; i < chip->hw->num_ins; i++) {
979 int val = (i * 2) | (c << 8);
980 if (c == 1) {
981 temp = vx_control_saturation;
982 temp.index = i;
983 temp.private_value = val;
984 if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
985 return err;
987 sprintf(name, "%s VU Meter", dir[c]);
988 temp = vx_control_vu_meter;
989 temp.index = i;
990 temp.name = name;
991 temp.private_value = val;
992 if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
993 return err;
994 sprintf(name, "%s Peak Meter", dir[c]);
995 temp = vx_control_peak_meter;
996 temp.index = i;
997 temp.name = name;
998 temp.private_value = val;
999 if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
1000 return err;
1003 vx_reset_audio_levels(chip);
1004 return 0;