Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * tosa.c  --  SoC audio for Tosa
0004  *
0005  * Copyright 2005 Wolfson Microelectronics PLC.
0006  * Copyright 2005 Openedhand Ltd.
0007  *
0008  * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
0009  *          Richard Purdie <richard@openedhand.com>
0010  *
0011  * GPIO's
0012  *  1 - Jack Insertion
0013  *  5 - Hookswitch (headset answer/hang up switch)
0014  */
0015 
0016 #include <linux/module.h>
0017 #include <linux/moduleparam.h>
0018 #include <linux/device.h>
0019 #include <linux/gpio/consumer.h>
0020 
0021 #include <sound/core.h>
0022 #include <sound/pcm.h>
0023 #include <sound/soc.h>
0024 
0025 #include <asm/mach-types.h>
0026 #include <linux/platform_data/asoc-pxa.h>
0027 
0028 #define TOSA_HP        0
0029 #define TOSA_MIC_INT   1
0030 #define TOSA_HEADSET   2
0031 #define TOSA_HP_OFF    3
0032 #define TOSA_SPK_ON    0
0033 #define TOSA_SPK_OFF   1
0034 
0035 static struct gpio_desc *tosa_mute;
0036 static int tosa_jack_func;
0037 static int tosa_spk_func;
0038 
0039 static void tosa_ext_control(struct snd_soc_dapm_context *dapm)
0040 {
0041 
0042     snd_soc_dapm_mutex_lock(dapm);
0043 
0044     /* set up jack connection */
0045     switch (tosa_jack_func) {
0046     case TOSA_HP:
0047         snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
0048         snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
0049         snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
0050         break;
0051     case TOSA_MIC_INT:
0052         snd_soc_dapm_enable_pin_unlocked(dapm, "Mic (Internal)");
0053         snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
0054         snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
0055         break;
0056     case TOSA_HEADSET:
0057         snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
0058         snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
0059         snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack");
0060         break;
0061     }
0062 
0063     if (tosa_spk_func == TOSA_SPK_ON)
0064         snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
0065     else
0066         snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
0067 
0068     snd_soc_dapm_sync_unlocked(dapm);
0069 
0070     snd_soc_dapm_mutex_unlock(dapm);
0071 }
0072 
0073 static int tosa_startup(struct snd_pcm_substream *substream)
0074 {
0075     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0076 
0077     /* check the jack status at stream startup */
0078     tosa_ext_control(&rtd->card->dapm);
0079 
0080     return 0;
0081 }
0082 
0083 static const struct snd_soc_ops tosa_ops = {
0084     .startup = tosa_startup,
0085 };
0086 
0087 static int tosa_get_jack(struct snd_kcontrol *kcontrol,
0088     struct snd_ctl_elem_value *ucontrol)
0089 {
0090     ucontrol->value.enumerated.item[0] = tosa_jack_func;
0091     return 0;
0092 }
0093 
0094 static int tosa_set_jack(struct snd_kcontrol *kcontrol,
0095     struct snd_ctl_elem_value *ucontrol)
0096 {
0097     struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
0098 
0099     if (tosa_jack_func == ucontrol->value.enumerated.item[0])
0100         return 0;
0101 
0102     tosa_jack_func = ucontrol->value.enumerated.item[0];
0103     tosa_ext_control(&card->dapm);
0104     return 1;
0105 }
0106 
0107 static int tosa_get_spk(struct snd_kcontrol *kcontrol,
0108     struct snd_ctl_elem_value *ucontrol)
0109 {
0110     ucontrol->value.enumerated.item[0] = tosa_spk_func;
0111     return 0;
0112 }
0113 
0114 static int tosa_set_spk(struct snd_kcontrol *kcontrol,
0115     struct snd_ctl_elem_value *ucontrol)
0116 {
0117     struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
0118 
0119     if (tosa_spk_func == ucontrol->value.enumerated.item[0])
0120         return 0;
0121 
0122     tosa_spk_func = ucontrol->value.enumerated.item[0];
0123     tosa_ext_control(&card->dapm);
0124     return 1;
0125 }
0126 
0127 /* tosa dapm event handlers */
0128 static int tosa_hp_event(struct snd_soc_dapm_widget *w,
0129     struct snd_kcontrol *k, int event)
0130 {
0131     gpiod_set_value(tosa_mute, SND_SOC_DAPM_EVENT_ON(event) ? 1 : 0);
0132     return 0;
0133 }
0134 
0135 /* tosa machine dapm widgets */
0136 static const struct snd_soc_dapm_widget tosa_dapm_widgets[] = {
0137 SND_SOC_DAPM_HP("Headphone Jack", tosa_hp_event),
0138 SND_SOC_DAPM_HP("Headset Jack", NULL),
0139 SND_SOC_DAPM_MIC("Mic (Internal)", NULL),
0140 SND_SOC_DAPM_SPK("Speaker", NULL),
0141 };
0142 
0143 /* tosa audio map */
0144 static const struct snd_soc_dapm_route audio_map[] = {
0145 
0146     /* headphone connected to HPOUTL, HPOUTR */
0147     {"Headphone Jack", NULL, "HPOUTL"},
0148     {"Headphone Jack", NULL, "HPOUTR"},
0149 
0150     /* ext speaker connected to LOUT2, ROUT2 */
0151     {"Speaker", NULL, "LOUT2"},
0152     {"Speaker", NULL, "ROUT2"},
0153 
0154     /* internal mic is connected to mic1, mic2 differential - with bias */
0155     {"MIC1", NULL, "Mic Bias"},
0156     {"MIC2", NULL, "Mic Bias"},
0157     {"Mic Bias", NULL, "Mic (Internal)"},
0158 
0159     /* headset is connected to HPOUTR, and LINEINR with bias */
0160     {"Headset Jack", NULL, "HPOUTR"},
0161     {"LINEINR", NULL, "Mic Bias"},
0162     {"Mic Bias", NULL, "Headset Jack"},
0163 };
0164 
0165 static const char * const jack_function[] = {"Headphone", "Mic", "Line",
0166     "Headset", "Off"};
0167 static const char * const spk_function[] = {"On", "Off"};
0168 static const struct soc_enum tosa_enum[] = {
0169     SOC_ENUM_SINGLE_EXT(5, jack_function),
0170     SOC_ENUM_SINGLE_EXT(2, spk_function),
0171 };
0172 
0173 static const struct snd_kcontrol_new tosa_controls[] = {
0174     SOC_ENUM_EXT("Jack Function", tosa_enum[0], tosa_get_jack,
0175         tosa_set_jack),
0176     SOC_ENUM_EXT("Speaker Function", tosa_enum[1], tosa_get_spk,
0177         tosa_set_spk),
0178 };
0179 
0180 SND_SOC_DAILINK_DEFS(ac97,
0181     DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-ac97")),
0182     DAILINK_COMP_ARRAY(COMP_CODEC("wm9712-codec", "wm9712-hifi")),
0183     DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
0184 
0185 SND_SOC_DAILINK_DEFS(ac97_aux,
0186     DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-ac97-aux")),
0187     DAILINK_COMP_ARRAY(COMP_CODEC("wm9712-codec", "wm9712-aux")),
0188     DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
0189 
0190 static struct snd_soc_dai_link tosa_dai[] = {
0191 {
0192     .name = "AC97",
0193     .stream_name = "AC97 HiFi",
0194     .ops = &tosa_ops,
0195     SND_SOC_DAILINK_REG(ac97),
0196 },
0197 {
0198     .name = "AC97 Aux",
0199     .stream_name = "AC97 Aux",
0200     .ops = &tosa_ops,
0201     SND_SOC_DAILINK_REG(ac97_aux),
0202 },
0203 };
0204 
0205 static struct snd_soc_card tosa = {
0206     .name = "Tosa",
0207     .owner = THIS_MODULE,
0208     .dai_link = tosa_dai,
0209     .num_links = ARRAY_SIZE(tosa_dai),
0210 
0211     .controls = tosa_controls,
0212     .num_controls = ARRAY_SIZE(tosa_controls),
0213     .dapm_widgets = tosa_dapm_widgets,
0214     .num_dapm_widgets = ARRAY_SIZE(tosa_dapm_widgets),
0215     .dapm_routes = audio_map,
0216     .num_dapm_routes = ARRAY_SIZE(audio_map),
0217     .fully_routed = true,
0218 };
0219 
0220 static int tosa_probe(struct platform_device *pdev)
0221 {
0222     struct snd_soc_card *card = &tosa;
0223     int ret;
0224 
0225     tosa_mute = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
0226     if (IS_ERR(tosa_mute))
0227         return dev_err_probe(&pdev->dev, PTR_ERR(tosa_mute),
0228                      "failed to get L_MUTE GPIO\n");
0229     gpiod_set_consumer_name(tosa_mute, "Headphone Jack");
0230 
0231     card->dev = &pdev->dev;
0232 
0233     ret = devm_snd_soc_register_card(&pdev->dev, card);
0234     if (ret) {
0235         dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
0236             ret);
0237     }
0238     return ret;
0239 }
0240 
0241 static struct platform_driver tosa_driver = {
0242     .driver     = {
0243         .name   = "tosa-audio",
0244         .pm     = &snd_soc_pm_ops,
0245     },
0246     .probe      = tosa_probe,
0247 };
0248 
0249 module_platform_driver(tosa_driver);
0250 
0251 /* Module information */
0252 MODULE_AUTHOR("Richard Purdie");
0253 MODULE_DESCRIPTION("ALSA SoC Tosa");
0254 MODULE_LICENSE("GPL");
0255 MODULE_ALIAS("platform:tosa-audio");