ALSA: cs5535audio: OLPC analog input support
[linux-2.6] / sound / pci / cs5535audio / cs5535audio_olpc.c
1 #include <linux/olpc.h>
2 #include <sound/driver.h>
3 #include <sound/core.h>
4 #include <sound/info.h>
5 #include <sound/control.h>
6 #include <sound/ac97_codec.h>
7 #include "cs5535audio.h"
8
9 /* OLPC has an additional feature on top of regular AD1888 codec
10 features. This is support for an analog input mode. This is a
11 2 step process. First, to turn off the AD1888 codec bias voltage
12 and high pass filter. Second, to tell the embedded controller to
13 reroute from a capacitive trace to a direct trace using an analog
14 switch. The *_ec()s are what talk to that controller */
15
16 static int snd_cs5535audio_ctl_info(struct snd_kcontrol *kcontrol,
17                                         struct snd_ctl_elem_info *uinfo)
18 {
19         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
20         uinfo->count = 1;
21         uinfo->value.integer.min = 0;
22         uinfo->value.integer.max = 1;
23         return 0;
24 }
25
26 #define AD1888_VREFOUT_EN_BIT (1 << 2)
27 #define AD1888_HPF_EN_BIT (1 << 12)
28 static int snd_cs5535audio_ctl_get(struct snd_kcontrol *kcontrol,
29                                         struct snd_ctl_elem_value *ucontrol)
30 {
31         struct cs5535audio *cs5535au = snd_kcontrol_chip(kcontrol);
32         u16 reg1, reg2;
33
34         /* if either AD1888 VRef Bias and High Pass Filter are enabled
35         or the EC is not in analog mode then flag as not in analog mode.
36         No EC command to read current analog state so we cache that. */
37         reg1 = snd_ac97_read(cs5535au->ac97, AC97_AD_MISC);
38         reg2 = snd_ac97_read(cs5535au->ac97, AC97_AD_TEST2);
39
40         if ((reg1 & AD1888_VREFOUT_EN_BIT) && (reg2 & AD1888_HPF_EN_BIT) &&
41                 cs5535au->ec_analog_input_mode)
42                 ucontrol->value.integer.value[0] = 1;
43         else
44                 ucontrol->value.integer.value[0] = 0;
45
46         return 0;
47 }
48
49 static int snd_cs5535audio_ctl_put(struct snd_kcontrol *kcontrol,
50                                         struct snd_ctl_elem_value *ucontrol)
51 {
52         int err;
53         struct cs5535audio *cs5535au = snd_kcontrol_chip(kcontrol);
54         u8 value;
55         struct snd_ac97 *ac97 = cs5535au->ac97;
56
57         /* value is 1 if analog input is desired */
58         value = ucontrol->value.integer.value[0];
59
60         /* use ec mode as flag to determine if any change needed */
61         if (cs5535au->ec_analog_input_mode == value)
62                 return 0;
63
64         /* sets High Z on VREF Bias if 1 */
65         if (value)
66                 err = snd_ac97_update_bits(ac97, AC97_AD_MISC,
67                                 AD1888_VREFOUT_EN_BIT, AD1888_VREFOUT_EN_BIT);
68         else
69                 err = snd_ac97_update_bits(ac97, AC97_AD_MISC,
70                                 AD1888_VREFOUT_EN_BIT, 0);
71         if (err < 0)
72                 snd_printk(KERN_ERR "Error updating AD_MISC %d\n", err);
73
74         /* turns off High Pass Filter if 1 */
75         if (value)
76                 err = snd_ac97_update_bits(ac97, AC97_AD_TEST2,
77                                 AD1888_HPF_EN_BIT, AD1888_HPF_EN_BIT);
78         else
79                 err = snd_ac97_update_bits(ac97, AC97_AD_TEST2,
80                                 AD1888_HPF_EN_BIT, 0);
81         if (err < 0)
82                 snd_printk(KERN_ERR "Error updating AD_TEST2 %d\n", err);
83
84         if (value)
85                 err = write_ec_command(0x01); /* activate MIC_AC_OFF */
86         else
87                 err = write_ec_command(0x02); /* deactivates MIC_AC_OFF */
88
89         if (err < 0)
90                 snd_printk(KERN_ERR "Error talking to EC %d\n", err);
91
92         cs5535au->ec_analog_input_mode = value;
93
94         return 1;
95 }
96
97 static struct snd_kcontrol_new snd_cs5535audio_controls __devinitdata =
98 {
99         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
100         .name = "Analog Input Switch",
101         .info = snd_cs5535audio_ctl_info,
102         .get = snd_cs5535audio_ctl_get,
103         .put = snd_cs5535audio_ctl_put,
104         .private_value = 0
105 };
106
107 int __devinit olpc_quirks(struct snd_card *card, struct snd_ac97 *ac97)
108 {
109         /* setup callback for mixer control that does analog input mode */
110         return snd_ctl_add(card, snd_ctl_new1(&snd_cs5535audio_controls,
111                                                 ac97->private_data));
112 }
113