12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b5ccc57bSAndres Salomon /*
3b5ccc57bSAndres Salomon * OLPC XO-1 additional sound features
4b5ccc57bSAndres Salomon *
5b5ccc57bSAndres Salomon * Copyright © 2006 Jaya Kumar <jayakumar.lkml@gmail.com>
6b5ccc57bSAndres Salomon * Copyright © 2007-2008 Andres Salomon <dilinger@debian.org>
7b5ccc57bSAndres Salomon */
857d4bf6dSJaya Kumar #include <sound/core.h>
957d4bf6dSJaya Kumar #include <sound/info.h>
1057d4bf6dSJaya Kumar #include <sound/control.h>
1157d4bf6dSJaya Kumar #include <sound/ac97_codec.h>
123c554946SAndres Salomon #include <linux/gpio.h>
13c8974be5SJordan Crouse
14c8974be5SJordan Crouse #include <asm/olpc.h>
1557d4bf6dSJaya Kumar #include "cs5535audio.h"
1657d4bf6dSJaya Kumar
173c554946SAndres Salomon #define DRV_NAME "cs5535audio-olpc"
183c554946SAndres Salomon
19d6276b78SAndres Salomon /*
20d6276b78SAndres Salomon * OLPC has an additional feature on top of the regular AD1888 codec features.
21d6276b78SAndres Salomon * It has an Analog Input mode that is switched into (after disabling the
22d6276b78SAndres Salomon * High Pass Filter) via GPIO. It is supported on B2 and later models.
23d6276b78SAndres Salomon */
olpc_analog_input(struct snd_ac97 * ac97,int on)24d6276b78SAndres Salomon void olpc_analog_input(struct snd_ac97 *ac97, int on)
25d6276b78SAndres Salomon {
26d6276b78SAndres Salomon int err;
27d6276b78SAndres Salomon
280fb497f5SAndres Salomon if (!machine_is_olpc())
290fb497f5SAndres Salomon return;
300fb497f5SAndres Salomon
31d6276b78SAndres Salomon /* update the High Pass Filter (via AC97_AD_TEST2) */
32d6276b78SAndres Salomon err = snd_ac97_update_bits(ac97, AC97_AD_TEST2,
33d6276b78SAndres Salomon 1 << AC97_AD_HPFD_SHIFT, on << AC97_AD_HPFD_SHIFT);
34d6276b78SAndres Salomon if (err < 0) {
3500980aa9STakashi Iwai dev_err(ac97->bus->card->dev,
3600980aa9STakashi Iwai "setting High Pass Filter - %d\n", err);
37d6276b78SAndres Salomon return;
38d6276b78SAndres Salomon }
39d6276b78SAndres Salomon
40d6276b78SAndres Salomon /* set Analog Input through GPIO */
413c554946SAndres Salomon gpio_set_value(OLPC_GPIO_MIC_AC, on);
42d6276b78SAndres Salomon }
4357d4bf6dSJaya Kumar
44bf1e5278SAndres Salomon /*
45bf1e5278SAndres Salomon * OLPC XO-1's V_REFOUT is a mic bias enable.
46bf1e5278SAndres Salomon */
olpc_mic_bias(struct snd_ac97 * ac97,int on)47bf1e5278SAndres Salomon void olpc_mic_bias(struct snd_ac97 *ac97, int on)
48bf1e5278SAndres Salomon {
49bf1e5278SAndres Salomon int err;
50bf1e5278SAndres Salomon
510fb497f5SAndres Salomon if (!machine_is_olpc())
520fb497f5SAndres Salomon return;
530fb497f5SAndres Salomon
54bf1e5278SAndres Salomon on = on ? 0 : 1;
55bf1e5278SAndres Salomon err = snd_ac97_update_bits(ac97, AC97_AD_MISC,
56bf1e5278SAndres Salomon 1 << AC97_AD_VREFD_SHIFT, on << AC97_AD_VREFD_SHIFT);
57bf1e5278SAndres Salomon if (err < 0)
5800980aa9STakashi Iwai dev_err(ac97->bus->card->dev, "setting MIC Bias - %d\n", err);
59bf1e5278SAndres Salomon }
60bf1e5278SAndres Salomon
olpc_dc_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)61466ae305SAndres Salomon static int olpc_dc_info(struct snd_kcontrol *kctl,
6257d4bf6dSJaya Kumar struct snd_ctl_elem_info *uinfo)
6357d4bf6dSJaya Kumar {
6457d4bf6dSJaya Kumar uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
6557d4bf6dSJaya Kumar uinfo->count = 1;
6657d4bf6dSJaya Kumar uinfo->value.integer.min = 0;
6757d4bf6dSJaya Kumar uinfo->value.integer.max = 1;
6857d4bf6dSJaya Kumar return 0;
6957d4bf6dSJaya Kumar }
7057d4bf6dSJaya Kumar
olpc_dc_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * v)71466ae305SAndres Salomon static int olpc_dc_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
7257d4bf6dSJaya Kumar {
733c554946SAndres Salomon v->value.integer.value[0] = gpio_get_value(OLPC_GPIO_MIC_AC);
7457d4bf6dSJaya Kumar return 0;
7557d4bf6dSJaya Kumar }
7657d4bf6dSJaya Kumar
olpc_dc_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * v)77466ae305SAndres Salomon static int olpc_dc_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
7857d4bf6dSJaya Kumar {
79466ae305SAndres Salomon struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
8057d4bf6dSJaya Kumar
81466ae305SAndres Salomon olpc_analog_input(cs5535au->ac97, v->value.integer.value[0]);
8257d4bf6dSJaya Kumar return 1;
8357d4bf6dSJaya Kumar }
8457d4bf6dSJaya Kumar
olpc_mic_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)85bf1e5278SAndres Salomon static int olpc_mic_info(struct snd_kcontrol *kctl,
86bf1e5278SAndres Salomon struct snd_ctl_elem_info *uinfo)
87bf1e5278SAndres Salomon {
88bf1e5278SAndres Salomon uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
89bf1e5278SAndres Salomon uinfo->count = 1;
90bf1e5278SAndres Salomon uinfo->value.integer.min = 0;
91bf1e5278SAndres Salomon uinfo->value.integer.max = 1;
92bf1e5278SAndres Salomon return 0;
93bf1e5278SAndres Salomon }
94bf1e5278SAndres Salomon
olpc_mic_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * v)95bf1e5278SAndres Salomon static int olpc_mic_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
96bf1e5278SAndres Salomon {
97bf1e5278SAndres Salomon struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
98bf1e5278SAndres Salomon struct snd_ac97 *ac97 = cs5535au->ac97;
99bf1e5278SAndres Salomon int i;
100bf1e5278SAndres Salomon
101bf1e5278SAndres Salomon i = (snd_ac97_read(ac97, AC97_AD_MISC) >> AC97_AD_VREFD_SHIFT) & 0x1;
102bf1e5278SAndres Salomon v->value.integer.value[0] = i ? 0 : 1;
103bf1e5278SAndres Salomon return 0;
104bf1e5278SAndres Salomon }
105bf1e5278SAndres Salomon
olpc_mic_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * v)106bf1e5278SAndres Salomon static int olpc_mic_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
107bf1e5278SAndres Salomon {
108bf1e5278SAndres Salomon struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
109bf1e5278SAndres Salomon
110bf1e5278SAndres Salomon olpc_mic_bias(cs5535au->ac97, v->value.integer.value[0]);
111bf1e5278SAndres Salomon return 1;
112bf1e5278SAndres Salomon }
113bf1e5278SAndres Salomon
114b4e5e707STakashi Iwai static const struct snd_kcontrol_new olpc_cs5535audio_ctls[] = {
11557d4bf6dSJaya Kumar {
11657d4bf6dSJaya Kumar .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
117466ae305SAndres Salomon .name = "DC Mode Enable",
118466ae305SAndres Salomon .info = olpc_dc_info,
119466ae305SAndres Salomon .get = olpc_dc_get,
120466ae305SAndres Salomon .put = olpc_dc_put,
121b5ccc57bSAndres Salomon .private_value = 0,
122bf1e5278SAndres Salomon },
123bf1e5278SAndres Salomon {
124bf1e5278SAndres Salomon .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
125bf1e5278SAndres Salomon .name = "MIC Bias Enable",
126bf1e5278SAndres Salomon .info = olpc_mic_info,
127bf1e5278SAndres Salomon .get = olpc_mic_get,
128bf1e5278SAndres Salomon .put = olpc_mic_put,
129bf1e5278SAndres Salomon .private_value = 0,
130bf1e5278SAndres Salomon },
13157d4bf6dSJaya Kumar };
13257d4bf6dSJaya Kumar
olpc_prequirks(struct snd_card * card,struct snd_ac97_template * ac97)133e23e7a14SBill Pemberton void olpc_prequirks(struct snd_card *card,
1343556d184SAndres Salomon struct snd_ac97_template *ac97)
1353556d184SAndres Salomon {
1363556d184SAndres Salomon if (!machine_is_olpc())
1373556d184SAndres Salomon return;
1383556d184SAndres Salomon
1393556d184SAndres Salomon /* invert EAPD if on an OLPC B3 or higher */
1403556d184SAndres Salomon if (olpc_board_at_least(olpc_board_pre(0xb3)))
1413556d184SAndres Salomon ac97->scaps |= AC97_SCAP_INV_EAPD;
1423556d184SAndres Salomon }
1433556d184SAndres Salomon
olpc_quirks(struct snd_card * card,struct snd_ac97 * ac97)144e23e7a14SBill Pemberton int olpc_quirks(struct snd_card *card, struct snd_ac97 *ac97)
14557d4bf6dSJaya Kumar {
146466ae305SAndres Salomon struct snd_ctl_elem_id elem;
147bf1e5278SAndres Salomon int i, err;
148466ae305SAndres Salomon
149c8974be5SJordan Crouse if (!machine_is_olpc())
150c8974be5SJordan Crouse return 0;
151c8974be5SJordan Crouse
1523c554946SAndres Salomon if (gpio_request(OLPC_GPIO_MIC_AC, DRV_NAME)) {
15300980aa9STakashi Iwai dev_err(card->dev, "unable to allocate MIC GPIO\n");
1543c554946SAndres Salomon return -EIO;
1553c554946SAndres Salomon }
1563c554946SAndres Salomon gpio_direction_output(OLPC_GPIO_MIC_AC, 0);
1573c554946SAndres Salomon
158466ae305SAndres Salomon /* drop the original AD1888 HPF control */
159466ae305SAndres Salomon memset(&elem, 0, sizeof(elem));
160466ae305SAndres Salomon elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16175b1a8f9SJoe Perches strscpy(elem.name, "High Pass Filter Enable", sizeof(elem.name));
162466ae305SAndres Salomon snd_ctl_remove_id(card, &elem);
163466ae305SAndres Salomon
164bf1e5278SAndres Salomon /* drop the original V_REFOUT control */
165bf1e5278SAndres Salomon memset(&elem, 0, sizeof(elem));
166bf1e5278SAndres Salomon elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16775b1a8f9SJoe Perches strscpy(elem.name, "V_REFOUT Enable", sizeof(elem.name));
168bf1e5278SAndres Salomon snd_ctl_remove_id(card, &elem);
169bf1e5278SAndres Salomon
170bf1e5278SAndres Salomon /* add the OLPC-specific controls */
171bf1e5278SAndres Salomon for (i = 0; i < ARRAY_SIZE(olpc_cs5535audio_ctls); i++) {
172bf1e5278SAndres Salomon err = snd_ctl_add(card, snd_ctl_new1(&olpc_cs5535audio_ctls[i],
17357d4bf6dSJaya Kumar ac97->private_data));
174*5eba4c64STakashi Iwai if (err < 0)
175bf1e5278SAndres Salomon return err;
176bf1e5278SAndres Salomon }
177bf1e5278SAndres Salomon
178c8f0eeebSAndres Salomon /* turn off the mic by default */
179c8f0eeebSAndres Salomon olpc_mic_bias(ac97, 0);
180bf1e5278SAndres Salomon return 0;
18157d4bf6dSJaya Kumar }
1823c554946SAndres Salomon
olpc_quirks_cleanup(void)183e23e7a14SBill Pemberton void olpc_quirks_cleanup(void)
1843c554946SAndres Salomon {
185*5eba4c64STakashi Iwai if (machine_is_olpc())
1863c554946SAndres Salomon gpio_free(OLPC_GPIO_MIC_AC);
1873c554946SAndres Salomon }
188