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
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"
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)
54 snd_soc_dapm_enable_pin(codec, "Ext Spk");
56 snd_soc_dapm_disable_pin(codec, "Ext Spk");
59 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
61 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
64 snd_soc_dapm_enable_pin(codec, "DMic");
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;
93 /* Set codec DAI configuration */
94 err = snd_soc_dai_set_fmt(codec_dai,
96 SND_SOC_DAIFMT_NB_NF |
97 SND_SOC_DAIFMT_CBM_CFM);
101 /* Set cpu DAI configuration */
102 err = snd_soc_dai_set_fmt(cpu_dai,
104 SND_SOC_DAIFMT_NB_NF |
105 SND_SOC_DAIFMT_CBM_CFM);
109 /* Set the codec system clock for DAC and ADC */
110 err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
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;
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])
138 n810_spk_func = ucontrol->value.integer.value[0];
139 n810_ext_control(codec);
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;
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])
160 n810_jack_func = ucontrol->value.integer.value[0];
161 n810_ext_control(codec);
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;
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])
182 n810_dmic_func = ucontrol->value.integer.value[0];
183 n810_ext_control(codec);
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);
194 gpio_set_value(N810_SPEAKER_AMP_GPIO, 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);
205 gpio_set_value(N810_HEADSET_AMP_GPIO, 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)
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));
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);
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,
284 /* Audio machine driver */
285 static struct snd_soc_machine snd_soc_machine_n810 = {
287 .dai_link = &n810_dai,
291 /* Audio private data */
292 static struct aic3x_setup_data n810_aic33_setup = {
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)
313 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
316 n810_snd_device = platform_device_alloc("soc-audio", -1);
317 if (!n810_snd_device)
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);
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 err = PTR_ERR(sys_clkout2_src);
335 sys_clkout2 = clk_get(dev, "sys_clkout2");
336 if (IS_ERR(sys_clkout2)) {
337 dev_err(dev, "Could not get sys_clkout2\n");
338 err = PTR_ERR(sys_clkout2);
342 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
343 * 96 MHz as its parent in order to get 12 MHz
345 func96m_clk = clk_get(dev, "func_96m_ck");
346 if (IS_ERR(func96m_clk)) {
347 dev_err(dev, "Could not get func 96M clock\n");
348 err = PTR_ERR(func96m_clk);
351 clk_set_parent(sys_clkout2_src, func96m_clk);
352 clk_set_rate(sys_clkout2, 12000000);
354 if (gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0)
356 if (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0)
358 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
359 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
363 clk_put(sys_clkout2);
365 clk_put(sys_clkout2_src);
367 platform_device_del(n810_snd_device);
369 platform_device_put(n810_snd_device);
374 static void __exit n810_soc_exit(void)
376 gpio_free(N810_SPEAKER_AMP_GPIO);
377 gpio_free(N810_HEADSET_AMP_GPIO);
378 clk_put(sys_clkout2_src);
379 clk_put(sys_clkout2);
380 clk_put(func96m_clk);
382 platform_device_unregister(n810_snd_device);
385 module_init(n810_soc_init);
386 module_exit(n810_soc_exit);
388 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
389 MODULE_DESCRIPTION("ALSA SoC Nokia N810");
390 MODULE_LICENSE("GPL");