[ARM] Move include/asm-arm/arch-* to arch/arm/*/include/mach
[firewire-audio.git] / sound / soc / omap / n810.c
blob7694621ec40b7e8747a80b5f2131b926a3b8f634
1 /*
2 * n810.c -- SoC audio for Nokia N810
4 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Jarkko Nikula <jarkko.nikula@nokia.com>
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.
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.
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
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>
31 #include <asm/mach-types.h>
32 #include <mach/hardware.h>
33 #include <linux/gpio.h>
34 #include <mach/mcbsp.h>
36 #include "omap-mcbsp.h"
37 #include "omap-pcm.h"
38 #include "../codecs/tlv320aic3x.h"
40 #define N810_HEADSET_AMP_GPIO 10
41 #define N810_SPEAKER_AMP_GPIO 101
43 static struct clk *sys_clkout2;
44 static struct clk *sys_clkout2_src;
45 static struct clk *func96m_clk;
47 static int n810_spk_func;
48 static int n810_jack_func;
49 static int n810_dmic_func;
51 static void n810_ext_control(struct snd_soc_codec *codec)
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");
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");
63 if (n810_dmic_func)
64 snd_soc_dapm_enable_pin(codec, "DMic");
65 else
66 snd_soc_dapm_disable_pin(codec, "DMic");
68 snd_soc_dapm_sync(codec);
71 static int n810_startup(struct snd_pcm_substream *substream)
73 struct snd_soc_pcm_runtime *rtd = substream->private_data;
74 struct snd_soc_codec *codec = rtd->socdev->codec;
76 n810_ext_control(codec);
77 return clk_enable(sys_clkout2);
80 static void n810_shutdown(struct snd_pcm_substream *substream)
82 clk_disable(sys_clkout2);
85 static int n810_hw_params(struct snd_pcm_substream *substream,
86 struct snd_pcm_hw_params *params)
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;
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;
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;
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);
113 return err;
116 static struct snd_soc_ops n810_ops = {
117 .startup = n810_startup,
118 .hw_params = n810_hw_params,
119 .shutdown = n810_shutdown,
122 static int n810_get_spk(struct snd_kcontrol *kcontrol,
123 struct snd_ctl_elem_value *ucontrol)
125 ucontrol->value.integer.value[0] = n810_spk_func;
127 return 0;
130 static int n810_set_spk(struct snd_kcontrol *kcontrol,
131 struct snd_ctl_elem_value *ucontrol)
133 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
135 if (n810_spk_func == ucontrol->value.integer.value[0])
136 return 0;
138 n810_spk_func = ucontrol->value.integer.value[0];
139 n810_ext_control(codec);
141 return 1;
144 static int n810_get_jack(struct snd_kcontrol *kcontrol,
145 struct snd_ctl_elem_value *ucontrol)
147 ucontrol->value.integer.value[0] = n810_jack_func;
149 return 0;
152 static int n810_set_jack(struct snd_kcontrol *kcontrol,
153 struct snd_ctl_elem_value *ucontrol)
155 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
157 if (n810_jack_func == ucontrol->value.integer.value[0])
158 return 0;
160 n810_jack_func = ucontrol->value.integer.value[0];
161 n810_ext_control(codec);
163 return 1;
166 static int n810_get_input(struct snd_kcontrol *kcontrol,
167 struct snd_ctl_elem_value *ucontrol)
169 ucontrol->value.integer.value[0] = n810_dmic_func;
171 return 0;
174 static int n810_set_input(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_value *ucontrol)
177 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
179 if (n810_dmic_func == ucontrol->value.integer.value[0])
180 return 0;
182 n810_dmic_func = ucontrol->value.integer.value[0];
183 n810_ext_control(codec);
185 return 1;
188 static int n810_spk_event(struct snd_soc_dapm_widget *w,
189 struct snd_kcontrol *k, int event)
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);
196 return 0;
199 static int n810_jack_event(struct snd_soc_dapm_widget *w,
200 struct snd_kcontrol *k, int event)
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);
207 return 0;
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),
216 static const struct snd_soc_dapm_route audio_map[] = {
217 {"Headphone Jack", NULL, "HPLOUT"},
218 {"Headphone Jack", NULL, "HPROUT"},
220 {"Ext Spk", NULL, "LLOUT"},
221 {"Ext Spk", NULL, "RLOUT"},
223 {"DMic Rate 64", NULL, "Mic Bias 2V"},
224 {"Mic Bias 2V", NULL, "DMic"},
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),
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),
245 static int n810_aic33_init(struct snd_soc_codec *codec)
247 int i, err;
249 /* Not connected */
250 snd_soc_dapm_disable_pin(codec, "MONO_LOUT");
251 snd_soc_dapm_disable_pin(codec, "HPLCOM");
252 snd_soc_dapm_disable_pin(codec, "HPRCOM");
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;
262 /* Add N810 specific widgets */
263 snd_soc_dapm_new_controls(codec, aic33_dapm_widgets,
264 ARRAY_SIZE(aic33_dapm_widgets));
266 /* Set up N810 specific audio path audio_map */
267 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
269 snd_soc_dapm_sync(codec);
271 return 0;
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,
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,
291 /* Audio private data */
292 static struct aic3x_setup_data n810_aic33_setup = {
293 .i2c_address = 0x18,
294 .gpio_func[0] = AIC3X_GPIO1_FUNC_DISABLED,
295 .gpio_func[1] = AIC3X_GPIO2_FUNC_DIGITAL_MIC_INPUT,
298 /* Audio subsystem */
299 static struct snd_soc_device n810_snd_devdata = {
300 .machine = &snd_soc_machine_n810,
301 .platform = &omap_soc_platform,
302 .codec_dev = &soc_codec_dev_aic3x,
303 .codec_data = &n810_aic33_setup,
306 static struct platform_device *n810_snd_device;
308 static int __init n810_soc_init(void)
310 int err;
311 struct device *dev;
313 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
314 return -ENODEV;
316 n810_snd_device = platform_device_alloc("soc-audio", -1);
317 if (!n810_snd_device)
318 return -ENOMEM;
320 platform_set_drvdata(n810_snd_device, &n810_snd_devdata);
321 n810_snd_devdata.dev = &n810_snd_device->dev;
322 *(unsigned int *)n810_dai.cpu_dai->private_data = 1; /* McBSP2 */
323 err = platform_device_add(n810_snd_device);
324 if (err)
325 goto err1;
327 dev = &n810_snd_device->dev;
329 sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
330 if (IS_ERR(sys_clkout2_src)) {
331 dev_err(dev, "Could not get sys_clkout2_src clock\n");
332 return -ENODEV;
334 sys_clkout2 = clk_get(dev, "sys_clkout2");
335 if (IS_ERR(sys_clkout2)) {
336 dev_err(dev, "Could not get sys_clkout2\n");
337 goto err1;
340 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
341 * 96 MHz as its parent in order to get 12 MHz
343 func96m_clk = clk_get(dev, "func_96m_ck");
344 if (IS_ERR(func96m_clk)) {
345 dev_err(dev, "Could not get func 96M clock\n");
346 goto err2;
348 clk_set_parent(sys_clkout2_src, func96m_clk);
349 clk_set_rate(sys_clkout2, 12000000);
351 if (gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0)
352 BUG();
353 if (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0)
354 BUG();
355 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
356 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
358 return 0;
359 err2:
360 clk_put(sys_clkout2);
361 platform_device_del(n810_snd_device);
362 err1:
363 platform_device_put(n810_snd_device);
365 return err;
369 static void __exit n810_soc_exit(void)
371 gpio_free(N810_SPEAKER_AMP_GPIO);
372 gpio_free(N810_HEADSET_AMP_GPIO);
374 platform_device_unregister(n810_snd_device);
377 module_init(n810_soc_init);
378 module_exit(n810_soc_exit);
380 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
381 MODULE_DESCRIPTION("ALSA SoC Nokia N810");
382 MODULE_LICENSE("GPL");