ALSA: oxygen: use SPDIF channel status symbols
[safe/jmp/linux-2.6] / sound / pci / oxygen / oxygen_mixer.c
index 21b227a..99c422c 100644 (file)
@@ -32,8 +32,8 @@ static int dac_volume_info(struct snd_kcontrol *ctl,
 
        info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
        info->count = chip->model->dac_channels;
-       info->value.integer.min = 0;
-       info->value.integer.max = 0xff;
+       info->value.integer.min = chip->model->dac_volume_min;
+       info->value.integer.max = chip->model->dac_volume_max;
        return 0;
 }
 
@@ -211,13 +211,13 @@ static unsigned int oxygen_spdif_rate(unsigned int oxygen_rate)
        case OXYGEN_RATE_64000:
                return 0xb << OXYGEN_SPDIF_CS_RATE_SHIFT;
        case OXYGEN_RATE_88200:
-               return 0x8 << OXYGEN_SPDIF_CS_RATE_SHIFT;
+               return IEC958_AES3_CON_FS_88200 << OXYGEN_SPDIF_CS_RATE_SHIFT;
        case OXYGEN_RATE_96000:
-               return 0xa << OXYGEN_SPDIF_CS_RATE_SHIFT;
+               return IEC958_AES3_CON_FS_96000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
        case OXYGEN_RATE_176400:
-               return 0xc << OXYGEN_SPDIF_CS_RATE_SHIFT;
+               return IEC958_AES3_CON_FS_176400 << OXYGEN_SPDIF_CS_RATE_SHIFT;
        case OXYGEN_RATE_192000:
-               return 0xe << OXYGEN_SPDIF_CS_RATE_SHIFT;
+               return IEC958_AES3_CON_FS_192000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
        }
 }
 
@@ -446,17 +446,62 @@ static int spdif_loopback_put(struct snd_kcontrol *ctl,
        return changed;
 }
 
+static int monitor_volume_info(struct snd_kcontrol *ctl,
+                              struct snd_ctl_elem_info *info)
+{
+       info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+       info->count = 1;
+       info->value.integer.min = 0;
+       info->value.integer.max = 1;
+       return 0;
+}
+
+static int monitor_get(struct snd_kcontrol *ctl,
+                      struct snd_ctl_elem_value *value)
+{
+       struct oxygen *chip = ctl->private_data;
+       u8 bit = ctl->private_value;
+       int invert = ctl->private_value & (1 << 8);
+
+       value->value.integer.value[0] =
+               !!invert ^ !!(oxygen_read8(chip, OXYGEN_ADC_MONITOR) & bit);
+       return 0;
+}
+
+static int monitor_put(struct snd_kcontrol *ctl,
+                      struct snd_ctl_elem_value *value)
+{
+       struct oxygen *chip = ctl->private_data;
+       u8 bit = ctl->private_value;
+       int invert = ctl->private_value & (1 << 8);
+       u8 oldreg, newreg;
+       int changed;
+
+       spin_lock_irq(&chip->reg_lock);
+       oldreg = oxygen_read8(chip, OXYGEN_ADC_MONITOR);
+       if ((!!value->value.integer.value[0] ^ !!invert) != 0)
+               newreg = oldreg | bit;
+       else
+               newreg = oldreg & ~bit;
+       changed = newreg != oldreg;
+       if (changed)
+               oxygen_write8(chip, OXYGEN_ADC_MONITOR, newreg);
+       spin_unlock_irq(&chip->reg_lock);
+       return changed;
+}
+
 static int ac97_switch_get(struct snd_kcontrol *ctl,
                           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;
@@ -465,14 +510,19 @@ static int ac97_switch_get(struct snd_kcontrol *ctl,
        return 0;
 }
 
-static void ac97_mute_ctl(struct oxygen *chip, unsigned int control)
+static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
 {
-       unsigned int index = chip->controls[control]->private_value & 0xff;
+       unsigned int priv_idx;
        u16 value;
 
-       value = oxygen_read_ac97(chip, 0, index);
+       if (!chip->controls[control])
+               return;
+       priv_idx = chip->controls[control]->private_value & 0xff;
+       value = oxygen_read_ac97(chip, 0, priv_idx);
        if (!(value & 0x8000)) {
-               oxygen_write_ac97(chip, 0, index, value | 0x8000);
+               oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000);
+               if (chip->model->ac97_switch)
+                       chip->model->ac97_switch(chip, priv_idx, 0x8000);
                snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
                               &chip->controls[control]->id);
        }
@@ -482,6 +532,7 @@ static int ac97_switch_put(struct snd_kcontrol *ctl,
                           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);
@@ -489,7 +540,7 @@ static int ac97_switch_put(struct snd_kcontrol *ctl,
        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;
@@ -497,20 +548,22 @@ static int ac97_switch_put(struct snd_kcontrol *ctl,
                newreg &= ~(1 << bitnr);
        change = newreg != oldreg;
        if (change) {
-               oxygen_write_ac97(chip, 0, index, newreg);
+               oxygen_write_ac97(chip, codec, index, newreg);
+               if (codec == 0 && chip->model->ac97_switch)
+                       chip->model->ac97_switch(chip, index, newreg & 0x8000);
                if (index == AC97_LINE) {
                        oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
                                                 newreg & 0x8000 ?
                                                 CM9780_GPO0 : 0, CM9780_GPO0);
                        if (!(newreg & 0x8000)) {
-                               ac97_mute_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
-                               ac97_mute_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
-                               ac97_mute_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
+                               mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
+                               mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
+                               mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
                        }
                } else if ((index == AC97_MIC || index == AC97_CD ||
                            index == AC97_VIDEO || index == AC97_AUX) &&
                           bitnr == 15 && !(newreg & 0x8000)) {
-                       ac97_mute_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
+                       mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
                        oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
                                                 CM9780_GPO0, CM9780_GPO0);
                }
@@ -533,11 +586,12 @@ static int ac97_volume_get(struct snd_kcontrol *ctl,
                           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);
@@ -548,12 +602,13 @@ static int ac97_volume_put(struct snd_kcontrol *ctl,
                           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));
@@ -561,30 +616,78 @@ static int ac97_volume_put(struct snd_kcontrol *ctl,
                ((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(monitor_db_scale, -1000, 1000, 0);
 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,6 +745,9 @@ static const struct snd_kcontrol_new controls[] = {
                .get = spdif_pcm_get,
                .put = spdif_pcm_put,
        },
+};
+
+static const struct snd_kcontrol_new spdif_input_controls[] = {
        {
                .iface = SNDRV_CTL_ELEM_IFACE_PCM,
                .device = 1,
@@ -667,15 +773,139 @@ static const struct snd_kcontrol_new controls[] = {
        },
 };
 
+static const struct {
+       unsigned int pcm_dev;
+       struct snd_kcontrol_new controls[2];
+} monitor_controls[] = {
+       {
+               .pcm_dev = CAPTURE_0_FROM_I2S_1,
+               .controls = {
+                       {
+                               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+                               .name = "Analog Input Monitor Switch",
+                               .info = snd_ctl_boolean_mono_info,
+                               .get = monitor_get,
+                               .put = monitor_put,
+                               .private_value = OXYGEN_ADC_MONITOR_A,
+                       },
+                       {
+                               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+                               .name = "Analog Input Monitor Volume",
+                               .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
+                                         SNDRV_CTL_ELEM_ACCESS_TLV_READ,
+                               .info = monitor_volume_info,
+                               .get = monitor_get,
+                               .put = monitor_put,
+                               .private_value = OXYGEN_ADC_MONITOR_A_HALF_VOL
+                                               | (1 << 8),
+                               .tlv = { .p = monitor_db_scale, },
+                       },
+               },
+       },
+       {
+               .pcm_dev = CAPTURE_0_FROM_I2S_2,
+               .controls = {
+                       {
+                               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+                               .name = "Analog Input Monitor Switch",
+                               .info = snd_ctl_boolean_mono_info,
+                               .get = monitor_get,
+                               .put = monitor_put,
+                               .private_value = OXYGEN_ADC_MONITOR_B,
+                       },
+                       {
+                               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+                               .name = "Analog Input Monitor Volume",
+                               .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
+                                         SNDRV_CTL_ELEM_ACCESS_TLV_READ,
+                               .info = monitor_volume_info,
+                               .get = monitor_get,
+                               .put = monitor_put,
+                               .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
+                                               | (1 << 8),
+                               .tlv = { .p = monitor_db_scale, },
+                       },
+               },
+       },
+       {
+               .pcm_dev = CAPTURE_2_FROM_I2S_2,
+               .controls = {
+                       {
+                               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+                               .name = "Analog Input Monitor Switch",
+                               .index = 1,
+                               .info = snd_ctl_boolean_mono_info,
+                               .get = monitor_get,
+                               .put = monitor_put,
+                               .private_value = OXYGEN_ADC_MONITOR_B,
+                       },
+                       {
+                               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+                               .name = "Analog Input Monitor Volume",
+                               .index = 1,
+                               .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
+                                         SNDRV_CTL_ELEM_ACCESS_TLV_READ,
+                               .info = monitor_volume_info,
+                               .get = monitor_get,
+                               .put = monitor_put,
+                               .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
+                                               | (1 << 8),
+                               .tlv = { .p = monitor_db_scale, },
+                       },
+               },
+       },
+       {
+               .pcm_dev = CAPTURE_1_FROM_SPDIF,
+               .controls = {
+                       {
+                               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+                               .name = "Digital Input Monitor Switch",
+                               .info = snd_ctl_boolean_mono_info,
+                               .get = monitor_get,
+                               .put = monitor_put,
+                               .private_value = OXYGEN_ADC_MONITOR_C,
+                       },
+                       {
+                               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+                               .name = "Digital Input Monitor Volume",
+                               .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
+                                         SNDRV_CTL_ELEM_ACCESS_TLV_READ,
+                               .info = monitor_volume_info,
+                               .get = monitor_get,
+                               .put = monitor_put,
+                               .private_value = OXYGEN_ADC_MONITOR_C_HALF_VOL
+                                               | (1 << 8),
+                               .tlv = { .p = monitor_db_scale, },
+                       },
+               },
+       },
+};
+
 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_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_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)
@@ -709,9 +939,18 @@ static int add_controls(struct oxygen *chip,
 
        for (i = 0; i < count; ++i) {
                template = controls[i];
-               err = chip->model->control_filter(&template);
-               if (err < 0)
-                       return err;
+               if (chip->model->control_filter) {
+                       err = chip->model->control_filter(&template);
+                       if (err < 0)
+                               return err;
+                       if (err == 1)
+                               continue;
+               }
+               if (!strcmp(template.name, "Master Playback Volume") &&
+                   chip->model->dac_tlv) {
+                       template.tlv.p = chip->model->dac_tlv;
+                       template.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
+               }
                ctl = snd_ctl_new1(&template, chip);
                if (!ctl)
                        return -ENOMEM;
@@ -729,16 +968,37 @@ static int add_controls(struct oxygen *chip,
 
 int oxygen_mixer_init(struct oxygen *chip)
 {
+       unsigned int i;
        int err;
 
        err = add_controls(chip, controls, ARRAY_SIZE(controls));
        if (err < 0)
                return err;
+       if (chip->model->pcm_dev_cfg & CAPTURE_1_FROM_SPDIF) {
+               err = add_controls(chip, spdif_input_controls,
+                                  ARRAY_SIZE(spdif_input_controls));
+               if (err < 0)
+                       return err;
+       }
+       for (i = 0; i < ARRAY_SIZE(monitor_controls); ++i) {
+               if (!(chip->model->pcm_dev_cfg & monitor_controls[i].pcm_dev))
+                       continue;
+               err = add_controls(chip, monitor_controls[i].controls,
+                                  ARRAY_SIZE(monitor_controls[i].controls));
+               if (err < 0)
+                       return err;
+       }
        if (chip->has_ac97_0) {
                err = add_controls(chip, ac97_controls,
                                   ARRAY_SIZE(ac97_controls));
                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;
 }