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