1 // SPDX-License-Identifier: GPL-2.0-only
3 * n810.c -- SoC audio for Nokia N810
5 * Copyright (C) 2008 Nokia Corporation
7 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
10 #include <linux/clk.h>
11 #include <linux/i2c.h>
12 #include <linux/platform_device.h>
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/soc.h>
17 #include <asm/mach-types.h>
18 #include <linux/gpio.h>
19 #include <linux/module.h>
20 #include <linux/platform_data/asoc-ti-mcbsp.h>
22 #include "omap-mcbsp.h"
24 #define N810_HEADSET_AMP_GPIO 10
25 #define N810_SPEAKER_AMP_GPIO 101
34 static struct clk
*sys_clkout2
;
35 static struct clk
*sys_clkout2_src
;
36 static struct clk
*func96m_clk
;
38 static int n810_spk_func
;
39 static int n810_jack_func
;
40 static int n810_dmic_func
;
42 static void n810_ext_control(struct snd_soc_dapm_context
*dapm
)
44 int hp
= 0, line1l
= 0;
46 switch (n810_jack_func
) {
58 snd_soc_dapm_mutex_lock(dapm
);
61 snd_soc_dapm_enable_pin_unlocked(dapm
, "Ext Spk");
63 snd_soc_dapm_disable_pin_unlocked(dapm
, "Ext Spk");
66 snd_soc_dapm_enable_pin_unlocked(dapm
, "Headphone Jack");
68 snd_soc_dapm_disable_pin_unlocked(dapm
, "Headphone Jack");
70 snd_soc_dapm_enable_pin_unlocked(dapm
, "HS Mic");
72 snd_soc_dapm_disable_pin_unlocked(dapm
, "HS Mic");
75 snd_soc_dapm_enable_pin_unlocked(dapm
, "DMic");
77 snd_soc_dapm_disable_pin_unlocked(dapm
, "DMic");
79 snd_soc_dapm_sync_unlocked(dapm
);
81 snd_soc_dapm_mutex_unlock(dapm
);
84 static int n810_startup(struct snd_pcm_substream
*substream
)
86 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
87 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
89 snd_pcm_hw_constraint_single(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
91 n810_ext_control(&rtd
->card
->dapm
);
92 return clk_prepare_enable(sys_clkout2
);
95 static void n810_shutdown(struct snd_pcm_substream
*substream
)
97 clk_disable_unprepare(sys_clkout2
);
100 static int n810_hw_params(struct snd_pcm_substream
*substream
,
101 struct snd_pcm_hw_params
*params
)
103 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
104 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
107 /* Set the codec system clock for DAC and ADC */
108 err
= snd_soc_dai_set_sysclk(codec_dai
, 0, 12000000,
114 static const struct snd_soc_ops n810_ops
= {
115 .startup
= n810_startup
,
116 .hw_params
= n810_hw_params
,
117 .shutdown
= n810_shutdown
,
120 static int n810_get_spk(struct snd_kcontrol
*kcontrol
,
121 struct snd_ctl_elem_value
*ucontrol
)
123 ucontrol
->value
.enumerated
.item
[0] = n810_spk_func
;
128 static int n810_set_spk(struct snd_kcontrol
*kcontrol
,
129 struct snd_ctl_elem_value
*ucontrol
)
131 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
133 if (n810_spk_func
== ucontrol
->value
.enumerated
.item
[0])
136 n810_spk_func
= ucontrol
->value
.enumerated
.item
[0];
137 n810_ext_control(&card
->dapm
);
142 static int n810_get_jack(struct snd_kcontrol
*kcontrol
,
143 struct snd_ctl_elem_value
*ucontrol
)
145 ucontrol
->value
.enumerated
.item
[0] = n810_jack_func
;
150 static int n810_set_jack(struct snd_kcontrol
*kcontrol
,
151 struct snd_ctl_elem_value
*ucontrol
)
153 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
155 if (n810_jack_func
== ucontrol
->value
.enumerated
.item
[0])
158 n810_jack_func
= ucontrol
->value
.enumerated
.item
[0];
159 n810_ext_control(&card
->dapm
);
164 static int n810_get_input(struct snd_kcontrol
*kcontrol
,
165 struct snd_ctl_elem_value
*ucontrol
)
167 ucontrol
->value
.enumerated
.item
[0] = n810_dmic_func
;
172 static int n810_set_input(struct snd_kcontrol
*kcontrol
,
173 struct snd_ctl_elem_value
*ucontrol
)
175 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
177 if (n810_dmic_func
== ucontrol
->value
.enumerated
.item
[0])
180 n810_dmic_func
= ucontrol
->value
.enumerated
.item
[0];
181 n810_ext_control(&card
->dapm
);
186 static int n810_spk_event(struct snd_soc_dapm_widget
*w
,
187 struct snd_kcontrol
*k
, int event
)
189 if (SND_SOC_DAPM_EVENT_ON(event
))
190 gpio_set_value(N810_SPEAKER_AMP_GPIO
, 1);
192 gpio_set_value(N810_SPEAKER_AMP_GPIO
, 0);
197 static int n810_jack_event(struct snd_soc_dapm_widget
*w
,
198 struct snd_kcontrol
*k
, int event
)
200 if (SND_SOC_DAPM_EVENT_ON(event
))
201 gpio_set_value(N810_HEADSET_AMP_GPIO
, 1);
203 gpio_set_value(N810_HEADSET_AMP_GPIO
, 0);
208 static const struct snd_soc_dapm_widget aic33_dapm_widgets
[] = {
209 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event
),
210 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event
),
211 SND_SOC_DAPM_MIC("DMic", NULL
),
212 SND_SOC_DAPM_MIC("HS Mic", NULL
),
215 static const struct snd_soc_dapm_route audio_map
[] = {
216 {"Headphone Jack", NULL
, "HPLOUT"},
217 {"Headphone Jack", NULL
, "HPROUT"},
219 {"Ext Spk", NULL
, "LLOUT"},
220 {"Ext Spk", NULL
, "RLOUT"},
222 {"DMic Rate 64", NULL
, "DMic"},
223 {"DMic", NULL
, "Mic Bias"},
226 * Note that the mic bias is coming from Retu/Vilma and we don't have
227 * control over it atm. The analog HS mic is not working. <- TODO
229 {"LINE1L", NULL
, "HS Mic"},
232 static const char *spk_function
[] = {"Off", "On"};
233 static const char *jack_function
[] = {"Off", "Headphone", "Headset", "Mic"};
234 static const char *input_function
[] = {"ADC", "Digital Mic"};
235 static const struct soc_enum n810_enum
[] = {
236 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function
), spk_function
),
237 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function
), jack_function
),
238 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function
), input_function
),
241 static const struct snd_kcontrol_new aic33_n810_controls
[] = {
242 SOC_ENUM_EXT("Speaker Function", n810_enum
[0],
243 n810_get_spk
, n810_set_spk
),
244 SOC_ENUM_EXT("Jack Function", n810_enum
[1],
245 n810_get_jack
, n810_set_jack
),
246 SOC_ENUM_EXT("Input Select", n810_enum
[2],
247 n810_get_input
, n810_set_input
),
250 /* Digital audio interface glue - connects codec <--> CPU */
251 SND_SOC_DAILINK_DEFS(aic33
,
252 DAILINK_COMP_ARRAY(COMP_CPU("48076000.mcbsp")),
253 DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic3x-codec.1-0018",
254 "tlv320aic3x-hifi")),
255 DAILINK_COMP_ARRAY(COMP_PLATFORM("48076000.mcbsp")));
257 static struct snd_soc_dai_link n810_dai
= {
258 .name
= "TLV320AIC33",
259 .stream_name
= "AIC33",
260 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
261 SND_SOC_DAIFMT_CBM_CFM
,
263 SND_SOC_DAILINK_REG(aic33
),
266 /* Audio machine driver */
267 static struct snd_soc_card snd_soc_n810
= {
269 .owner
= THIS_MODULE
,
270 .dai_link
= &n810_dai
,
273 .controls
= aic33_n810_controls
,
274 .num_controls
= ARRAY_SIZE(aic33_n810_controls
),
275 .dapm_widgets
= aic33_dapm_widgets
,
276 .num_dapm_widgets
= ARRAY_SIZE(aic33_dapm_widgets
),
277 .dapm_routes
= audio_map
,
278 .num_dapm_routes
= ARRAY_SIZE(audio_map
),
279 .fully_routed
= true,
282 static struct platform_device
*n810_snd_device
;
284 static int __init
n810_soc_init(void)
289 if (!of_have_populated_dt() ||
290 (!of_machine_is_compatible("nokia,n810") &&
291 !of_machine_is_compatible("nokia,n810-wimax")))
294 n810_snd_device
= platform_device_alloc("soc-audio", -1);
295 if (!n810_snd_device
)
298 platform_set_drvdata(n810_snd_device
, &snd_soc_n810
);
299 err
= platform_device_add(n810_snd_device
);
303 dev
= &n810_snd_device
->dev
;
305 sys_clkout2_src
= clk_get(dev
, "sys_clkout2_src");
306 if (IS_ERR(sys_clkout2_src
)) {
307 dev_err(dev
, "Could not get sys_clkout2_src clock\n");
308 err
= PTR_ERR(sys_clkout2_src
);
311 sys_clkout2
= clk_get(dev
, "sys_clkout2");
312 if (IS_ERR(sys_clkout2
)) {
313 dev_err(dev
, "Could not get sys_clkout2\n");
314 err
= PTR_ERR(sys_clkout2
);
318 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
319 * 96 MHz as its parent in order to get 12 MHz
321 func96m_clk
= clk_get(dev
, "func_96m_ck");
322 if (IS_ERR(func96m_clk
)) {
323 dev_err(dev
, "Could not get func 96M clock\n");
324 err
= PTR_ERR(func96m_clk
);
327 clk_set_parent(sys_clkout2_src
, func96m_clk
);
328 clk_set_rate(sys_clkout2
, 12000000);
330 if (WARN_ON((gpio_request(N810_HEADSET_AMP_GPIO
, "hs_amp") < 0) ||
331 (gpio_request(N810_SPEAKER_AMP_GPIO
, "spk_amp") < 0))) {
336 gpio_direction_output(N810_HEADSET_AMP_GPIO
, 0);
337 gpio_direction_output(N810_SPEAKER_AMP_GPIO
, 0);
341 clk_put(sys_clkout2
);
343 clk_put(sys_clkout2_src
);
345 platform_device_del(n810_snd_device
);
347 platform_device_put(n810_snd_device
);
352 static void __exit
n810_soc_exit(void)
354 gpio_free(N810_SPEAKER_AMP_GPIO
);
355 gpio_free(N810_HEADSET_AMP_GPIO
);
356 clk_put(sys_clkout2_src
);
357 clk_put(sys_clkout2
);
358 clk_put(func96m_clk
);
360 platform_device_unregister(n810_snd_device
);
363 module_init(n810_soc_init
);
364 module_exit(n810_soc_exit
);
366 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
367 MODULE_DESCRIPTION("ALSA SoC Nokia N810");
368 MODULE_LICENSE("GPL");