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
) {
57 snd_soc_dapm_mutex_lock(dapm
);
60 snd_soc_dapm_enable_pin_unlocked(dapm
, "Ext Spk");
62 snd_soc_dapm_disable_pin_unlocked(dapm
, "Ext Spk");
65 snd_soc_dapm_enable_pin_unlocked(dapm
, "Headphone Jack");
67 snd_soc_dapm_disable_pin_unlocked(dapm
, "Headphone Jack");
69 snd_soc_dapm_enable_pin_unlocked(dapm
, "HS Mic");
71 snd_soc_dapm_disable_pin_unlocked(dapm
, "HS Mic");
74 snd_soc_dapm_enable_pin_unlocked(dapm
, "DMic");
76 snd_soc_dapm_disable_pin_unlocked(dapm
, "DMic");
78 snd_soc_dapm_sync_unlocked(dapm
);
80 snd_soc_dapm_mutex_unlock(dapm
);
83 static int n810_startup(struct snd_pcm_substream
*substream
)
85 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
86 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
88 snd_pcm_hw_constraint_single(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
90 n810_ext_control(&rtd
->card
->dapm
);
91 return clk_prepare_enable(sys_clkout2
);
94 static void n810_shutdown(struct snd_pcm_substream
*substream
)
96 clk_disable_unprepare(sys_clkout2
);
99 static int n810_hw_params(struct snd_pcm_substream
*substream
,
100 struct snd_pcm_hw_params
*params
)
102 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
103 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
106 /* Set the codec system clock for DAC and ADC */
107 err
= snd_soc_dai_set_sysclk(codec_dai
, 0, 12000000,
113 static const struct snd_soc_ops n810_ops
= {
114 .startup
= n810_startup
,
115 .hw_params
= n810_hw_params
,
116 .shutdown
= n810_shutdown
,
119 static int n810_get_spk(struct snd_kcontrol
*kcontrol
,
120 struct snd_ctl_elem_value
*ucontrol
)
122 ucontrol
->value
.enumerated
.item
[0] = n810_spk_func
;
127 static int n810_set_spk(struct snd_kcontrol
*kcontrol
,
128 struct snd_ctl_elem_value
*ucontrol
)
130 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
132 if (n810_spk_func
== ucontrol
->value
.enumerated
.item
[0])
135 n810_spk_func
= ucontrol
->value
.enumerated
.item
[0];
136 n810_ext_control(&card
->dapm
);
141 static int n810_get_jack(struct snd_kcontrol
*kcontrol
,
142 struct snd_ctl_elem_value
*ucontrol
)
144 ucontrol
->value
.enumerated
.item
[0] = n810_jack_func
;
149 static int n810_set_jack(struct snd_kcontrol
*kcontrol
,
150 struct snd_ctl_elem_value
*ucontrol
)
152 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
154 if (n810_jack_func
== ucontrol
->value
.enumerated
.item
[0])
157 n810_jack_func
= ucontrol
->value
.enumerated
.item
[0];
158 n810_ext_control(&card
->dapm
);
163 static int n810_get_input(struct snd_kcontrol
*kcontrol
,
164 struct snd_ctl_elem_value
*ucontrol
)
166 ucontrol
->value
.enumerated
.item
[0] = n810_dmic_func
;
171 static int n810_set_input(struct snd_kcontrol
*kcontrol
,
172 struct snd_ctl_elem_value
*ucontrol
)
174 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
176 if (n810_dmic_func
== ucontrol
->value
.enumerated
.item
[0])
179 n810_dmic_func
= ucontrol
->value
.enumerated
.item
[0];
180 n810_ext_control(&card
->dapm
);
185 static int n810_spk_event(struct snd_soc_dapm_widget
*w
,
186 struct snd_kcontrol
*k
, int event
)
188 if (SND_SOC_DAPM_EVENT_ON(event
))
189 gpio_set_value(N810_SPEAKER_AMP_GPIO
, 1);
191 gpio_set_value(N810_SPEAKER_AMP_GPIO
, 0);
196 static int n810_jack_event(struct snd_soc_dapm_widget
*w
,
197 struct snd_kcontrol
*k
, int event
)
199 if (SND_SOC_DAPM_EVENT_ON(event
))
200 gpio_set_value(N810_HEADSET_AMP_GPIO
, 1);
202 gpio_set_value(N810_HEADSET_AMP_GPIO
, 0);
207 static const struct snd_soc_dapm_widget aic33_dapm_widgets
[] = {
208 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event
),
209 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event
),
210 SND_SOC_DAPM_MIC("DMic", NULL
),
211 SND_SOC_DAPM_MIC("HS Mic", NULL
),
214 static const struct snd_soc_dapm_route audio_map
[] = {
215 {"Headphone Jack", NULL
, "HPLOUT"},
216 {"Headphone Jack", NULL
, "HPROUT"},
218 {"Ext Spk", NULL
, "LLOUT"},
219 {"Ext Spk", NULL
, "RLOUT"},
221 {"DMic Rate 64", NULL
, "DMic"},
222 {"DMic", NULL
, "Mic Bias"},
225 * Note that the mic bias is coming from Retu/Vilma and we don't have
226 * control over it atm. The analog HS mic is not working. <- TODO
228 {"LINE1L", NULL
, "HS Mic"},
231 static const char *spk_function
[] = {"Off", "On"};
232 static const char *jack_function
[] = {"Off", "Headphone", "Headset", "Mic"};
233 static const char *input_function
[] = {"ADC", "Digital Mic"};
234 static const struct soc_enum n810_enum
[] = {
235 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function
), spk_function
),
236 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function
), jack_function
),
237 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function
), input_function
),
240 static const struct snd_kcontrol_new aic33_n810_controls
[] = {
241 SOC_ENUM_EXT("Speaker Function", n810_enum
[0],
242 n810_get_spk
, n810_set_spk
),
243 SOC_ENUM_EXT("Jack Function", n810_enum
[1],
244 n810_get_jack
, n810_set_jack
),
245 SOC_ENUM_EXT("Input Select", n810_enum
[2],
246 n810_get_input
, n810_set_input
),
249 /* Digital audio interface glue - connects codec <--> CPU */
250 static struct snd_soc_dai_link n810_dai
= {
251 .name
= "TLV320AIC33",
252 .stream_name
= "AIC33",
253 .cpu_dai_name
= "48076000.mcbsp",
254 .platform_name
= "48076000.mcbsp",
255 .codec_name
= "tlv320aic3x-codec.1-0018",
256 .codec_dai_name
= "tlv320aic3x-hifi",
257 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
258 SND_SOC_DAIFMT_CBM_CFM
,
262 /* Audio machine driver */
263 static struct snd_soc_card snd_soc_n810
= {
265 .owner
= THIS_MODULE
,
266 .dai_link
= &n810_dai
,
269 .controls
= aic33_n810_controls
,
270 .num_controls
= ARRAY_SIZE(aic33_n810_controls
),
271 .dapm_widgets
= aic33_dapm_widgets
,
272 .num_dapm_widgets
= ARRAY_SIZE(aic33_dapm_widgets
),
273 .dapm_routes
= audio_map
,
274 .num_dapm_routes
= ARRAY_SIZE(audio_map
),
275 .fully_routed
= true,
278 static struct platform_device
*n810_snd_device
;
280 static int __init
n810_soc_init(void)
285 if (!of_have_populated_dt() ||
286 (!of_machine_is_compatible("nokia,n810") &&
287 !of_machine_is_compatible("nokia,n810-wimax")))
290 n810_snd_device
= platform_device_alloc("soc-audio", -1);
291 if (!n810_snd_device
)
294 platform_set_drvdata(n810_snd_device
, &snd_soc_n810
);
295 err
= platform_device_add(n810_snd_device
);
299 dev
= &n810_snd_device
->dev
;
301 sys_clkout2_src
= clk_get(dev
, "sys_clkout2_src");
302 if (IS_ERR(sys_clkout2_src
)) {
303 dev_err(dev
, "Could not get sys_clkout2_src clock\n");
304 err
= PTR_ERR(sys_clkout2_src
);
307 sys_clkout2
= clk_get(dev
, "sys_clkout2");
308 if (IS_ERR(sys_clkout2
)) {
309 dev_err(dev
, "Could not get sys_clkout2\n");
310 err
= PTR_ERR(sys_clkout2
);
314 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
315 * 96 MHz as its parent in order to get 12 MHz
317 func96m_clk
= clk_get(dev
, "func_96m_ck");
318 if (IS_ERR(func96m_clk
)) {
319 dev_err(dev
, "Could not get func 96M clock\n");
320 err
= PTR_ERR(func96m_clk
);
323 clk_set_parent(sys_clkout2_src
, func96m_clk
);
324 clk_set_rate(sys_clkout2
, 12000000);
326 if (WARN_ON((gpio_request(N810_HEADSET_AMP_GPIO
, "hs_amp") < 0) ||
327 (gpio_request(N810_SPEAKER_AMP_GPIO
, "spk_amp") < 0))) {
332 gpio_direction_output(N810_HEADSET_AMP_GPIO
, 0);
333 gpio_direction_output(N810_SPEAKER_AMP_GPIO
, 0);
337 clk_put(sys_clkout2
);
339 clk_put(sys_clkout2_src
);
341 platform_device_del(n810_snd_device
);
343 platform_device_put(n810_snd_device
);
348 static void __exit
n810_soc_exit(void)
350 gpio_free(N810_SPEAKER_AMP_GPIO
);
351 gpio_free(N810_HEADSET_AMP_GPIO
);
352 clk_put(sys_clkout2_src
);
353 clk_put(sys_clkout2
);
354 clk_put(func96m_clk
);
356 platform_device_unregister(n810_snd_device
);
359 module_init(n810_soc_init
);
360 module_exit(n810_soc_exit
);
362 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
363 MODULE_DESCRIPTION("ALSA SoC Nokia N810");
364 MODULE_LICENSE("GPL");