1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * tosa.c -- SoC audio for Tosa
5 * Copyright 2005 Wolfson Microelectronics PLC.
6 * Copyright 2005 Openedhand Ltd.
13 * 5 - Hookswitch (headset answer/hang up switch)
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/device.h>
19 #include <linux/gpio.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/soc.h>
25 #include <asm/mach-types.h>
26 #include <mach/tosa.h>
27 #include <mach/audio.h>
30 #define TOSA_MIC_INT 1
31 #define TOSA_HEADSET 2
34 #define TOSA_SPK_OFF 1
36 static int tosa_jack_func;
37 static int tosa_spk_func;
39 static void tosa_ext_control(struct snd_soc_dapm_context *dapm)
42 snd_soc_dapm_mutex_lock(dapm);
44 /* set up jack connection */
45 switch (tosa_jack_func) {
47 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
48 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
49 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
52 snd_soc_dapm_enable_pin_unlocked(dapm, "Mic (Internal)");
53 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
54 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
57 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
58 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
59 snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack");
63 if (tosa_spk_func == TOSA_SPK_ON)
64 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
66 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
68 snd_soc_dapm_sync_unlocked(dapm);
70 snd_soc_dapm_mutex_unlock(dapm);
73 static int tosa_startup(struct snd_pcm_substream *substream)
75 struct snd_soc_pcm_runtime *rtd = substream->private_data;
77 /* check the jack status at stream startup */
78 tosa_ext_control(&rtd->card->dapm);
83 static const struct snd_soc_ops tosa_ops = {
84 .startup = tosa_startup,
87 static int tosa_get_jack(struct snd_kcontrol *kcontrol,
88 struct snd_ctl_elem_value *ucontrol)
90 ucontrol->value.enumerated.item[0] = tosa_jack_func;
94 static int tosa_set_jack(struct snd_kcontrol *kcontrol,
95 struct snd_ctl_elem_value *ucontrol)
97 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
99 if (tosa_jack_func == ucontrol->value.enumerated.item[0])
102 tosa_jack_func = ucontrol->value.enumerated.item[0];
103 tosa_ext_control(&card->dapm);
107 static int tosa_get_spk(struct snd_kcontrol *kcontrol,
108 struct snd_ctl_elem_value *ucontrol)
110 ucontrol->value.enumerated.item[0] = tosa_spk_func;
114 static int tosa_set_spk(struct snd_kcontrol *kcontrol,
115 struct snd_ctl_elem_value *ucontrol)
117 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
119 if (tosa_spk_func == ucontrol->value.enumerated.item[0])
122 tosa_spk_func = ucontrol->value.enumerated.item[0];
123 tosa_ext_control(&card->dapm);
127 /* tosa dapm event handlers */
128 static int tosa_hp_event(struct snd_soc_dapm_widget *w,
129 struct snd_kcontrol *k, int event)
131 gpio_set_value(TOSA_GPIO_L_MUTE, SND_SOC_DAPM_EVENT_ON(event) ? 1 : 0);
135 /* tosa machine dapm widgets */
136 static const struct snd_soc_dapm_widget tosa_dapm_widgets[] = {
137 SND_SOC_DAPM_HP("Headphone Jack", tosa_hp_event),
138 SND_SOC_DAPM_HP("Headset Jack", NULL),
139 SND_SOC_DAPM_MIC("Mic (Internal)", NULL),
140 SND_SOC_DAPM_SPK("Speaker", NULL),
144 static const struct snd_soc_dapm_route audio_map[] = {
146 /* headphone connected to HPOUTL, HPOUTR */
147 {"Headphone Jack", NULL, "HPOUTL"},
148 {"Headphone Jack", NULL, "HPOUTR"},
150 /* ext speaker connected to LOUT2, ROUT2 */
151 {"Speaker", NULL, "LOUT2"},
152 {"Speaker", NULL, "ROUT2"},
154 /* internal mic is connected to mic1, mic2 differential - with bias */
155 {"MIC1", NULL, "Mic Bias"},
156 {"MIC2", NULL, "Mic Bias"},
157 {"Mic Bias", NULL, "Mic (Internal)"},
159 /* headset is connected to HPOUTR, and LINEINR with bias */
160 {"Headset Jack", NULL, "HPOUTR"},
161 {"LINEINR", NULL, "Mic Bias"},
162 {"Mic Bias", NULL, "Headset Jack"},
165 static const char * const jack_function[] = {"Headphone", "Mic", "Line",
167 static const char * const spk_function[] = {"On", "Off"};
168 static const struct soc_enum tosa_enum[] = {
169 SOC_ENUM_SINGLE_EXT(5, jack_function),
170 SOC_ENUM_SINGLE_EXT(2, spk_function),
173 static const struct snd_kcontrol_new tosa_controls[] = {
174 SOC_ENUM_EXT("Jack Function", tosa_enum[0], tosa_get_jack,
176 SOC_ENUM_EXT("Speaker Function", tosa_enum[1], tosa_get_spk,
180 static struct snd_soc_dai_link tosa_dai[] = {
183 .stream_name = "AC97 HiFi",
184 .cpu_dai_name = "pxa2xx-ac97",
185 .codec_dai_name = "wm9712-hifi",
186 .platform_name = "pxa-pcm-audio",
187 .codec_name = "wm9712-codec",
192 .stream_name = "AC97 Aux",
193 .cpu_dai_name = "pxa2xx-ac97-aux",
194 .codec_dai_name = "wm9712-aux",
195 .platform_name = "pxa-pcm-audio",
196 .codec_name = "wm9712-codec",
201 static struct snd_soc_card tosa = {
203 .owner = THIS_MODULE,
204 .dai_link = tosa_dai,
205 .num_links = ARRAY_SIZE(tosa_dai),
207 .controls = tosa_controls,
208 .num_controls = ARRAY_SIZE(tosa_controls),
209 .dapm_widgets = tosa_dapm_widgets,
210 .num_dapm_widgets = ARRAY_SIZE(tosa_dapm_widgets),
211 .dapm_routes = audio_map,
212 .num_dapm_routes = ARRAY_SIZE(audio_map),
213 .fully_routed = true,
216 static int tosa_probe(struct platform_device *pdev)
218 struct snd_soc_card *card = ⤩
221 ret = gpio_request_one(TOSA_GPIO_L_MUTE, GPIOF_OUT_INIT_LOW,
226 card->dev = &pdev->dev;
228 ret = devm_snd_soc_register_card(&pdev->dev, card);
230 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
232 gpio_free(TOSA_GPIO_L_MUTE);
237 static int tosa_remove(struct platform_device *pdev)
239 gpio_free(TOSA_GPIO_L_MUTE);
243 static struct platform_driver tosa_driver = {
245 .name = "tosa-audio",
246 .pm = &snd_soc_pm_ops,
249 .remove = tosa_remove,
252 module_platform_driver(tosa_driver);
254 /* Module information */
255 MODULE_AUTHOR("Richard Purdie");
256 MODULE_DESCRIPTION("ALSA SoC Tosa");
257 MODULE_LICENSE("GPL");
258 MODULE_ALIAS("platform:tosa-audio");