Linux Audio

Check our new training course

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