Path: blob/master/sound/pci/cs5535audio/cs5535audio_olpc.c
26451 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* OLPC XO-1 additional sound features3*4* Copyright © 2006 Jaya Kumar <[email protected]>5* Copyright © 2007-2008 Andres Salomon <[email protected]>6*/7#include <sound/core.h>8#include <sound/info.h>9#include <sound/control.h>10#include <sound/ac97_codec.h>11#include <linux/gpio.h>1213#include <asm/olpc.h>14#include "cs5535audio.h"1516#define DRV_NAME "cs5535audio-olpc"1718/*19* OLPC has an additional feature on top of the regular AD1888 codec features.20* It has an Analog Input mode that is switched into (after disabling the21* High Pass Filter) via GPIO. It is supported on B2 and later models.22*/23void olpc_analog_input(struct snd_ac97 *ac97, int on)24{25int err;2627if (!machine_is_olpc())28return;2930/* update the High Pass Filter (via AC97_AD_TEST2) */31err = snd_ac97_update_bits(ac97, AC97_AD_TEST2,321 << AC97_AD_HPFD_SHIFT, on << AC97_AD_HPFD_SHIFT);33if (err < 0) {34dev_err(ac97->bus->card->dev,35"setting High Pass Filter - %d\n", err);36return;37}3839/* set Analog Input through GPIO */40gpio_set_value(OLPC_GPIO_MIC_AC, on);41}4243/*44* OLPC XO-1's V_REFOUT is a mic bias enable.45*/46void olpc_mic_bias(struct snd_ac97 *ac97, int on)47{48int err;4950if (!machine_is_olpc())51return;5253on = on ? 0 : 1;54err = snd_ac97_update_bits(ac97, AC97_AD_MISC,551 << AC97_AD_VREFD_SHIFT, on << AC97_AD_VREFD_SHIFT);56if (err < 0)57dev_err(ac97->bus->card->dev, "setting MIC Bias - %d\n", err);58}5960static int olpc_dc_info(struct snd_kcontrol *kctl,61struct snd_ctl_elem_info *uinfo)62{63uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;64uinfo->count = 1;65uinfo->value.integer.min = 0;66uinfo->value.integer.max = 1;67return 0;68}6970static int olpc_dc_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)71{72v->value.integer.value[0] = gpio_get_value(OLPC_GPIO_MIC_AC);73return 0;74}7576static int olpc_dc_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)77{78struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);7980olpc_analog_input(cs5535au->ac97, v->value.integer.value[0]);81return 1;82}8384static int olpc_mic_info(struct snd_kcontrol *kctl,85struct snd_ctl_elem_info *uinfo)86{87uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;88uinfo->count = 1;89uinfo->value.integer.min = 0;90uinfo->value.integer.max = 1;91return 0;92}9394static int olpc_mic_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)95{96struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);97struct snd_ac97 *ac97 = cs5535au->ac97;98int i;99100i = (snd_ac97_read(ac97, AC97_AD_MISC) >> AC97_AD_VREFD_SHIFT) & 0x1;101v->value.integer.value[0] = i ? 0 : 1;102return 0;103}104105static int olpc_mic_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)106{107struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);108109olpc_mic_bias(cs5535au->ac97, v->value.integer.value[0]);110return 1;111}112113static const struct snd_kcontrol_new olpc_cs5535audio_ctls[] = {114{115.iface = SNDRV_CTL_ELEM_IFACE_MIXER,116.name = "DC Mode Enable",117.info = olpc_dc_info,118.get = olpc_dc_get,119.put = olpc_dc_put,120.private_value = 0,121},122{123.iface = SNDRV_CTL_ELEM_IFACE_MIXER,124.name = "MIC Bias Enable",125.info = olpc_mic_info,126.get = olpc_mic_get,127.put = olpc_mic_put,128.private_value = 0,129},130};131132void olpc_prequirks(struct snd_card *card,133struct snd_ac97_template *ac97)134{135if (!machine_is_olpc())136return;137138/* invert EAPD if on an OLPC B3 or higher */139if (olpc_board_at_least(olpc_board_pre(0xb3)))140ac97->scaps |= AC97_SCAP_INV_EAPD;141}142143int olpc_quirks(struct snd_card *card, struct snd_ac97 *ac97)144{145struct snd_ctl_elem_id elem;146int i, err;147148if (!machine_is_olpc())149return 0;150151if (gpio_request(OLPC_GPIO_MIC_AC, DRV_NAME)) {152dev_err(card->dev, "unable to allocate MIC GPIO\n");153return -EIO;154}155gpio_direction_output(OLPC_GPIO_MIC_AC, 0);156157/* drop the original AD1888 HPF control */158memset(&elem, 0, sizeof(elem));159elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;160strscpy(elem.name, "High Pass Filter Enable", sizeof(elem.name));161snd_ctl_remove_id(card, &elem);162163/* drop the original V_REFOUT control */164memset(&elem, 0, sizeof(elem));165elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;166strscpy(elem.name, "V_REFOUT Enable", sizeof(elem.name));167snd_ctl_remove_id(card, &elem);168169/* add the OLPC-specific controls */170for (i = 0; i < ARRAY_SIZE(olpc_cs5535audio_ctls); i++) {171err = snd_ctl_add(card, snd_ctl_new1(&olpc_cs5535audio_ctls[i],172ac97->private_data));173if (err < 0)174return err;175}176177/* turn off the mic by default */178olpc_mic_bias(ac97, 0);179return 0;180}181182void olpc_quirks_cleanup(void)183{184if (machine_is_olpc())185gpio_free(OLPC_GPIO_MIC_AC);186}187188189