1 // SPDX-License-Identifier: GPL-2.0-only
3 * tegra_rt5640.c - Tegra machine ASoC driver for boards using RT5640 codec.
5 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
7 * Based on code copyright/by:
9 * Copyright (C) 2010-2012 - NVIDIA, Inc.
10 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
11 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
12 * Copyright 2007 Wolfson Microelectronics PLC.
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/gpio.h>
19 #include <linux/of_gpio.h>
21 #include <sound/core.h>
22 #include <sound/jack.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
27 #include "../codecs/rt5640.h"
29 #include "tegra_asoc_utils.h"
31 #define DRV_NAME "tegra-snd-rt5640"
34 struct tegra_asoc_utils_data util_data;
36 enum of_gpio_flags gpio_hp_det_flags;
39 static int tegra_rt5640_asoc_hw_params(struct snd_pcm_substream *substream,
40 struct snd_pcm_hw_params *params)
42 struct snd_soc_pcm_runtime *rtd = substream->private_data;
43 struct snd_soc_dai *codec_dai = rtd->codec_dai;
44 struct snd_soc_card *card = rtd->card;
45 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
49 srate = params_rate(params);
52 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
54 dev_err(card->dev, "Can't configure clocks\n");
58 err = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_MCLK, mclk,
61 dev_err(card->dev, "codec_dai clock not set\n");
68 static const struct snd_soc_ops tegra_rt5640_ops = {
69 .hw_params = tegra_rt5640_asoc_hw_params,
72 static struct snd_soc_jack tegra_rt5640_hp_jack;
74 static struct snd_soc_jack_pin tegra_rt5640_hp_jack_pins[] = {
77 .mask = SND_JACK_HEADPHONE,
81 static struct snd_soc_jack_gpio tegra_rt5640_hp_jack_gpio = {
82 .name = "Headphone detection",
83 .report = SND_JACK_HEADPHONE,
88 static const struct snd_soc_dapm_widget tegra_rt5640_dapm_widgets[] = {
89 SND_SOC_DAPM_HP("Headphones", NULL),
90 SND_SOC_DAPM_SPK("Speakers", NULL),
91 SND_SOC_DAPM_MIC("Mic Jack", NULL),
94 static const struct snd_kcontrol_new tegra_rt5640_controls[] = {
95 SOC_DAPM_PIN_SWITCH("Speakers"),
98 static int tegra_rt5640_asoc_init(struct snd_soc_pcm_runtime *rtd)
100 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(rtd->card);
102 snd_soc_card_jack_new(rtd->card, "Headphones", SND_JACK_HEADPHONE,
103 &tegra_rt5640_hp_jack, tegra_rt5640_hp_jack_pins,
104 ARRAY_SIZE(tegra_rt5640_hp_jack_pins));
106 if (gpio_is_valid(machine->gpio_hp_det)) {
107 tegra_rt5640_hp_jack_gpio.gpio = machine->gpio_hp_det;
108 tegra_rt5640_hp_jack_gpio.invert =
109 !!(machine->gpio_hp_det_flags & OF_GPIO_ACTIVE_LOW);
110 snd_soc_jack_add_gpios(&tegra_rt5640_hp_jack,
112 &tegra_rt5640_hp_jack_gpio);
118 static struct snd_soc_dai_link tegra_rt5640_dai = {
120 .stream_name = "RT5640 PCM",
121 .codec_dai_name = "rt5640-aif1",
122 .init = tegra_rt5640_asoc_init,
123 .ops = &tegra_rt5640_ops,
124 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
125 SND_SOC_DAIFMT_CBS_CFS,
128 static struct snd_soc_card snd_soc_tegra_rt5640 = {
129 .name = "tegra-rt5640",
130 .owner = THIS_MODULE,
131 .dai_link = &tegra_rt5640_dai,
133 .controls = tegra_rt5640_controls,
134 .num_controls = ARRAY_SIZE(tegra_rt5640_controls),
135 .dapm_widgets = tegra_rt5640_dapm_widgets,
136 .num_dapm_widgets = ARRAY_SIZE(tegra_rt5640_dapm_widgets),
137 .fully_routed = true,
140 static int tegra_rt5640_probe(struct platform_device *pdev)
142 struct device_node *np = pdev->dev.of_node;
143 struct snd_soc_card *card = &snd_soc_tegra_rt5640;
144 struct tegra_rt5640 *machine;
147 machine = devm_kzalloc(&pdev->dev,
148 sizeof(struct tegra_rt5640), GFP_KERNEL);
152 card->dev = &pdev->dev;
153 snd_soc_card_set_drvdata(card, machine);
155 machine->gpio_hp_det = of_get_named_gpio_flags(
156 np, "nvidia,hp-det-gpios", 0, &machine->gpio_hp_det_flags);
157 if (machine->gpio_hp_det == -EPROBE_DEFER)
158 return -EPROBE_DEFER;
160 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
164 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
168 tegra_rt5640_dai.codec_of_node = of_parse_phandle(np,
169 "nvidia,audio-codec", 0);
170 if (!tegra_rt5640_dai.codec_of_node) {
172 "Property 'nvidia,audio-codec' missing or invalid\n");
177 tegra_rt5640_dai.cpu_of_node = of_parse_phandle(np,
178 "nvidia,i2s-controller", 0);
179 if (!tegra_rt5640_dai.cpu_of_node) {
181 "Property 'nvidia,i2s-controller' missing or invalid\n");
186 tegra_rt5640_dai.platform_of_node = tegra_rt5640_dai.cpu_of_node;
188 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
192 ret = snd_soc_register_card(card);
194 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
202 tegra_asoc_utils_fini(&machine->util_data);
207 static int tegra_rt5640_remove(struct platform_device *pdev)
209 struct snd_soc_card *card = platform_get_drvdata(pdev);
210 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
212 snd_soc_unregister_card(card);
214 tegra_asoc_utils_fini(&machine->util_data);
219 static const struct of_device_id tegra_rt5640_of_match[] = {
220 { .compatible = "nvidia,tegra-audio-rt5640", },
224 static struct platform_driver tegra_rt5640_driver = {
227 .pm = &snd_soc_pm_ops,
228 .of_match_table = tegra_rt5640_of_match,
230 .probe = tegra_rt5640_probe,
231 .remove = tegra_rt5640_remove,
233 module_platform_driver(tegra_rt5640_driver);
235 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
236 MODULE_DESCRIPTION("Tegra+RT5640 machine ASoC driver");
237 MODULE_LICENSE("GPL v2");
238 MODULE_ALIAS("platform:" DRV_NAME);
239 MODULE_DEVICE_TABLE(of, tegra_rt5640_of_match);