ASoC: i.MX: we can do mono
[deliverable/linux.git] / sound / soc / omap / n810.c
CommitLineData
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
44enum {
45 N810_JACK_DISABLED,
46 N810_JACK_HP,
47 N810_JACK_HS,
48 N810_JACK_MIC,
49};
50
2e74796a
JN
51static struct clk *sys_clkout2;
52static struct clk *sys_clkout2_src;
53static struct clk *func96m_clk;
54
55static int n810_spk_func;
56static int n810_jack_func;
90b9e476 57static int n810_dmic_func;
2e74796a
JN
58
59static void n810_ext_control(struct snd_soc_codec *codec)
60{
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
LG
74 if (n810_spk_func)
75 snd_soc_dapm_enable_pin(codec, "Ext Spk");
76 else
77 snd_soc_dapm_disable_pin(codec, "Ext Spk");
78
f8d5fc92 79 if (hp)
a5302181
LG
80 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
81 else
82 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
f8d5fc92
JN
83 if (line1l)
84 snd_soc_dapm_enable_pin(codec, "LINE1L");
85 else
86 snd_soc_dapm_disable_pin(codec, "LINE1L");
a5302181
LG
87
88 if (n810_dmic_func)
89 snd_soc_dapm_enable_pin(codec, "DMic");
90 else
ac8615ba 91 snd_soc_dapm_disable_pin(codec, "DMic");
2e74796a 92
a5302181 93 snd_soc_dapm_sync(codec);
2e74796a
JN
94}
95
96static 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
109static void n810_shutdown(struct snd_pcm_substream *substream)
110{
111 clk_disable(sys_clkout2);
112}
113
114static 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
LG
118 struct snd_soc_dai *codec_dai = rtd->codec_dai;
119 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e74796a
JN
120 int err;
121
122 /* Set codec DAI configuration */
64105cfd 123 err = snd_soc_dai_set_fmt(codec_dai,
2e74796a
JN
124 SND_SOC_DAIFMT_I2S |
125 SND_SOC_DAIFMT_NB_NF |
126 SND_SOC_DAIFMT_CBM_CFM);
127 if (err < 0)
128 return err;
129
130 /* Set cpu DAI configuration */
64105cfd 131 err = snd_soc_dai_set_fmt(cpu_dai,
2e74796a
JN
132 SND_SOC_DAIFMT_I2S |
133 SND_SOC_DAIFMT_NB_NF |
134 SND_SOC_DAIFMT_CBM_CFM);
135 if (err < 0)
136 return err;
137
138 /* Set the codec system clock for DAC and ADC */
64105cfd 139 err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
2e74796a
JN
140 SND_SOC_CLOCK_IN);
141
142 return err;
143}
144
145static struct snd_soc_ops n810_ops = {
146 .startup = n810_startup,
147 .hw_params = n810_hw_params,
148 .shutdown = n810_shutdown,
149};
150
151static int n810_get_spk(struct snd_kcontrol *kcontrol,
152 struct snd_ctl_elem_value *ucontrol)
153{
154 ucontrol->value.integer.value[0] = n810_spk_func;
155
156 return 0;
157}
158
159static int n810_set_spk(struct snd_kcontrol *kcontrol,
160 struct snd_ctl_elem_value *ucontrol)
161{
162 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
163
164 if (n810_spk_func == ucontrol->value.integer.value[0])
165 return 0;
166
167 n810_spk_func = ucontrol->value.integer.value[0];
168 n810_ext_control(codec);
169
170 return 1;
171}
172
173static int n810_get_jack(struct snd_kcontrol *kcontrol,
174 struct snd_ctl_elem_value *ucontrol)
175{
176 ucontrol->value.integer.value[0] = n810_jack_func;
177
178 return 0;
179}
180
181static int n810_set_jack(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_value *ucontrol)
183{
184 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
185
186 if (n810_jack_func == ucontrol->value.integer.value[0])
187 return 0;
188
189 n810_jack_func = ucontrol->value.integer.value[0];
190 n810_ext_control(codec);
191
192 return 1;
193}
194
90b9e476
JN
195static int n810_get_input(struct snd_kcontrol *kcontrol,
196 struct snd_ctl_elem_value *ucontrol)
197{
198 ucontrol->value.integer.value[0] = n810_dmic_func;
199
200 return 0;
201}
202
203static int n810_set_input(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 (n810_dmic_func == ucontrol->value.integer.value[0])
209 return 0;
210
211 n810_dmic_func = ucontrol->value.integer.value[0];
212 n810_ext_control(codec);
213
214 return 1;
215}
216
2e74796a
JN
217static int n810_spk_event(struct snd_soc_dapm_widget *w,
218 struct snd_kcontrol *k, int event)
219{
220 if (SND_SOC_DAPM_EVENT_ON(event))
f99a633a 221 gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
2e74796a 222 else
f99a633a 223 gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
2e74796a
JN
224
225 return 0;
226}
227
228static int n810_jack_event(struct snd_soc_dapm_widget *w,
229 struct snd_kcontrol *k, int event)
230{
231 if (SND_SOC_DAPM_EVENT_ON(event))
f99a633a 232 gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
2e74796a 233 else
f99a633a 234 gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
2e74796a
JN
235
236 return 0;
237}
238
239static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
240 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
241 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
90b9e476 242 SND_SOC_DAPM_MIC("DMic", NULL),
2e74796a
JN
243};
244
1a250598 245static const struct snd_soc_dapm_route audio_map[] = {
2e74796a
JN
246 {"Headphone Jack", NULL, "HPLOUT"},
247 {"Headphone Jack", NULL, "HPROUT"},
248
249 {"Ext Spk", NULL, "LLOUT"},
250 {"Ext Spk", NULL, "RLOUT"},
90b9e476
JN
251
252 {"DMic Rate 64", NULL, "Mic Bias 2V"},
253 {"Mic Bias 2V", NULL, "DMic"},
2e74796a
JN
254};
255
256static const char *spk_function[] = {"Off", "On"};
f8d5fc92 257static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
90b9e476 258static const char *input_function[] = {"ADC", "Digital Mic"};
2e74796a 259static const struct soc_enum n810_enum[] = {
3c172791
JN
260 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
261 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
90b9e476 262 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
2e74796a
JN
263};
264
265static const struct snd_kcontrol_new aic33_n810_controls[] = {
266 SOC_ENUM_EXT("Speaker Function", n810_enum[0],
267 n810_get_spk, n810_set_spk),
268 SOC_ENUM_EXT("Jack Function", n810_enum[1],
269 n810_get_jack, n810_set_jack),
90b9e476
JN
270 SOC_ENUM_EXT("Input Select", n810_enum[2],
271 n810_get_input, n810_set_input),
2e74796a
JN
272};
273
f0fba2ad 274static int n810_aic33_init(struct snd_soc_pcm_runtime *rtd)
2e74796a 275{
f0fba2ad 276 struct snd_soc_codec *codec = rtd->codec;
eb5f6d75 277 int err;
2e74796a
JN
278
279 /* Not connected */
869fbb36
MB
280 snd_soc_dapm_nc_pin(codec, "MONO_LOUT");
281 snd_soc_dapm_nc_pin(codec, "HPLCOM");
282 snd_soc_dapm_nc_pin(codec, "HPRCOM");
13b9d2ab
JN
283 snd_soc_dapm_nc_pin(codec, "MIC3L");
284 snd_soc_dapm_nc_pin(codec, "MIC3R");
285 snd_soc_dapm_nc_pin(codec, "LINE1R");
286 snd_soc_dapm_nc_pin(codec, "LINE2L");
287 snd_soc_dapm_nc_pin(codec, "LINE2R");
2e74796a
JN
288
289 /* Add N810 specific controls */
eb5f6d75
PZ
290 err = snd_soc_add_controls(codec, aic33_n810_controls,
291 ARRAY_SIZE(aic33_n810_controls));
292 if (err < 0)
293 return err;
2e74796a
JN
294
295 /* Add N810 specific widgets */
1a250598
MB
296 snd_soc_dapm_new_controls(codec, aic33_dapm_widgets,
297 ARRAY_SIZE(aic33_dapm_widgets));
2e74796a
JN
298
299 /* Set up N810 specific audio path audio_map */
1a250598 300 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
2e74796a 301
a5302181 302 snd_soc_dapm_sync(codec);
2e74796a
JN
303
304 return 0;
305}
306
307/* Digital audio interface glue - connects codec <--> CPU */
308static struct snd_soc_dai_link n810_dai = {
309 .name = "TLV320AIC33",
310 .stream_name = "AIC33",
f0fba2ad
LG
311 .cpu_dai_name = "omap-mcbsp-dai.1",
312 .platform_name = "omap-pcm-audio",
313 .codec_name = "tlv320aic3x-codec.2-0018",
314 .codec_dai_name = "tlv320aic3x-hifi",
2e74796a
JN
315 .init = n810_aic33_init,
316 .ops = &n810_ops,
317};
318
319/* Audio machine driver */
87506549 320static struct snd_soc_card snd_soc_n810 = {
2e74796a
JN
321 .name = "N810",
322 .dai_link = &n810_dai,
323 .num_links = 1,
324};
325
2e74796a
JN
326static struct platform_device *n810_snd_device;
327
328static int __init n810_soc_init(void)
329{
330 int err;
331 struct device *dev;
332
6e132fa6 333 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
2e74796a
JN
334 return -ENODEV;
335
336 n810_snd_device = platform_device_alloc("soc-audio", -1);
337 if (!n810_snd_device)
338 return -ENOMEM;
339
f0fba2ad 340 platform_set_drvdata(n810_snd_device, &snd_soc_n810);
2e74796a
JN
341 err = platform_device_add(n810_snd_device);
342 if (err)
343 goto err1;
344
345 dev = &n810_snd_device->dev;
346
347 sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
348 if (IS_ERR(sys_clkout2_src)) {
349 dev_err(dev, "Could not get sys_clkout2_src clock\n");
e784539f
JN
350 err = PTR_ERR(sys_clkout2_src);
351 goto err2;
2e74796a
JN
352 }
353 sys_clkout2 = clk_get(dev, "sys_clkout2");
354 if (IS_ERR(sys_clkout2)) {
355 dev_err(dev, "Could not get sys_clkout2\n");
e784539f
JN
356 err = PTR_ERR(sys_clkout2);
357 goto err3;
2e74796a
JN
358 }
359 /*
360 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
361 * 96 MHz as its parent in order to get 12 MHz
362 */
363 func96m_clk = clk_get(dev, "func_96m_ck");
364 if (IS_ERR(func96m_clk)) {
365 dev_err(dev, "Could not get func 96M clock\n");
e784539f
JN
366 err = PTR_ERR(func96m_clk);
367 goto err4;
2e74796a
JN
368 }
369 clk_set_parent(sys_clkout2_src, func96m_clk);
370 clk_set_rate(sys_clkout2, 12000000);
371
f4c1724f
AB
372 BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) ||
373 (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0));
374
f99a633a
JN
375 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
376 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
2e74796a
JN
377
378 return 0;
e784539f 379err4:
2e74796a 380 clk_put(sys_clkout2);
e784539f
JN
381err3:
382 clk_put(sys_clkout2_src);
383err2:
2e74796a
JN
384 platform_device_del(n810_snd_device);
385err1:
386 platform_device_put(n810_snd_device);
387
388 return err;
2e74796a
JN
389}
390
391static void __exit n810_soc_exit(void)
392{
f99a633a
JN
393 gpio_free(N810_SPEAKER_AMP_GPIO);
394 gpio_free(N810_HEADSET_AMP_GPIO);
e784539f
JN
395 clk_put(sys_clkout2_src);
396 clk_put(sys_clkout2);
397 clk_put(func96m_clk);
f99a633a 398
2e74796a
JN
399 platform_device_unregister(n810_snd_device);
400}
401
402module_init(n810_soc_init);
403module_exit(n810_soc_exit);
404
b08f7a62 405MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
2e74796a
JN
406MODULE_DESCRIPTION("ALSA SoC Nokia N810");
407MODULE_LICENSE("GPL");
This page took 0.180343 seconds and 5 git commands to generate.