Commit | Line | Data |
---|---|---|
2e74796a JN |
1 | /* |
2 | * n810.c -- SoC audio for Nokia N810 | |
3 | * | |
4 | * Copyright (C) 2008 Nokia Corporation | |
5 | * | |
b08f7a62 | 6 | * Contact: Jarkko Nikula <jhnikula@gmail.com> |
2e74796a JN |
7 | * |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
20 | * 02110-1301 USA | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <linux/clk.h> | |
aa6b904e | 25 | #include <linux/i2c.h> |
2e74796a JN |
26 | #include <linux/platform_device.h> |
27 | #include <sound/core.h> | |
28 | #include <sound/pcm.h> | |
29 | #include <sound/soc.h> | |
30 | #include <sound/soc-dapm.h> | |
31 | ||
32 | #include <asm/mach-types.h> | |
a09e64fb | 33 | #include <mach/hardware.h> |
f99a633a | 34 | #include <linux/gpio.h> |
ce491cf8 | 35 | #include <plat/mcbsp.h> |
2e74796a JN |
36 | |
37 | #include "omap-mcbsp.h" | |
38 | #include "omap-pcm.h" | |
39 | #include "../codecs/tlv320aic3x.h" | |
40 | ||
f99a633a JN |
41 | #define N810_HEADSET_AMP_GPIO 10 |
42 | #define N810_SPEAKER_AMP_GPIO 101 | |
2e74796a | 43 | |
f8d5fc92 JN |
44 | enum { |
45 | N810_JACK_DISABLED, | |
46 | N810_JACK_HP, | |
47 | N810_JACK_HS, | |
48 | N810_JACK_MIC, | |
49 | }; | |
50 | ||
2e74796a JN |
51 | static struct clk *sys_clkout2; |
52 | static struct clk *sys_clkout2_src; | |
53 | static struct clk *func96m_clk; | |
54 | ||
55 | static int n810_spk_func; | |
56 | static int n810_jack_func; | |
90b9e476 | 57 | static int n810_dmic_func; |
2e74796a JN |
58 | |
59 | static void n810_ext_control(struct snd_soc_codec *codec) | |
60 | { | |
ce6120cc | 61 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
f8d5fc92 JN |
62 | int hp = 0, line1l = 0; |
63 | ||
64 | switch (n810_jack_func) { | |
65 | case N810_JACK_HS: | |
66 | line1l = 1; | |
67 | case N810_JACK_HP: | |
68 | hp = 1; | |
69 | break; | |
70 | case N810_JACK_MIC: | |
71 | line1l = 1; | |
72 | break; | |
73 | } | |
74 | ||
a5302181 | 75 | if (n810_spk_func) |
ce6120cc | 76 | snd_soc_dapm_enable_pin(dapm, "Ext Spk"); |
a5302181 | 77 | else |
ce6120cc | 78 | snd_soc_dapm_disable_pin(dapm, "Ext Spk"); |
a5302181 | 79 | |
f8d5fc92 | 80 | if (hp) |
ce6120cc | 81 | snd_soc_dapm_enable_pin(dapm, "Headphone Jack"); |
a5302181 | 82 | else |
ce6120cc | 83 | snd_soc_dapm_disable_pin(dapm, "Headphone Jack"); |
f8d5fc92 | 84 | if (line1l) |
ce6120cc | 85 | snd_soc_dapm_enable_pin(dapm, "LINE1L"); |
f8d5fc92 | 86 | else |
ce6120cc | 87 | snd_soc_dapm_disable_pin(dapm, "LINE1L"); |
a5302181 LG |
88 | |
89 | if (n810_dmic_func) | |
ce6120cc | 90 | snd_soc_dapm_enable_pin(dapm, "DMic"); |
a5302181 | 91 | else |
ce6120cc | 92 | snd_soc_dapm_disable_pin(dapm, "DMic"); |
2e74796a | 93 | |
ce6120cc | 94 | snd_soc_dapm_sync(dapm); |
2e74796a JN |
95 | } |
96 | ||
97 | static int n810_startup(struct snd_pcm_substream *substream) | |
98 | { | |
0be43050 | 99 | struct snd_pcm_runtime *runtime = substream->runtime; |
2e74796a | 100 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
f0fba2ad | 101 | struct snd_soc_codec *codec = rtd->codec; |
2e74796a | 102 | |
0be43050 JN |
103 | snd_pcm_hw_constraint_minmax(runtime, |
104 | SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2); | |
105 | ||
2e74796a JN |
106 | n810_ext_control(codec); |
107 | return clk_enable(sys_clkout2); | |
108 | } | |
109 | ||
110 | static void n810_shutdown(struct snd_pcm_substream *substream) | |
111 | { | |
112 | clk_disable(sys_clkout2); | |
113 | } | |
114 | ||
115 | static int n810_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; | |
2e74796a JN |
121 | int err; |
122 | ||
123 | /* Set codec DAI configuration */ | |
64105cfd | 124 | err = snd_soc_dai_set_fmt(codec_dai, |
2e74796a JN |
125 | SND_SOC_DAIFMT_I2S | |
126 | SND_SOC_DAIFMT_NB_NF | | |
127 | SND_SOC_DAIFMT_CBM_CFM); | |
128 | if (err < 0) | |
129 | return err; | |
130 | ||
131 | /* Set cpu DAI configuration */ | |
64105cfd | 132 | err = snd_soc_dai_set_fmt(cpu_dai, |
2e74796a JN |
133 | SND_SOC_DAIFMT_I2S | |
134 | SND_SOC_DAIFMT_NB_NF | | |
135 | SND_SOC_DAIFMT_CBM_CFM); | |
136 | if (err < 0) | |
137 | return err; | |
138 | ||
139 | /* Set the codec system clock for DAC and ADC */ | |
64105cfd | 140 | err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000, |
2e74796a JN |
141 | SND_SOC_CLOCK_IN); |
142 | ||
143 | return err; | |
144 | } | |
145 | ||
146 | static struct snd_soc_ops n810_ops = { | |
147 | .startup = n810_startup, | |
148 | .hw_params = n810_hw_params, | |
149 | .shutdown = n810_shutdown, | |
150 | }; | |
151 | ||
152 | static int n810_get_spk(struct snd_kcontrol *kcontrol, | |
153 | struct snd_ctl_elem_value *ucontrol) | |
154 | { | |
155 | ucontrol->value.integer.value[0] = n810_spk_func; | |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
160 | static int n810_set_spk(struct snd_kcontrol *kcontrol, | |
161 | struct snd_ctl_elem_value *ucontrol) | |
162 | { | |
163 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
164 | ||
165 | if (n810_spk_func == ucontrol->value.integer.value[0]) | |
166 | return 0; | |
167 | ||
168 | n810_spk_func = ucontrol->value.integer.value[0]; | |
169 | n810_ext_control(codec); | |
170 | ||
171 | return 1; | |
172 | } | |
173 | ||
174 | static int n810_get_jack(struct snd_kcontrol *kcontrol, | |
175 | struct snd_ctl_elem_value *ucontrol) | |
176 | { | |
177 | ucontrol->value.integer.value[0] = n810_jack_func; | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
182 | static int n810_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 (n810_jack_func == ucontrol->value.integer.value[0]) | |
188 | return 0; | |
189 | ||
190 | n810_jack_func = ucontrol->value.integer.value[0]; | |
191 | n810_ext_control(codec); | |
192 | ||
193 | return 1; | |
194 | } | |
195 | ||
90b9e476 JN |
196 | static int n810_get_input(struct snd_kcontrol *kcontrol, |
197 | struct snd_ctl_elem_value *ucontrol) | |
198 | { | |
199 | ucontrol->value.integer.value[0] = n810_dmic_func; | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
204 | static int n810_set_input(struct snd_kcontrol *kcontrol, | |
205 | struct snd_ctl_elem_value *ucontrol) | |
206 | { | |
207 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
208 | ||
209 | if (n810_dmic_func == ucontrol->value.integer.value[0]) | |
210 | return 0; | |
211 | ||
212 | n810_dmic_func = ucontrol->value.integer.value[0]; | |
213 | n810_ext_control(codec); | |
214 | ||
215 | return 1; | |
216 | } | |
217 | ||
2e74796a JN |
218 | static int n810_spk_event(struct snd_soc_dapm_widget *w, |
219 | struct snd_kcontrol *k, int event) | |
220 | { | |
221 | if (SND_SOC_DAPM_EVENT_ON(event)) | |
f99a633a | 222 | gpio_set_value(N810_SPEAKER_AMP_GPIO, 1); |
2e74796a | 223 | else |
f99a633a | 224 | gpio_set_value(N810_SPEAKER_AMP_GPIO, 0); |
2e74796a JN |
225 | |
226 | return 0; | |
227 | } | |
228 | ||
229 | static int n810_jack_event(struct snd_soc_dapm_widget *w, | |
230 | struct snd_kcontrol *k, int event) | |
231 | { | |
232 | if (SND_SOC_DAPM_EVENT_ON(event)) | |
f99a633a | 233 | gpio_set_value(N810_HEADSET_AMP_GPIO, 1); |
2e74796a | 234 | else |
f99a633a | 235 | gpio_set_value(N810_HEADSET_AMP_GPIO, 0); |
2e74796a JN |
236 | |
237 | return 0; | |
238 | } | |
239 | ||
240 | static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = { | |
241 | SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event), | |
242 | SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event), | |
90b9e476 | 243 | SND_SOC_DAPM_MIC("DMic", NULL), |
2e74796a JN |
244 | }; |
245 | ||
1a250598 | 246 | static const struct snd_soc_dapm_route audio_map[] = { |
2e74796a JN |
247 | {"Headphone Jack", NULL, "HPLOUT"}, |
248 | {"Headphone Jack", NULL, "HPROUT"}, | |
249 | ||
250 | {"Ext Spk", NULL, "LLOUT"}, | |
251 | {"Ext Spk", NULL, "RLOUT"}, | |
90b9e476 JN |
252 | |
253 | {"DMic Rate 64", NULL, "Mic Bias 2V"}, | |
254 | {"Mic Bias 2V", NULL, "DMic"}, | |
2e74796a JN |
255 | }; |
256 | ||
257 | static const char *spk_function[] = {"Off", "On"}; | |
f8d5fc92 | 258 | static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"}; |
90b9e476 | 259 | static const char *input_function[] = {"ADC", "Digital Mic"}; |
2e74796a | 260 | static const struct soc_enum n810_enum[] = { |
3c172791 JN |
261 | SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function), |
262 | SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function), | |
90b9e476 | 263 | SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function), |
2e74796a JN |
264 | }; |
265 | ||
266 | static const struct snd_kcontrol_new aic33_n810_controls[] = { | |
267 | SOC_ENUM_EXT("Speaker Function", n810_enum[0], | |
268 | n810_get_spk, n810_set_spk), | |
269 | SOC_ENUM_EXT("Jack Function", n810_enum[1], | |
270 | n810_get_jack, n810_set_jack), | |
90b9e476 JN |
271 | SOC_ENUM_EXT("Input Select", n810_enum[2], |
272 | n810_get_input, n810_set_input), | |
2e74796a JN |
273 | }; |
274 | ||
f0fba2ad | 275 | static int n810_aic33_init(struct snd_soc_pcm_runtime *rtd) |
2e74796a | 276 | { |
f0fba2ad | 277 | struct snd_soc_codec *codec = rtd->codec; |
ce6120cc | 278 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
eb5f6d75 | 279 | int err; |
2e74796a JN |
280 | |
281 | /* Not connected */ | |
ce6120cc LG |
282 | snd_soc_dapm_nc_pin(dapm, "MONO_LOUT"); |
283 | snd_soc_dapm_nc_pin(dapm, "HPLCOM"); | |
284 | snd_soc_dapm_nc_pin(dapm, "HPRCOM"); | |
285 | snd_soc_dapm_nc_pin(dapm, "MIC3L"); | |
286 | snd_soc_dapm_nc_pin(dapm, "MIC3R"); | |
287 | snd_soc_dapm_nc_pin(dapm, "LINE1R"); | |
288 | snd_soc_dapm_nc_pin(dapm, "LINE2L"); | |
289 | snd_soc_dapm_nc_pin(dapm, "LINE2R"); | |
2e74796a JN |
290 | |
291 | /* Add N810 specific controls */ | |
eb5f6d75 PZ |
292 | err = snd_soc_add_controls(codec, aic33_n810_controls, |
293 | ARRAY_SIZE(aic33_n810_controls)); | |
294 | if (err < 0) | |
295 | return err; | |
2e74796a JN |
296 | |
297 | /* Add N810 specific widgets */ | |
ce6120cc | 298 | snd_soc_dapm_new_controls(dapm, aic33_dapm_widgets, |
1a250598 | 299 | ARRAY_SIZE(aic33_dapm_widgets)); |
2e74796a JN |
300 | |
301 | /* Set up N810 specific audio path audio_map */ | |
ce6120cc | 302 | snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map)); |
2e74796a | 303 | |
ce6120cc | 304 | snd_soc_dapm_sync(dapm); |
2e74796a JN |
305 | |
306 | return 0; | |
307 | } | |
308 | ||
309 | /* Digital audio interface glue - connects codec <--> CPU */ | |
310 | static struct snd_soc_dai_link n810_dai = { | |
311 | .name = "TLV320AIC33", | |
312 | .stream_name = "AIC33", | |
f0fba2ad LG |
313 | .cpu_dai_name = "omap-mcbsp-dai.1", |
314 | .platform_name = "omap-pcm-audio", | |
315 | .codec_name = "tlv320aic3x-codec.2-0018", | |
316 | .codec_dai_name = "tlv320aic3x-hifi", | |
2e74796a JN |
317 | .init = n810_aic33_init, |
318 | .ops = &n810_ops, | |
319 | }; | |
320 | ||
321 | /* Audio machine driver */ | |
87506549 | 322 | static struct snd_soc_card snd_soc_n810 = { |
2e74796a JN |
323 | .name = "N810", |
324 | .dai_link = &n810_dai, | |
325 | .num_links = 1, | |
326 | }; | |
327 | ||
2e74796a JN |
328 | static struct platform_device *n810_snd_device; |
329 | ||
330 | static int __init n810_soc_init(void) | |
331 | { | |
332 | int err; | |
333 | struct device *dev; | |
334 | ||
6e132fa6 | 335 | if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax())) |
2e74796a JN |
336 | return -ENODEV; |
337 | ||
338 | n810_snd_device = platform_device_alloc("soc-audio", -1); | |
339 | if (!n810_snd_device) | |
340 | return -ENOMEM; | |
341 | ||
f0fba2ad | 342 | platform_set_drvdata(n810_snd_device, &snd_soc_n810); |
2e74796a JN |
343 | err = platform_device_add(n810_snd_device); |
344 | if (err) | |
345 | goto err1; | |
346 | ||
347 | dev = &n810_snd_device->dev; | |
348 | ||
349 | sys_clkout2_src = clk_get(dev, "sys_clkout2_src"); | |
350 | if (IS_ERR(sys_clkout2_src)) { | |
351 | dev_err(dev, "Could not get sys_clkout2_src clock\n"); | |
e784539f JN |
352 | err = PTR_ERR(sys_clkout2_src); |
353 | goto err2; | |
2e74796a JN |
354 | } |
355 | sys_clkout2 = clk_get(dev, "sys_clkout2"); | |
356 | if (IS_ERR(sys_clkout2)) { | |
357 | dev_err(dev, "Could not get sys_clkout2\n"); | |
e784539f JN |
358 | err = PTR_ERR(sys_clkout2); |
359 | goto err3; | |
2e74796a JN |
360 | } |
361 | /* | |
362 | * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use | |
363 | * 96 MHz as its parent in order to get 12 MHz | |
364 | */ | |
365 | func96m_clk = clk_get(dev, "func_96m_ck"); | |
366 | if (IS_ERR(func96m_clk)) { | |
367 | dev_err(dev, "Could not get func 96M clock\n"); | |
e784539f JN |
368 | err = PTR_ERR(func96m_clk); |
369 | goto err4; | |
2e74796a JN |
370 | } |
371 | clk_set_parent(sys_clkout2_src, func96m_clk); | |
372 | clk_set_rate(sys_clkout2, 12000000); | |
373 | ||
f4c1724f AB |
374 | BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) || |
375 | (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0)); | |
376 | ||
f99a633a JN |
377 | gpio_direction_output(N810_HEADSET_AMP_GPIO, 0); |
378 | gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0); | |
2e74796a JN |
379 | |
380 | return 0; | |
e784539f | 381 | err4: |
2e74796a | 382 | clk_put(sys_clkout2); |
e784539f JN |
383 | err3: |
384 | clk_put(sys_clkout2_src); | |
385 | err2: | |
2e74796a JN |
386 | platform_device_del(n810_snd_device); |
387 | err1: | |
388 | platform_device_put(n810_snd_device); | |
389 | ||
390 | return err; | |
2e74796a JN |
391 | } |
392 | ||
393 | static void __exit n810_soc_exit(void) | |
394 | { | |
f99a633a JN |
395 | gpio_free(N810_SPEAKER_AMP_GPIO); |
396 | gpio_free(N810_HEADSET_AMP_GPIO); | |
e784539f JN |
397 | clk_put(sys_clkout2_src); |
398 | clk_put(sys_clkout2); | |
399 | clk_put(func96m_clk); | |
f99a633a | 400 | |
2e74796a JN |
401 | platform_device_unregister(n810_snd_device); |
402 | } | |
403 | ||
404 | module_init(n810_soc_init); | |
405 | module_exit(n810_soc_exit); | |
406 | ||
b08f7a62 | 407 | MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>"); |
2e74796a JN |
408 | MODULE_DESCRIPTION("ALSA SoC Nokia N810"); |
409 | MODULE_LICENSE("GPL"); |