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