2 * corgi.c -- SoC audio for Corgi
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
7 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
8 * Richard Purdie <richard@openedhand.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/timer.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
22 #include <linux/gpio.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
28 #include <asm/mach-types.h>
29 #include <mach/corgi.h>
30 #include <mach/audio.h>
32 #include "../codecs/wm8731.h"
33 #include "pxa2xx-i2s.h"
38 #define CORGI_HEADSET 3
39 #define CORGI_HP_OFF 4
40 #define CORGI_SPK_ON 0
41 #define CORGI_SPK_OFF 1
43 /* audio clock in Hz - rounded from 12.235MHz */
44 #define CORGI_AUDIO_CLOCK 12288000
46 static int corgi_jack_func;
47 static int corgi_spk_func;
49 static void corgi_ext_control(struct snd_soc_codec *codec)
51 /* set up jack connection */
52 switch (corgi_jack_func) {
54 /* set = unmute headphone */
55 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
56 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
57 snd_soc_dapm_disable_pin(codec, "Mic Jack");
58 snd_soc_dapm_disable_pin(codec, "Line Jack");
59 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
60 snd_soc_dapm_disable_pin(codec, "Headset Jack");
63 /* reset = mute headphone */
64 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
65 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
66 snd_soc_dapm_enable_pin(codec, "Mic Jack");
67 snd_soc_dapm_disable_pin(codec, "Line Jack");
68 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
69 snd_soc_dapm_disable_pin(codec, "Headset Jack");
72 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
73 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
74 snd_soc_dapm_disable_pin(codec, "Mic Jack");
75 snd_soc_dapm_enable_pin(codec, "Line Jack");
76 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
77 snd_soc_dapm_disable_pin(codec, "Headset Jack");
80 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
81 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
82 snd_soc_dapm_enable_pin(codec, "Mic Jack");
83 snd_soc_dapm_disable_pin(codec, "Line Jack");
84 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
85 snd_soc_dapm_enable_pin(codec, "Headset Jack");
89 if (corgi_spk_func == CORGI_SPK_ON)
90 snd_soc_dapm_enable_pin(codec, "Ext Spk");
92 snd_soc_dapm_disable_pin(codec, "Ext Spk");
94 /* signal a DAPM event */
95 snd_soc_dapm_sync(codec);
98 static int corgi_startup(struct snd_pcm_substream *substream)
100 struct snd_soc_pcm_runtime *rtd = substream->private_data;
101 struct snd_soc_codec *codec = rtd->codec;
103 mutex_lock(&codec->mutex);
105 /* check the jack status at stream startup */
106 corgi_ext_control(codec);
108 mutex_unlock(&codec->mutex);
113 /* we need to unmute the HP at shutdown as the mute burns power on corgi */
114 static void corgi_shutdown(struct snd_pcm_substream *substream)
116 /* set = unmute headphone */
117 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
118 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
121 static int corgi_hw_params(struct snd_pcm_substream *substream,
122 struct snd_pcm_hw_params *params)
124 struct snd_soc_pcm_runtime *rtd = substream->private_data;
125 struct snd_soc_dai *codec_dai = rtd->codec_dai;
126 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
127 unsigned int clk = 0;
130 switch (params_rate(params)) {
144 /* set codec DAI configuration */
145 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
146 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
150 /* set cpu DAI configuration */
151 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
152 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
156 /* set the codec system clock for DAC and ADC */
157 ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
162 /* set the I2S system clock as input (unused) */
163 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
171 static struct snd_soc_ops corgi_ops = {
172 .startup = corgi_startup,
173 .hw_params = corgi_hw_params,
174 .shutdown = corgi_shutdown,
177 static int corgi_get_jack(struct snd_kcontrol *kcontrol,
178 struct snd_ctl_elem_value *ucontrol)
180 ucontrol->value.integer.value[0] = corgi_jack_func;
184 static int corgi_set_jack(struct snd_kcontrol *kcontrol,
185 struct snd_ctl_elem_value *ucontrol)
187 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
189 if (corgi_jack_func == ucontrol->value.integer.value[0])
192 corgi_jack_func = ucontrol->value.integer.value[0];
193 corgi_ext_control(codec);
197 static int corgi_get_spk(struct snd_kcontrol *kcontrol,
198 struct snd_ctl_elem_value *ucontrol)
200 ucontrol->value.integer.value[0] = corgi_spk_func;
204 static int corgi_set_spk(struct snd_kcontrol *kcontrol,
205 struct snd_ctl_elem_value *ucontrol)
207 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
209 if (corgi_spk_func == ucontrol->value.integer.value[0])
212 corgi_spk_func = ucontrol->value.integer.value[0];
213 corgi_ext_control(codec);
217 static int corgi_amp_event(struct snd_soc_dapm_widget *w,
218 struct snd_kcontrol *k, int event)
220 gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
224 static int corgi_mic_event(struct snd_soc_dapm_widget *w,
225 struct snd_kcontrol *k, int event)
227 gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
231 /* corgi machine dapm widgets */
232 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
233 SND_SOC_DAPM_HP("Headphone Jack", NULL),
234 SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
235 SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
236 SND_SOC_DAPM_LINE("Line Jack", NULL),
237 SND_SOC_DAPM_HP("Headset Jack", NULL),
240 /* Corgi machine audio map (connections to the codec pins) */
241 static const struct snd_soc_dapm_route audio_map[] = {
243 /* headset Jack - in = micin, out = LHPOUT*/
244 {"Headset Jack", NULL, "LHPOUT"},
246 /* headphone connected to LHPOUT1, RHPOUT1 */
247 {"Headphone Jack", NULL, "LHPOUT"},
248 {"Headphone Jack", NULL, "RHPOUT"},
250 /* speaker connected to LOUT, ROUT */
251 {"Ext Spk", NULL, "ROUT"},
252 {"Ext Spk", NULL, "LOUT"},
254 /* mic is connected to MICIN (via right channel of headphone jack) */
255 {"MICIN", NULL, "Mic Jack"},
257 /* Same as the above but no mic bias for line signals */
258 {"MICIN", NULL, "Line Jack"},
261 static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
263 static const char *spk_function[] = {"On", "Off"};
264 static const struct soc_enum corgi_enum[] = {
265 SOC_ENUM_SINGLE_EXT(5, jack_function),
266 SOC_ENUM_SINGLE_EXT(2, spk_function),
269 static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
270 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
272 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
277 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
279 static int corgi_wm8731_init(struct snd_soc_pcm_runtime *rtd)
281 struct snd_soc_codec *codec = rtd->codec;
284 snd_soc_dapm_nc_pin(codec, "LLINEIN");
285 snd_soc_dapm_nc_pin(codec, "RLINEIN");
287 /* Add corgi specific controls */
288 err = snd_soc_add_controls(codec, wm8731_corgi_controls,
289 ARRAY_SIZE(wm8731_corgi_controls));
293 /* Add corgi specific widgets */
294 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
295 ARRAY_SIZE(wm8731_dapm_widgets));
297 /* Set up corgi specific audio path audio_map */
298 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
300 snd_soc_dapm_sync(codec);
304 /* corgi digital audio interface glue - connects codec <--> CPU */
305 static struct snd_soc_dai_link corgi_dai = {
307 .stream_name = "WM8731",
308 .cpu_dai_name = "pxa-is2-dai",
309 .codec_dai_name = "wm8731-hifi",
310 .platform_name = "pxa-pcm-audio",
311 .codec_name = "wm8731-codec-0.001a",
312 .init = corgi_wm8731_init,
316 /* corgi audio machine driver */
317 static struct snd_soc_card snd_soc_corgi = {
319 .dai_link = &corgi_dai,
323 static struct platform_device *corgi_snd_device;
325 static int __init corgi_init(void)
329 if (!(machine_is_corgi() || machine_is_shepherd() ||
333 corgi_snd_device = platform_device_alloc("soc-audio", -1);
334 if (!corgi_snd_device)
337 platform_set_drvdata(corgi_snd_device, &snd_soc_corgi);
338 ret = platform_device_add(corgi_snd_device);
341 platform_device_put(corgi_snd_device);
346 static void __exit corgi_exit(void)
348 platform_device_unregister(corgi_snd_device);
351 module_init(corgi_init);
352 module_exit(corgi_exit);
354 /* Module information */
355 MODULE_AUTHOR("Richard Purdie");
356 MODULE_DESCRIPTION("ALSA SoC Corgi");
357 MODULE_LICENSE("GPL");