Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6-block.git] / sound / pci / cs5535audio / cs5535audio_olpc.c
index f20e741827253cf366c9581e5dbe4ec0127e4bb9..5c6814335cd727a83e55c58a73ef85965f633841 100644 (file)
@@ -1,4 +1,14 @@
-#include <sound/driver.h>
+/*
+ * OLPC XO-1 additional sound features
+ *
+ * Copyright © 2006  Jaya Kumar <jayakumar.lkml@gmail.com>
+ * Copyright © 2007-2008  Andres Salomon <dilinger@debian.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
 #include <sound/core.h>
 #include <sound/info.h>
 #include <sound/control.h>
 #include <asm/olpc.h>
 #include "cs5535audio.h"
 
-/* OLPC has an additional feature on top of regular AD1888 codec
-features. This is support for an analog input mode. This is a
-2 step process. First, to turn off the AD1888 codec bias voltage
-and high pass filter. Second, to tell the embedded controller to
-reroute from a capacitive trace to a direct trace using an analog
-switch. The *_ec()s are what talk to that controller */
+/*
+ * OLPC has an additional feature on top of the regular AD1888 codec features.
+ * It has an Analog Input mode that is switched into (after disabling the
+ * High Pass Filter) via GPIO.  It is supported on B2 and later models.
+ */
+void olpc_analog_input(struct snd_ac97 *ac97, int on)
+{
+       int err;
+
+       if (!machine_is_olpc())
+               return;
+
+       /* update the High Pass Filter (via AC97_AD_TEST2) */
+       err = snd_ac97_update_bits(ac97, AC97_AD_TEST2,
+                       1 << AC97_AD_HPFD_SHIFT, on << AC97_AD_HPFD_SHIFT);
+       if (err < 0) {
+               snd_printk(KERN_ERR "setting High Pass Filter - %d\n", err);
+               return;
+       }
+
+       /* set Analog Input through GPIO */
+       if (on)
+               geode_gpio_set(OLPC_GPIO_MIC_AC, GPIO_OUTPUT_VAL);
+       else
+               geode_gpio_clear(OLPC_GPIO_MIC_AC, GPIO_OUTPUT_VAL);
+}
 
-static int snd_cs5535audio_ctl_info(struct snd_kcontrol *kcontrol,
-                                       struct snd_ctl_elem_info *uinfo)
+/*
+ * OLPC XO-1's V_REFOUT is a mic bias enable.
+ */
+void olpc_mic_bias(struct snd_ac97 *ac97, int on)
+{
+       int err;
+
+       if (!machine_is_olpc())
+               return;
+
+       on = on ? 0 : 1;
+       err = snd_ac97_update_bits(ac97, AC97_AD_MISC,
+                       1 << AC97_AD_VREFD_SHIFT, on << AC97_AD_VREFD_SHIFT);
+       if (err < 0)
+               snd_printk(KERN_ERR "setting MIC Bias - %d\n", err);
+}
+
+static int olpc_dc_info(struct snd_kcontrol *kctl,
+               struct snd_ctl_elem_info *uinfo)
 {
        uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
        uinfo->count = 1;
@@ -24,59 +71,67 @@ static int snd_cs5535audio_ctl_info(struct snd_kcontrol *kcontrol,
        return 0;
 }
 
-#define AD1888_VREFOUT_EN_BIT (1 << 2)
-#define AD1888_HPF_EN_BIT (1 << 12)
-static int snd_cs5535audio_ctl_get(struct snd_kcontrol *kcontrol,
-                                       struct snd_ctl_elem_value *ucontrol)
+static int olpc_dc_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
 {
-       struct cs5535audio *cs5535au = snd_kcontrol_chip(kcontrol);
-       u8 val;
+       v->value.integer.value[0] = geode_gpio_isset(OLPC_GPIO_MIC_AC,
+                       GPIO_OUTPUT_VAL);
+       return 0;
+}
 
-       val = snd_ac97_read(cs5535au->ac97, AC97_AD_TEST2);
-       val >>= AC97_AD_HPFD_SHIFT;
-       ucontrol->value.integer.value[0] = val & 0x1;
+static int olpc_dc_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
+{
+       struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
 
+       olpc_analog_input(cs5535au->ac97, v->value.integer.value[0]);
+       return 1;
+}
+
+static int olpc_mic_info(struct snd_kcontrol *kctl,
+               struct snd_ctl_elem_info *uinfo)
+{
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
+       uinfo->count = 1;
+       uinfo->value.integer.min = 0;
+       uinfo->value.integer.max = 1;
        return 0;
 }
 
-static int snd_cs5535audio_ctl_put(struct snd_kcontrol *kcontrol,
-                                       struct snd_ctl_elem_value *ucontrol)
+static int olpc_mic_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
 {
-       int err;
-       struct cs5535audio *cs5535au = snd_kcontrol_chip(kcontrol);
-       u8 value;
+       struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
        struct snd_ac97 *ac97 = cs5535au->ac97;
+       int i;
 
-       /* value is 1 if analog input is desired */
-       value = ucontrol->value.integer.value[0];
-
-       /* turns off High Pass Filter if 1 */
-       if (value)
-               err = snd_ac97_update_bits(ac97, AC97_AD_TEST2,
-                               AD1888_HPF_EN_BIT, AD1888_HPF_EN_BIT);
-       else
-               err = snd_ac97_update_bits(ac97, AC97_AD_TEST2,
-                               AD1888_HPF_EN_BIT, 0);
-       if (err < 0)
-               snd_printk(KERN_ERR "Error updating AD_TEST2 %d\n", err);
+       i = (snd_ac97_read(ac97, AC97_AD_MISC) >> AC97_AD_VREFD_SHIFT) & 0x1;
+       v->value.integer.value[0] = i ? 0 : 1;
+       return 0;
+}
 
-       /* B2 and newer writes directly to a GPIO pin */
-       if (value)
-               geode_gpio_set(OLPC_GPIO_MIC_AC, GPIO_OUTPUT_VAL);
-       else
-               geode_gpio_clear(OLPC_GPIO_MIC_AC, GPIO_OUTPUT_VAL);
+static int olpc_mic_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
+{
+       struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
 
+       olpc_mic_bias(cs5535au->ac97, v->value.integer.value[0]);
        return 1;
 }
 
-static struct snd_kcontrol_new snd_cs5535audio_controls __devinitdata =
+static struct snd_kcontrol_new olpc_cs5535audio_ctls[] __devinitdata = {
 {
        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
-       .name = "Analog Input Switch",
-       .info = snd_cs5535audio_ctl_info,
-       .get = snd_cs5535audio_ctl_get,
-       .put = snd_cs5535audio_ctl_put,
-       .private_value = 0
+       .name = "DC Mode Enable",
+       .info = olpc_dc_info,
+       .get = olpc_dc_get,
+       .put = olpc_dc_put,
+       .private_value = 0,
+},
+{
+       .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+       .name = "MIC Bias Enable",
+       .info = olpc_mic_info,
+       .get = olpc_mic_get,
+       .put = olpc_mic_put,
+       .private_value = 0,
+},
 };
 
 void __devinit olpc_prequirks(struct snd_card *card,
@@ -92,11 +147,33 @@ void __devinit olpc_prequirks(struct snd_card *card,
 
 int __devinit olpc_quirks(struct snd_card *card, struct snd_ac97 *ac97)
 {
+       struct snd_ctl_elem_id elem;
+       int i, err;
+
        if (!machine_is_olpc())
                return 0;
 
-       /* setup callback for mixer control that does analog input mode */
-       return snd_ctl_add(card, snd_ctl_new1(&snd_cs5535audio_controls,
-                                               ac97->private_data));
+       /* drop the original AD1888 HPF control */
+       memset(&elem, 0, sizeof(elem));
+       elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
+       strncpy(elem.name, "High Pass Filter Enable", sizeof(elem.name));
+       snd_ctl_remove_id(card, &elem);
+
+       /* drop the original V_REFOUT control */
+       memset(&elem, 0, sizeof(elem));
+       elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
+       strncpy(elem.name, "V_REFOUT Enable", sizeof(elem.name));
+       snd_ctl_remove_id(card, &elem);
+
+       /* add the OLPC-specific controls */
+       for (i = 0; i < ARRAY_SIZE(olpc_cs5535audio_ctls); i++) {
+               err = snd_ctl_add(card, snd_ctl_new1(&olpc_cs5535audio_ctls[i],
+                               ac97->private_data));
+               if (err < 0)
+                       return err;
+       }
+
+       /* turn off the mic by default */
+       olpc_mic_bias(ac97, 0);
+       return 0;
 }
-