2 * n810.c -- SoC audio for Nokia N810
4 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.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>
31 #include <asm/mach-types.h>
32 #include <mach/hardware.h>
33 #include <linux/gpio.h>
34 #include <linux/module.h>
35 #include <plat/mcbsp.h>
37 #include "omap-mcbsp.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 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
61 int hp
= 0, line1l
= 0;
63 switch (n810_jack_func
) {
75 snd_soc_dapm_enable_pin(dapm
, "Ext Spk");
77 snd_soc_dapm_disable_pin(dapm
, "Ext Spk");
80 snd_soc_dapm_enable_pin(dapm
, "Headphone Jack");
82 snd_soc_dapm_disable_pin(dapm
, "Headphone Jack");
84 snd_soc_dapm_enable_pin(dapm
, "LINE1L");
86 snd_soc_dapm_disable_pin(dapm
, "LINE1L");
89 snd_soc_dapm_enable_pin(dapm
, "DMic");
91 snd_soc_dapm_disable_pin(dapm
, "DMic");
93 snd_soc_dapm_sync(dapm
);
96 static int n810_startup(struct snd_pcm_substream
*substream
)
98 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
99 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
100 struct snd_soc_codec
*codec
= rtd
->codec
;
102 snd_pcm_hw_constraint_minmax(runtime
,
103 SNDRV_PCM_HW_PARAM_CHANNELS
, 2, 2);
105 n810_ext_control(codec
);
106 return clk_enable(sys_clkout2
);
109 static void n810_shutdown(struct snd_pcm_substream
*substream
)
111 clk_disable(sys_clkout2
);
114 static int n810_hw_params(struct snd_pcm_substream
*substream
,
115 struct snd_pcm_hw_params
*params
)
117 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
118 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
121 /* Set the codec system clock for DAC and ADC */
122 err
= snd_soc_dai_set_sysclk(codec_dai
, 0, 12000000,
128 static struct snd_soc_ops n810_ops
= {
129 .startup
= n810_startup
,
130 .hw_params
= n810_hw_params
,
131 .shutdown
= n810_shutdown
,
134 static int n810_get_spk(struct snd_kcontrol
*kcontrol
,
135 struct snd_ctl_elem_value
*ucontrol
)
137 ucontrol
->value
.integer
.value
[0] = n810_spk_func
;
142 static int n810_set_spk(struct snd_kcontrol
*kcontrol
,
143 struct snd_ctl_elem_value
*ucontrol
)
145 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
147 if (n810_spk_func
== ucontrol
->value
.integer
.value
[0])
150 n810_spk_func
= ucontrol
->value
.integer
.value
[0];
151 n810_ext_control(codec
);
156 static int n810_get_jack(struct snd_kcontrol
*kcontrol
,
157 struct snd_ctl_elem_value
*ucontrol
)
159 ucontrol
->value
.integer
.value
[0] = n810_jack_func
;
164 static int n810_set_jack(struct snd_kcontrol
*kcontrol
,
165 struct snd_ctl_elem_value
*ucontrol
)
167 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
169 if (n810_jack_func
== ucontrol
->value
.integer
.value
[0])
172 n810_jack_func
= ucontrol
->value
.integer
.value
[0];
173 n810_ext_control(codec
);
178 static int n810_get_input(struct snd_kcontrol
*kcontrol
,
179 struct snd_ctl_elem_value
*ucontrol
)
181 ucontrol
->value
.integer
.value
[0] = n810_dmic_func
;
186 static int n810_set_input(struct snd_kcontrol
*kcontrol
,
187 struct snd_ctl_elem_value
*ucontrol
)
189 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
191 if (n810_dmic_func
== ucontrol
->value
.integer
.value
[0])
194 n810_dmic_func
= ucontrol
->value
.integer
.value
[0];
195 n810_ext_control(codec
);
200 static int n810_spk_event(struct snd_soc_dapm_widget
*w
,
201 struct snd_kcontrol
*k
, int event
)
203 if (SND_SOC_DAPM_EVENT_ON(event
))
204 gpio_set_value(N810_SPEAKER_AMP_GPIO
, 1);
206 gpio_set_value(N810_SPEAKER_AMP_GPIO
, 0);
211 static int n810_jack_event(struct snd_soc_dapm_widget
*w
,
212 struct snd_kcontrol
*k
, int event
)
214 if (SND_SOC_DAPM_EVENT_ON(event
))
215 gpio_set_value(N810_HEADSET_AMP_GPIO
, 1);
217 gpio_set_value(N810_HEADSET_AMP_GPIO
, 0);
222 static const struct snd_soc_dapm_widget aic33_dapm_widgets
[] = {
223 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event
),
224 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event
),
225 SND_SOC_DAPM_MIC("DMic", NULL
),
228 static const struct snd_soc_dapm_route audio_map
[] = {
229 {"Headphone Jack", NULL
, "HPLOUT"},
230 {"Headphone Jack", NULL
, "HPROUT"},
232 {"Ext Spk", NULL
, "LLOUT"},
233 {"Ext Spk", NULL
, "RLOUT"},
235 {"DMic Rate 64", NULL
, "Mic Bias 2V"},
236 {"Mic Bias 2V", NULL
, "DMic"},
239 static const char *spk_function
[] = {"Off", "On"};
240 static const char *jack_function
[] = {"Off", "Headphone", "Headset", "Mic"};
241 static const char *input_function
[] = {"ADC", "Digital Mic"};
242 static const struct soc_enum n810_enum
[] = {
243 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function
), spk_function
),
244 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function
), jack_function
),
245 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function
), input_function
),
248 static const struct snd_kcontrol_new aic33_n810_controls
[] = {
249 SOC_ENUM_EXT("Speaker Function", n810_enum
[0],
250 n810_get_spk
, n810_set_spk
),
251 SOC_ENUM_EXT("Jack Function", n810_enum
[1],
252 n810_get_jack
, n810_set_jack
),
253 SOC_ENUM_EXT("Input Select", n810_enum
[2],
254 n810_get_input
, n810_set_input
),
257 static int n810_aic33_init(struct snd_soc_pcm_runtime
*rtd
)
259 struct snd_soc_codec
*codec
= rtd
->codec
;
260 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
263 snd_soc_dapm_nc_pin(dapm
, "MONO_LOUT");
264 snd_soc_dapm_nc_pin(dapm
, "HPLCOM");
265 snd_soc_dapm_nc_pin(dapm
, "HPRCOM");
266 snd_soc_dapm_nc_pin(dapm
, "MIC3L");
267 snd_soc_dapm_nc_pin(dapm
, "MIC3R");
268 snd_soc_dapm_nc_pin(dapm
, "LINE1R");
269 snd_soc_dapm_nc_pin(dapm
, "LINE2L");
270 snd_soc_dapm_nc_pin(dapm
, "LINE2R");
275 /* Digital audio interface glue - connects codec <--> CPU */
276 static struct snd_soc_dai_link n810_dai
= {
277 .name
= "TLV320AIC33",
278 .stream_name
= "AIC33",
279 .cpu_dai_name
= "omap-mcbsp-dai.1",
280 .platform_name
= "omap-pcm-audio",
281 .codec_name
= "tlv320aic3x-codec.2-0018",
282 .codec_dai_name
= "tlv320aic3x-hifi",
283 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
284 SND_SOC_DAIFMT_CBM_CFM
,
285 .init
= n810_aic33_init
,
289 /* Audio machine driver */
290 static struct snd_soc_card snd_soc_n810
= {
292 .owner
= THIS_MODULE
,
293 .dai_link
= &n810_dai
,
296 .controls
= aic33_n810_controls
,
297 .num_controls
= ARRAY_SIZE(aic33_n810_controls
),
298 .dapm_widgets
= aic33_dapm_widgets
,
299 .num_dapm_widgets
= ARRAY_SIZE(aic33_dapm_widgets
),
300 .dapm_routes
= audio_map
,
301 .num_dapm_routes
= ARRAY_SIZE(audio_map
),
304 static struct platform_device
*n810_snd_device
;
306 static int __init
n810_soc_init(void)
311 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
314 n810_snd_device
= platform_device_alloc("soc-audio", -1);
315 if (!n810_snd_device
)
318 platform_set_drvdata(n810_snd_device
, &snd_soc_n810
);
319 err
= platform_device_add(n810_snd_device
);
323 dev
= &n810_snd_device
->dev
;
325 sys_clkout2_src
= clk_get(dev
, "sys_clkout2_src");
326 if (IS_ERR(sys_clkout2_src
)) {
327 dev_err(dev
, "Could not get sys_clkout2_src clock\n");
328 err
= PTR_ERR(sys_clkout2_src
);
331 sys_clkout2
= clk_get(dev
, "sys_clkout2");
332 if (IS_ERR(sys_clkout2
)) {
333 dev_err(dev
, "Could not get sys_clkout2\n");
334 err
= PTR_ERR(sys_clkout2
);
338 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
339 * 96 MHz as its parent in order to get 12 MHz
341 func96m_clk
= clk_get(dev
, "func_96m_ck");
342 if (IS_ERR(func96m_clk
)) {
343 dev_err(dev
, "Could not get func 96M clock\n");
344 err
= PTR_ERR(func96m_clk
);
347 clk_set_parent(sys_clkout2_src
, func96m_clk
);
348 clk_set_rate(sys_clkout2
, 12000000);
350 BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO
, "hs_amp") < 0) ||
351 (gpio_request(N810_SPEAKER_AMP_GPIO
, "spk_amp") < 0));
353 gpio_direction_output(N810_HEADSET_AMP_GPIO
, 0);
354 gpio_direction_output(N810_SPEAKER_AMP_GPIO
, 0);
358 clk_put(sys_clkout2
);
360 clk_put(sys_clkout2_src
);
362 platform_device_del(n810_snd_device
);
364 platform_device_put(n810_snd_device
);
369 static void __exit
n810_soc_exit(void)
371 gpio_free(N810_SPEAKER_AMP_GPIO
);
372 gpio_free(N810_HEADSET_AMP_GPIO
);
373 clk_put(sys_clkout2_src
);
374 clk_put(sys_clkout2
);
375 clk_put(func96m_clk
);
377 platform_device_unregister(n810_snd_device
);
380 module_init(n810_soc_init
);
381 module_exit(n810_soc_exit
);
383 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
384 MODULE_DESCRIPTION("ALSA SoC Nokia N810");
385 MODULE_LICENSE("GPL");