2 * n810.c -- SoC audio for Nokia N810
4 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Jarkko Nikula <jhnikula@gmail.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
50 static struct clk *sys_clkout2;
51 static struct clk *sys_clkout2_src;
52 static struct clk *func96m_clk;
54 static int n810_spk_func;
55 static int n810_jack_func;
56 static int n810_dmic_func;
58 static void n810_ext_control(struct snd_soc_codec *codec)
60 int hp = 0, line1l = 0;
62 switch (n810_jack_func) {
74 snd_soc_dapm_enable_pin(codec, "Ext Spk");
76 snd_soc_dapm_disable_pin(codec, "Ext Spk");
79 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
81 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
83 snd_soc_dapm_enable_pin(codec, "LINE1L");
85 snd_soc_dapm_disable_pin(codec, "LINE1L");
88 snd_soc_dapm_enable_pin(codec, "DMic");
90 snd_soc_dapm_disable_pin(codec, "DMic");
92 snd_soc_dapm_sync(codec);
95 static int n810_startup(struct snd_pcm_substream *substream)
97 struct snd_pcm_runtime *runtime = substream->runtime;
98 struct snd_soc_pcm_runtime *rtd = substream->private_data;
99 struct snd_soc_codec *codec = rtd->socdev->card->codec;
101 snd_pcm_hw_constraint_minmax(runtime,
102 SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
104 n810_ext_control(codec);
105 return clk_enable(sys_clkout2);
108 static void n810_shutdown(struct snd_pcm_substream *substream)
110 clk_disable(sys_clkout2);
113 static int n810_hw_params(struct snd_pcm_substream *substream,
114 struct snd_pcm_hw_params *params)
116 struct snd_soc_pcm_runtime *rtd = substream->private_data;
117 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
118 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
121 /* Set codec DAI configuration */
122 err = snd_soc_dai_set_fmt(codec_dai,
124 SND_SOC_DAIFMT_NB_NF |
125 SND_SOC_DAIFMT_CBM_CFM);
129 /* Set cpu DAI configuration */
130 err = snd_soc_dai_set_fmt(cpu_dai,
132 SND_SOC_DAIFMT_NB_NF |
133 SND_SOC_DAIFMT_CBM_CFM);
137 /* Set the codec system clock for DAC and ADC */
138 err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
144 static struct snd_soc_ops n810_ops = {
145 .startup = n810_startup,
146 .hw_params = n810_hw_params,
147 .shutdown = n810_shutdown,
150 static int n810_get_spk(struct snd_kcontrol *kcontrol,
151 struct snd_ctl_elem_value *ucontrol)
153 ucontrol->value.integer.value[0] = n810_spk_func;
158 static int n810_set_spk(struct snd_kcontrol *kcontrol,
159 struct snd_ctl_elem_value *ucontrol)
161 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
163 if (n810_spk_func == ucontrol->value.integer.value[0])
166 n810_spk_func = ucontrol->value.integer.value[0];
167 n810_ext_control(codec);
172 static int n810_get_jack(struct snd_kcontrol *kcontrol,
173 struct snd_ctl_elem_value *ucontrol)
175 ucontrol->value.integer.value[0] = n810_jack_func;
180 static int n810_set_jack(struct snd_kcontrol *kcontrol,
181 struct snd_ctl_elem_value *ucontrol)
183 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
185 if (n810_jack_func == ucontrol->value.integer.value[0])
188 n810_jack_func = ucontrol->value.integer.value[0];
189 n810_ext_control(codec);
194 static int n810_get_input(struct snd_kcontrol *kcontrol,
195 struct snd_ctl_elem_value *ucontrol)
197 ucontrol->value.integer.value[0] = n810_dmic_func;
202 static int n810_set_input(struct snd_kcontrol *kcontrol,
203 struct snd_ctl_elem_value *ucontrol)
205 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
207 if (n810_dmic_func == ucontrol->value.integer.value[0])
210 n810_dmic_func = ucontrol->value.integer.value[0];
211 n810_ext_control(codec);
216 static int n810_spk_event(struct snd_soc_dapm_widget *w,
217 struct snd_kcontrol *k, int event)
219 if (SND_SOC_DAPM_EVENT_ON(event))
220 gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
222 gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
227 static int n810_jack_event(struct snd_soc_dapm_widget *w,
228 struct snd_kcontrol *k, int event)
230 if (SND_SOC_DAPM_EVENT_ON(event))
231 gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
233 gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
238 static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
239 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
240 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
241 SND_SOC_DAPM_MIC("DMic", NULL),
244 static const struct snd_soc_dapm_route audio_map[] = {
245 {"Headphone Jack", NULL, "HPLOUT"},
246 {"Headphone Jack", NULL, "HPROUT"},
248 {"Ext Spk", NULL, "LLOUT"},
249 {"Ext Spk", NULL, "RLOUT"},
251 {"DMic Rate 64", NULL, "Mic Bias 2V"},
252 {"Mic Bias 2V", NULL, "DMic"},
255 static const char *spk_function[] = {"Off", "On"};
256 static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
257 static const char *input_function[] = {"ADC", "Digital Mic"};
258 static const struct soc_enum n810_enum[] = {
259 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
260 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
261 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
264 static const struct snd_kcontrol_new aic33_n810_controls[] = {
265 SOC_ENUM_EXT("Speaker Function", n810_enum[0],
266 n810_get_spk, n810_set_spk),
267 SOC_ENUM_EXT("Jack Function", n810_enum[1],
268 n810_get_jack, n810_set_jack),
269 SOC_ENUM_EXT("Input Select", n810_enum[2],
270 n810_get_input, n810_set_input),
273 static int n810_aic33_init(struct snd_soc_codec *codec)
278 snd_soc_dapm_nc_pin(codec, "MONO_LOUT");
279 snd_soc_dapm_nc_pin(codec, "HPLCOM");
280 snd_soc_dapm_nc_pin(codec, "HPRCOM");
281 snd_soc_dapm_nc_pin(codec, "MIC3L");
282 snd_soc_dapm_nc_pin(codec, "MIC3R");
283 snd_soc_dapm_nc_pin(codec, "LINE1R");
284 snd_soc_dapm_nc_pin(codec, "LINE2L");
285 snd_soc_dapm_nc_pin(codec, "LINE2R");
287 /* Add N810 specific controls */
288 err = snd_soc_add_controls(codec, aic33_n810_controls,
289 ARRAY_SIZE(aic33_n810_controls));
293 /* Add N810 specific widgets */
294 snd_soc_dapm_new_controls(codec, aic33_dapm_widgets,
295 ARRAY_SIZE(aic33_dapm_widgets));
297 /* Set up N810 specific audio path audio_map */
298 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
300 snd_soc_dapm_sync(codec);
305 /* Digital audio interface glue - connects codec <--> CPU */
306 static struct snd_soc_dai_link n810_dai = {
307 .name = "TLV320AIC33",
308 .stream_name = "AIC33",
309 .cpu_dai = &omap_mcbsp_dai[0],
310 .codec_dai = &aic3x_dai,
311 .init = n810_aic33_init,
315 /* Audio machine driver */
316 static struct snd_soc_card snd_soc_n810 = {
318 .platform = &omap_soc_platform,
319 .dai_link = &n810_dai,
323 /* Audio private data */
324 static struct aic3x_setup_data n810_aic33_setup = {
327 .gpio_func[0] = AIC3X_GPIO1_FUNC_DISABLED,
328 .gpio_func[1] = AIC3X_GPIO2_FUNC_DIGITAL_MIC_INPUT,
331 /* Audio subsystem */
332 static struct snd_soc_device n810_snd_devdata = {
333 .card = &snd_soc_n810,
334 .codec_dev = &soc_codec_dev_aic3x,
335 .codec_data = &n810_aic33_setup,
338 static struct platform_device *n810_snd_device;
340 static int __init n810_soc_init(void)
345 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
348 n810_snd_device = platform_device_alloc("soc-audio", -1);
349 if (!n810_snd_device)
352 platform_set_drvdata(n810_snd_device, &n810_snd_devdata);
353 n810_snd_devdata.dev = &n810_snd_device->dev;
354 *(unsigned int *)n810_dai.cpu_dai->private_data = 1; /* McBSP2 */
355 err = platform_device_add(n810_snd_device);
359 dev = &n810_snd_device->dev;
361 sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
362 if (IS_ERR(sys_clkout2_src)) {
363 dev_err(dev, "Could not get sys_clkout2_src clock\n");
364 err = PTR_ERR(sys_clkout2_src);
367 sys_clkout2 = clk_get(dev, "sys_clkout2");
368 if (IS_ERR(sys_clkout2)) {
369 dev_err(dev, "Could not get sys_clkout2\n");
370 err = PTR_ERR(sys_clkout2);
374 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
375 * 96 MHz as its parent in order to get 12 MHz
377 func96m_clk = clk_get(dev, "func_96m_ck");
378 if (IS_ERR(func96m_clk)) {
379 dev_err(dev, "Could not get func 96M clock\n");
380 err = PTR_ERR(func96m_clk);
383 clk_set_parent(sys_clkout2_src, func96m_clk);
384 clk_set_rate(sys_clkout2, 12000000);
386 if (gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0)
388 if (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0)
390 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
391 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
395 clk_put(sys_clkout2);
397 clk_put(sys_clkout2_src);
399 platform_device_del(n810_snd_device);
401 platform_device_put(n810_snd_device);
406 static void __exit n810_soc_exit(void)
408 gpio_free(N810_SPEAKER_AMP_GPIO);
409 gpio_free(N810_HEADSET_AMP_GPIO);
410 clk_put(sys_clkout2_src);
411 clk_put(sys_clkout2);
412 clk_put(func96m_clk);
414 platform_device_unregister(n810_snd_device);
417 module_init(n810_soc_init);
418 module_exit(n810_soc_exit);
420 MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
421 MODULE_DESCRIPTION("ALSA SoC Nokia N810");
422 MODULE_LICENSE("GPL");