Commit | Line | Data |
---|---|---|
a1eb4b3c LG |
1 | /* |
2 | * corgi.c -- SoC audio for Corgi | |
3 | * | |
4 | * Copyright 2005 Wolfson Microelectronics PLC. | |
5 | * Copyright 2005 Openedhand Ltd. | |
6 | * | |
d331124d | 7 | * Authors: Liam Girdwood <lrg@slimlogic.co.uk> |
a1eb4b3c LG |
8 | * Richard Purdie <richard@openedhand.com> |
9 | * | |
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. | |
a1eb4b3c LG |
14 | */ |
15 | ||
16 | #include <linux/module.h> | |
17 | #include <linux/moduleparam.h> | |
18 | #include <linux/timer.h> | |
fc996757 | 19 | #include <linux/i2c.h> |
a1eb4b3c LG |
20 | #include <linux/interrupt.h> |
21 | #include <linux/platform_device.h> | |
6168cda9 | 22 | #include <linux/gpio.h> |
a1eb4b3c LG |
23 | #include <sound/core.h> |
24 | #include <sound/pcm.h> | |
25 | #include <sound/soc.h> | |
26 | #include <sound/soc-dapm.h> | |
27 | ||
28 | #include <asm/mach-types.h> | |
a09e64fb RK |
29 | #include <mach/corgi.h> |
30 | #include <mach/audio.h> | |
a1eb4b3c LG |
31 | |
32 | #include "../codecs/wm8731.h" | |
d928b25a | 33 | #include "pxa2xx-i2s.h" |
a1eb4b3c LG |
34 | |
35 | #define CORGI_HP 0 | |
36 | #define CORGI_MIC 1 | |
37 | #define CORGI_LINE 2 | |
38 | #define CORGI_HEADSET 3 | |
39 | #define CORGI_HP_OFF 4 | |
40 | #define CORGI_SPK_ON 0 | |
41 | #define CORGI_SPK_OFF 1 | |
42 | ||
43 | /* audio clock in Hz - rounded from 12.235MHz */ | |
44 | #define CORGI_AUDIO_CLOCK 12288000 | |
45 | ||
46 | static int corgi_jack_func; | |
47 | static int corgi_spk_func; | |
48 | ||
49 | static void corgi_ext_control(struct snd_soc_codec *codec) | |
50 | { | |
ce6120cc LG |
51 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
52 | ||
a1eb4b3c LG |
53 | /* set up jack connection */ |
54 | switch (corgi_jack_func) { | |
55 | case CORGI_HP: | |
a1eb4b3c | 56 | /* set = unmute headphone */ |
6168cda9 EM |
57 | gpio_set_value(CORGI_GPIO_MUTE_L, 1); |
58 | gpio_set_value(CORGI_GPIO_MUTE_R, 1); | |
ce6120cc LG |
59 | snd_soc_dapm_disable_pin(dapm, "Mic Jack"); |
60 | snd_soc_dapm_disable_pin(dapm, "Line Jack"); | |
61 | snd_soc_dapm_enable_pin(dapm, "Headphone Jack"); | |
62 | snd_soc_dapm_disable_pin(dapm, "Headset Jack"); | |
a1eb4b3c LG |
63 | break; |
64 | case CORGI_MIC: | |
a1eb4b3c | 65 | /* reset = mute headphone */ |
6168cda9 EM |
66 | gpio_set_value(CORGI_GPIO_MUTE_L, 0); |
67 | gpio_set_value(CORGI_GPIO_MUTE_R, 0); | |
ce6120cc LG |
68 | snd_soc_dapm_enable_pin(dapm, "Mic Jack"); |
69 | snd_soc_dapm_disable_pin(dapm, "Line Jack"); | |
70 | snd_soc_dapm_disable_pin(dapm, "Headphone Jack"); | |
71 | snd_soc_dapm_disable_pin(dapm, "Headset Jack"); | |
a1eb4b3c LG |
72 | break; |
73 | case CORGI_LINE: | |
6168cda9 EM |
74 | gpio_set_value(CORGI_GPIO_MUTE_L, 0); |
75 | gpio_set_value(CORGI_GPIO_MUTE_R, 0); | |
ce6120cc LG |
76 | snd_soc_dapm_disable_pin(dapm, "Mic Jack"); |
77 | snd_soc_dapm_enable_pin(dapm, "Line Jack"); | |
78 | snd_soc_dapm_disable_pin(dapm, "Headphone Jack"); | |
79 | snd_soc_dapm_disable_pin(dapm, "Headset Jack"); | |
a1eb4b3c LG |
80 | break; |
81 | case CORGI_HEADSET: | |
6168cda9 EM |
82 | gpio_set_value(CORGI_GPIO_MUTE_L, 0); |
83 | gpio_set_value(CORGI_GPIO_MUTE_R, 1); | |
ce6120cc LG |
84 | snd_soc_dapm_enable_pin(dapm, "Mic Jack"); |
85 | snd_soc_dapm_disable_pin(dapm, "Line Jack"); | |
86 | snd_soc_dapm_disable_pin(dapm, "Headphone Jack"); | |
87 | snd_soc_dapm_enable_pin(dapm, "Headset Jack"); | |
a1eb4b3c LG |
88 | break; |
89 | } | |
90 | ||
91 | if (corgi_spk_func == CORGI_SPK_ON) | |
ce6120cc | 92 | snd_soc_dapm_enable_pin(dapm, "Ext Spk"); |
a5302181 | 93 | else |
ce6120cc | 94 | snd_soc_dapm_disable_pin(dapm, "Ext Spk"); |
a1eb4b3c LG |
95 | |
96 | /* signal a DAPM event */ | |
ce6120cc | 97 | snd_soc_dapm_sync(dapm); |
a1eb4b3c LG |
98 | } |
99 | ||
100 | static int corgi_startup(struct snd_pcm_substream *substream) | |
101 | { | |
102 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad | 103 | struct snd_soc_codec *codec = rtd->codec; |
a1eb4b3c LG |
104 | |
105 | /* check the jack status at stream startup */ | |
106 | corgi_ext_control(codec); | |
107 | return 0; | |
108 | } | |
109 | ||
110 | /* we need to unmute the HP at shutdown as the mute burns power on corgi */ | |
5220ed6b | 111 | static void corgi_shutdown(struct snd_pcm_substream *substream) |
a1eb4b3c | 112 | { |
a1eb4b3c | 113 | /* set = unmute headphone */ |
6168cda9 EM |
114 | gpio_set_value(CORGI_GPIO_MUTE_L, 1); |
115 | gpio_set_value(CORGI_GPIO_MUTE_R, 1); | |
a1eb4b3c LG |
116 | } |
117 | ||
d928b25a LG |
118 | static int corgi_hw_params(struct snd_pcm_substream *substream, |
119 | struct snd_pcm_hw_params *params) | |
120 | { | |
121 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad LG |
122 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
123 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
d928b25a LG |
124 | unsigned int clk = 0; |
125 | int ret = 0; | |
126 | ||
127 | switch (params_rate(params)) { | |
128 | case 8000: | |
129 | case 16000: | |
130 | case 48000: | |
131 | case 96000: | |
132 | clk = 12288000; | |
133 | break; | |
134 | case 11025: | |
135 | case 22050: | |
136 | case 44100: | |
137 | clk = 11289600; | |
138 | break; | |
139 | } | |
140 | ||
141 | /* set codec DAI configuration */ | |
64105cfd | 142 | ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S | |
d928b25a LG |
143 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); |
144 | if (ret < 0) | |
145 | return ret; | |
146 | ||
147 | /* set cpu DAI configuration */ | |
64105cfd | 148 | ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S | |
d928b25a LG |
149 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); |
150 | if (ret < 0) | |
151 | return ret; | |
152 | ||
153 | /* set the codec system clock for DAC and ADC */ | |
9745e824 | 154 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk, |
d928b25a LG |
155 | SND_SOC_CLOCK_IN); |
156 | if (ret < 0) | |
157 | return ret; | |
158 | ||
159 | /* set the I2S system clock as input (unused) */ | |
64105cfd | 160 | ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0, |
d928b25a LG |
161 | SND_SOC_CLOCK_IN); |
162 | if (ret < 0) | |
163 | return ret; | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
a1eb4b3c LG |
168 | static struct snd_soc_ops corgi_ops = { |
169 | .startup = corgi_startup, | |
d928b25a | 170 | .hw_params = corgi_hw_params, |
a1eb4b3c LG |
171 | .shutdown = corgi_shutdown, |
172 | }; | |
173 | ||
174 | static int corgi_get_jack(struct snd_kcontrol *kcontrol, | |
175 | struct snd_ctl_elem_value *ucontrol) | |
176 | { | |
177 | ucontrol->value.integer.value[0] = corgi_jack_func; | |
178 | return 0; | |
179 | } | |
180 | ||
181 | static int corgi_set_jack(struct snd_kcontrol *kcontrol, | |
182 | struct snd_ctl_elem_value *ucontrol) | |
183 | { | |
184 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
185 | ||
186 | if (corgi_jack_func == ucontrol->value.integer.value[0]) | |
187 | return 0; | |
188 | ||
189 | corgi_jack_func = ucontrol->value.integer.value[0]; | |
190 | corgi_ext_control(codec); | |
191 | return 1; | |
192 | } | |
193 | ||
194 | static int corgi_get_spk(struct snd_kcontrol *kcontrol, | |
195 | struct snd_ctl_elem_value *ucontrol) | |
196 | { | |
197 | ucontrol->value.integer.value[0] = corgi_spk_func; | |
198 | return 0; | |
199 | } | |
200 | ||
201 | static int corgi_set_spk(struct snd_kcontrol *kcontrol, | |
202 | struct snd_ctl_elem_value *ucontrol) | |
203 | { | |
204 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
205 | ||
206 | if (corgi_spk_func == ucontrol->value.integer.value[0]) | |
207 | return 0; | |
208 | ||
209 | corgi_spk_func = ucontrol->value.integer.value[0]; | |
210 | corgi_ext_control(codec); | |
211 | return 1; | |
212 | } | |
213 | ||
338c7ed0 JN |
214 | static int corgi_amp_event(struct snd_soc_dapm_widget *w, |
215 | struct snd_kcontrol *k, int event) | |
a1eb4b3c | 216 | { |
6168cda9 | 217 | gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event)); |
a1eb4b3c LG |
218 | return 0; |
219 | } | |
220 | ||
338c7ed0 JN |
221 | static int corgi_mic_event(struct snd_soc_dapm_widget *w, |
222 | struct snd_kcontrol *k, int event) | |
a1eb4b3c | 223 | { |
6168cda9 | 224 | gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event)); |
a1eb4b3c LG |
225 | return 0; |
226 | } | |
227 | ||
228 | /* corgi machine dapm widgets */ | |
229 | static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { | |
230 | SND_SOC_DAPM_HP("Headphone Jack", NULL), | |
231 | SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event), | |
232 | SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event), | |
233 | SND_SOC_DAPM_LINE("Line Jack", NULL), | |
234 | SND_SOC_DAPM_HP("Headset Jack", NULL), | |
235 | }; | |
236 | ||
237 | /* Corgi machine audio map (connections to the codec pins) */ | |
25191c45 | 238 | static const struct snd_soc_dapm_route audio_map[] = { |
a1eb4b3c LG |
239 | |
240 | /* headset Jack - in = micin, out = LHPOUT*/ | |
241 | {"Headset Jack", NULL, "LHPOUT"}, | |
242 | ||
243 | /* headphone connected to LHPOUT1, RHPOUT1 */ | |
244 | {"Headphone Jack", NULL, "LHPOUT"}, | |
245 | {"Headphone Jack", NULL, "RHPOUT"}, | |
246 | ||
247 | /* speaker connected to LOUT, ROUT */ | |
248 | {"Ext Spk", NULL, "ROUT"}, | |
249 | {"Ext Spk", NULL, "LOUT"}, | |
250 | ||
251 | /* mic is connected to MICIN (via right channel of headphone jack) */ | |
252 | {"MICIN", NULL, "Mic Jack"}, | |
253 | ||
254 | /* Same as the above but no mic bias for line signals */ | |
255 | {"MICIN", NULL, "Line Jack"}, | |
a1eb4b3c LG |
256 | }; |
257 | ||
258 | static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset", | |
259 | "Off"}; | |
260 | static const char *spk_function[] = {"On", "Off"}; | |
261 | static const struct soc_enum corgi_enum[] = { | |
262 | SOC_ENUM_SINGLE_EXT(5, jack_function), | |
263 | SOC_ENUM_SINGLE_EXT(2, spk_function), | |
264 | }; | |
265 | ||
266 | static const struct snd_kcontrol_new wm8731_corgi_controls[] = { | |
267 | SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack, | |
268 | corgi_set_jack), | |
269 | SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk, | |
270 | corgi_set_spk), | |
271 | }; | |
272 | ||
273 | /* | |
274 | * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device | |
275 | */ | |
f0fba2ad | 276 | static int corgi_wm8731_init(struct snd_soc_pcm_runtime *rtd) |
a1eb4b3c | 277 | { |
f0fba2ad | 278 | struct snd_soc_codec *codec = rtd->codec; |
ce6120cc | 279 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
eb5f6d75 | 280 | int err; |
a1eb4b3c | 281 | |
ce6120cc LG |
282 | snd_soc_dapm_nc_pin(dapm, "LLINEIN"); |
283 | snd_soc_dapm_nc_pin(dapm, "RLINEIN"); | |
a1eb4b3c LG |
284 | |
285 | /* Add corgi specific controls */ | |
eb5f6d75 PZ |
286 | err = snd_soc_add_controls(codec, wm8731_corgi_controls, |
287 | ARRAY_SIZE(wm8731_corgi_controls)); | |
288 | if (err < 0) | |
289 | return err; | |
a1eb4b3c LG |
290 | |
291 | /* Add corgi specific widgets */ | |
ce6120cc | 292 | snd_soc_dapm_new_controls(dapm, wm8731_dapm_widgets, |
25191c45 | 293 | ARRAY_SIZE(wm8731_dapm_widgets)); |
a1eb4b3c LG |
294 | |
295 | /* Set up corgi specific audio path audio_map */ | |
ce6120cc | 296 | snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map)); |
a1eb4b3c | 297 | |
ce6120cc | 298 | snd_soc_dapm_sync(dapm); |
a1eb4b3c LG |
299 | return 0; |
300 | } | |
301 | ||
a1eb4b3c LG |
302 | /* corgi digital audio interface glue - connects codec <--> CPU */ |
303 | static struct snd_soc_dai_link corgi_dai = { | |
304 | .name = "WM8731", | |
305 | .stream_name = "WM8731", | |
f0fba2ad LG |
306 | .cpu_dai_name = "pxa-is2-dai", |
307 | .codec_dai_name = "wm8731-hifi", | |
308 | .platform_name = "pxa-pcm-audio", | |
309 | .codec_name = "wm8731-codec-0.001a", | |
a1eb4b3c | 310 | .init = corgi_wm8731_init, |
d928b25a | 311 | .ops = &corgi_ops, |
a1eb4b3c LG |
312 | }; |
313 | ||
314 | /* corgi audio machine driver */ | |
87506549 | 315 | static struct snd_soc_card snd_soc_corgi = { |
a1eb4b3c LG |
316 | .name = "Corgi", |
317 | .dai_link = &corgi_dai, | |
318 | .num_links = 1, | |
a1eb4b3c LG |
319 | }; |
320 | ||
a1eb4b3c LG |
321 | static struct platform_device *corgi_snd_device; |
322 | ||
323 | static int __init corgi_init(void) | |
324 | { | |
325 | int ret; | |
326 | ||
1bfcd361 MB |
327 | if (!(machine_is_corgi() || machine_is_shepherd() || |
328 | machine_is_husky())) | |
a1eb4b3c LG |
329 | return -ENODEV; |
330 | ||
331 | corgi_snd_device = platform_device_alloc("soc-audio", -1); | |
332 | if (!corgi_snd_device) | |
333 | return -ENOMEM; | |
334 | ||
f0fba2ad | 335 | platform_set_drvdata(corgi_snd_device, &snd_soc_corgi); |
a1eb4b3c LG |
336 | ret = platform_device_add(corgi_snd_device); |
337 | ||
338 | if (ret) | |
339 | platform_device_put(corgi_snd_device); | |
340 | ||
341 | return ret; | |
342 | } | |
343 | ||
344 | static void __exit corgi_exit(void) | |
345 | { | |
346 | platform_device_unregister(corgi_snd_device); | |
347 | } | |
348 | ||
349 | module_init(corgi_init); | |
350 | module_exit(corgi_exit); | |
351 | ||
352 | /* Module information */ | |
353 | MODULE_AUTHOR("Richard Purdie"); | |
354 | MODULE_DESCRIPTION("ALSA SoC Corgi"); | |
355 | MODULE_LICENSE("GPL"); |