1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * corgi.c -- SoC audio for Corgi
5 * Copyright 2005 Wolfson Microelectronics PLC.
6 * Copyright 2005 Openedhand Ltd.
8 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
9 * Richard Purdie <richard@openedhand.com>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/timer.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
18 #include <linux/gpio.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/soc.h>
23 #include <asm/mach-types.h>
24 #include <linux/platform_data/asoc-pxa.h>
26 #include "../codecs/wm8731.h"
27 #include "pxa2xx-i2s.h"
32 #define CORGI_HEADSET 3
33 #define CORGI_HP_OFF 4
34 #define CORGI_SPK_ON 0
35 #define CORGI_SPK_OFF 1
37 /* audio clock in Hz - rounded from 12.235MHz */
38 #define CORGI_AUDIO_CLOCK 12288000
40 static int corgi_jack_func;
41 static int corgi_spk_func;
43 static struct gpio_desc *gpiod_mute_l, *gpiod_mute_r,
44 *gpiod_apm_on, *gpiod_mic_bias;
46 static void corgi_ext_control(struct snd_soc_dapm_context *dapm)
48 snd_soc_dapm_mutex_lock(dapm);
50 /* set up jack connection */
51 switch (corgi_jack_func) {
53 /* set = unmute headphone */
54 gpiod_set_value(gpiod_mute_l, 1);
55 gpiod_set_value(gpiod_mute_r, 1);
56 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
57 snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
58 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
59 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
62 /* reset = mute headphone */
63 gpiod_set_value(gpiod_mute_l, 0);
64 gpiod_set_value(gpiod_mute_r, 0);
65 snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack");
66 snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
67 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
68 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
71 gpiod_set_value(gpiod_mute_l, 0);
72 gpiod_set_value(gpiod_mute_r, 0);
73 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
74 snd_soc_dapm_enable_pin_unlocked(dapm, "Line Jack");
75 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
76 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
79 gpiod_set_value(gpiod_mute_l, 0);
80 gpiod_set_value(gpiod_mute_r, 1);
81 snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack");
82 snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
83 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
84 snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack");
88 if (corgi_spk_func == CORGI_SPK_ON)
89 snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk");
91 snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk");
93 /* signal a DAPM event */
94 snd_soc_dapm_sync_unlocked(dapm);
96 snd_soc_dapm_mutex_unlock(dapm);
99 static int corgi_startup(struct snd_pcm_substream *substream)
101 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
103 /* check the jack status at stream startup */
104 corgi_ext_control(&rtd->card->dapm);
109 /* we need to unmute the HP at shutdown as the mute burns power on corgi */
110 static void corgi_shutdown(struct snd_pcm_substream *substream)
112 /* set = unmute headphone */
113 gpiod_set_value(gpiod_mute_l, 1);
114 gpiod_set_value(gpiod_mute_r, 1);
117 static int corgi_hw_params(struct snd_pcm_substream *substream,
118 struct snd_pcm_hw_params *params)
120 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
121 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
122 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
123 unsigned int clk = 0;
126 switch (params_rate(params)) {
140 /* set the codec system clock for DAC and ADC */
141 ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
146 /* set the I2S system clock as input (unused) */
147 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
155 static const struct snd_soc_ops corgi_ops = {
156 .startup = corgi_startup,
157 .hw_params = corgi_hw_params,
158 .shutdown = corgi_shutdown,
161 static int corgi_get_jack(struct snd_kcontrol *kcontrol,
162 struct snd_ctl_elem_value *ucontrol)
164 ucontrol->value.enumerated.item[0] = corgi_jack_func;
168 static int corgi_set_jack(struct snd_kcontrol *kcontrol,
169 struct snd_ctl_elem_value *ucontrol)
171 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
173 if (corgi_jack_func == ucontrol->value.enumerated.item[0])
176 corgi_jack_func = ucontrol->value.enumerated.item[0];
177 corgi_ext_control(&card->dapm);
181 static int corgi_get_spk(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_value *ucontrol)
184 ucontrol->value.enumerated.item[0] = corgi_spk_func;
188 static int corgi_set_spk(struct snd_kcontrol *kcontrol,
189 struct snd_ctl_elem_value *ucontrol)
191 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
193 if (corgi_spk_func == ucontrol->value.enumerated.item[0])
196 corgi_spk_func = ucontrol->value.enumerated.item[0];
197 corgi_ext_control(&card->dapm);
201 static int corgi_amp_event(struct snd_soc_dapm_widget *w,
202 struct snd_kcontrol *k, int event)
204 gpiod_set_value(gpiod_apm_on, SND_SOC_DAPM_EVENT_ON(event));
208 static int corgi_mic_event(struct snd_soc_dapm_widget *w,
209 struct snd_kcontrol *k, int event)
211 gpiod_set_value(gpiod_mic_bias, SND_SOC_DAPM_EVENT_ON(event));
215 /* corgi machine dapm widgets */
216 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
217 SND_SOC_DAPM_HP("Headphone Jack", NULL),
218 SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
219 SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
220 SND_SOC_DAPM_LINE("Line Jack", NULL),
221 SND_SOC_DAPM_HP("Headset Jack", NULL),
224 /* Corgi machine audio map (connections to the codec pins) */
225 static const struct snd_soc_dapm_route corgi_audio_map[] = {
227 /* headset Jack - in = micin, out = LHPOUT*/
228 {"Headset Jack", NULL, "LHPOUT"},
230 /* headphone connected to LHPOUT1, RHPOUT1 */
231 {"Headphone Jack", NULL, "LHPOUT"},
232 {"Headphone Jack", NULL, "RHPOUT"},
234 /* speaker connected to LOUT, ROUT */
235 {"Ext Spk", NULL, "ROUT"},
236 {"Ext Spk", NULL, "LOUT"},
238 /* mic is connected to MICIN (via right channel of headphone jack) */
239 {"MICIN", NULL, "Mic Jack"},
241 /* Same as the above but no mic bias for line signals */
242 {"MICIN", NULL, "Line Jack"},
245 static const char * const jack_function[] = {"Headphone", "Mic", "Line",
247 static const char * const spk_function[] = {"On", "Off"};
248 static const struct soc_enum corgi_enum[] = {
249 SOC_ENUM_SINGLE_EXT(5, jack_function),
250 SOC_ENUM_SINGLE_EXT(2, spk_function),
253 static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
254 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
256 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
260 /* corgi digital audio interface glue - connects codec <--> CPU */
261 SND_SOC_DAILINK_DEFS(wm8731,
262 DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-i2s")),
263 DAILINK_COMP_ARRAY(COMP_CODEC("wm8731.0-001b", "wm8731-hifi")),
264 DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
266 static struct snd_soc_dai_link corgi_dai = {
268 .stream_name = "WM8731",
269 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
270 SND_SOC_DAIFMT_CBS_CFS,
272 SND_SOC_DAILINK_REG(wm8731),
275 /* corgi audio machine driver */
276 static struct snd_soc_card corgi = {
278 .owner = THIS_MODULE,
279 .dai_link = &corgi_dai,
282 .controls = wm8731_corgi_controls,
283 .num_controls = ARRAY_SIZE(wm8731_corgi_controls),
284 .dapm_widgets = wm8731_dapm_widgets,
285 .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
286 .dapm_routes = corgi_audio_map,
287 .num_dapm_routes = ARRAY_SIZE(corgi_audio_map),
288 .fully_routed = true,
291 static int corgi_probe(struct platform_device *pdev)
293 struct snd_soc_card *card = &corgi;
296 card->dev = &pdev->dev;
298 gpiod_mute_l = devm_gpiod_get(&pdev->dev, "mute-l", GPIOD_OUT_HIGH);
299 if (IS_ERR(gpiod_mute_l))
300 return PTR_ERR(gpiod_mute_l);
301 gpiod_mute_r = devm_gpiod_get(&pdev->dev, "mute-r", GPIOD_OUT_HIGH);
302 if (IS_ERR(gpiod_mute_r))
303 return PTR_ERR(gpiod_mute_r);
304 gpiod_apm_on = devm_gpiod_get(&pdev->dev, "apm-on", GPIOD_OUT_LOW);
305 if (IS_ERR(gpiod_apm_on))
306 return PTR_ERR(gpiod_apm_on);
307 gpiod_mic_bias = devm_gpiod_get(&pdev->dev, "mic-bias", GPIOD_OUT_LOW);
308 if (IS_ERR(gpiod_mic_bias))
309 return PTR_ERR(gpiod_mic_bias);
311 ret = devm_snd_soc_register_card(&pdev->dev, card);
313 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
318 static struct platform_driver corgi_driver = {
320 .name = "corgi-audio",
321 .pm = &snd_soc_pm_ops,
323 .probe = corgi_probe,
326 module_platform_driver(corgi_driver);
328 /* Module information */
329 MODULE_AUTHOR("Richard Purdie");
330 MODULE_DESCRIPTION("ALSA SoC Corgi");
331 MODULE_LICENSE("GPL");
332 MODULE_ALIAS("platform:corgi-audio");