treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[linux-block.git] / sound / pci / cs5535audio / cs5535audio_olpc.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
b5ccc57b
AS
2/*
3 * OLPC XO-1 additional sound features
4 *
5 * Copyright © 2006 Jaya Kumar <jayakumar.lkml@gmail.com>
6 * Copyright © 2007-2008 Andres Salomon <dilinger@debian.org>
b5ccc57b 7 */
57d4bf6d
JK
8#include <sound/core.h>
9#include <sound/info.h>
10#include <sound/control.h>
11#include <sound/ac97_codec.h>
3c554946 12#include <linux/gpio.h>
c8974be5
JC
13
14#include <asm/olpc.h>
57d4bf6d
JK
15#include "cs5535audio.h"
16
3c554946
AS
17#define DRV_NAME "cs5535audio-olpc"
18
d6276b78
AS
19/*
20 * OLPC has an additional feature on top of the regular AD1888 codec features.
21 * It has an Analog Input mode that is switched into (after disabling the
22 * High Pass Filter) via GPIO. It is supported on B2 and later models.
23 */
24void olpc_analog_input(struct snd_ac97 *ac97, int on)
25{
26 int err;
27
0fb497f5
AS
28 if (!machine_is_olpc())
29 return;
30
d6276b78
AS
31 /* update the High Pass Filter (via AC97_AD_TEST2) */
32 err = snd_ac97_update_bits(ac97, AC97_AD_TEST2,
33 1 << AC97_AD_HPFD_SHIFT, on << AC97_AD_HPFD_SHIFT);
34 if (err < 0) {
00980aa9
TI
35 dev_err(ac97->bus->card->dev,
36 "setting High Pass Filter - %d\n", err);
d6276b78
AS
37 return;
38 }
39
40 /* set Analog Input through GPIO */
3c554946 41 gpio_set_value(OLPC_GPIO_MIC_AC, on);
d6276b78 42}
57d4bf6d 43
bf1e5278
AS
44/*
45 * OLPC XO-1's V_REFOUT is a mic bias enable.
46 */
47void olpc_mic_bias(struct snd_ac97 *ac97, int on)
48{
49 int err;
50
0fb497f5
AS
51 if (!machine_is_olpc())
52 return;
53
bf1e5278
AS
54 on = on ? 0 : 1;
55 err = snd_ac97_update_bits(ac97, AC97_AD_MISC,
56 1 << AC97_AD_VREFD_SHIFT, on << AC97_AD_VREFD_SHIFT);
57 if (err < 0)
00980aa9 58 dev_err(ac97->bus->card->dev, "setting MIC Bias - %d\n", err);
bf1e5278
AS
59}
60
466ae305
AS
61static int olpc_dc_info(struct snd_kcontrol *kctl,
62 struct snd_ctl_elem_info *uinfo)
57d4bf6d
JK
63{
64 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
65 uinfo->count = 1;
66 uinfo->value.integer.min = 0;
67 uinfo->value.integer.max = 1;
68 return 0;
69}
70
466ae305 71static int olpc_dc_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
57d4bf6d 72{
3c554946 73 v->value.integer.value[0] = gpio_get_value(OLPC_GPIO_MIC_AC);
57d4bf6d
JK
74 return 0;
75}
76
466ae305 77static int olpc_dc_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
57d4bf6d 78{
466ae305 79 struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
57d4bf6d 80
466ae305 81 olpc_analog_input(cs5535au->ac97, v->value.integer.value[0]);
57d4bf6d
JK
82 return 1;
83}
84
bf1e5278
AS
85static int olpc_mic_info(struct snd_kcontrol *kctl,
86 struct snd_ctl_elem_info *uinfo)
87{
88 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
89 uinfo->count = 1;
90 uinfo->value.integer.min = 0;
91 uinfo->value.integer.max = 1;
92 return 0;
93}
94
95static int olpc_mic_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
96{
97 struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
98 struct snd_ac97 *ac97 = cs5535au->ac97;
99 int i;
100
101 i = (snd_ac97_read(ac97, AC97_AD_MISC) >> AC97_AD_VREFD_SHIFT) & 0x1;
102 v->value.integer.value[0] = i ? 0 : 1;
103 return 0;
104}
105
106static int olpc_mic_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
107{
108 struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
109
110 olpc_mic_bias(cs5535au->ac97, v->value.integer.value[0]);
111 return 1;
112}
113
e23e7a14 114static struct snd_kcontrol_new olpc_cs5535audio_ctls[] = {
57d4bf6d
JK
115{
116 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
466ae305
AS
117 .name = "DC Mode Enable",
118 .info = olpc_dc_info,
119 .get = olpc_dc_get,
120 .put = olpc_dc_put,
b5ccc57b 121 .private_value = 0,
bf1e5278
AS
122},
123{
124 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
125 .name = "MIC Bias Enable",
126 .info = olpc_mic_info,
127 .get = olpc_mic_get,
128 .put = olpc_mic_put,
129 .private_value = 0,
130},
57d4bf6d
JK
131};
132
e23e7a14
BP
133void olpc_prequirks(struct snd_card *card,
134 struct snd_ac97_template *ac97)
3556d184
AS
135{
136 if (!machine_is_olpc())
137 return;
138
139 /* invert EAPD if on an OLPC B3 or higher */
140 if (olpc_board_at_least(olpc_board_pre(0xb3)))
141 ac97->scaps |= AC97_SCAP_INV_EAPD;
142}
143
e23e7a14 144int olpc_quirks(struct snd_card *card, struct snd_ac97 *ac97)
57d4bf6d 145{
466ae305 146 struct snd_ctl_elem_id elem;
bf1e5278 147 int i, err;
466ae305 148
c8974be5
JC
149 if (!machine_is_olpc())
150 return 0;
151
3c554946 152 if (gpio_request(OLPC_GPIO_MIC_AC, DRV_NAME)) {
00980aa9 153 dev_err(card->dev, "unable to allocate MIC GPIO\n");
3c554946
AS
154 return -EIO;
155 }
156 gpio_direction_output(OLPC_GPIO_MIC_AC, 0);
157
466ae305
AS
158 /* drop the original AD1888 HPF control */
159 memset(&elem, 0, sizeof(elem));
160 elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
57a4451d 161 strlcpy(elem.name, "High Pass Filter Enable", sizeof(elem.name));
466ae305
AS
162 snd_ctl_remove_id(card, &elem);
163
bf1e5278
AS
164 /* drop the original V_REFOUT control */
165 memset(&elem, 0, sizeof(elem));
166 elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
57a4451d 167 strlcpy(elem.name, "V_REFOUT Enable", sizeof(elem.name));
bf1e5278
AS
168 snd_ctl_remove_id(card, &elem);
169
170 /* add the OLPC-specific controls */
171 for (i = 0; i < ARRAY_SIZE(olpc_cs5535audio_ctls); i++) {
172 err = snd_ctl_add(card, snd_ctl_new1(&olpc_cs5535audio_ctls[i],
173 ac97->private_data));
3c554946
AS
174 if (err < 0) {
175 gpio_free(OLPC_GPIO_MIC_AC);
bf1e5278 176 return err;
3c554946 177 }
bf1e5278
AS
178 }
179
c8f0eeeb
AS
180 /* turn off the mic by default */
181 olpc_mic_bias(ac97, 0);
bf1e5278 182 return 0;
57d4bf6d 183}
3c554946 184
e23e7a14 185void olpc_quirks_cleanup(void)
3c554946
AS
186{
187 gpio_free(OLPC_GPIO_MIC_AC);
188}