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/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/gpio.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dapm.h>
27 #include <asm/mach-types.h>
28 #include <mach/pxa-regs.h>
29 #include <mach/hardware.h>
30 #include <mach/corgi.h>
31 #include <mach/audio.h>
33 #include "../codecs/wm8731.h"
34 #include "pxa2xx-pcm.h"
35 #include "pxa2xx-i2s.h"
40 #define CORGI_HEADSET 3
41 #define CORGI_HP_OFF 4
42 #define CORGI_SPK_ON 0
43 #define CORGI_SPK_OFF 1
45 /* audio clock in Hz - rounded from 12.235MHz */
46 #define CORGI_AUDIO_CLOCK 12288000
48 static int corgi_jack_func;
49 static int corgi_spk_func;
51 static void corgi_ext_control(struct snd_soc_codec *codec)
53 /* set up jack connection */
54 switch (corgi_jack_func) {
56 /* set = unmute headphone */
57 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
58 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
59 snd_soc_dapm_disable_pin(codec, "Mic Jack");
60 snd_soc_dapm_disable_pin(codec, "Line Jack");
61 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
62 snd_soc_dapm_disable_pin(codec, "Headset Jack");
65 /* reset = mute headphone */
66 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
67 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
68 snd_soc_dapm_enable_pin(codec, "Mic Jack");
69 snd_soc_dapm_disable_pin(codec, "Line Jack");
70 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
71 snd_soc_dapm_disable_pin(codec, "Headset Jack");
74 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
75 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
76 snd_soc_dapm_disable_pin(codec, "Mic Jack");
77 snd_soc_dapm_enable_pin(codec, "Line Jack");
78 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
79 snd_soc_dapm_disable_pin(codec, "Headset Jack");
82 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
83 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
84 snd_soc_dapm_enable_pin(codec, "Mic Jack");
85 snd_soc_dapm_disable_pin(codec, "Line Jack");
86 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
87 snd_soc_dapm_enable_pin(codec, "Headset Jack");
91 if (corgi_spk_func == CORGI_SPK_ON)
92 snd_soc_dapm_enable_pin(codec, "Ext Spk");
94 snd_soc_dapm_disable_pin(codec, "Ext Spk");
96 /* signal a DAPM event */
97 snd_soc_dapm_sync(codec);
100 static int corgi_startup(struct snd_pcm_substream *substream)
102 struct snd_soc_pcm_runtime *rtd = substream->private_data;
103 struct snd_soc_codec *codec = rtd->socdev->codec;
105 /* check the jack status at stream startup */
106 corgi_ext_control(codec);
110 /* we need to unmute the HP at shutdown as the mute burns power on corgi */
111 static int corgi_shutdown(struct snd_pcm_substream *substream)
113 struct snd_soc_pcm_runtime *rtd = substream->private_data;
114 struct snd_soc_codec *codec = rtd->socdev->codec;
116 /* set = unmute headphone */
117 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
118 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
122 static int corgi_hw_params(struct snd_pcm_substream *substream,
123 struct snd_pcm_hw_params *params)
125 struct snd_soc_pcm_runtime *rtd = substream->private_data;
126 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
127 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
128 unsigned int clk = 0;
131 switch (params_rate(params)) {
145 /* set codec DAI configuration */
146 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
147 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
151 /* set cpu DAI configuration */
152 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
153 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
157 /* set the codec system clock for DAC and ADC */
158 ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK, clk,
163 /* set the I2S system clock as input (unused) */
164 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
172 static struct snd_soc_ops corgi_ops = {
173 .startup = corgi_startup,
174 .hw_params = corgi_hw_params,
175 .shutdown = corgi_shutdown,
178 static int corgi_get_jack(struct snd_kcontrol *kcontrol,
179 struct snd_ctl_elem_value *ucontrol)
181 ucontrol->value.integer.value[0] = corgi_jack_func;
185 static int corgi_set_jack(struct snd_kcontrol *kcontrol,
186 struct snd_ctl_elem_value *ucontrol)
188 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
190 if (corgi_jack_func == ucontrol->value.integer.value[0])
193 corgi_jack_func = ucontrol->value.integer.value[0];
194 corgi_ext_control(codec);
198 static int corgi_get_spk(struct snd_kcontrol *kcontrol,
199 struct snd_ctl_elem_value *ucontrol)
201 ucontrol->value.integer.value[0] = corgi_spk_func;
205 static int corgi_set_spk(struct snd_kcontrol *kcontrol,
206 struct snd_ctl_elem_value *ucontrol)
208 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
210 if (corgi_spk_func == ucontrol->value.integer.value[0])
213 corgi_spk_func = ucontrol->value.integer.value[0];
214 corgi_ext_control(codec);
218 static int corgi_amp_event(struct snd_soc_dapm_widget *w,
219 struct snd_kcontrol *k, int event)
221 gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
225 static int corgi_mic_event(struct snd_soc_dapm_widget *w,
226 struct snd_kcontrol *k, int event)
228 gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
232 /* corgi machine dapm widgets */
233 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
234 SND_SOC_DAPM_HP("Headphone Jack", NULL),
235 SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
236 SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
237 SND_SOC_DAPM_LINE("Line Jack", NULL),
238 SND_SOC_DAPM_HP("Headset Jack", NULL),
241 /* Corgi machine audio map (connections to the codec pins) */
242 static const struct snd_soc_dapm_route audio_map[] = {
244 /* headset Jack - in = micin, out = LHPOUT*/
245 {"Headset Jack", NULL, "LHPOUT"},
247 /* headphone connected to LHPOUT1, RHPOUT1 */
248 {"Headphone Jack", NULL, "LHPOUT"},
249 {"Headphone Jack", NULL, "RHPOUT"},
251 /* speaker connected to LOUT, ROUT */
252 {"Ext Spk", NULL, "ROUT"},
253 {"Ext Spk", NULL, "LOUT"},
255 /* mic is connected to MICIN (via right channel of headphone jack) */
256 {"MICIN", NULL, "Mic Jack"},
258 /* Same as the above but no mic bias for line signals */
259 {"MICIN", NULL, "Line Jack"},
262 static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
264 static const char *spk_function[] = {"On", "Off"};
265 static const struct soc_enum corgi_enum[] = {
266 SOC_ENUM_SINGLE_EXT(5, jack_function),
267 SOC_ENUM_SINGLE_EXT(2, spk_function),
270 static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
271 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
273 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
278 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
280 static int corgi_wm8731_init(struct snd_soc_codec *codec)
284 snd_soc_dapm_nc_pin(codec, "LLINEIN");
285 snd_soc_dapm_nc_pin(codec, "RLINEIN");
287 /* Add corgi specific controls */
288 for (i = 0; i < ARRAY_SIZE(wm8731_corgi_controls); i++) {
289 err = snd_ctl_add(codec->card,
290 snd_soc_cnew(&wm8731_corgi_controls[i], codec, NULL));
295 /* Add corgi specific widgets */
296 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
297 ARRAY_SIZE(wm8731_dapm_widgets));
299 /* Set up corgi specific audio path audio_map */
300 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
302 snd_soc_dapm_sync(codec);
306 /* corgi digital audio interface glue - connects codec <--> CPU */
307 static struct snd_soc_dai_link corgi_dai = {
309 .stream_name = "WM8731",
310 .cpu_dai = &pxa_i2s_dai,
311 .codec_dai = &wm8731_dai,
312 .init = corgi_wm8731_init,
316 /* corgi audio machine driver */
317 static struct snd_soc_machine snd_soc_machine_corgi = {
319 .dai_link = &corgi_dai,
323 /* corgi audio private data */
324 static struct wm8731_setup_data corgi_wm8731_setup = {
329 /* corgi audio subsystem */
330 static struct snd_soc_device corgi_snd_devdata = {
331 .machine = &snd_soc_machine_corgi,
332 .platform = &pxa2xx_soc_platform,
333 .codec_dev = &soc_codec_dev_wm8731,
334 .codec_data = &corgi_wm8731_setup,
337 static struct platform_device *corgi_snd_device;
339 static int __init corgi_init(void)
343 if (!(machine_is_corgi() || machine_is_shepherd() ||
347 corgi_snd_device = platform_device_alloc("soc-audio", -1);
348 if (!corgi_snd_device)
351 platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
352 corgi_snd_devdata.dev = &corgi_snd_device->dev;
353 ret = platform_device_add(corgi_snd_device);
356 platform_device_put(corgi_snd_device);
361 static void __exit corgi_exit(void)
363 platform_device_unregister(corgi_snd_device);
366 module_init(corgi_init);
367 module_exit(corgi_exit);
369 /* Module information */
370 MODULE_AUTHOR("Richard Purdie");
371 MODULE_DESCRIPTION("ALSA SoC Corgi");
372 MODULE_LICENSE("GPL");