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