ASoC: Decouple DAPM from CODECs
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / omap / n810.c
1 /*
2 * n810.c -- SoC audio for Nokia N810
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
6 * Contact: Jarkko Nikula <jhnikula@gmail.com>
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>
25 #include <linux/i2c.h>
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>
33 #include <mach/hardware.h>
34 #include <linux/gpio.h>
35 #include <plat/mcbsp.h>
36
37 #include "omap-mcbsp.h"
38 #include "omap-pcm.h"
39 #include "../codecs/tlv320aic3x.h"
40
41 #define N810_HEADSET_AMP_GPIO 10
42 #define N810_SPEAKER_AMP_GPIO 101
43
44 enum {
45 N810_JACK_DISABLED,
46 N810_JACK_HP,
47 N810_JACK_HS,
48 N810_JACK_MIC,
49 };
50
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;
57 static int n810_dmic_func;
58
59 static void n810_ext_control(struct snd_soc_codec *codec)
60 {
61 struct snd_soc_dapm_context *dapm = &codec->dapm;
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
75 if (n810_spk_func)
76 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
77 else
78 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
79
80 if (hp)
81 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
82 else
83 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
84 if (line1l)
85 snd_soc_dapm_enable_pin(dapm, "LINE1L");
86 else
87 snd_soc_dapm_disable_pin(dapm, "LINE1L");
88
89 if (n810_dmic_func)
90 snd_soc_dapm_enable_pin(dapm, "DMic");
91 else
92 snd_soc_dapm_disable_pin(dapm, "DMic");
93
94 snd_soc_dapm_sync(dapm);
95 }
96
97 static int n810_startup(struct snd_pcm_substream *substream)
98 {
99 struct snd_pcm_runtime *runtime = substream->runtime;
100 struct snd_soc_pcm_runtime *rtd = substream->private_data;
101 struct snd_soc_codec *codec = rtd->codec;
102
103 snd_pcm_hw_constraint_minmax(runtime,
104 SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
105
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;
119 struct snd_soc_dai *codec_dai = rtd->codec_dai;
120 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
121 int err;
122
123 /* Set codec DAI configuration */
124 err = snd_soc_dai_set_fmt(codec_dai,
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 */
132 err = snd_soc_dai_set_fmt(cpu_dai,
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 */
140 err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
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
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
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))
222 gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
223 else
224 gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
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))
233 gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
234 else
235 gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
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),
243 SND_SOC_DAPM_MIC("DMic", NULL),
244 };
245
246 static const struct snd_soc_dapm_route audio_map[] = {
247 {"Headphone Jack", NULL, "HPLOUT"},
248 {"Headphone Jack", NULL, "HPROUT"},
249
250 {"Ext Spk", NULL, "LLOUT"},
251 {"Ext Spk", NULL, "RLOUT"},
252
253 {"DMic Rate 64", NULL, "Mic Bias 2V"},
254 {"Mic Bias 2V", NULL, "DMic"},
255 };
256
257 static const char *spk_function[] = {"Off", "On"};
258 static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
259 static const char *input_function[] = {"ADC", "Digital Mic"};
260 static const struct soc_enum n810_enum[] = {
261 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
262 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
263 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
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),
271 SOC_ENUM_EXT("Input Select", n810_enum[2],
272 n810_get_input, n810_set_input),
273 };
274
275 static int n810_aic33_init(struct snd_soc_pcm_runtime *rtd)
276 {
277 struct snd_soc_codec *codec = rtd->codec;
278 struct snd_soc_dapm_context *dapm = &codec->dapm;
279 int err;
280
281 /* Not connected */
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");
290
291 /* Add N810 specific controls */
292 err = snd_soc_add_controls(codec, aic33_n810_controls,
293 ARRAY_SIZE(aic33_n810_controls));
294 if (err < 0)
295 return err;
296
297 /* Add N810 specific widgets */
298 snd_soc_dapm_new_controls(dapm, aic33_dapm_widgets,
299 ARRAY_SIZE(aic33_dapm_widgets));
300
301 /* Set up N810 specific audio path audio_map */
302 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
303
304 snd_soc_dapm_sync(dapm);
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",
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",
317 .init = n810_aic33_init,
318 .ops = &n810_ops,
319 };
320
321 /* Audio machine driver */
322 static struct snd_soc_card snd_soc_n810 = {
323 .name = "N810",
324 .dai_link = &n810_dai,
325 .num_links = 1,
326 };
327
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
335 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
336 return -ENODEV;
337
338 n810_snd_device = platform_device_alloc("soc-audio", -1);
339 if (!n810_snd_device)
340 return -ENOMEM;
341
342 platform_set_drvdata(n810_snd_device, &snd_soc_n810);
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");
352 err = PTR_ERR(sys_clkout2_src);
353 goto err2;
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");
358 err = PTR_ERR(sys_clkout2);
359 goto err3;
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");
368 err = PTR_ERR(func96m_clk);
369 goto err4;
370 }
371 clk_set_parent(sys_clkout2_src, func96m_clk);
372 clk_set_rate(sys_clkout2, 12000000);
373
374 BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) ||
375 (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0));
376
377 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
378 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
379
380 return 0;
381 err4:
382 clk_put(sys_clkout2);
383 err3:
384 clk_put(sys_clkout2_src);
385 err2:
386 platform_device_del(n810_snd_device);
387 err1:
388 platform_device_put(n810_snd_device);
389
390 return err;
391 }
392
393 static void __exit n810_soc_exit(void)
394 {
395 gpio_free(N810_SPEAKER_AMP_GPIO);
396 gpio_free(N810_HEADSET_AMP_GPIO);
397 clk_put(sys_clkout2_src);
398 clk_put(sys_clkout2);
399 clk_put(func96m_clk);
400
401 platform_device_unregister(n810_snd_device);
402 }
403
404 module_init(n810_soc_init);
405 module_exit(n810_soc_exit);
406
407 MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
408 MODULE_DESCRIPTION("ALSA SoC Nokia N810");
409 MODULE_LICENSE("GPL");