[ALSA] oxygen: add front panel controls

Add mixer controls for the front panel AC97 codec.

Signed-off-by: Clemens Ladisch <clemens@ladisch.de>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
diff --git a/sound/pci/oxygen/oxygen.h b/sound/pci/oxygen/oxygen.h
index 55ce244..e71c534 100644
--- a/sound/pci/oxygen/oxygen.h
+++ b/sound/pci/oxygen/oxygen.h
@@ -86,7 +86,7 @@
 			       struct snd_pcm_hw_params *params);
 	void (*update_dac_volume)(struct oxygen *chip);
 	void (*update_dac_mute)(struct oxygen *chip);
-	void (*ac97_switch_hook)(struct oxygen *chip,
+	void (*ac97_switch_hook)(struct oxygen *chip, unsigned int codec,
 				 unsigned int reg, int mute);
 	size_t model_data_size;
 	u8 dac_channels;
diff --git a/sound/pci/oxygen/oxygen_lib.c b/sound/pci/oxygen/oxygen_lib.c
index de6bf41..540e56b 100644
--- a/sound/pci/oxygen/oxygen_lib.c
+++ b/sound/pci/oxygen/oxygen_lib.c
@@ -365,8 +365,7 @@
 		oxygen_write_ac97(chip, 1, AC97_AUX, 0x8808);
 		oxygen_write_ac97(chip, 1, AC97_PCM, 0x0808);
 		oxygen_write_ac97(chip, 1, AC97_REC_SEL, 0x0000);
-		oxygen_write_ac97(chip, 1, AC97_REC_GAIN, 0x8000);
-		oxygen_ac97_clear_bits(chip, 1, AC97_REC_GAIN, 0x1c00);
+		oxygen_write_ac97(chip, 1, AC97_REC_GAIN, 0x0000);
 		oxygen_ac97_set_bits(chip, 1, 0x6a, 0x0040);
 	}
 }
diff --git a/sound/pci/oxygen/oxygen_mixer.c b/sound/pci/oxygen/oxygen_mixer.c
index cf34b12..a8e4623 100644
--- a/sound/pci/oxygen/oxygen_mixer.c
+++ b/sound/pci/oxygen/oxygen_mixer.c
@@ -450,13 +450,14 @@
 			   struct snd_ctl_elem_value *value)
 {
 	struct oxygen *chip = ctl->private_data;
+	unsigned int codec = (ctl->private_value >> 24) & 1;
 	unsigned int index = ctl->private_value & 0xff;
 	unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
 	int invert = ctl->private_value & (1 << 16);
 	u16 reg;
 
 	mutex_lock(&chip->mutex);
-	reg = oxygen_read_ac97(chip, 0, index);
+	reg = oxygen_read_ac97(chip, codec, index);
 	mutex_unlock(&chip->mutex);
 	if (!(reg & (1 << bitnr)) ^ !invert)
 		value->value.integer.value[0] = 1;
@@ -469,6 +470,7 @@
 			   struct snd_ctl_elem_value *value)
 {
 	struct oxygen *chip = ctl->private_data;
+	unsigned int codec = (ctl->private_value >> 24) & 1;
 	unsigned int index = ctl->private_value & 0xff;
 	unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
 	int invert = ctl->private_value & (1 << 16);
@@ -476,7 +478,7 @@
 	int change;
 
 	mutex_lock(&chip->mutex);
-	oldreg = oxygen_read_ac97(chip, 0, index);
+	oldreg = oxygen_read_ac97(chip, codec, index);
 	newreg = oldreg;
 	if (!value->value.integer.value[0] ^ !invert)
 		newreg |= 1 << bitnr;
@@ -484,9 +486,9 @@
 		newreg &= ~(1 << bitnr);
 	change = newreg != oldreg;
 	if (change) {
-		oxygen_write_ac97(chip, 0, index, newreg);
+		oxygen_write_ac97(chip, codec, index, newreg);
 		if (bitnr == 15 && chip->model->ac97_switch_hook)
-			chip->model->ac97_switch_hook(chip, index,
+			chip->model->ac97_switch_hook(chip, codec, index,
 						      newreg & 0x8000);
 	}
 	mutex_unlock(&chip->mutex);
@@ -507,11 +509,12 @@
 			   struct snd_ctl_elem_value *value)
 {
 	struct oxygen *chip = ctl->private_data;
-	unsigned int index = ctl->private_value;
+	unsigned int codec = (ctl->private_value >> 24) & 1;
+	unsigned int index = ctl->private_value & 0xff;
 	u16 reg;
 
 	mutex_lock(&chip->mutex);
-	reg = oxygen_read_ac97(chip, 0, index);
+	reg = oxygen_read_ac97(chip, codec, index);
 	mutex_unlock(&chip->mutex);
 	value->value.integer.value[0] = 31 - (reg & 0x1f);
 	value->value.integer.value[1] = 31 - ((reg >> 8) & 0x1f);
@@ -522,12 +525,13 @@
 			   struct snd_ctl_elem_value *value)
 {
 	struct oxygen *chip = ctl->private_data;
-	unsigned int index = ctl->private_value;
+	unsigned int codec = (ctl->private_value >> 24) & 1;
+	unsigned int index = ctl->private_value & 0xff;
 	u16 oldreg, newreg;
 	int change;
 
 	mutex_lock(&chip->mutex);
-	oldreg = oxygen_read_ac97(chip, 0, index);
+	oldreg = oxygen_read_ac97(chip, codec, index);
 	newreg = oldreg;
 	newreg = (newreg & ~0x1f) |
 		(31 - (value->value.integer.value[0] & 0x1f));
@@ -535,30 +539,77 @@
 		((31 - (value->value.integer.value[0] & 0x1f)) << 8);
 	change = newreg != oldreg;
 	if (change)
-		oxygen_write_ac97(chip, 0, index, newreg);
+		oxygen_write_ac97(chip, codec, index, newreg);
 	mutex_unlock(&chip->mutex);
 	return change;
 }
 
-#define AC97_SWITCH(xname, index, bitnr, invert) { \
+static int ac97_fp_rec_volume_info(struct snd_kcontrol *ctl,
+				   struct snd_ctl_elem_info *info)
+{
+	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+	info->count = 2;
+	info->value.integer.min = 0;
+	info->value.integer.max = 7;
+	return 0;
+}
+
+static int ac97_fp_rec_volume_get(struct snd_kcontrol *ctl,
+				  struct snd_ctl_elem_value *value)
+{
+	struct oxygen *chip = ctl->private_data;
+	u16 reg;
+
+	mutex_lock(&chip->mutex);
+	reg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
+	mutex_unlock(&chip->mutex);
+	value->value.integer.value[0] = reg & 7;
+	value->value.integer.value[1] = (reg >> 8) & 7;
+	return 0;
+}
+
+static int ac97_fp_rec_volume_put(struct snd_kcontrol *ctl,
+				  struct snd_ctl_elem_value *value)
+{
+	struct oxygen *chip = ctl->private_data;
+	u16 oldreg, newreg;
+	int change;
+
+	mutex_lock(&chip->mutex);
+	oldreg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
+	newreg = oldreg & ~0x0707;
+	newreg = newreg | (value->value.integer.value[0] & 7);
+	newreg = newreg | ((value->value.integer.value[0] & 7) << 8);
+	change = newreg != oldreg;
+	if (change)
+		oxygen_write_ac97(chip, 1, AC97_REC_GAIN, newreg);
+	mutex_unlock(&chip->mutex);
+	return change;
+}
+
+#define AC97_SWITCH(xname, codec, index, bitnr, invert) { \
 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
 		.name = xname, \
 		.info = snd_ctl_boolean_mono_info, \
 		.get = ac97_switch_get, \
 		.put = ac97_switch_put, \
-		.private_value = ((invert) << 16) | ((bitnr) << 8) | (index), \
+		.private_value = ((codec) << 24) | ((invert) << 16) | \
+				 ((bitnr) << 8) | (index), \
 	}
-#define AC97_VOLUME(xname, index) { \
+#define AC97_VOLUME(xname, codec, index) { \
 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
 		.name = xname, \
+		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
+			  SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
 		.info = ac97_volume_info, \
 		.get = ac97_volume_get, \
 		.put = ac97_volume_put, \
 		.tlv = { .p = ac97_db_scale, }, \
-		.private_value = (index), \
+		.private_value = ((codec) << 24) | (index), \
 	}
 
 static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0);
+static DECLARE_TLV_DB_SCALE(ac97_rec_db_scale, 0, 150, 0);
 
 static const struct snd_kcontrol_new controls[] = {
 	{
@@ -642,15 +693,31 @@
 };
 
 static const struct snd_kcontrol_new ac97_controls[] = {
-	AC97_VOLUME("Mic Capture Volume", AC97_MIC),
-	AC97_SWITCH("Mic Capture Switch", AC97_MIC, 15, 1),
-	AC97_SWITCH("Mic Boost (+20dB)", AC97_MIC, 6, 0),
-	AC97_VOLUME("Line Capture Volume", AC97_LINE),
-	AC97_SWITCH("Line Capture Switch", AC97_LINE, 15, 1),
-	AC97_VOLUME("CD Capture Volume", AC97_CD),
-	AC97_SWITCH("CD Capture Switch", AC97_CD, 15, 1),
-	AC97_VOLUME("Aux Capture Volume", AC97_AUX),
-	AC97_SWITCH("Aux Capture Switch", AC97_AUX, 15, 1),
+	AC97_VOLUME("Mic Capture Volume", 0, AC97_MIC),
+	AC97_SWITCH("Mic Capture Switch", 0, AC97_MIC, 15, 1),
+	AC97_SWITCH("Mic Boost (+20dB)", 0, AC97_MIC, 6, 0),
+	AC97_VOLUME("Line Capture Volume", 0, AC97_LINE),
+	AC97_SWITCH("Line Capture Switch", 0, AC97_LINE, 15, 1),
+	AC97_VOLUME("CD Capture Volume", 0, AC97_CD),
+	AC97_SWITCH("CD Capture Switch", 0, AC97_CD, 15, 1),
+	AC97_VOLUME("Aux Capture Volume", 0, AC97_AUX),
+	AC97_SWITCH("Aux Capture Switch", 0, AC97_AUX, 15, 1),
+};
+
+static const struct snd_kcontrol_new ac97_fp_controls[] = {
+	AC97_VOLUME("Front Panel Playback Volume", 1, AC97_HEADPHONE),
+	AC97_SWITCH("Front Panel Playback Switch", 1, AC97_HEADPHONE, 15, 1),
+	{
+		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+		.name = "Front Panel Capture Volume",
+		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
+			  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
+		.info = ac97_fp_rec_volume_info,
+		.get = ac97_fp_rec_volume_get,
+		.put = ac97_fp_rec_volume_put,
+		.tlv = { .p = ac97_rec_db_scale, },
+	},
+	AC97_SWITCH("Front Panel Capture Switch", 1, AC97_REC_GAIN, 15, 1),
 };
 
 static void oxygen_any_ctl_free(struct snd_kcontrol *ctl)
@@ -717,5 +784,11 @@
 		if (err < 0)
 			return err;
 	}
+	if (chip->has_ac97_1) {
+		err = add_controls(chip, ac97_fp_controls,
+				   ARRAY_SIZE(ac97_fp_controls));
+		if (err < 0)
+			return err;
+	}
 	return chip->model->mixer_init ? chip->model->mixer_init(chip) : 0;
 }
diff --git a/sound/pci/oxygen/virtuoso.c b/sound/pci/oxygen/virtuoso.c
index 23bfab4..2e1a699 100644
--- a/sound/pci/oxygen/virtuoso.c
+++ b/sound/pci/oxygen/virtuoso.c
@@ -247,9 +247,11 @@
 	}
 }
 
-static void xonar_ac97_switch_hook(struct oxygen *chip,
+static void xonar_ac97_switch_hook(struct oxygen *chip, unsigned int codec,
 				   unsigned int reg, int mute)
 {
+	if (codec != 0)
+		return;
 	/* line-in is exclusive */
 	switch (reg) {
 	case AC97_LINE: