]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
a1eb4b3c LG |
2 | /* |
3 | * corgi.c -- SoC audio for Corgi | |
4 | * | |
5 | * Copyright 2005 Wolfson Microelectronics PLC. | |
6 | * Copyright 2005 Openedhand Ltd. | |
7 | * | |
d331124d | 8 | * Authors: Liam Girdwood <lrg@slimlogic.co.uk> |
a1eb4b3c | 9 | * Richard Purdie <richard@openedhand.com> |
a1eb4b3c LG |
10 | */ |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/moduleparam.h> | |
14 | #include <linux/timer.h> | |
fc996757 | 15 | #include <linux/i2c.h> |
a1eb4b3c LG |
16 | #include <linux/interrupt.h> |
17 | #include <linux/platform_device.h> | |
6168cda9 | 18 | #include <linux/gpio.h> |
a1eb4b3c LG |
19 | #include <sound/core.h> |
20 | #include <sound/pcm.h> | |
21 | #include <sound/soc.h> | |
a1eb4b3c LG |
22 | |
23 | #include <asm/mach-types.h> | |
a09e64fb RK |
24 | #include <mach/corgi.h> |
25 | #include <mach/audio.h> | |
a1eb4b3c LG |
26 | |
27 | #include "../codecs/wm8731.h" | |
d928b25a | 28 | #include "pxa2xx-i2s.h" |
a1eb4b3c LG |
29 | |
30 | #define CORGI_HP 0 | |
31 | #define CORGI_MIC 1 | |
32 | #define CORGI_LINE 2 | |
33 | #define CORGI_HEADSET 3 | |
34 | #define CORGI_HP_OFF 4 | |
35 | #define CORGI_SPK_ON 0 | |
36 | #define CORGI_SPK_OFF 1 | |
37 | ||
38 | /* audio clock in Hz - rounded from 12.235MHz */ | |
39 | #define CORGI_AUDIO_CLOCK 12288000 | |
40 | ||
41 | static int corgi_jack_func; | |
42 | static int corgi_spk_func; | |
43 | ||
022658be | 44 | static void corgi_ext_control(struct snd_soc_dapm_context *dapm) |
a1eb4b3c | 45 | { |
4d9e7348 CK |
46 | snd_soc_dapm_mutex_lock(dapm); |
47 | ||
a1eb4b3c LG |
48 | /* set up jack connection */ |
49 | switch (corgi_jack_func) { | |
50 | case CORGI_HP: | |
a1eb4b3c | 51 | /* set = unmute headphone */ |
6168cda9 EM |
52 | gpio_set_value(CORGI_GPIO_MUTE_L, 1); |
53 | gpio_set_value(CORGI_GPIO_MUTE_R, 1); | |
4d9e7348 CK |
54 | snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack"); |
55 | snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack"); | |
56 | snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack"); | |
57 | snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack"); | |
a1eb4b3c LG |
58 | break; |
59 | case CORGI_MIC: | |
a1eb4b3c | 60 | /* reset = mute headphone */ |
6168cda9 EM |
61 | gpio_set_value(CORGI_GPIO_MUTE_L, 0); |
62 | gpio_set_value(CORGI_GPIO_MUTE_R, 0); | |
4d9e7348 CK |
63 | snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack"); |
64 | snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack"); | |
65 | snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack"); | |
66 | snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack"); | |
a1eb4b3c LG |
67 | break; |
68 | case CORGI_LINE: | |
6168cda9 EM |
69 | gpio_set_value(CORGI_GPIO_MUTE_L, 0); |
70 | gpio_set_value(CORGI_GPIO_MUTE_R, 0); | |
4d9e7348 CK |
71 | snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack"); |
72 | snd_soc_dapm_enable_pin_unlocked(dapm, "Line Jack"); | |
73 | snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack"); | |
74 | snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack"); | |
a1eb4b3c LG |
75 | break; |
76 | case CORGI_HEADSET: | |
6168cda9 EM |
77 | gpio_set_value(CORGI_GPIO_MUTE_L, 0); |
78 | gpio_set_value(CORGI_GPIO_MUTE_R, 1); | |
4d9e7348 CK |
79 | snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack"); |
80 | snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack"); | |
81 | snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack"); | |
82 | snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack"); | |
a1eb4b3c LG |
83 | break; |
84 | } | |
85 | ||
86 | if (corgi_spk_func == CORGI_SPK_ON) | |
4d9e7348 | 87 | snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk"); |
a5302181 | 88 | else |
4d9e7348 | 89 | snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk"); |
a1eb4b3c LG |
90 | |
91 | /* signal a DAPM event */ | |
4d9e7348 CK |
92 | snd_soc_dapm_sync_unlocked(dapm); |
93 | ||
94 | snd_soc_dapm_mutex_unlock(dapm); | |
a1eb4b3c LG |
95 | } |
96 | ||
97 | static int corgi_startup(struct snd_pcm_substream *substream) | |
98 | { | |
99 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
71a29560 | 100 | |
a1eb4b3c | 101 | /* check the jack status at stream startup */ |
e14de47a | 102 | corgi_ext_control(&rtd->card->dapm); |
71a29560 | 103 | |
a1eb4b3c LG |
104 | return 0; |
105 | } | |
106 | ||
107 | /* we need to unmute the HP at shutdown as the mute burns power on corgi */ | |
5220ed6b | 108 | static void corgi_shutdown(struct snd_pcm_substream *substream) |
a1eb4b3c | 109 | { |
a1eb4b3c | 110 | /* set = unmute headphone */ |
6168cda9 EM |
111 | gpio_set_value(CORGI_GPIO_MUTE_L, 1); |
112 | gpio_set_value(CORGI_GPIO_MUTE_R, 1); | |
a1eb4b3c LG |
113 | } |
114 | ||
d928b25a LG |
115 | static int corgi_hw_params(struct snd_pcm_substream *substream, |
116 | struct snd_pcm_hw_params *params) | |
117 | { | |
118 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad LG |
119 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
120 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
d928b25a LG |
121 | unsigned int clk = 0; |
122 | int ret = 0; | |
123 | ||
124 | switch (params_rate(params)) { | |
125 | case 8000: | |
126 | case 16000: | |
127 | case 48000: | |
128 | case 96000: | |
129 | clk = 12288000; | |
130 | break; | |
131 | case 11025: | |
132 | case 22050: | |
133 | case 44100: | |
134 | clk = 11289600; | |
135 | break; | |
136 | } | |
137 | ||
d928b25a | 138 | /* set the codec system clock for DAC and ADC */ |
9745e824 | 139 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk, |
d928b25a LG |
140 | SND_SOC_CLOCK_IN); |
141 | if (ret < 0) | |
142 | return ret; | |
143 | ||
144 | /* set the I2S system clock as input (unused) */ | |
64105cfd | 145 | ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0, |
d928b25a LG |
146 | SND_SOC_CLOCK_IN); |
147 | if (ret < 0) | |
148 | return ret; | |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
943b7311 | 153 | static const struct snd_soc_ops corgi_ops = { |
a1eb4b3c | 154 | .startup = corgi_startup, |
d928b25a | 155 | .hw_params = corgi_hw_params, |
a1eb4b3c LG |
156 | .shutdown = corgi_shutdown, |
157 | }; | |
158 | ||
159 | static int corgi_get_jack(struct snd_kcontrol *kcontrol, | |
160 | struct snd_ctl_elem_value *ucontrol) | |
161 | { | |
1457ad0e | 162 | ucontrol->value.enumerated.item[0] = corgi_jack_func; |
a1eb4b3c LG |
163 | return 0; |
164 | } | |
165 | ||
166 | static int corgi_set_jack(struct snd_kcontrol *kcontrol, | |
167 | struct snd_ctl_elem_value *ucontrol) | |
168 | { | |
022658be | 169 | struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); |
a1eb4b3c | 170 | |
1457ad0e | 171 | if (corgi_jack_func == ucontrol->value.enumerated.item[0]) |
a1eb4b3c LG |
172 | return 0; |
173 | ||
1457ad0e | 174 | corgi_jack_func = ucontrol->value.enumerated.item[0]; |
022658be | 175 | corgi_ext_control(&card->dapm); |
a1eb4b3c LG |
176 | return 1; |
177 | } | |
178 | ||
179 | static int corgi_get_spk(struct snd_kcontrol *kcontrol, | |
180 | struct snd_ctl_elem_value *ucontrol) | |
181 | { | |
1457ad0e | 182 | ucontrol->value.enumerated.item[0] = corgi_spk_func; |
a1eb4b3c LG |
183 | return 0; |
184 | } | |
185 | ||
186 | static int corgi_set_spk(struct snd_kcontrol *kcontrol, | |
187 | struct snd_ctl_elem_value *ucontrol) | |
188 | { | |
022658be | 189 | struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); |
a1eb4b3c | 190 | |
1457ad0e | 191 | if (corgi_spk_func == ucontrol->value.enumerated.item[0]) |
a1eb4b3c LG |
192 | return 0; |
193 | ||
1457ad0e | 194 | corgi_spk_func = ucontrol->value.enumerated.item[0]; |
022658be | 195 | corgi_ext_control(&card->dapm); |
a1eb4b3c LG |
196 | return 1; |
197 | } | |
198 | ||
338c7ed0 JN |
199 | static int corgi_amp_event(struct snd_soc_dapm_widget *w, |
200 | struct snd_kcontrol *k, int event) | |
a1eb4b3c | 201 | { |
6168cda9 | 202 | gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event)); |
a1eb4b3c LG |
203 | return 0; |
204 | } | |
205 | ||
338c7ed0 JN |
206 | static int corgi_mic_event(struct snd_soc_dapm_widget *w, |
207 | struct snd_kcontrol *k, int event) | |
a1eb4b3c | 208 | { |
6168cda9 | 209 | gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event)); |
a1eb4b3c LG |
210 | return 0; |
211 | } | |
212 | ||
213 | /* corgi machine dapm widgets */ | |
214 | static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { | |
215 | SND_SOC_DAPM_HP("Headphone Jack", NULL), | |
216 | SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event), | |
217 | SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event), | |
218 | SND_SOC_DAPM_LINE("Line Jack", NULL), | |
219 | SND_SOC_DAPM_HP("Headset Jack", NULL), | |
220 | }; | |
221 | ||
222 | /* Corgi machine audio map (connections to the codec pins) */ | |
32696af1 | 223 | static const struct snd_soc_dapm_route corgi_audio_map[] = { |
a1eb4b3c LG |
224 | |
225 | /* headset Jack - in = micin, out = LHPOUT*/ | |
226 | {"Headset Jack", NULL, "LHPOUT"}, | |
227 | ||
228 | /* headphone connected to LHPOUT1, RHPOUT1 */ | |
229 | {"Headphone Jack", NULL, "LHPOUT"}, | |
230 | {"Headphone Jack", NULL, "RHPOUT"}, | |
231 | ||
232 | /* speaker connected to LOUT, ROUT */ | |
233 | {"Ext Spk", NULL, "ROUT"}, | |
234 | {"Ext Spk", NULL, "LOUT"}, | |
235 | ||
236 | /* mic is connected to MICIN (via right channel of headphone jack) */ | |
237 | {"MICIN", NULL, "Mic Jack"}, | |
238 | ||
239 | /* Same as the above but no mic bias for line signals */ | |
240 | {"MICIN", NULL, "Line Jack"}, | |
a1eb4b3c LG |
241 | }; |
242 | ||
92be5810 LPC |
243 | static const char * const jack_function[] = {"Headphone", "Mic", "Line", |
244 | "Headset", "Off"}; | |
245 | static const char * const spk_function[] = {"On", "Off"}; | |
a1eb4b3c LG |
246 | static const struct soc_enum corgi_enum[] = { |
247 | SOC_ENUM_SINGLE_EXT(5, jack_function), | |
248 | SOC_ENUM_SINGLE_EXT(2, spk_function), | |
249 | }; | |
250 | ||
251 | static const struct snd_kcontrol_new wm8731_corgi_controls[] = { | |
252 | SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack, | |
253 | corgi_set_jack), | |
254 | SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk, | |
255 | corgi_set_spk), | |
256 | }; | |
257 | ||
a1eb4b3c LG |
258 | /* corgi digital audio interface glue - connects codec <--> CPU */ |
259 | static struct snd_soc_dai_link corgi_dai = { | |
260 | .name = "WM8731", | |
261 | .stream_name = "WM8731", | |
a3adfa00 | 262 | .cpu_dai_name = "pxa2xx-i2s", |
f0fba2ad LG |
263 | .codec_dai_name = "wm8731-hifi", |
264 | .platform_name = "pxa-pcm-audio", | |
99b59f3c | 265 | .codec_name = "wm8731.0-001b", |
74f4dd56 AL |
266 | .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
267 | SND_SOC_DAIFMT_CBS_CFS, | |
d928b25a | 268 | .ops = &corgi_ops, |
a1eb4b3c LG |
269 | }; |
270 | ||
271 | /* corgi audio machine driver */ | |
34be9244 | 272 | static struct snd_soc_card corgi = { |
a1eb4b3c | 273 | .name = "Corgi", |
561c6a17 | 274 | .owner = THIS_MODULE, |
a1eb4b3c LG |
275 | .dai_link = &corgi_dai, |
276 | .num_links = 1, | |
32696af1 AL |
277 | |
278 | .controls = wm8731_corgi_controls, | |
279 | .num_controls = ARRAY_SIZE(wm8731_corgi_controls), | |
280 | .dapm_widgets = wm8731_dapm_widgets, | |
281 | .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets), | |
282 | .dapm_routes = corgi_audio_map, | |
283 | .num_dapm_routes = ARRAY_SIZE(corgi_audio_map), | |
46d0d8df | 284 | .fully_routed = true, |
a1eb4b3c LG |
285 | }; |
286 | ||
570f6fe1 | 287 | static int corgi_probe(struct platform_device *pdev) |
a1eb4b3c | 288 | { |
34be9244 | 289 | struct snd_soc_card *card = &corgi; |
a1eb4b3c LG |
290 | int ret; |
291 | ||
34be9244 | 292 | card->dev = &pdev->dev; |
a1eb4b3c | 293 | |
2fd7076a | 294 | ret = devm_snd_soc_register_card(&pdev->dev, card); |
a1eb4b3c | 295 | if (ret) |
34be9244 AL |
296 | dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", |
297 | ret); | |
a1eb4b3c LG |
298 | return ret; |
299 | } | |
300 | ||
34be9244 AL |
301 | static struct platform_driver corgi_driver = { |
302 | .driver = { | |
303 | .name = "corgi-audio", | |
7db1698f | 304 | .pm = &snd_soc_pm_ops, |
34be9244 AL |
305 | }, |
306 | .probe = corgi_probe, | |
34be9244 AL |
307 | }; |
308 | ||
309 | module_platform_driver(corgi_driver); | |
a1eb4b3c LG |
310 | |
311 | /* Module information */ | |
312 | MODULE_AUTHOR("Richard Purdie"); | |
313 | MODULE_DESCRIPTION("ALSA SoC Corgi"); | |
314 | MODULE_LICENSE("GPL"); | |
34be9244 | 315 | MODULE_ALIAS("platform:corgi-audio"); |