2 * tegra_rt5677.c - Tegra machine ASoC driver for boards using RT5677 codec.
4 * Copyright (c) 2014, The Chromium OS Authors. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Based on code copyright/by:
20 * Copyright (C) 2010-2012 - NVIDIA, Inc.
21 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
22 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
23 * Copyright 2007 Wolfson Microelectronics PLC.
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/gpio.h>
30 #include <linux/of_gpio.h>
32 #include <sound/core.h>
33 #include <sound/jack.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/soc.h>
38 #include "../codecs/rt5677.h"
40 #include "tegra_asoc_utils.h"
42 #define DRV_NAME "tegra-snd-rt5677"
45 struct tegra_asoc_utils_data util_data;
52 static int tegra_rt5677_asoc_hw_params(struct snd_pcm_substream *substream,
53 struct snd_pcm_hw_params *params)
55 struct snd_soc_pcm_runtime *rtd = substream->private_data;
56 struct snd_soc_dai *codec_dai = rtd->codec_dai;
57 struct snd_soc_card *card = rtd->card;
58 struct tegra_rt5677 *machine = snd_soc_card_get_drvdata(card);
61 srate = params_rate(params);
64 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
66 dev_err(card->dev, "Can't configure clocks\n");
70 err = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_MCLK, mclk,
73 dev_err(card->dev, "codec_dai clock not set\n");
80 static int tegra_rt5677_event_hp(struct snd_soc_dapm_widget *w,
81 struct snd_kcontrol *k, int event)
83 struct snd_soc_dapm_context *dapm = w->dapm;
84 struct snd_soc_card *card = dapm->card;
85 struct tegra_rt5677 *machine = snd_soc_card_get_drvdata(card);
87 if (!gpio_is_valid(machine->gpio_hp_en))
90 gpio_set_value_cansleep(machine->gpio_hp_en,
91 SND_SOC_DAPM_EVENT_ON(event));
96 static struct snd_soc_ops tegra_rt5677_ops = {
97 .hw_params = tegra_rt5677_asoc_hw_params,
100 static struct snd_soc_jack tegra_rt5677_hp_jack;
102 static struct snd_soc_jack_pin tegra_rt5677_hp_jack_pins = {
104 .mask = SND_JACK_HEADPHONE,
106 static struct snd_soc_jack_gpio tegra_rt5677_hp_jack_gpio = {
107 .name = "Headphone detection",
108 .report = SND_JACK_HEADPHONE,
109 .debounce_time = 150,
112 static struct snd_soc_jack tegra_rt5677_mic_jack;
114 static struct snd_soc_jack_pin tegra_rt5677_mic_jack_pins = {
115 .pin = "Headset Mic",
116 .mask = SND_JACK_MICROPHONE,
119 static struct snd_soc_jack_gpio tegra_rt5677_mic_jack_gpio = {
120 .name = "Headset Mic detection",
121 .report = SND_JACK_MICROPHONE,
122 .debounce_time = 150,
126 static const struct snd_soc_dapm_widget tegra_rt5677_dapm_widgets[] = {
127 SND_SOC_DAPM_SPK("Speaker", NULL),
128 SND_SOC_DAPM_HP("Headphone", tegra_rt5677_event_hp),
129 SND_SOC_DAPM_MIC("Headset Mic", NULL),
130 SND_SOC_DAPM_MIC("Internal Mic 1", NULL),
131 SND_SOC_DAPM_MIC("Internal Mic 2", NULL),
134 static const struct snd_kcontrol_new tegra_rt5677_controls[] = {
135 SOC_DAPM_PIN_SWITCH("Speaker"),
136 SOC_DAPM_PIN_SWITCH("Headphone"),
137 SOC_DAPM_PIN_SWITCH("Headset Mic"),
138 SOC_DAPM_PIN_SWITCH("Internal Mic 1"),
139 SOC_DAPM_PIN_SWITCH("Internal Mic 2"),
142 static int tegra_rt5677_asoc_init(struct snd_soc_pcm_runtime *rtd)
144 struct snd_soc_dai *codec_dai = rtd->codec_dai;
145 struct snd_soc_codec *codec = codec_dai->codec;
146 struct snd_soc_dapm_context *dapm = &codec->dapm;
147 struct tegra_rt5677 *machine = snd_soc_card_get_drvdata(rtd->card);
149 snd_soc_card_jack_new(rtd->card, "Headphone Jack", SND_JACK_HEADPHONE,
150 &tegra_rt5677_hp_jack,
151 &tegra_rt5677_hp_jack_pins, 1);
153 if (gpio_is_valid(machine->gpio_hp_det)) {
154 tegra_rt5677_hp_jack_gpio.gpio = machine->gpio_hp_det;
155 snd_soc_jack_add_gpios(&tegra_rt5677_hp_jack, 1,
156 &tegra_rt5677_hp_jack_gpio);
160 snd_soc_card_jack_new(rtd->card, "Mic Jack", SND_JACK_MICROPHONE,
161 &tegra_rt5677_mic_jack,
162 &tegra_rt5677_mic_jack_pins, 1);
164 if (gpio_is_valid(machine->gpio_mic_present)) {
165 tegra_rt5677_mic_jack_gpio.gpio = machine->gpio_mic_present;
166 snd_soc_jack_add_gpios(&tegra_rt5677_mic_jack, 1,
167 &tegra_rt5677_mic_jack_gpio);
170 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
175 static int tegra_rt5677_card_remove(struct snd_soc_card *card)
177 struct tegra_rt5677 *machine = snd_soc_card_get_drvdata(card);
179 if (gpio_is_valid(machine->gpio_hp_det)) {
180 snd_soc_jack_free_gpios(&tegra_rt5677_hp_jack, 1,
181 &tegra_rt5677_hp_jack_gpio);
184 if (gpio_is_valid(machine->gpio_mic_present)) {
185 snd_soc_jack_free_gpios(&tegra_rt5677_mic_jack, 1,
186 &tegra_rt5677_mic_jack_gpio);
192 static struct snd_soc_dai_link tegra_rt5677_dai = {
194 .stream_name = "RT5677 PCM",
195 .codec_dai_name = "rt5677-aif1",
196 .init = tegra_rt5677_asoc_init,
197 .ops = &tegra_rt5677_ops,
198 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
199 SND_SOC_DAIFMT_CBS_CFS,
202 static struct snd_soc_card snd_soc_tegra_rt5677 = {
203 .name = "tegra-rt5677",
204 .owner = THIS_MODULE,
205 .remove = tegra_rt5677_card_remove,
206 .dai_link = &tegra_rt5677_dai,
208 .controls = tegra_rt5677_controls,
209 .num_controls = ARRAY_SIZE(tegra_rt5677_controls),
210 .dapm_widgets = tegra_rt5677_dapm_widgets,
211 .num_dapm_widgets = ARRAY_SIZE(tegra_rt5677_dapm_widgets),
212 .fully_routed = true,
215 static int tegra_rt5677_probe(struct platform_device *pdev)
217 struct device_node *np = pdev->dev.of_node;
218 struct snd_soc_card *card = &snd_soc_tegra_rt5677;
219 struct tegra_rt5677 *machine;
222 machine = devm_kzalloc(&pdev->dev,
223 sizeof(struct tegra_rt5677), GFP_KERNEL);
227 card->dev = &pdev->dev;
228 platform_set_drvdata(pdev, card);
229 snd_soc_card_set_drvdata(card, machine);
231 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
232 if (machine->gpio_hp_det == -EPROBE_DEFER)
233 return -EPROBE_DEFER;
235 machine->gpio_mic_present = of_get_named_gpio(np,
236 "nvidia,mic-present-gpios", 0);
237 if (machine->gpio_mic_present == -EPROBE_DEFER)
238 return -EPROBE_DEFER;
240 machine->gpio_hp_en = of_get_named_gpio(np, "nvidia,hp-en-gpios", 0);
241 if (machine->gpio_hp_en == -EPROBE_DEFER)
242 return -EPROBE_DEFER;
243 if (gpio_is_valid(machine->gpio_hp_en)) {
244 ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_en,
245 GPIOF_OUT_INIT_LOW, "hp_en");
247 dev_err(card->dev, "cannot get hp_en gpio\n");
252 machine->gpio_dmic_clk_en = of_get_named_gpio(np,
253 "nvidia,dmic-clk-en-gpios", 0);
254 if (machine->gpio_dmic_clk_en == -EPROBE_DEFER)
255 return -EPROBE_DEFER;
256 if (gpio_is_valid(machine->gpio_dmic_clk_en)) {
257 ret = devm_gpio_request_one(&pdev->dev,
258 machine->gpio_dmic_clk_en,
259 GPIOF_OUT_INIT_HIGH, "dmic_clk_en");
261 dev_err(card->dev, "cannot get dmic_clk_en gpio\n");
266 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
270 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
274 tegra_rt5677_dai.codec_of_node = of_parse_phandle(np,
275 "nvidia,audio-codec", 0);
276 if (!tegra_rt5677_dai.codec_of_node) {
278 "Property 'nvidia,audio-codec' missing or invalid\n");
283 tegra_rt5677_dai.cpu_of_node = of_parse_phandle(np,
284 "nvidia,i2s-controller", 0);
285 if (!tegra_rt5677_dai.cpu_of_node) {
287 "Property 'nvidia,i2s-controller' missing or invalid\n");
291 tegra_rt5677_dai.platform_of_node = tegra_rt5677_dai.cpu_of_node;
293 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
297 ret = snd_soc_register_card(card);
299 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
307 tegra_asoc_utils_fini(&machine->util_data);
312 static int tegra_rt5677_remove(struct platform_device *pdev)
314 struct snd_soc_card *card = platform_get_drvdata(pdev);
315 struct tegra_rt5677 *machine = snd_soc_card_get_drvdata(card);
317 snd_soc_unregister_card(card);
319 tegra_asoc_utils_fini(&machine->util_data);
324 static const struct of_device_id tegra_rt5677_of_match[] = {
325 { .compatible = "nvidia,tegra-audio-rt5677", },
329 static struct platform_driver tegra_rt5677_driver = {
332 .owner = THIS_MODULE,
333 .pm = &snd_soc_pm_ops,
334 .of_match_table = tegra_rt5677_of_match,
336 .probe = tegra_rt5677_probe,
337 .remove = tegra_rt5677_remove,
339 module_platform_driver(tegra_rt5677_driver);
341 MODULE_AUTHOR("Anatol Pomozov <anatol@google.com>");
342 MODULE_DESCRIPTION("Tegra+RT5677 machine ASoC driver");
343 MODULE_LICENSE("GPL v2");
344 MODULE_ALIAS("platform:" DRV_NAME);
345 MODULE_DEVICE_TABLE(of, tegra_rt5677_of_match);