Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / sound / soc / pxa / corgi.c
blob5b22f332c2f3f1c91c2f072d5a17eddf465fcabd
1 /*
2 * corgi.c -- SoC audio for Corgi
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
7 * Authors: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
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.
15 * Revision history
16 * 30th Nov 2005 Initial version.
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/timer.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
30 #include <asm/mach-types.h>
31 #include <asm/hardware/scoop.h>
32 #include <asm/arch/pxa-regs.h>
33 #include <asm/arch/hardware.h>
34 #include <asm/arch/corgi.h>
35 #include <asm/arch/audio.h>
37 #include "../codecs/wm8731.h"
38 #include "pxa2xx-pcm.h"
39 #include "pxa2xx-i2s.h"
41 #define CORGI_HP 0
42 #define CORGI_MIC 1
43 #define CORGI_LINE 2
44 #define CORGI_HEADSET 3
45 #define CORGI_HP_OFF 4
46 #define CORGI_SPK_ON 0
47 #define CORGI_SPK_OFF 1
49 /* audio clock in Hz - rounded from 12.235MHz */
50 #define CORGI_AUDIO_CLOCK 12288000
52 static int corgi_jack_func;
53 static int corgi_spk_func;
55 static void corgi_ext_control(struct snd_soc_codec *codec)
57 int spk = 0, mic = 0, line = 0, hp = 0, hs = 0;
59 /* set up jack connection */
60 switch (corgi_jack_func) {
61 case CORGI_HP:
62 hp = 1;
63 /* set = unmute headphone */
64 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
65 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
66 break;
67 case CORGI_MIC:
68 mic = 1;
69 /* reset = mute headphone */
70 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
71 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
72 break;
73 case CORGI_LINE:
74 line = 1;
75 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
76 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
77 break;
78 case CORGI_HEADSET:
79 hs = 1;
80 mic = 1;
81 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
82 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
83 break;
86 if (corgi_spk_func == CORGI_SPK_ON)
87 spk = 1;
89 /* set the enpoints to their new connetion states */
90 snd_soc_dapm_set_endpoint(codec, "Ext Spk", spk);
91 snd_soc_dapm_set_endpoint(codec, "Mic Jack", mic);
92 snd_soc_dapm_set_endpoint(codec, "Line Jack", line);
93 snd_soc_dapm_set_endpoint(codec, "Headphone Jack", hp);
94 snd_soc_dapm_set_endpoint(codec, "Headset Jack", hs);
96 /* signal a DAPM event */
97 snd_soc_dapm_sync_endpoints(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);
107 return 0;
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 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
118 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
119 return 0;
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_codec_dai *codec_dai = rtd->dai->codec_dai;
127 struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
128 unsigned int clk = 0;
129 int ret = 0;
131 switch (params_rate(params)) {
132 case 8000:
133 case 16000:
134 case 48000:
135 case 96000:
136 clk = 12288000;
137 break;
138 case 11025:
139 case 22050:
140 case 44100:
141 clk = 11289600;
142 break;
145 /* set codec DAI configuration */
146 ret = codec_dai->dai_ops.set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
147 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
148 if (ret < 0)
149 return ret;
151 /* set cpu DAI configuration */
152 ret = cpu_dai->dai_ops.set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
153 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
154 if (ret < 0)
155 return ret;
157 /* set the codec system clock for DAC and ADC */
158 ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8731_SYSCLK, clk,
159 SND_SOC_CLOCK_IN);
160 if (ret < 0)
161 return ret;
163 /* set the I2S system clock as input (unused) */
164 ret = cpu_dai->dai_ops.set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
165 SND_SOC_CLOCK_IN);
166 if (ret < 0)
167 return ret;
169 return 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;
182 return 0;
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])
191 return 0;
193 corgi_jack_func = ucontrol->value.integer.value[0];
194 corgi_ext_control(codec);
195 return 1;
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;
202 return 0;
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])
211 return 0;
213 corgi_spk_func = ucontrol->value.integer.value[0];
214 corgi_ext_control(codec);
215 return 1;
218 <<<<<<< HEAD:sound/soc/pxa/corgi.c
219 static int corgi_amp_event(struct snd_soc_dapm_widget *w, int event)
220 =======
221 static int corgi_amp_event(struct snd_soc_dapm_widget *w,
222 struct snd_kcontrol *k, int event)
223 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:sound/soc/pxa/corgi.c
225 if (SND_SOC_DAPM_EVENT_ON(event))
226 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_APM_ON);
227 else
228 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_APM_ON);
230 return 0;
233 <<<<<<< HEAD:sound/soc/pxa/corgi.c
234 static int corgi_mic_event(struct snd_soc_dapm_widget *w, int event)
235 =======
236 static int corgi_mic_event(struct snd_soc_dapm_widget *w,
237 struct snd_kcontrol *k, int event)
238 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:sound/soc/pxa/corgi.c
240 if (SND_SOC_DAPM_EVENT_ON(event))
241 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MIC_BIAS);
242 else
243 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MIC_BIAS);
245 return 0;
248 /* corgi machine dapm widgets */
249 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
250 SND_SOC_DAPM_HP("Headphone Jack", NULL),
251 SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
252 SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
253 SND_SOC_DAPM_LINE("Line Jack", NULL),
254 SND_SOC_DAPM_HP("Headset Jack", NULL),
257 /* Corgi machine audio map (connections to the codec pins) */
258 static const char *audio_map[][3] = {
260 /* headset Jack - in = micin, out = LHPOUT*/
261 {"Headset Jack", NULL, "LHPOUT"},
263 /* headphone connected to LHPOUT1, RHPOUT1 */
264 {"Headphone Jack", NULL, "LHPOUT"},
265 {"Headphone Jack", NULL, "RHPOUT"},
267 /* speaker connected to LOUT, ROUT */
268 {"Ext Spk", NULL, "ROUT"},
269 {"Ext Spk", NULL, "LOUT"},
271 /* mic is connected to MICIN (via right channel of headphone jack) */
272 {"MICIN", NULL, "Mic Jack"},
274 /* Same as the above but no mic bias for line signals */
275 {"MICIN", NULL, "Line Jack"},
277 {NULL, NULL, NULL},
280 static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
281 "Off"};
282 static const char *spk_function[] = {"On", "Off"};
283 static const struct soc_enum corgi_enum[] = {
284 SOC_ENUM_SINGLE_EXT(5, jack_function),
285 SOC_ENUM_SINGLE_EXT(2, spk_function),
288 static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
289 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
290 corgi_set_jack),
291 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
292 corgi_set_spk),
296 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
298 static int corgi_wm8731_init(struct snd_soc_codec *codec)
300 int i, err;
302 snd_soc_dapm_set_endpoint(codec, "LLINEIN", 0);
303 snd_soc_dapm_set_endpoint(codec, "RLINEIN", 0);
305 /* Add corgi specific controls */
306 for (i = 0; i < ARRAY_SIZE(wm8731_corgi_controls); i++) {
307 err = snd_ctl_add(codec->card,
308 snd_soc_cnew(&wm8731_corgi_controls[i],codec, NULL));
309 if (err < 0)
310 return err;
313 /* Add corgi specific widgets */
314 for(i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++) {
315 snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]);
318 /* Set up corgi specific audio path audio_map */
319 for(i = 0; audio_map[i][0] != NULL; i++) {
320 snd_soc_dapm_connect_input(codec, audio_map[i][0],
321 audio_map[i][1], audio_map[i][2]);
324 snd_soc_dapm_sync_endpoints(codec);
325 return 0;
328 /* corgi digital audio interface glue - connects codec <--> CPU */
329 static struct snd_soc_dai_link corgi_dai = {
330 .name = "WM8731",
331 .stream_name = "WM8731",
332 .cpu_dai = &pxa_i2s_dai,
333 .codec_dai = &wm8731_dai,
334 .init = corgi_wm8731_init,
335 .ops = &corgi_ops,
338 /* corgi audio machine driver */
339 static struct snd_soc_machine snd_soc_machine_corgi = {
340 .name = "Corgi",
341 .dai_link = &corgi_dai,
342 .num_links = 1,
345 /* corgi audio private data */
346 static struct wm8731_setup_data corgi_wm8731_setup = {
347 .i2c_address = 0x1b,
350 /* corgi audio subsystem */
351 static struct snd_soc_device corgi_snd_devdata = {
352 .machine = &snd_soc_machine_corgi,
353 .platform = &pxa2xx_soc_platform,
354 .codec_dev = &soc_codec_dev_wm8731,
355 .codec_data = &corgi_wm8731_setup,
358 static struct platform_device *corgi_snd_device;
360 static int __init corgi_init(void)
362 int ret;
364 if (!(machine_is_corgi() || machine_is_shepherd() || machine_is_husky()))
365 return -ENODEV;
367 corgi_snd_device = platform_device_alloc("soc-audio", -1);
368 if (!corgi_snd_device)
369 return -ENOMEM;
371 platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
372 corgi_snd_devdata.dev = &corgi_snd_device->dev;
373 ret = platform_device_add(corgi_snd_device);
375 if (ret)
376 platform_device_put(corgi_snd_device);
378 return ret;
381 static void __exit corgi_exit(void)
383 platform_device_unregister(corgi_snd_device);
386 module_init(corgi_init);
387 module_exit(corgi_exit);
389 /* Module information */
390 MODULE_AUTHOR("Richard Purdie");
391 MODULE_DESCRIPTION("ALSA SoC Corgi");
392 MODULE_LICENSE("GPL");