xref: /openbmc/linux/sound/drivers/vx/vx_mixer.c (revision f15cbe6f1a4b4d9df59142fc8e4abb973302cf44)
1 /*
2  * Driver for Digigram VX soundcards
3  *
4  * Common mixer part
5  *
6  * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7  *
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.
12  *
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.
17  *
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
21  */
22 
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"
28 
29 
30 /*
31  * write a codec data (24bit)
32  */
33 static void vx_write_codec_reg(struct vx_core *chip, int codec, unsigned int data)
34 {
35 	unsigned long flags;
36 
37 	snd_assert(chip->ops->write_codec, return);
38 
39 	if (chip->chip_status & VX_STAT_IS_STALE)
40 		return;
41 
42 	spin_lock_irqsave(&chip->lock, flags);
43 	chip->ops->write_codec(chip, codec, data);
44 	spin_unlock_irqrestore(&chip->lock, flags);
45 }
46 
47 /*
48  * Data type used to access the Codec
49  */
50 union vx_codec_data {
51 	u32 l;
52 #ifdef SNDRV_BIG_ENDIAN
53 	struct w {
54 		u16 h;
55 		u16 l;
56 	} w;
57 	struct b {
58 		u8 hh;
59 		u8 mh;
60 		u8 ml;
61 		u8 ll;
62 	} b;
63 #else /* LITTLE_ENDIAN */
64 	struct w {
65 		u16 l;
66 		u16 h;
67 	} w;
68 	struct b {
69 		u8 ll;
70 		u8 ml;
71 		u8 mh;
72 		u8 hh;
73 	} b;
74 #endif
75 };
76 
77 #define SET_CDC_DATA_SEL(di,s)          ((di).b.mh = (u8) (s))
78 #define SET_CDC_DATA_REG(di,r)          ((di).b.ml = (u8) (r))
79 #define SET_CDC_DATA_VAL(di,d)          ((di).b.ll = (u8) (d))
80 #define SET_CDC_DATA_INIT(di)           ((di).l = 0L, SET_CDC_DATA_SEL(di,XX_CODEC_SELECTOR))
81 
82 /*
83  * set up codec register and write the value
84  * @codec: the codec id, 0 or 1
85  * @reg: register index
86  * @val: data value
87  */
88 static void vx_set_codec_reg(struct vx_core *chip, int codec, int reg, int val)
89 {
90 	union vx_codec_data data;
91 	/* DAC control register */
92 	SET_CDC_DATA_INIT(data);
93 	SET_CDC_DATA_REG(data, reg);
94 	SET_CDC_DATA_VAL(data, val);
95 	vx_write_codec_reg(chip, codec, data.l);
96 }
97 
98 
99 /*
100  * vx_set_analog_output_level - set the output attenuation level
101  * @codec: the output codec, 0 or 1.  (1 for VXP440 only)
102  * @left: left output level, 0 = mute
103  * @right: right output level
104  */
105 static void vx_set_analog_output_level(struct vx_core *chip, int codec, int left, int right)
106 {
107 	left  = chip->hw->output_level_max - left;
108 	right = chip->hw->output_level_max - right;
109 
110 	if (chip->ops->akm_write) {
111 		chip->ops->akm_write(chip, XX_CODEC_LEVEL_LEFT_REGISTER, left);
112 		chip->ops->akm_write(chip, XX_CODEC_LEVEL_RIGHT_REGISTER, right);
113 	} else {
114 		/* convert to attenuation level: 0 = 0dB (max), 0xe3 = -113.5 dB (min) */
115 		vx_set_codec_reg(chip, codec, XX_CODEC_LEVEL_LEFT_REGISTER, left);
116 		vx_set_codec_reg(chip, codec, XX_CODEC_LEVEL_RIGHT_REGISTER, right);
117 	}
118 }
119 
120 
121 /*
122  * vx_toggle_dac_mute -  mute/unmute DAC
123  * @mute: 0 = unmute, 1 = mute
124  */
125 
126 #define DAC_ATTEN_MIN	0x08
127 #define DAC_ATTEN_MAX	0x38
128 
129 void vx_toggle_dac_mute(struct vx_core *chip, int mute)
130 {
131 	unsigned int i;
132 	for (i = 0; i < chip->hw->num_codecs; i++) {
133 		if (chip->ops->akm_write)
134 			chip->ops->akm_write(chip, XX_CODEC_DAC_CONTROL_REGISTER, mute); /* XXX */
135 		else
136 			vx_set_codec_reg(chip, i, XX_CODEC_DAC_CONTROL_REGISTER,
137 					 mute ? DAC_ATTEN_MAX : DAC_ATTEN_MIN);
138 	}
139 }
140 
141 /*
142  * vx_reset_codec - reset and initialize the codecs
143  */
144 void vx_reset_codec(struct vx_core *chip, int cold_reset)
145 {
146 	unsigned int i;
147 	int port = chip->type >= VX_TYPE_VXPOCKET ? 0x75 : 0x65;
148 
149 	chip->ops->reset_codec(chip);
150 
151 	/* AKM codecs should be initialized in reset_codec callback */
152 	if (! chip->ops->akm_write) {
153 		/* initialize old codecs */
154 		for (i = 0; i < chip->hw->num_codecs; i++) {
155 			/* DAC control register (change level when zero crossing + mute) */
156 			vx_set_codec_reg(chip, i, XX_CODEC_DAC_CONTROL_REGISTER, DAC_ATTEN_MAX);
157 			/* ADC control register */
158 			vx_set_codec_reg(chip, i, XX_CODEC_ADC_CONTROL_REGISTER, 0x00);
159 			/* Port mode register */
160 			vx_set_codec_reg(chip, i, XX_CODEC_PORT_MODE_REGISTER, port);
161 			/* Clock control register */
162 			vx_set_codec_reg(chip, i, XX_CODEC_CLOCK_CONTROL_REGISTER, 0x00);
163 		}
164 	}
165 
166 	/* mute analog output */
167 	for (i = 0; i < chip->hw->num_codecs; i++) {
168 		chip->output_level[i][0] = 0;
169 		chip->output_level[i][1] = 0;
170 		vx_set_analog_output_level(chip, i, 0, 0);
171 	}
172 }
173 
174 /*
175  * change the audio input source
176  * @src: the target source (VX_AUDIO_SRC_XXX)
177  */
178 static void vx_change_audio_source(struct vx_core *chip, int src)
179 {
180 	unsigned long flags;
181 
182 	if (chip->chip_status & VX_STAT_IS_STALE)
183 		return;
184 
185 	spin_lock_irqsave(&chip->lock, flags);
186 	chip->ops->change_audio_source(chip, src);
187 	spin_unlock_irqrestore(&chip->lock, flags);
188 }
189 
190 
191 /*
192  * change the audio source if necessary and possible
193  * returns 1 if the source is actually changed.
194  */
195 int vx_sync_audio_source(struct vx_core *chip)
196 {
197 	if (chip->audio_source_target == chip->audio_source ||
198 	    chip->pcm_running)
199 		return 0;
200 	vx_change_audio_source(chip, chip->audio_source_target);
201 	chip->audio_source = chip->audio_source_target;
202 	return 1;
203 }
204 
205 
206 /*
207  * audio level, mute, monitoring
208  */
209 struct vx_audio_level {
210 	unsigned int has_level: 1;
211 	unsigned int has_monitor_level: 1;
212 	unsigned int has_mute: 1;
213 	unsigned int has_monitor_mute: 1;
214 	unsigned int mute;
215 	unsigned int monitor_mute;
216 	short level;
217 	short monitor_level;
218 };
219 
220 static int vx_adjust_audio_level(struct vx_core *chip, int audio, int capture,
221 				 struct vx_audio_level *info)
222 {
223 	struct vx_rmh rmh;
224 
225 	if (chip->chip_status & VX_STAT_IS_STALE)
226 		return -EBUSY;
227 
228         vx_init_rmh(&rmh, CMD_AUDIO_LEVEL_ADJUST);
229 	if (capture)
230 		rmh.Cmd[0] |= COMMAND_RECORD_MASK;
231 	/* Add Audio IO mask */
232 	rmh.Cmd[1] = 1 << audio;
233 	rmh.Cmd[2] = 0;
234 	if (info->has_level) {
235 		rmh.Cmd[0] |=  VALID_AUDIO_IO_DIGITAL_LEVEL;
236 		rmh.Cmd[2] |= info->level;
237         }
238 	if (info->has_monitor_level) {
239 		rmh.Cmd[0] |=  VALID_AUDIO_IO_MONITORING_LEVEL;
240 		rmh.Cmd[2] |= ((unsigned int)info->monitor_level << 10);
241         }
242 	if (info->has_mute) {
243 		rmh.Cmd[0] |= VALID_AUDIO_IO_MUTE_LEVEL;
244 		if (info->mute)
245 			rmh.Cmd[2] |= AUDIO_IO_HAS_MUTE_LEVEL;
246 	}
247 	if (info->has_monitor_mute) {
248 		/* validate flag for M2 at least to unmute it */
249 		rmh.Cmd[0] |=  VALID_AUDIO_IO_MUTE_MONITORING_1 | VALID_AUDIO_IO_MUTE_MONITORING_2;
250 		if (info->monitor_mute)
251 			rmh.Cmd[2] |= AUDIO_IO_HAS_MUTE_MONITORING_1;
252 	}
253 
254 	return vx_send_msg(chip, &rmh);
255 }
256 
257 
258 #if 0 // not used
259 static int vx_read_audio_level(struct vx_core *chip, int audio, int capture,
260 			       struct vx_audio_level *info)
261 {
262 	int err;
263 	struct vx_rmh rmh;
264 
265 	memset(info, 0, sizeof(*info));
266         vx_init_rmh(&rmh, CMD_GET_AUDIO_LEVELS);
267 	if (capture)
268 		rmh.Cmd[0] |= COMMAND_RECORD_MASK;
269 	/* Add Audio IO mask */
270 	rmh.Cmd[1] = 1 << audio;
271 	err = vx_send_msg(chip, &rmh);
272 	if (err < 0)
273 		return err;
274 	info.level = rmh.Stat[0] & MASK_DSP_WORD_LEVEL;
275 	info.monitor_level = (rmh.Stat[0] >> 10) & MASK_DSP_WORD_LEVEL;
276 	info.mute = (rmh.Stat[i] & AUDIO_IO_HAS_MUTE_LEVEL) ? 1 : 0;
277 	info.monitor_mute = (rmh.Stat[i] & AUDIO_IO_HAS_MUTE_MONITORING_1) ? 1 : 0;
278 	return 0;
279 }
280 #endif // not used
281 
282 /*
283  * set the monitoring level and mute state of the given audio
284  * no more static, because must be called from vx_pcm to demute monitoring
285  */
286 int vx_set_monitor_level(struct vx_core *chip, int audio, int level, int active)
287 {
288 	struct vx_audio_level info;
289 
290 	memset(&info, 0, sizeof(info));
291 	info.has_monitor_level = 1;
292 	info.monitor_level = level;
293 	info.has_monitor_mute = 1;
294 	info.monitor_mute = !active;
295 	chip->audio_monitor[audio] = level;
296 	chip->audio_monitor_active[audio] = active;
297 	return vx_adjust_audio_level(chip, audio, 0, &info); /* playback only */
298 }
299 
300 
301 /*
302  * set the mute status of the given audio
303  */
304 static int vx_set_audio_switch(struct vx_core *chip, int audio, int active)
305 {
306 	struct vx_audio_level info;
307 
308 	memset(&info, 0, sizeof(info));
309 	info.has_mute = 1;
310 	info.mute = !active;
311 	chip->audio_active[audio] = active;
312 	return vx_adjust_audio_level(chip, audio, 0, &info); /* playback only */
313 }
314 
315 /*
316  * set the mute status of the given audio
317  */
318 static int vx_set_audio_gain(struct vx_core *chip, int audio, int capture, int level)
319 {
320 	struct vx_audio_level info;
321 
322 	memset(&info, 0, sizeof(info));
323 	info.has_level = 1;
324 	info.level = level;
325 	chip->audio_gain[capture][audio] = level;
326 	return vx_adjust_audio_level(chip, audio, capture, &info);
327 }
328 
329 /*
330  * reset all audio levels
331  */
332 static void vx_reset_audio_levels(struct vx_core *chip)
333 {
334 	unsigned int i, c;
335 	struct vx_audio_level info;
336 
337 	memset(chip->audio_gain, 0, sizeof(chip->audio_gain));
338 	memset(chip->audio_active, 0, sizeof(chip->audio_active));
339 	memset(chip->audio_monitor, 0, sizeof(chip->audio_monitor));
340 	memset(chip->audio_monitor_active, 0, sizeof(chip->audio_monitor_active));
341 
342 	for (c = 0; c < 2; c++) {
343 		for (i = 0; i < chip->hw->num_ins * 2; i++) {
344 			memset(&info, 0, sizeof(info));
345 			if (c == 0) {
346 				info.has_monitor_level = 1;
347 				info.has_mute = 1;
348 				info.has_monitor_mute = 1;
349 			}
350 			info.has_level = 1;
351 			info.level = CVAL_0DB; /* default: 0dB */
352 			vx_adjust_audio_level(chip, i, c, &info);
353 			chip->audio_gain[c][i] = CVAL_0DB;
354 			chip->audio_monitor[i] = CVAL_0DB;
355 		}
356 	}
357 }
358 
359 
360 /*
361  * VU, peak meter record
362  */
363 
364 #define VU_METER_CHANNELS	2
365 
366 struct vx_vu_meter {
367 	int saturated;
368 	int vu_level;
369 	int peak_level;
370 };
371 
372 /*
373  * get the VU and peak meter values
374  * @audio: the audio index
375  * @capture: 0 = playback, 1 = capture operation
376  * @info: the array of vx_vu_meter records (size = 2).
377  */
378 static int vx_get_audio_vu_meter(struct vx_core *chip, int audio, int capture, struct vx_vu_meter *info)
379 {
380 	struct vx_rmh rmh;
381 	int i, err;
382 
383 	if (chip->chip_status & VX_STAT_IS_STALE)
384 		return -EBUSY;
385 
386 	vx_init_rmh(&rmh, CMD_AUDIO_VU_PIC_METER);
387 	rmh.LgStat += 2 * VU_METER_CHANNELS;
388 	if (capture)
389 		rmh.Cmd[0] |= COMMAND_RECORD_MASK;
390 
391         /* Add Audio IO mask */
392 	rmh.Cmd[1] = 0;
393 	for (i = 0; i < VU_METER_CHANNELS; i++)
394 		rmh.Cmd[1] |= 1 << (audio + i);
395 	err = vx_send_msg(chip, &rmh);
396 	if (err < 0)
397 		return err;
398 	/* Read response */
399 	for (i = 0; i < 2 * VU_METER_CHANNELS; i +=2) {
400 		info->saturated = (rmh.Stat[0] & (1 << (audio + i))) ? 1 : 0;
401 		info->vu_level = rmh.Stat[i + 1];
402 		info->peak_level = rmh.Stat[i + 2];
403 		info++;
404 	}
405 	return 0;
406 }
407 
408 
409 /*
410  * control API entries
411  */
412 
413 /*
414  * output level control
415  */
416 static int vx_output_level_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
417 {
418 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
419 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
420 	uinfo->count = 2;
421 	uinfo->value.integer.min = 0;
422 	uinfo->value.integer.max = chip->hw->output_level_max;
423 	return 0;
424 }
425 
426 static int vx_output_level_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
427 {
428 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
429 	int codec = kcontrol->id.index;
430 	mutex_lock(&chip->mixer_mutex);
431 	ucontrol->value.integer.value[0] = chip->output_level[codec][0];
432 	ucontrol->value.integer.value[1] = chip->output_level[codec][1];
433 	mutex_unlock(&chip->mixer_mutex);
434 	return 0;
435 }
436 
437 static int vx_output_level_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
438 {
439 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
440 	int codec = kcontrol->id.index;
441 	unsigned int val[2], vmax;
442 
443 	vmax = chip->hw->output_level_max;
444 	val[0] = ucontrol->value.integer.value[0];
445 	val[1] = ucontrol->value.integer.value[1];
446 	if (val[0] > vmax || val[1] > vmax)
447 		return -EINVAL;
448 	mutex_lock(&chip->mixer_mutex);
449 	if (val[0] != chip->output_level[codec][0] ||
450 	    val[1] != chip->output_level[codec][1]) {
451 		vx_set_analog_output_level(chip, codec, val[0], val[1]);
452 		chip->output_level[codec][0] = val[0];
453 		chip->output_level[codec][1] = val[1];
454 		mutex_unlock(&chip->mixer_mutex);
455 		return 1;
456 	}
457 	mutex_unlock(&chip->mixer_mutex);
458 	return 0;
459 }
460 
461 static struct snd_kcontrol_new vx_control_output_level = {
462 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
463 	.access =	(SNDRV_CTL_ELEM_ACCESS_READWRITE |
464 			 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
465 	.name =		"Master Playback Volume",
466 	.info =		vx_output_level_info,
467 	.get =		vx_output_level_get,
468 	.put =		vx_output_level_put,
469 	/* tlv will be filled later */
470 };
471 
472 /*
473  * audio source select
474  */
475 static int vx_audio_src_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
476 {
477 	static char *texts_mic[3] = {
478 		"Digital", "Line", "Mic"
479 	};
480 	static char *texts_vx2[2] = {
481 		"Digital", "Analog"
482 	};
483 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
484 
485 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
486 	uinfo->count = 1;
487 	if (chip->type >= VX_TYPE_VXPOCKET) {
488 		uinfo->value.enumerated.items = 3;
489 		if (uinfo->value.enumerated.item > 2)
490 			uinfo->value.enumerated.item = 2;
491 		strcpy(uinfo->value.enumerated.name,
492 		       texts_mic[uinfo->value.enumerated.item]);
493 	} else {
494 		uinfo->value.enumerated.items = 2;
495 		if (uinfo->value.enumerated.item > 1)
496 			uinfo->value.enumerated.item = 1;
497 		strcpy(uinfo->value.enumerated.name,
498 		       texts_vx2[uinfo->value.enumerated.item]);
499 	}
500 	return 0;
501 }
502 
503 static int vx_audio_src_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
504 {
505 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
506 	ucontrol->value.enumerated.item[0] = chip->audio_source_target;
507 	return 0;
508 }
509 
510 static int vx_audio_src_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
511 {
512 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
513 
514 	if (chip->type >= VX_TYPE_VXPOCKET) {
515 		if (ucontrol->value.enumerated.item[0] > 2)
516 			return -EINVAL;
517 	} else {
518 		if (ucontrol->value.enumerated.item[0] > 1)
519 			return -EINVAL;
520 	}
521 	mutex_lock(&chip->mixer_mutex);
522 	if (chip->audio_source_target != ucontrol->value.enumerated.item[0]) {
523 		chip->audio_source_target = ucontrol->value.enumerated.item[0];
524 		vx_sync_audio_source(chip);
525 		mutex_unlock(&chip->mixer_mutex);
526 		return 1;
527 	}
528 	mutex_unlock(&chip->mixer_mutex);
529 	return 0;
530 }
531 
532 static struct snd_kcontrol_new vx_control_audio_src = {
533 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
534 	.name =		"Capture Source",
535 	.info =		vx_audio_src_info,
536 	.get =		vx_audio_src_get,
537 	.put =		vx_audio_src_put,
538 };
539 
540 /*
541  * clock mode selection
542  */
543 static int vx_clock_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
544 {
545 	static char *texts[3] = {
546 		"Auto", "Internal", "External"
547 	};
548 
549 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
550 	uinfo->count = 1;
551 	uinfo->value.enumerated.items = 3;
552 	if (uinfo->value.enumerated.item > 2)
553 		uinfo->value.enumerated.item = 2;
554 	strcpy(uinfo->value.enumerated.name,
555 	       texts[uinfo->value.enumerated.item]);
556 	return 0;
557 }
558 
559 static int vx_clock_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
560 {
561 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
562 	ucontrol->value.enumerated.item[0] = chip->clock_mode;
563 	return 0;
564 }
565 
566 static int vx_clock_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
567 {
568 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
569 
570 	if (ucontrol->value.enumerated.item[0] > 2)
571 		return -EINVAL;
572 	mutex_lock(&chip->mixer_mutex);
573 	if (chip->clock_mode != ucontrol->value.enumerated.item[0]) {
574 		chip->clock_mode = ucontrol->value.enumerated.item[0];
575 		vx_set_clock(chip, chip->freq);
576 		mutex_unlock(&chip->mixer_mutex);
577 		return 1;
578 	}
579 	mutex_unlock(&chip->mixer_mutex);
580 	return 0;
581 }
582 
583 static struct snd_kcontrol_new vx_control_clock_mode = {
584 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
585 	.name =		"Clock Mode",
586 	.info =		vx_clock_mode_info,
587 	.get =		vx_clock_mode_get,
588 	.put =		vx_clock_mode_put,
589 };
590 
591 /*
592  * Audio Gain
593  */
594 static int vx_audio_gain_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
595 {
596 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
597 	uinfo->count = 2;
598 	uinfo->value.integer.min = 0;
599 	uinfo->value.integer.max = CVAL_MAX;
600 	return 0;
601 }
602 
603 static int vx_audio_gain_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
604 {
605 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
606 	int audio = kcontrol->private_value & 0xff;
607 	int capture = (kcontrol->private_value >> 8) & 1;
608 
609 	mutex_lock(&chip->mixer_mutex);
610 	ucontrol->value.integer.value[0] = chip->audio_gain[capture][audio];
611 	ucontrol->value.integer.value[1] = chip->audio_gain[capture][audio+1];
612 	mutex_unlock(&chip->mixer_mutex);
613 	return 0;
614 }
615 
616 static int vx_audio_gain_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
617 {
618 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
619 	int audio = kcontrol->private_value & 0xff;
620 	int capture = (kcontrol->private_value >> 8) & 1;
621 	unsigned int val[2];
622 
623 	val[0] = ucontrol->value.integer.value[0];
624 	val[1] = ucontrol->value.integer.value[1];
625 	if (val[0] > CVAL_MAX || val[1] > CVAL_MAX)
626 		return -EINVAL;
627 	mutex_lock(&chip->mixer_mutex);
628 	if (val[0] != chip->audio_gain[capture][audio] ||
629 	    val[1] != chip->audio_gain[capture][audio+1]) {
630 		vx_set_audio_gain(chip, audio, capture, val[0]);
631 		vx_set_audio_gain(chip, audio+1, capture, val[1]);
632 		mutex_unlock(&chip->mixer_mutex);
633 		return 1;
634 	}
635 	mutex_unlock(&chip->mixer_mutex);
636 	return 0;
637 }
638 
639 static int vx_audio_monitor_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
640 {
641 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
642 	int audio = kcontrol->private_value & 0xff;
643 
644 	mutex_lock(&chip->mixer_mutex);
645 	ucontrol->value.integer.value[0] = chip->audio_monitor[audio];
646 	ucontrol->value.integer.value[1] = chip->audio_monitor[audio+1];
647 	mutex_unlock(&chip->mixer_mutex);
648 	return 0;
649 }
650 
651 static int vx_audio_monitor_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
652 {
653 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
654 	int audio = kcontrol->private_value & 0xff;
655 	unsigned int val[2];
656 
657 	val[0] = ucontrol->value.integer.value[0];
658 	val[1] = ucontrol->value.integer.value[1];
659 	if (val[0] > CVAL_MAX || val[1] > CVAL_MAX)
660 		return -EINVAL;
661 
662 	mutex_lock(&chip->mixer_mutex);
663 	if (val[0] != chip->audio_monitor[audio] ||
664 	    val[1] != chip->audio_monitor[audio+1]) {
665 		vx_set_monitor_level(chip, audio, val[0],
666 				     chip->audio_monitor_active[audio]);
667 		vx_set_monitor_level(chip, audio+1, val[1],
668 				     chip->audio_monitor_active[audio+1]);
669 		mutex_unlock(&chip->mixer_mutex);
670 		return 1;
671 	}
672 	mutex_unlock(&chip->mixer_mutex);
673 	return 0;
674 }
675 
676 #define vx_audio_sw_info	snd_ctl_boolean_stereo_info
677 
678 static int vx_audio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
679 {
680 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
681 	int audio = kcontrol->private_value & 0xff;
682 
683 	mutex_lock(&chip->mixer_mutex);
684 	ucontrol->value.integer.value[0] = chip->audio_active[audio];
685 	ucontrol->value.integer.value[1] = chip->audio_active[audio+1];
686 	mutex_unlock(&chip->mixer_mutex);
687 	return 0;
688 }
689 
690 static int vx_audio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
691 {
692 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
693 	int audio = kcontrol->private_value & 0xff;
694 
695 	mutex_lock(&chip->mixer_mutex);
696 	if (ucontrol->value.integer.value[0] != chip->audio_active[audio] ||
697 	    ucontrol->value.integer.value[1] != chip->audio_active[audio+1]) {
698 		vx_set_audio_switch(chip, audio,
699 				    !!ucontrol->value.integer.value[0]);
700 		vx_set_audio_switch(chip, audio+1,
701 				    !!ucontrol->value.integer.value[1]);
702 		mutex_unlock(&chip->mixer_mutex);
703 		return 1;
704 	}
705 	mutex_unlock(&chip->mixer_mutex);
706 	return 0;
707 }
708 
709 static int vx_monitor_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
710 {
711 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
712 	int audio = kcontrol->private_value & 0xff;
713 
714 	mutex_lock(&chip->mixer_mutex);
715 	ucontrol->value.integer.value[0] = chip->audio_monitor_active[audio];
716 	ucontrol->value.integer.value[1] = chip->audio_monitor_active[audio+1];
717 	mutex_unlock(&chip->mixer_mutex);
718 	return 0;
719 }
720 
721 static int vx_monitor_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
722 {
723 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
724 	int audio = kcontrol->private_value & 0xff;
725 
726 	mutex_lock(&chip->mixer_mutex);
727 	if (ucontrol->value.integer.value[0] != chip->audio_monitor_active[audio] ||
728 	    ucontrol->value.integer.value[1] != chip->audio_monitor_active[audio+1]) {
729 		vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
730 				     !!ucontrol->value.integer.value[0]);
731 		vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
732 				     !!ucontrol->value.integer.value[1]);
733 		mutex_unlock(&chip->mixer_mutex);
734 		return 1;
735 	}
736 	mutex_unlock(&chip->mixer_mutex);
737 	return 0;
738 }
739 
740 static const DECLARE_TLV_DB_SCALE(db_scale_audio_gain, -10975, 25, 0);
741 
742 static struct snd_kcontrol_new vx_control_audio_gain = {
743 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
744 	.access =	(SNDRV_CTL_ELEM_ACCESS_READWRITE |
745 			 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
746 	/* name will be filled later */
747 	.info =         vx_audio_gain_info,
748 	.get =          vx_audio_gain_get,
749 	.put =          vx_audio_gain_put,
750 	.tlv = { .p = db_scale_audio_gain },
751 };
752 static struct snd_kcontrol_new vx_control_output_switch = {
753 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
754 	.name =         "PCM Playback Switch",
755 	.info =         vx_audio_sw_info,
756 	.get =          vx_audio_sw_get,
757 	.put =          vx_audio_sw_put
758 };
759 static struct snd_kcontrol_new vx_control_monitor_gain = {
760 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
761 	.name =         "Monitoring Volume",
762 	.access =	(SNDRV_CTL_ELEM_ACCESS_READWRITE |
763 			 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
764 	.info =         vx_audio_gain_info,	/* shared */
765 	.get =          vx_audio_monitor_get,
766 	.put =          vx_audio_monitor_put,
767 	.tlv = { .p = db_scale_audio_gain },
768 };
769 static struct snd_kcontrol_new vx_control_monitor_switch = {
770 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
771 	.name =         "Monitoring Switch",
772 	.info =         vx_audio_sw_info,	/* shared */
773 	.get =          vx_monitor_sw_get,
774 	.put =          vx_monitor_sw_put
775 };
776 
777 
778 /*
779  * IEC958 status bits
780  */
781 static int vx_iec958_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
782 {
783 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
784 	uinfo->count = 1;
785 	return 0;
786 }
787 
788 static int vx_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
789 {
790 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
791 
792 	mutex_lock(&chip->mixer_mutex);
793 	ucontrol->value.iec958.status[0] = (chip->uer_bits >> 0) & 0xff;
794 	ucontrol->value.iec958.status[1] = (chip->uer_bits >> 8) & 0xff;
795 	ucontrol->value.iec958.status[2] = (chip->uer_bits >> 16) & 0xff;
796 	ucontrol->value.iec958.status[3] = (chip->uer_bits >> 24) & 0xff;
797 	mutex_unlock(&chip->mixer_mutex);
798         return 0;
799 }
800 
801 static int vx_iec958_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
802 {
803 	ucontrol->value.iec958.status[0] = 0xff;
804 	ucontrol->value.iec958.status[1] = 0xff;
805 	ucontrol->value.iec958.status[2] = 0xff;
806 	ucontrol->value.iec958.status[3] = 0xff;
807         return 0;
808 }
809 
810 static int vx_iec958_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
811 {
812 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
813 	unsigned int val;
814 
815 	val = (ucontrol->value.iec958.status[0] << 0) |
816 	      (ucontrol->value.iec958.status[1] << 8) |
817 	      (ucontrol->value.iec958.status[2] << 16) |
818 	      (ucontrol->value.iec958.status[3] << 24);
819 	mutex_lock(&chip->mixer_mutex);
820 	if (chip->uer_bits != val) {
821 		chip->uer_bits = val;
822 		vx_set_iec958_status(chip, val);
823 		mutex_unlock(&chip->mixer_mutex);
824 		return 1;
825 	}
826 	mutex_unlock(&chip->mixer_mutex);
827 	return 0;
828 }
829 
830 static struct snd_kcontrol_new vx_control_iec958_mask = {
831 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
832 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
833 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
834 	.info =		vx_iec958_info,	/* shared */
835 	.get =		vx_iec958_mask_get,
836 };
837 
838 static struct snd_kcontrol_new vx_control_iec958 = {
839 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
840 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
841 	.info =         vx_iec958_info,
842 	.get =          vx_iec958_get,
843 	.put =          vx_iec958_put
844 };
845 
846 
847 /*
848  * VU meter
849  */
850 
851 #define METER_MAX	0xff
852 #define METER_SHIFT	16
853 
854 static int vx_vu_meter_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
855 {
856 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
857 	uinfo->count = 2;
858 	uinfo->value.integer.min = 0;
859 	uinfo->value.integer.max = METER_MAX;
860 	return 0;
861 }
862 
863 static int vx_vu_meter_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
864 {
865 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
866 	struct vx_vu_meter meter[2];
867 	int audio = kcontrol->private_value & 0xff;
868 	int capture = (kcontrol->private_value >> 8) & 1;
869 
870 	vx_get_audio_vu_meter(chip, audio, capture, meter);
871 	ucontrol->value.integer.value[0] = meter[0].vu_level >> METER_SHIFT;
872 	ucontrol->value.integer.value[1] = meter[1].vu_level >> METER_SHIFT;
873 	return 0;
874 }
875 
876 static int vx_peak_meter_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
877 {
878 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
879 	struct vx_vu_meter meter[2];
880 	int audio = kcontrol->private_value & 0xff;
881 	int capture = (kcontrol->private_value >> 8) & 1;
882 
883 	vx_get_audio_vu_meter(chip, audio, capture, meter);
884 	ucontrol->value.integer.value[0] = meter[0].peak_level >> METER_SHIFT;
885 	ucontrol->value.integer.value[1] = meter[1].peak_level >> METER_SHIFT;
886 	return 0;
887 }
888 
889 #define vx_saturation_info	snd_ctl_boolean_stereo_info
890 
891 static int vx_saturation_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
892 {
893 	struct vx_core *chip = snd_kcontrol_chip(kcontrol);
894 	struct vx_vu_meter meter[2];
895 	int audio = kcontrol->private_value & 0xff;
896 
897 	vx_get_audio_vu_meter(chip, audio, 1, meter); /* capture only */
898 	ucontrol->value.integer.value[0] = meter[0].saturated;
899 	ucontrol->value.integer.value[1] = meter[1].saturated;
900 	return 0;
901 }
902 
903 static struct snd_kcontrol_new vx_control_vu_meter = {
904 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
905 	.access =	SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
906 	/* name will be filled later */
907 	.info =		vx_vu_meter_info,
908 	.get =		vx_vu_meter_get,
909 };
910 
911 static struct snd_kcontrol_new vx_control_peak_meter = {
912 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
913 	.access =	SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
914 	/* name will be filled later */
915 	.info =		vx_vu_meter_info,	/* shared */
916 	.get =		vx_peak_meter_get,
917 };
918 
919 static struct snd_kcontrol_new vx_control_saturation = {
920 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
921 	.name =		"Input Saturation",
922 	.access =	SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
923 	.info =		vx_saturation_info,
924 	.get =		vx_saturation_get,
925 };
926 
927 
928 
929 /*
930  *
931  */
932 
933 int snd_vx_mixer_new(struct vx_core *chip)
934 {
935 	unsigned int i, c;
936 	int err;
937 	struct snd_kcontrol_new temp;
938 	struct snd_card *card = chip->card;
939 	char name[32];
940 
941 	strcpy(card->mixername, card->driver);
942 
943 	/* output level controls */
944 	for (i = 0; i < chip->hw->num_outs; i++) {
945 		temp = vx_control_output_level;
946 		temp.index = i;
947 		temp.tlv.p = chip->hw->output_level_db_scale;
948 		if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
949 			return err;
950 	}
951 
952 	/* PCM volumes, switches, monitoring */
953 	for (i = 0; i < chip->hw->num_outs; i++) {
954 		int val = i * 2;
955 		temp = vx_control_audio_gain;
956 		temp.index = i;
957 		temp.name = "PCM Playback Volume";
958 		temp.private_value = val;
959 		if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
960 			return err;
961 		temp = vx_control_output_switch;
962 		temp.index = i;
963 		temp.private_value = val;
964 		if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
965 			return err;
966 		temp = vx_control_monitor_gain;
967 		temp.index = i;
968 		temp.private_value = val;
969 		if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
970 			return err;
971 		temp = vx_control_monitor_switch;
972 		temp.index = i;
973 		temp.private_value = val;
974 		if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
975 			return err;
976 	}
977 	for (i = 0; i < chip->hw->num_outs; i++) {
978 		temp = vx_control_audio_gain;
979 		temp.index = i;
980 		temp.name = "PCM Capture Volume";
981 		temp.private_value = (i * 2) | (1 << 8);
982 		if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
983 			return err;
984 	}
985 
986 	/* Audio source */
987 	if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_audio_src, chip))) < 0)
988 		return err;
989 	/* clock mode */
990 	if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_clock_mode, chip))) < 0)
991 		return err;
992 	/* IEC958 controls */
993 	if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_iec958_mask, chip))) < 0)
994 		return err;
995 	if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_iec958, chip))) < 0)
996 		return err;
997 	/* VU, peak, saturation meters */
998 	for (c = 0; c < 2; c++) {
999 		static char *dir[2] = { "Output", "Input" };
1000 		for (i = 0; i < chip->hw->num_ins; i++) {
1001 			int val = (i * 2) | (c << 8);
1002 			if (c == 1) {
1003 				temp = vx_control_saturation;
1004 				temp.index = i;
1005 				temp.private_value = val;
1006 				if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
1007 					return err;
1008 			}
1009 			sprintf(name, "%s VU Meter", dir[c]);
1010 			temp = vx_control_vu_meter;
1011 			temp.index = i;
1012 			temp.name = name;
1013 			temp.private_value = val;
1014 			if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
1015 				return err;
1016 			sprintf(name, "%s Peak Meter", dir[c]);
1017 			temp = vx_control_peak_meter;
1018 			temp.index = i;
1019 			temp.name = name;
1020 			temp.private_value = val;
1021 			if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
1022 				return err;
1023 		}
1024 	}
1025 	vx_reset_audio_levels(chip);
1026 	return 0;
1027 }
1028