2 * tegra_wm8903.c - Tegra machine ASoC driver for boards using WM8903 codec.
4 * Author: Stephen Warren <swarren@nvidia.com>
5 * Copyright (C) 2010-2012 - NVIDIA, Inc.
7 * Based on code copyright/by:
9 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
11 * Copyright 2007 Wolfson Microelectronics PLC.
12 * Author: Graeme Gregory
13 * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
31 #include <linux/module.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
34 #include <linux/gpio.h>
35 #include <linux/of_gpio.h>
37 #include <sound/core.h>
38 #include <sound/jack.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
43 #include "../codecs/wm8903.h"
45 #include "tegra_asoc_utils.h"
47 #define DRV_NAME "tegra-snd-wm8903"
55 struct tegra_asoc_utils_data util_data;
58 static int tegra_wm8903_hw_params(struct snd_pcm_substream *substream,
59 struct snd_pcm_hw_params *params)
61 struct snd_soc_pcm_runtime *rtd = substream->private_data;
62 struct snd_soc_dai *codec_dai = rtd->codec_dai;
63 struct snd_soc_card *card = rtd->card;
64 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
68 srate = params_rate(params);
79 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
80 while (mclk < 6000000)
83 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
85 dev_err(card->dev, "Can't configure clocks\n");
89 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
92 dev_err(card->dev, "codec_dai clock not set\n");
99 static struct snd_soc_ops tegra_wm8903_ops = {
100 .hw_params = tegra_wm8903_hw_params,
103 static struct snd_soc_jack tegra_wm8903_hp_jack;
105 static struct snd_soc_jack_pin tegra_wm8903_hp_jack_pins[] = {
107 .pin = "Headphone Jack",
108 .mask = SND_JACK_HEADPHONE,
112 static struct snd_soc_jack_gpio tegra_wm8903_hp_jack_gpio = {
113 .name = "headphone detect",
114 .report = SND_JACK_HEADPHONE,
115 .debounce_time = 150,
119 static struct snd_soc_jack tegra_wm8903_mic_jack;
121 static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = {
124 .mask = SND_JACK_MICROPHONE,
128 static int tegra_wm8903_event_int_spk(struct snd_soc_dapm_widget *w,
129 struct snd_kcontrol *k, int event)
131 struct snd_soc_dapm_context *dapm = w->dapm;
132 struct snd_soc_card *card = dapm->card;
133 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
135 if (!gpio_is_valid(machine->gpio_spkr_en))
138 gpio_set_value_cansleep(machine->gpio_spkr_en,
139 SND_SOC_DAPM_EVENT_ON(event));
144 static int tegra_wm8903_event_hp(struct snd_soc_dapm_widget *w,
145 struct snd_kcontrol *k, int event)
147 struct snd_soc_dapm_context *dapm = w->dapm;
148 struct snd_soc_card *card = dapm->card;
149 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
151 if (!gpio_is_valid(machine->gpio_hp_mute))
154 gpio_set_value_cansleep(machine->gpio_hp_mute,
155 !SND_SOC_DAPM_EVENT_ON(event));
160 static const struct snd_soc_dapm_widget tegra_wm8903_dapm_widgets[] = {
161 SND_SOC_DAPM_SPK("Int Spk", tegra_wm8903_event_int_spk),
162 SND_SOC_DAPM_HP("Headphone Jack", tegra_wm8903_event_hp),
163 SND_SOC_DAPM_MIC("Mic Jack", NULL),
166 static const struct snd_kcontrol_new tegra_wm8903_controls[] = {
167 SOC_DAPM_PIN_SWITCH("Int Spk"),
170 static int tegra_wm8903_init(struct snd_soc_pcm_runtime *rtd)
172 struct snd_soc_dai *codec_dai = rtd->codec_dai;
173 struct snd_soc_codec *codec = codec_dai->codec;
174 struct snd_soc_card *card = rtd->card;
175 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
177 if (gpio_is_valid(machine->gpio_hp_det)) {
178 tegra_wm8903_hp_jack_gpio.gpio = machine->gpio_hp_det;
179 snd_soc_card_jack_new(rtd->card, "Headphone Jack",
180 SND_JACK_HEADPHONE, &tegra_wm8903_hp_jack,
181 tegra_wm8903_hp_jack_pins,
182 ARRAY_SIZE(tegra_wm8903_hp_jack_pins));
183 snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack,
185 &tegra_wm8903_hp_jack_gpio);
188 snd_soc_card_jack_new(rtd->card, "Mic Jack", SND_JACK_MICROPHONE,
189 &tegra_wm8903_mic_jack,
190 tegra_wm8903_mic_jack_pins,
191 ARRAY_SIZE(tegra_wm8903_mic_jack_pins));
192 wm8903_mic_detect(codec, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE,
195 snd_soc_dapm_force_enable_pin(&card->dapm, "MICBIAS");
200 static int tegra_wm8903_remove(struct snd_soc_card *card)
202 struct snd_soc_pcm_runtime *rtd =
203 snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
204 struct snd_soc_dai *codec_dai = rtd->codec_dai;
205 struct snd_soc_codec *codec = codec_dai->codec;
206 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
208 if (gpio_is_valid(machine->gpio_hp_det)) {
209 snd_soc_jack_free_gpios(&tegra_wm8903_hp_jack, 1,
210 &tegra_wm8903_hp_jack_gpio);
213 wm8903_mic_detect(codec, NULL, 0, 0);
218 static struct snd_soc_dai_link tegra_wm8903_dai = {
220 .stream_name = "WM8903 PCM",
221 .codec_dai_name = "wm8903-hifi",
222 .init = tegra_wm8903_init,
223 .ops = &tegra_wm8903_ops,
224 .dai_fmt = SND_SOC_DAIFMT_I2S |
225 SND_SOC_DAIFMT_NB_NF |
226 SND_SOC_DAIFMT_CBS_CFS,
229 static struct snd_soc_card snd_soc_tegra_wm8903 = {
230 .name = "tegra-wm8903",
231 .driver_name = "tegra",
232 .owner = THIS_MODULE,
233 .dai_link = &tegra_wm8903_dai,
235 .remove = tegra_wm8903_remove,
236 .controls = tegra_wm8903_controls,
237 .num_controls = ARRAY_SIZE(tegra_wm8903_controls),
238 .dapm_widgets = tegra_wm8903_dapm_widgets,
239 .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
240 .fully_routed = true,
243 static int tegra_wm8903_driver_probe(struct platform_device *pdev)
245 struct device_node *np = pdev->dev.of_node;
246 struct snd_soc_card *card = &snd_soc_tegra_wm8903;
247 struct tegra_wm8903 *machine;
250 machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8903),
253 dev_err(&pdev->dev, "Can't allocate tegra_wm8903 struct\n");
257 card->dev = &pdev->dev;
258 platform_set_drvdata(pdev, card);
259 snd_soc_card_set_drvdata(card, machine);
261 machine->gpio_spkr_en = of_get_named_gpio(np, "nvidia,spkr-en-gpios",
263 if (machine->gpio_spkr_en == -EPROBE_DEFER)
264 return -EPROBE_DEFER;
265 if (gpio_is_valid(machine->gpio_spkr_en)) {
266 ret = devm_gpio_request_one(&pdev->dev, machine->gpio_spkr_en,
267 GPIOF_OUT_INIT_LOW, "spkr_en");
269 dev_err(card->dev, "cannot get spkr_en gpio\n");
274 machine->gpio_hp_mute = of_get_named_gpio(np, "nvidia,hp-mute-gpios",
276 if (machine->gpio_hp_mute == -EPROBE_DEFER)
277 return -EPROBE_DEFER;
278 if (gpio_is_valid(machine->gpio_hp_mute)) {
279 ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_mute,
280 GPIOF_OUT_INIT_HIGH, "hp_mute");
282 dev_err(card->dev, "cannot get hp_mute gpio\n");
287 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
288 if (machine->gpio_hp_det == -EPROBE_DEFER)
289 return -EPROBE_DEFER;
291 machine->gpio_int_mic_en = of_get_named_gpio(np,
292 "nvidia,int-mic-en-gpios", 0);
293 if (machine->gpio_int_mic_en == -EPROBE_DEFER)
294 return -EPROBE_DEFER;
295 if (gpio_is_valid(machine->gpio_int_mic_en)) {
296 /* Disable int mic; enable signal is active-high */
297 ret = devm_gpio_request_one(&pdev->dev,
298 machine->gpio_int_mic_en,
299 GPIOF_OUT_INIT_LOW, "int_mic_en");
301 dev_err(card->dev, "cannot get int_mic_en gpio\n");
306 machine->gpio_ext_mic_en = of_get_named_gpio(np,
307 "nvidia,ext-mic-en-gpios", 0);
308 if (machine->gpio_ext_mic_en == -EPROBE_DEFER)
309 return -EPROBE_DEFER;
310 if (gpio_is_valid(machine->gpio_ext_mic_en)) {
311 /* Enable ext mic; enable signal is active-low */
312 ret = devm_gpio_request_one(&pdev->dev,
313 machine->gpio_ext_mic_en,
314 GPIOF_OUT_INIT_LOW, "ext_mic_en");
316 dev_err(card->dev, "cannot get ext_mic_en gpio\n");
321 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
325 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
329 tegra_wm8903_dai.codec_of_node = of_parse_phandle(np,
330 "nvidia,audio-codec", 0);
331 if (!tegra_wm8903_dai.codec_of_node) {
333 "Property 'nvidia,audio-codec' missing or invalid\n");
338 tegra_wm8903_dai.cpu_of_node = of_parse_phandle(np,
339 "nvidia,i2s-controller", 0);
340 if (!tegra_wm8903_dai.cpu_of_node) {
342 "Property 'nvidia,i2s-controller' missing or invalid\n");
347 tegra_wm8903_dai.platform_of_node = tegra_wm8903_dai.cpu_of_node;
349 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
353 ret = snd_soc_register_card(card);
355 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
363 tegra_asoc_utils_fini(&machine->util_data);
368 static int tegra_wm8903_driver_remove(struct platform_device *pdev)
370 struct snd_soc_card *card = platform_get_drvdata(pdev);
371 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
373 snd_soc_unregister_card(card);
375 tegra_asoc_utils_fini(&machine->util_data);
380 static const struct of_device_id tegra_wm8903_of_match[] = {
381 { .compatible = "nvidia,tegra-audio-wm8903", },
385 static struct platform_driver tegra_wm8903_driver = {
388 .pm = &snd_soc_pm_ops,
389 .of_match_table = tegra_wm8903_of_match,
391 .probe = tegra_wm8903_driver_probe,
392 .remove = tegra_wm8903_driver_remove,
394 module_platform_driver(tegra_wm8903_driver);
396 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
397 MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
398 MODULE_LICENSE("GPL");
399 MODULE_ALIAS("platform:" DRV_NAME);
400 MODULE_DEVICE_TABLE(of, tegra_wm8903_of_match);