Commit | Line | Data |
---|---|---|
7fb290d0 LG |
1 | /* |
2 | * spitz.c -- SoC audio for Sharp SL-Cxx00 models Spitz, Borzoi and Akita | |
3 | * | |
4 | * Copyright 2005 Wolfson Microelectronics PLC. | |
5 | * Copyright 2005 Openedhand Ltd. | |
6 | * | |
d331124d | 7 | * Authors: Liam Girdwood <lrg@slimlogic.co.uk> |
7fb290d0 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. | |
14 | * | |
7fb290d0 LG |
15 | */ |
16 | ||
17 | #include <linux/module.h> | |
18 | #include <linux/moduleparam.h> | |
19 | #include <linux/timer.h> | |
20 | #include <linux/interrupt.h> | |
21 | #include <linux/platform_device.h> | |
fff14720 | 22 | #include <linux/gpio.h> |
7fb290d0 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 | 29 | #include <mach/spitz.h> |
7fb290d0 | 30 | #include "../codecs/wm8750.h" |
97952f60 | 31 | #include "pxa2xx-i2s.h" |
7fb290d0 LG |
32 | |
33 | #define SPITZ_HP 0 | |
34 | #define SPITZ_MIC 1 | |
35 | #define SPITZ_LINE 2 | |
36 | #define SPITZ_HEADSET 3 | |
37 | #define SPITZ_HP_OFF 4 | |
38 | #define SPITZ_SPK_ON 0 | |
39 | #define SPITZ_SPK_OFF 1 | |
40 | ||
41 | /* audio clock in Hz - rounded from 12.235MHz */ | |
42 | #define SPITZ_AUDIO_CLOCK 12288000 | |
43 | ||
44 | static int spitz_jack_func; | |
45 | static int spitz_spk_func; | |
46 | ||
47 | static void spitz_ext_control(struct snd_soc_codec *codec) | |
48 | { | |
49 | if (spitz_spk_func == SPITZ_SPK_ON) | |
a5302181 | 50 | snd_soc_dapm_enable_pin(codec, "Ext Spk"); |
7fb290d0 | 51 | else |
a5302181 | 52 | snd_soc_dapm_disable_pin(codec, "Ext Spk"); |
7fb290d0 LG |
53 | |
54 | /* set up jack connection */ | |
55 | switch (spitz_jack_func) { | |
56 | case SPITZ_HP: | |
57 | /* enable and unmute hp jack, disable mic bias */ | |
a5302181 LG |
58 | snd_soc_dapm_disable_pin(codec, "Headset Jack"); |
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"); | |
fff14720 EM |
62 | gpio_set_value(SPITZ_GPIO_MUTE_L, 1); |
63 | gpio_set_value(SPITZ_GPIO_MUTE_R, 1); | |
7fb290d0 LG |
64 | break; |
65 | case SPITZ_MIC: | |
66 | /* enable mic jack and bias, mute hp */ | |
a5302181 LG |
67 | snd_soc_dapm_disable_pin(codec, "Headphone Jack"); |
68 | snd_soc_dapm_disable_pin(codec, "Headset Jack"); | |
69 | snd_soc_dapm_disable_pin(codec, "Line Jack"); | |
70 | snd_soc_dapm_enable_pin(codec, "Mic Jack"); | |
fff14720 EM |
71 | gpio_set_value(SPITZ_GPIO_MUTE_L, 0); |
72 | gpio_set_value(SPITZ_GPIO_MUTE_R, 0); | |
7fb290d0 LG |
73 | break; |
74 | case SPITZ_LINE: | |
75 | /* enable line jack, disable mic bias and mute hp */ | |
a5302181 LG |
76 | snd_soc_dapm_disable_pin(codec, "Headphone Jack"); |
77 | snd_soc_dapm_disable_pin(codec, "Headset Jack"); | |
78 | snd_soc_dapm_disable_pin(codec, "Mic Jack"); | |
79 | snd_soc_dapm_enable_pin(codec, "Line Jack"); | |
fff14720 EM |
80 | gpio_set_value(SPITZ_GPIO_MUTE_L, 0); |
81 | gpio_set_value(SPITZ_GPIO_MUTE_R, 0); | |
7fb290d0 LG |
82 | break; |
83 | case SPITZ_HEADSET: | |
84 | /* enable and unmute headset jack enable mic bias, mute L hp */ | |
a5302181 LG |
85 | snd_soc_dapm_disable_pin(codec, "Headphone Jack"); |
86 | snd_soc_dapm_enable_pin(codec, "Mic Jack"); | |
87 | snd_soc_dapm_disable_pin(codec, "Line Jack"); | |
88 | snd_soc_dapm_enable_pin(codec, "Headset Jack"); | |
fff14720 EM |
89 | gpio_set_value(SPITZ_GPIO_MUTE_L, 0); |
90 | gpio_set_value(SPITZ_GPIO_MUTE_R, 1); | |
7fb290d0 LG |
91 | break; |
92 | case SPITZ_HP_OFF: | |
93 | ||
94 | /* jack removed, everything off */ | |
a5302181 LG |
95 | snd_soc_dapm_disable_pin(codec, "Headphone Jack"); |
96 | snd_soc_dapm_disable_pin(codec, "Headset Jack"); | |
97 | snd_soc_dapm_disable_pin(codec, "Mic Jack"); | |
98 | snd_soc_dapm_disable_pin(codec, "Line Jack"); | |
fff14720 EM |
99 | gpio_set_value(SPITZ_GPIO_MUTE_L, 0); |
100 | gpio_set_value(SPITZ_GPIO_MUTE_R, 0); | |
7fb290d0 LG |
101 | break; |
102 | } | |
a5302181 | 103 | snd_soc_dapm_sync(codec); |
7fb290d0 LG |
104 | } |
105 | ||
106 | static int spitz_startup(struct snd_pcm_substream *substream) | |
107 | { | |
108 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad | 109 | struct snd_soc_codec *codec = rtd->codec; |
7fb290d0 | 110 | |
71a29560 MB |
111 | mutex_lock(&codec->mutex); |
112 | ||
7fb290d0 LG |
113 | /* check the jack status at stream startup */ |
114 | spitz_ext_control(codec); | |
71a29560 MB |
115 | |
116 | mutex_unlock(&codec->mutex); | |
117 | ||
7fb290d0 LG |
118 | return 0; |
119 | } | |
120 | ||
97952f60 LG |
121 | static int spitz_hw_params(struct snd_pcm_substream *substream, |
122 | struct snd_pcm_hw_params *params) | |
123 | { | |
124 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad LG |
125 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
126 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
97952f60 LG |
127 | unsigned int clk = 0; |
128 | int ret = 0; | |
129 | ||
130 | switch (params_rate(params)) { | |
131 | case 8000: | |
132 | case 16000: | |
133 | case 48000: | |
134 | case 96000: | |
135 | clk = 12288000; | |
136 | break; | |
137 | case 11025: | |
138 | case 22050: | |
139 | case 44100: | |
140 | clk = 11289600; | |
141 | break; | |
142 | } | |
143 | ||
144 | /* set codec DAI configuration */ | |
64105cfd | 145 | ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S | |
97952f60 LG |
146 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); |
147 | if (ret < 0) | |
148 | return ret; | |
149 | ||
150 | /* set cpu DAI configuration */ | |
64105cfd | 151 | ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S | |
97952f60 LG |
152 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); |
153 | if (ret < 0) | |
154 | return ret; | |
155 | ||
156 | /* set the codec system clock for DAC and ADC */ | |
64105cfd | 157 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk, |
97952f60 LG |
158 | SND_SOC_CLOCK_IN); |
159 | if (ret < 0) | |
160 | return ret; | |
161 | ||
162 | /* set the I2S system clock as input (unused) */ | |
64105cfd | 163 | ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0, |
97952f60 LG |
164 | SND_SOC_CLOCK_IN); |
165 | if (ret < 0) | |
166 | return ret; | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
7fb290d0 LG |
171 | static struct snd_soc_ops spitz_ops = { |
172 | .startup = spitz_startup, | |
97952f60 | 173 | .hw_params = spitz_hw_params, |
7fb290d0 LG |
174 | }; |
175 | ||
176 | static int spitz_get_jack(struct snd_kcontrol *kcontrol, | |
177 | struct snd_ctl_elem_value *ucontrol) | |
178 | { | |
179 | ucontrol->value.integer.value[0] = spitz_jack_func; | |
180 | return 0; | |
181 | } | |
182 | ||
183 | static int spitz_set_jack(struct snd_kcontrol *kcontrol, | |
184 | struct snd_ctl_elem_value *ucontrol) | |
185 | { | |
186 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
187 | ||
188 | if (spitz_jack_func == ucontrol->value.integer.value[0]) | |
189 | return 0; | |
190 | ||
191 | spitz_jack_func = ucontrol->value.integer.value[0]; | |
192 | spitz_ext_control(codec); | |
193 | return 1; | |
194 | } | |
195 | ||
196 | static int spitz_get_spk(struct snd_kcontrol *kcontrol, | |
197 | struct snd_ctl_elem_value *ucontrol) | |
198 | { | |
199 | ucontrol->value.integer.value[0] = spitz_spk_func; | |
200 | return 0; | |
201 | } | |
202 | ||
203 | static int spitz_set_spk(struct snd_kcontrol *kcontrol, | |
204 | struct snd_ctl_elem_value *ucontrol) | |
205 | { | |
206 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
207 | ||
208 | if (spitz_spk_func == ucontrol->value.integer.value[0]) | |
209 | return 0; | |
210 | ||
211 | spitz_spk_func = ucontrol->value.integer.value[0]; | |
212 | spitz_ext_control(codec); | |
213 | return 1; | |
214 | } | |
215 | ||
338c7ed0 JN |
216 | static int spitz_mic_bias(struct snd_soc_dapm_widget *w, |
217 | struct snd_kcontrol *k, int event) | |
7fb290d0 | 218 | { |
fff14720 EM |
219 | if (machine_is_borzoi() || machine_is_spitz()) |
220 | gpio_set_value(SPITZ_GPIO_MIC_BIAS, | |
221 | SND_SOC_DAPM_EVENT_ON(event)); | |
7fb290d0 | 222 | |
f72de663 EM |
223 | if (machine_is_akita()) |
224 | gpio_set_value(AKITA_GPIO_MIC_BIAS, | |
225 | SND_SOC_DAPM_EVENT_ON(event)); | |
7fb290d0 | 226 | |
7fb290d0 LG |
227 | return 0; |
228 | } | |
229 | ||
230 | /* spitz machine dapm widgets */ | |
231 | static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = { | |
232 | SND_SOC_DAPM_HP("Headphone Jack", NULL), | |
233 | SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias), | |
234 | SND_SOC_DAPM_SPK("Ext Spk", NULL), | |
235 | SND_SOC_DAPM_LINE("Line Jack", NULL), | |
236 | ||
237 | /* headset is a mic and mono headphone */ | |
238 | SND_SOC_DAPM_HP("Headset Jack", NULL), | |
239 | }; | |
240 | ||
241 | /* Spitz machine audio_map */ | |
25191c45 | 242 | static const struct snd_soc_dapm_route audio_map[] = { |
7fb290d0 LG |
243 | |
244 | /* headphone connected to LOUT1, ROUT1 */ | |
245 | {"Headphone Jack", NULL, "LOUT1"}, | |
246 | {"Headphone Jack", NULL, "ROUT1"}, | |
247 | ||
248 | /* headset connected to ROUT1 and LINPUT1 with bias (def below) */ | |
249 | {"Headset Jack", NULL, "ROUT1"}, | |
250 | ||
251 | /* ext speaker connected to LOUT2, ROUT2 */ | |
252 | {"Ext Spk", NULL , "ROUT2"}, | |
253 | {"Ext Spk", NULL , "LOUT2"}, | |
254 | ||
255 | /* mic is connected to input 1 - with bias */ | |
256 | {"LINPUT1", NULL, "Mic Bias"}, | |
257 | {"Mic Bias", NULL, "Mic Jack"}, | |
258 | ||
259 | /* line is connected to input 1 - no bias */ | |
260 | {"LINPUT1", NULL, "Line Jack"}, | |
7fb290d0 LG |
261 | }; |
262 | ||
263 | static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset", | |
264 | "Off"}; | |
265 | static const char *spk_function[] = {"On", "Off"}; | |
266 | static const struct soc_enum spitz_enum[] = { | |
267 | SOC_ENUM_SINGLE_EXT(5, jack_function), | |
268 | SOC_ENUM_SINGLE_EXT(2, spk_function), | |
269 | }; | |
270 | ||
271 | static const struct snd_kcontrol_new wm8750_spitz_controls[] = { | |
272 | SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack, | |
273 | spitz_set_jack), | |
274 | SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk, | |
275 | spitz_set_spk), | |
276 | }; | |
277 | ||
278 | /* | |
279 | * Logic for a wm8750 as connected on a Sharp SL-Cxx00 Device | |
280 | */ | |
f0fba2ad | 281 | static int spitz_wm8750_init(struct snd_soc_pcm_runtime *rtd) |
7fb290d0 | 282 | { |
f0fba2ad | 283 | struct snd_soc_codec *codec = rtd->codec; |
eb5f6d75 | 284 | int err; |
7fb290d0 LG |
285 | |
286 | /* NC codec pins */ | |
35f5e54d MB |
287 | snd_soc_dapm_nc_pin(codec, "RINPUT1"); |
288 | snd_soc_dapm_nc_pin(codec, "LINPUT2"); | |
289 | snd_soc_dapm_nc_pin(codec, "RINPUT2"); | |
290 | snd_soc_dapm_nc_pin(codec, "LINPUT3"); | |
291 | snd_soc_dapm_nc_pin(codec, "RINPUT3"); | |
292 | snd_soc_dapm_nc_pin(codec, "OUT3"); | |
293 | snd_soc_dapm_nc_pin(codec, "MONO1"); | |
7fb290d0 LG |
294 | |
295 | /* Add spitz specific controls */ | |
eb5f6d75 PZ |
296 | err = snd_soc_add_controls(codec, wm8750_spitz_controls, |
297 | ARRAY_SIZE(wm8750_spitz_controls)); | |
298 | if (err < 0) | |
299 | return err; | |
7fb290d0 LG |
300 | |
301 | /* Add spitz specific widgets */ | |
25191c45 MB |
302 | snd_soc_dapm_new_controls(codec, wm8750_dapm_widgets, |
303 | ARRAY_SIZE(wm8750_dapm_widgets)); | |
7fb290d0 | 304 | |
25191c45 MB |
305 | /* Set up spitz specific audio paths */ |
306 | snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); | |
7fb290d0 | 307 | |
a5302181 | 308 | snd_soc_dapm_sync(codec); |
7fb290d0 LG |
309 | return 0; |
310 | } | |
311 | ||
7fb290d0 LG |
312 | /* spitz digital audio interface glue - connects codec <--> CPU */ |
313 | static struct snd_soc_dai_link spitz_dai = { | |
314 | .name = "wm8750", | |
315 | .stream_name = "WM8750", | |
f0fba2ad LG |
316 | .cpu_dai_name = "pxa-is2", |
317 | .codec_dai_name = "wm8750-hifi", | |
318 | .platform_name = "pxa-pcm-audio", | |
319 | .codec_name = "wm8750-codec.0-001a", | |
7fb290d0 | 320 | .init = spitz_wm8750_init, |
97952f60 | 321 | .ops = &spitz_ops, |
7fb290d0 LG |
322 | }; |
323 | ||
324 | /* spitz audio machine driver */ | |
87506549 | 325 | static struct snd_soc_card snd_soc_spitz = { |
7fb290d0 LG |
326 | .name = "Spitz", |
327 | .dai_link = &spitz_dai, | |
328 | .num_links = 1, | |
7fb290d0 LG |
329 | }; |
330 | ||
7fb290d0 LG |
331 | static struct platform_device *spitz_snd_device; |
332 | ||
333 | static int __init spitz_init(void) | |
334 | { | |
335 | int ret; | |
336 | ||
337 | if (!(machine_is_spitz() || machine_is_borzoi() || machine_is_akita())) | |
338 | return -ENODEV; | |
339 | ||
340 | spitz_snd_device = platform_device_alloc("soc-audio", -1); | |
341 | if (!spitz_snd_device) | |
342 | return -ENOMEM; | |
343 | ||
f0fba2ad | 344 | platform_set_drvdata(spitz_snd_device, &snd_soc_spitz); |
7fb290d0 LG |
345 | ret = platform_device_add(spitz_snd_device); |
346 | ||
347 | if (ret) | |
348 | platform_device_put(spitz_snd_device); | |
349 | ||
350 | return ret; | |
351 | } | |
352 | ||
353 | static void __exit spitz_exit(void) | |
354 | { | |
355 | platform_device_unregister(spitz_snd_device); | |
356 | } | |
357 | ||
358 | module_init(spitz_init); | |
359 | module_exit(spitz_exit); | |
360 | ||
361 | MODULE_AUTHOR("Richard Purdie"); | |
362 | MODULE_DESCRIPTION("ALSA SoC Spitz"); | |
363 | MODULE_LICENSE("GPL"); |