pandora: defconfig: update
[pandora-kernel.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 <jarkko.nikula@bitmer.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
31 #include <asm/mach-types.h>
32 #include <mach/hardware.h>
33 #include <linux/gpio.h>
34 #include <linux/module.h>
35 #include <plat/mcbsp.h>
36
37 #include "omap-mcbsp.h"
38 #include "omap-pcm.h"
39
40 #define N810_HEADSET_AMP_GPIO   10
41 #define N810_SPEAKER_AMP_GPIO   101
42
43 enum {
44         N810_JACK_DISABLED,
45         N810_JACK_HP,
46         N810_JACK_HS,
47         N810_JACK_MIC,
48 };
49
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;
56 static int n810_dmic_func;
57
58 static void n810_ext_control(struct snd_soc_codec *codec)
59 {
60         struct snd_soc_dapm_context *dapm = &codec->dapm;
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
74         if (n810_spk_func)
75                 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
76         else
77                 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
78
79         if (hp)
80                 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
81         else
82                 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
83         if (line1l)
84                 snd_soc_dapm_enable_pin(dapm, "LINE1L");
85         else
86                 snd_soc_dapm_disable_pin(dapm, "LINE1L");
87
88         if (n810_dmic_func)
89                 snd_soc_dapm_enable_pin(dapm, "DMic");
90         else
91                 snd_soc_dapm_disable_pin(dapm, "DMic");
92
93         snd_soc_dapm_sync(dapm);
94 }
95
96 static int n810_startup(struct snd_pcm_substream *substream)
97 {
98         struct snd_pcm_runtime *runtime = substream->runtime;
99         struct snd_soc_pcm_runtime *rtd = substream->private_data;
100         struct snd_soc_codec *codec = rtd->codec;
101
102         snd_pcm_hw_constraint_minmax(runtime,
103                                      SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
104
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;
118         struct snd_soc_dai *codec_dai = rtd->codec_dai;
119         int err;
120
121         /* Set the codec system clock for DAC and ADC */
122         err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
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
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
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))
204                 gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
205         else
206                 gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
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))
215                 gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
216         else
217                 gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
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),
225         SND_SOC_DAPM_MIC("DMic", NULL),
226 };
227
228 static const struct snd_soc_dapm_route audio_map[] = {
229         {"Headphone Jack", NULL, "HPLOUT"},
230         {"Headphone Jack", NULL, "HPROUT"},
231
232         {"Ext Spk", NULL, "LLOUT"},
233         {"Ext Spk", NULL, "RLOUT"},
234
235         {"DMic Rate 64", NULL, "Mic Bias 2V"},
236         {"Mic Bias 2V", NULL, "DMic"},
237 };
238
239 static const char *spk_function[] = {"Off", "On"};
240 static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
241 static const char *input_function[] = {"ADC", "Digital Mic"};
242 static const struct soc_enum n810_enum[] = {
243         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
244         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
245         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
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),
253         SOC_ENUM_EXT("Input Select",  n810_enum[2],
254                      n810_get_input, n810_set_input),
255 };
256
257 static int n810_aic33_init(struct snd_soc_pcm_runtime *rtd)
258 {
259         struct snd_soc_codec *codec = rtd->codec;
260         struct snd_soc_dapm_context *dapm = &codec->dapm;
261
262         /* Not connected */
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");
271
272         return 0;
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",
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",
283         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
284                    SND_SOC_DAIFMT_CBM_CFM,
285         .init = n810_aic33_init,
286         .ops = &n810_ops,
287 };
288
289 /* Audio machine driver */
290 static struct snd_soc_card snd_soc_n810 = {
291         .name = "N810",
292         .dai_link = &n810_dai,
293         .num_links = 1,
294
295         .controls = aic33_n810_controls,
296         .num_controls = ARRAY_SIZE(aic33_n810_controls),
297         .dapm_widgets = aic33_dapm_widgets,
298         .num_dapm_widgets = ARRAY_SIZE(aic33_dapm_widgets),
299         .dapm_routes = audio_map,
300         .num_dapm_routes = ARRAY_SIZE(audio_map),
301 };
302
303 static struct platform_device *n810_snd_device;
304
305 static int __init n810_soc_init(void)
306 {
307         int err;
308         struct device *dev;
309
310         if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
311                 return -ENODEV;
312
313         n810_snd_device = platform_device_alloc("soc-audio", -1);
314         if (!n810_snd_device)
315                 return -ENOMEM;
316
317         platform_set_drvdata(n810_snd_device, &snd_soc_n810);
318         err = platform_device_add(n810_snd_device);
319         if (err)
320                 goto err1;
321
322         dev = &n810_snd_device->dev;
323
324         sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
325         if (IS_ERR(sys_clkout2_src)) {
326                 dev_err(dev, "Could not get sys_clkout2_src clock\n");
327                 err = PTR_ERR(sys_clkout2_src);
328                 goto err2;
329         }
330         sys_clkout2 = clk_get(dev, "sys_clkout2");
331         if (IS_ERR(sys_clkout2)) {
332                 dev_err(dev, "Could not get sys_clkout2\n");
333                 err = PTR_ERR(sys_clkout2);
334                 goto err3;
335         }
336         /*
337          * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
338          * 96 MHz as its parent in order to get 12 MHz
339          */
340         func96m_clk = clk_get(dev, "func_96m_ck");
341         if (IS_ERR(func96m_clk)) {
342                 dev_err(dev, "Could not get func 96M clock\n");
343                 err = PTR_ERR(func96m_clk);
344                 goto err4;
345         }
346         clk_set_parent(sys_clkout2_src, func96m_clk);
347         clk_set_rate(sys_clkout2, 12000000);
348
349         BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) ||
350                (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0));
351
352         gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
353         gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
354
355         return 0;
356 err4:
357         clk_put(sys_clkout2);
358 err3:
359         clk_put(sys_clkout2_src);
360 err2:
361         platform_device_del(n810_snd_device);
362 err1:
363         platform_device_put(n810_snd_device);
364
365         return err;
366 }
367
368 static void __exit n810_soc_exit(void)
369 {
370         gpio_free(N810_SPEAKER_AMP_GPIO);
371         gpio_free(N810_HEADSET_AMP_GPIO);
372         clk_put(sys_clkout2_src);
373         clk_put(sys_clkout2);
374         clk_put(func96m_clk);
375
376         platform_device_unregister(n810_snd_device);
377 }
378
379 module_init(n810_soc_init);
380 module_exit(n810_soc_exit);
381
382 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
383 MODULE_DESCRIPTION("ALSA SoC Nokia N810");
384 MODULE_LICENSE("GPL");