1 // SPDX-License-Identifier: GPL-2.0-only
3 * tegra_wm8903.c - Tegra machine ASoC driver for boards using WM8903 codec.
5 * Author: Stephen Warren <swarren@nvidia.com>
6 * Copyright (C) 2010-2012 - NVIDIA, Inc.
8 * Based on code copyright/by:
10 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
12 * Copyright 2007 Wolfson Microelectronics PLC.
13 * Author: Graeme Gregory
14 * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/gpio.h>
21 #include <linux/of_gpio.h>
23 #include <sound/core.h>
24 #include <sound/jack.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
29 #include "../codecs/wm8903.h"
31 #include "tegra_asoc_utils.h"
33 #define DRV_NAME "tegra-snd-wm8903"
41 struct tegra_asoc_utils_data util_data
;
44 static int tegra_wm8903_hw_params(struct snd_pcm_substream
*substream
,
45 struct snd_pcm_hw_params
*params
)
47 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
48 struct snd_soc_dai
*codec_dai
= asoc_rtd_to_codec(rtd
, 0);
49 struct snd_soc_card
*card
= rtd
->card
;
50 struct tegra_wm8903
*machine
= snd_soc_card_get_drvdata(card
);
54 srate
= params_rate(params
);
65 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
66 while (mclk
< 6000000)
69 err
= tegra_asoc_utils_set_rate(&machine
->util_data
, srate
, mclk
);
71 dev_err(card
->dev
, "Can't configure clocks\n");
75 err
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
78 dev_err(card
->dev
, "codec_dai clock not set\n");
85 static const struct snd_soc_ops tegra_wm8903_ops
= {
86 .hw_params
= tegra_wm8903_hw_params
,
89 static struct snd_soc_jack tegra_wm8903_hp_jack
;
91 static struct snd_soc_jack_pin tegra_wm8903_hp_jack_pins
[] = {
93 .pin
= "Headphone Jack",
94 .mask
= SND_JACK_HEADPHONE
,
98 static struct snd_soc_jack_gpio tegra_wm8903_hp_jack_gpio
= {
99 .name
= "headphone detect",
100 .report
= SND_JACK_HEADPHONE
,
101 .debounce_time
= 150,
105 static struct snd_soc_jack tegra_wm8903_mic_jack
;
107 static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins
[] = {
110 .mask
= SND_JACK_MICROPHONE
,
114 static int tegra_wm8903_event_int_spk(struct snd_soc_dapm_widget
*w
,
115 struct snd_kcontrol
*k
, int event
)
117 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
118 struct snd_soc_card
*card
= dapm
->card
;
119 struct tegra_wm8903
*machine
= snd_soc_card_get_drvdata(card
);
121 if (!gpio_is_valid(machine
->gpio_spkr_en
))
124 gpio_set_value_cansleep(machine
->gpio_spkr_en
,
125 SND_SOC_DAPM_EVENT_ON(event
));
130 static int tegra_wm8903_event_hp(struct snd_soc_dapm_widget
*w
,
131 struct snd_kcontrol
*k
, int event
)
133 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
134 struct snd_soc_card
*card
= dapm
->card
;
135 struct tegra_wm8903
*machine
= snd_soc_card_get_drvdata(card
);
137 if (!gpio_is_valid(machine
->gpio_hp_mute
))
140 gpio_set_value_cansleep(machine
->gpio_hp_mute
,
141 !SND_SOC_DAPM_EVENT_ON(event
));
146 static int tegra_wm8903_event_int_mic(struct snd_soc_dapm_widget
*w
,
147 struct snd_kcontrol
*k
, int event
)
149 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
150 struct snd_soc_card
*card
= dapm
->card
;
151 struct tegra_wm8903
*machine
= snd_soc_card_get_drvdata(card
);
153 if (!gpio_is_valid(machine
->gpio_int_mic_en
))
156 gpio_set_value_cansleep(machine
->gpio_int_mic_en
,
157 SND_SOC_DAPM_EVENT_ON(event
));
162 static const struct snd_soc_dapm_widget tegra_wm8903_dapm_widgets
[] = {
163 SND_SOC_DAPM_SPK("Int Spk", tegra_wm8903_event_int_spk
),
164 SND_SOC_DAPM_HP("Headphone Jack", tegra_wm8903_event_hp
),
165 SND_SOC_DAPM_MIC("Mic Jack", NULL
),
166 SND_SOC_DAPM_MIC("Int Mic", tegra_wm8903_event_int_mic
),
169 static const struct snd_kcontrol_new tegra_wm8903_controls
[] = {
170 SOC_DAPM_PIN_SWITCH("Int Spk"),
171 SOC_DAPM_PIN_SWITCH("Int Mic"),
174 static int tegra_wm8903_init(struct snd_soc_pcm_runtime
*rtd
)
176 struct snd_soc_dai
*codec_dai
= asoc_rtd_to_codec(rtd
, 0);
177 struct snd_soc_component
*component
= codec_dai
->component
;
178 struct snd_soc_card
*card
= rtd
->card
;
179 struct tegra_wm8903
*machine
= snd_soc_card_get_drvdata(card
);
182 if (gpio_is_valid(machine
->gpio_hp_det
)) {
183 tegra_wm8903_hp_jack_gpio
.gpio
= machine
->gpio_hp_det
;
184 snd_soc_card_jack_new(rtd
->card
, "Headphone Jack",
185 SND_JACK_HEADPHONE
, &tegra_wm8903_hp_jack
,
186 tegra_wm8903_hp_jack_pins
,
187 ARRAY_SIZE(tegra_wm8903_hp_jack_pins
));
188 snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack
,
190 &tegra_wm8903_hp_jack_gpio
);
193 if (of_property_read_bool(card
->dev
->of_node
, "nvidia,headset"))
194 shrt
= SND_JACK_MICROPHONE
;
196 snd_soc_card_jack_new(rtd
->card
, "Mic Jack", SND_JACK_MICROPHONE
,
197 &tegra_wm8903_mic_jack
,
198 tegra_wm8903_mic_jack_pins
,
199 ARRAY_SIZE(tegra_wm8903_mic_jack_pins
));
200 wm8903_mic_detect(component
, &tegra_wm8903_mic_jack
, SND_JACK_MICROPHONE
,
203 snd_soc_dapm_force_enable_pin(&card
->dapm
, "MICBIAS");
208 static int tegra_wm8903_remove(struct snd_soc_card
*card
)
210 struct snd_soc_pcm_runtime
*rtd
=
211 snd_soc_get_pcm_runtime(card
, &card
->dai_link
[0]);
212 struct snd_soc_dai
*codec_dai
= asoc_rtd_to_codec(rtd
, 0);
213 struct snd_soc_component
*component
= codec_dai
->component
;
215 wm8903_mic_detect(component
, NULL
, 0, 0);
220 SND_SOC_DAILINK_DEFS(hifi
,
221 DAILINK_COMP_ARRAY(COMP_EMPTY()),
222 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "wm8903-hifi")),
223 DAILINK_COMP_ARRAY(COMP_EMPTY()));
225 static struct snd_soc_dai_link tegra_wm8903_dai
= {
227 .stream_name
= "WM8903 PCM",
228 .init
= tegra_wm8903_init
,
229 .ops
= &tegra_wm8903_ops
,
230 .dai_fmt
= SND_SOC_DAIFMT_I2S
|
231 SND_SOC_DAIFMT_NB_NF
|
232 SND_SOC_DAIFMT_CBS_CFS
,
233 SND_SOC_DAILINK_REG(hifi
),
236 static struct snd_soc_card snd_soc_tegra_wm8903
= {
237 .name
= "tegra-wm8903",
238 .owner
= THIS_MODULE
,
239 .dai_link
= &tegra_wm8903_dai
,
241 .remove
= tegra_wm8903_remove
,
242 .controls
= tegra_wm8903_controls
,
243 .num_controls
= ARRAY_SIZE(tegra_wm8903_controls
),
244 .dapm_widgets
= tegra_wm8903_dapm_widgets
,
245 .num_dapm_widgets
= ARRAY_SIZE(tegra_wm8903_dapm_widgets
),
246 .fully_routed
= true,
249 static int tegra_wm8903_driver_probe(struct platform_device
*pdev
)
251 struct device_node
*np
= pdev
->dev
.of_node
;
252 struct snd_soc_card
*card
= &snd_soc_tegra_wm8903
;
253 struct tegra_wm8903
*machine
;
256 machine
= devm_kzalloc(&pdev
->dev
, sizeof(struct tegra_wm8903
),
261 card
->dev
= &pdev
->dev
;
262 snd_soc_card_set_drvdata(card
, machine
);
264 machine
->gpio_spkr_en
= of_get_named_gpio(np
, "nvidia,spkr-en-gpios",
266 if (machine
->gpio_spkr_en
== -EPROBE_DEFER
)
267 return -EPROBE_DEFER
;
268 if (gpio_is_valid(machine
->gpio_spkr_en
)) {
269 ret
= devm_gpio_request_one(&pdev
->dev
, machine
->gpio_spkr_en
,
270 GPIOF_OUT_INIT_LOW
, "spkr_en");
272 dev_err(card
->dev
, "cannot get spkr_en gpio\n");
277 machine
->gpio_hp_mute
= of_get_named_gpio(np
, "nvidia,hp-mute-gpios",
279 if (machine
->gpio_hp_mute
== -EPROBE_DEFER
)
280 return -EPROBE_DEFER
;
281 if (gpio_is_valid(machine
->gpio_hp_mute
)) {
282 ret
= devm_gpio_request_one(&pdev
->dev
, machine
->gpio_hp_mute
,
283 GPIOF_OUT_INIT_HIGH
, "hp_mute");
285 dev_err(card
->dev
, "cannot get hp_mute gpio\n");
290 machine
->gpio_hp_det
= of_get_named_gpio(np
, "nvidia,hp-det-gpios", 0);
291 if (machine
->gpio_hp_det
== -EPROBE_DEFER
)
292 return -EPROBE_DEFER
;
294 machine
->gpio_int_mic_en
= of_get_named_gpio(np
,
295 "nvidia,int-mic-en-gpios", 0);
296 if (machine
->gpio_int_mic_en
== -EPROBE_DEFER
)
297 return -EPROBE_DEFER
;
298 if (gpio_is_valid(machine
->gpio_int_mic_en
)) {
299 /* Disable int mic; enable signal is active-high */
300 ret
= devm_gpio_request_one(&pdev
->dev
,
301 machine
->gpio_int_mic_en
,
302 GPIOF_OUT_INIT_LOW
, "int_mic_en");
304 dev_err(card
->dev
, "cannot get int_mic_en gpio\n");
309 machine
->gpio_ext_mic_en
= of_get_named_gpio(np
,
310 "nvidia,ext-mic-en-gpios", 0);
311 if (machine
->gpio_ext_mic_en
== -EPROBE_DEFER
)
312 return -EPROBE_DEFER
;
313 if (gpio_is_valid(machine
->gpio_ext_mic_en
)) {
314 /* Enable ext mic; enable signal is active-low */
315 ret
= devm_gpio_request_one(&pdev
->dev
,
316 machine
->gpio_ext_mic_en
,
317 GPIOF_OUT_INIT_LOW
, "ext_mic_en");
319 dev_err(card
->dev
, "cannot get ext_mic_en gpio\n");
324 ret
= snd_soc_of_parse_card_name(card
, "nvidia,model");
328 ret
= snd_soc_of_parse_audio_routing(card
, "nvidia,audio-routing");
332 tegra_wm8903_dai
.codecs
->of_node
= of_parse_phandle(np
,
333 "nvidia,audio-codec", 0);
334 if (!tegra_wm8903_dai
.codecs
->of_node
) {
336 "Property 'nvidia,audio-codec' missing or invalid\n");
340 tegra_wm8903_dai
.cpus
->of_node
= of_parse_phandle(np
,
341 "nvidia,i2s-controller", 0);
342 if (!tegra_wm8903_dai
.cpus
->of_node
) {
344 "Property 'nvidia,i2s-controller' missing or invalid\n");
348 tegra_wm8903_dai
.platforms
->of_node
= tegra_wm8903_dai
.cpus
->of_node
;
350 ret
= tegra_asoc_utils_init(&machine
->util_data
, &pdev
->dev
);
354 ret
= devm_snd_soc_register_card(&pdev
->dev
, card
);
356 return dev_err_probe(&pdev
->dev
, ret
,
357 "snd_soc_register_card failed\n");
362 static const struct of_device_id tegra_wm8903_of_match
[] = {
363 { .compatible
= "nvidia,tegra-audio-wm8903", },
367 static struct platform_driver tegra_wm8903_driver
= {
370 .pm
= &snd_soc_pm_ops
,
371 .of_match_table
= tegra_wm8903_of_match
,
373 .probe
= tegra_wm8903_driver_probe
,
375 module_platform_driver(tegra_wm8903_driver
);
377 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
378 MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
379 MODULE_LICENSE("GPL");
380 MODULE_ALIAS("platform:" DRV_NAME
);
381 MODULE_DEVICE_TABLE(of
, tegra_wm8903_of_match
);