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/i2c.h>
26 #include <linux/platform_device.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/soc.h>
30 #include <sound/soc-dapm.h>
32 #include <asm/mach-types.h>
33 #include <mach/hardware.h>
34 #include <linux/gpio.h>
35 #include <plat/mcbsp.h>
37 #include "omap-mcbsp.h"
39 #include "../codecs/tlv320aic3x.h"
41 #define N810_HEADSET_AMP_GPIO 10
42 #define N810_SPEAKER_AMP_GPIO 101
51 static struct clk
*sys_clkout2
;
52 static struct clk
*sys_clkout2_src
;
53 static struct clk
*func96m_clk
;
55 static int n810_spk_func
;
56 static int n810_jack_func
;
57 static int n810_dmic_func
;
59 static void n810_ext_control(struct snd_soc_codec
*codec
)
61 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
62 int hp
= 0, line1l
= 0;
64 switch (n810_jack_func
) {
76 snd_soc_dapm_enable_pin(dapm
, "Ext Spk");
78 snd_soc_dapm_disable_pin(dapm
, "Ext Spk");
81 snd_soc_dapm_enable_pin(dapm
, "Headphone Jack");
83 snd_soc_dapm_disable_pin(dapm
, "Headphone Jack");
85 snd_soc_dapm_enable_pin(dapm
, "LINE1L");
87 snd_soc_dapm_disable_pin(dapm
, "LINE1L");
90 snd_soc_dapm_enable_pin(dapm
, "DMic");
92 snd_soc_dapm_disable_pin(dapm
, "DMic");
94 snd_soc_dapm_sync(dapm
);
97 static int n810_startup(struct snd_pcm_substream
*substream
)
99 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
100 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
101 struct snd_soc_codec
*codec
= rtd
->codec
;
103 snd_pcm_hw_constraint_minmax(runtime
,
104 SNDRV_PCM_HW_PARAM_CHANNELS
, 2, 2);
106 n810_ext_control(codec
);
107 return clk_enable(sys_clkout2
);
110 static void n810_shutdown(struct snd_pcm_substream
*substream
)
112 clk_disable(sys_clkout2
);
115 static int n810_hw_params(struct snd_pcm_substream
*substream
,
116 struct snd_pcm_hw_params
*params
)
118 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
119 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
120 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
123 /* Set codec DAI configuration */
124 err
= snd_soc_dai_set_fmt(codec_dai
,
126 SND_SOC_DAIFMT_NB_NF
|
127 SND_SOC_DAIFMT_CBM_CFM
);
131 /* Set cpu DAI configuration */
132 err
= snd_soc_dai_set_fmt(cpu_dai
,
134 SND_SOC_DAIFMT_NB_NF
|
135 SND_SOC_DAIFMT_CBM_CFM
);
139 /* Set the codec system clock for DAC and ADC */
140 err
= snd_soc_dai_set_sysclk(codec_dai
, 0, 12000000,
146 static struct snd_soc_ops n810_ops
= {
147 .startup
= n810_startup
,
148 .hw_params
= n810_hw_params
,
149 .shutdown
= n810_shutdown
,
152 static int n810_get_spk(struct snd_kcontrol
*kcontrol
,
153 struct snd_ctl_elem_value
*ucontrol
)
155 ucontrol
->value
.integer
.value
[0] = n810_spk_func
;
160 static int n810_set_spk(struct snd_kcontrol
*kcontrol
,
161 struct snd_ctl_elem_value
*ucontrol
)
163 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
165 if (n810_spk_func
== ucontrol
->value
.integer
.value
[0])
168 n810_spk_func
= ucontrol
->value
.integer
.value
[0];
169 n810_ext_control(codec
);
174 static int n810_get_jack(struct snd_kcontrol
*kcontrol
,
175 struct snd_ctl_elem_value
*ucontrol
)
177 ucontrol
->value
.integer
.value
[0] = n810_jack_func
;
182 static int n810_set_jack(struct snd_kcontrol
*kcontrol
,
183 struct snd_ctl_elem_value
*ucontrol
)
185 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
187 if (n810_jack_func
== ucontrol
->value
.integer
.value
[0])
190 n810_jack_func
= ucontrol
->value
.integer
.value
[0];
191 n810_ext_control(codec
);
196 static int n810_get_input(struct snd_kcontrol
*kcontrol
,
197 struct snd_ctl_elem_value
*ucontrol
)
199 ucontrol
->value
.integer
.value
[0] = n810_dmic_func
;
204 static int n810_set_input(struct snd_kcontrol
*kcontrol
,
205 struct snd_ctl_elem_value
*ucontrol
)
207 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
209 if (n810_dmic_func
== ucontrol
->value
.integer
.value
[0])
212 n810_dmic_func
= ucontrol
->value
.integer
.value
[0];
213 n810_ext_control(codec
);
218 static int n810_spk_event(struct snd_soc_dapm_widget
*w
,
219 struct snd_kcontrol
*k
, int event
)
221 if (SND_SOC_DAPM_EVENT_ON(event
))
222 gpio_set_value(N810_SPEAKER_AMP_GPIO
, 1);
224 gpio_set_value(N810_SPEAKER_AMP_GPIO
, 0);
229 static int n810_jack_event(struct snd_soc_dapm_widget
*w
,
230 struct snd_kcontrol
*k
, int event
)
232 if (SND_SOC_DAPM_EVENT_ON(event
))
233 gpio_set_value(N810_HEADSET_AMP_GPIO
, 1);
235 gpio_set_value(N810_HEADSET_AMP_GPIO
, 0);
240 static const struct snd_soc_dapm_widget aic33_dapm_widgets
[] = {
241 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event
),
242 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event
),
243 SND_SOC_DAPM_MIC("DMic", NULL
),
246 static const struct snd_soc_dapm_route audio_map
[] = {
247 {"Headphone Jack", NULL
, "HPLOUT"},
248 {"Headphone Jack", NULL
, "HPROUT"},
250 {"Ext Spk", NULL
, "LLOUT"},
251 {"Ext Spk", NULL
, "RLOUT"},
253 {"DMic Rate 64", NULL
, "Mic Bias 2V"},
254 {"Mic Bias 2V", NULL
, "DMic"},
257 static const char *spk_function
[] = {"Off", "On"};
258 static const char *jack_function
[] = {"Off", "Headphone", "Headset", "Mic"};
259 static const char *input_function
[] = {"ADC", "Digital Mic"};
260 static const struct soc_enum n810_enum
[] = {
261 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function
), spk_function
),
262 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function
), jack_function
),
263 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function
), input_function
),
266 static const struct snd_kcontrol_new aic33_n810_controls
[] = {
267 SOC_ENUM_EXT("Speaker Function", n810_enum
[0],
268 n810_get_spk
, n810_set_spk
),
269 SOC_ENUM_EXT("Jack Function", n810_enum
[1],
270 n810_get_jack
, n810_set_jack
),
271 SOC_ENUM_EXT("Input Select", n810_enum
[2],
272 n810_get_input
, n810_set_input
),
275 static int n810_aic33_init(struct snd_soc_pcm_runtime
*rtd
)
277 struct snd_soc_codec
*codec
= rtd
->codec
;
278 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
282 snd_soc_dapm_nc_pin(dapm
, "MONO_LOUT");
283 snd_soc_dapm_nc_pin(dapm
, "HPLCOM");
284 snd_soc_dapm_nc_pin(dapm
, "HPRCOM");
285 snd_soc_dapm_nc_pin(dapm
, "MIC3L");
286 snd_soc_dapm_nc_pin(dapm
, "MIC3R");
287 snd_soc_dapm_nc_pin(dapm
, "LINE1R");
288 snd_soc_dapm_nc_pin(dapm
, "LINE2L");
289 snd_soc_dapm_nc_pin(dapm
, "LINE2R");
291 /* Add N810 specific controls */
292 err
= snd_soc_add_controls(codec
, aic33_n810_controls
,
293 ARRAY_SIZE(aic33_n810_controls
));
297 /* Add N810 specific widgets */
298 snd_soc_dapm_new_controls(dapm
, aic33_dapm_widgets
,
299 ARRAY_SIZE(aic33_dapm_widgets
));
301 /* Set up N810 specific audio path audio_map */
302 snd_soc_dapm_add_routes(dapm
, audio_map
, ARRAY_SIZE(audio_map
));
304 snd_soc_dapm_sync(dapm
);
309 /* Digital audio interface glue - connects codec <--> CPU */
310 static struct snd_soc_dai_link n810_dai
= {
311 .name
= "TLV320AIC33",
312 .stream_name
= "AIC33",
313 .cpu_dai_name
= "omap-mcbsp-dai.1",
314 .platform_name
= "omap-pcm-audio",
315 .codec_name
= "tlv320aic3x-codec.2-0018",
316 .codec_dai_name
= "tlv320aic3x-hifi",
317 .init
= n810_aic33_init
,
321 /* Audio machine driver */
322 static struct snd_soc_card snd_soc_n810
= {
324 .dai_link
= &n810_dai
,
328 static struct platform_device
*n810_snd_device
;
330 static int __init
n810_soc_init(void)
335 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
338 n810_snd_device
= platform_device_alloc("soc-audio", -1);
339 if (!n810_snd_device
)
342 platform_set_drvdata(n810_snd_device
, &snd_soc_n810
);
343 err
= platform_device_add(n810_snd_device
);
347 dev
= &n810_snd_device
->dev
;
349 sys_clkout2_src
= clk_get(dev
, "sys_clkout2_src");
350 if (IS_ERR(sys_clkout2_src
)) {
351 dev_err(dev
, "Could not get sys_clkout2_src clock\n");
352 err
= PTR_ERR(sys_clkout2_src
);
355 sys_clkout2
= clk_get(dev
, "sys_clkout2");
356 if (IS_ERR(sys_clkout2
)) {
357 dev_err(dev
, "Could not get sys_clkout2\n");
358 err
= PTR_ERR(sys_clkout2
);
362 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
363 * 96 MHz as its parent in order to get 12 MHz
365 func96m_clk
= clk_get(dev
, "func_96m_ck");
366 if (IS_ERR(func96m_clk
)) {
367 dev_err(dev
, "Could not get func 96M clock\n");
368 err
= PTR_ERR(func96m_clk
);
371 clk_set_parent(sys_clkout2_src
, func96m_clk
);
372 clk_set_rate(sys_clkout2
, 12000000);
374 BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO
, "hs_amp") < 0) ||
375 (gpio_request(N810_SPEAKER_AMP_GPIO
, "spk_amp") < 0));
377 gpio_direction_output(N810_HEADSET_AMP_GPIO
, 0);
378 gpio_direction_output(N810_SPEAKER_AMP_GPIO
, 0);
382 clk_put(sys_clkout2
);
384 clk_put(sys_clkout2_src
);
386 platform_device_del(n810_snd_device
);
388 platform_device_put(n810_snd_device
);
393 static void __exit
n810_soc_exit(void)
395 gpio_free(N810_SPEAKER_AMP_GPIO
);
396 gpio_free(N810_HEADSET_AMP_GPIO
);
397 clk_put(sys_clkout2_src
);
398 clk_put(sys_clkout2
);
399 clk_put(func96m_clk
);
401 platform_device_unregister(n810_snd_device
);
404 module_init(n810_soc_init
);
405 module_exit(n810_soc_exit
);
407 MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
408 MODULE_DESCRIPTION("ALSA SoC Nokia N810");
409 MODULE_LICENSE("GPL");