1 // SPDX-License-Identifier: GPL-2.0-only
3 * tegra_sgtl5000.c - Tegra machine ASoC driver for boards using SGTL5000 codec
5 * Author: Marcel Ziswiler <marcel@ziswiler.com>
7 * Based on code copyright/by:
9 * Copyright (C) 2010-2012 - NVIDIA, Inc.
10 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
11 * Copyright 2007 Wolfson Microelectronics PLC.
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/gpio.h>
18 #include <linux/of_gpio.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
25 #include "../codecs/sgtl5000.h"
27 #include "tegra_asoc_utils.h"
29 #define DRV_NAME "tegra-snd-sgtl5000"
31 struct tegra_sgtl5000 {
32 struct tegra_asoc_utils_data util_data;
35 static int tegra_sgtl5000_hw_params(struct snd_pcm_substream *substream,
36 struct snd_pcm_hw_params *params)
38 struct snd_soc_pcm_runtime *rtd = substream->private_data;
39 struct snd_soc_dai *codec_dai = rtd->codec_dai;
40 struct snd_soc_card *card = rtd->card;
41 struct tegra_sgtl5000 *machine = snd_soc_card_get_drvdata(card);
45 srate = params_rate(params);
58 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
60 dev_err(card->dev, "Can't configure clocks\n");
64 err = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk,
67 dev_err(card->dev, "codec_dai clock not set\n");
74 static const struct snd_soc_ops tegra_sgtl5000_ops = {
75 .hw_params = tegra_sgtl5000_hw_params,
78 static const struct snd_soc_dapm_widget tegra_sgtl5000_dapm_widgets[] = {
79 SND_SOC_DAPM_HP("Headphone Jack", NULL),
80 SND_SOC_DAPM_LINE("Line In Jack", NULL),
81 SND_SOC_DAPM_MIC("Mic Jack", NULL),
84 SND_SOC_DAILINK_DEFS(hifi,
85 DAILINK_COMP_ARRAY(COMP_EMPTY()),
86 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "sgtl5000")),
87 DAILINK_COMP_ARRAY(COMP_EMPTY()));
89 static struct snd_soc_dai_link tegra_sgtl5000_dai = {
91 .stream_name = "HiFi",
92 .ops = &tegra_sgtl5000_ops,
93 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
94 SND_SOC_DAIFMT_CBS_CFS,
95 SND_SOC_DAILINK_REG(hifi),
98 static struct snd_soc_card snd_soc_tegra_sgtl5000 = {
99 .name = "tegra-sgtl5000",
100 .driver_name = "tegra",
101 .owner = THIS_MODULE,
102 .dai_link = &tegra_sgtl5000_dai,
104 .dapm_widgets = tegra_sgtl5000_dapm_widgets,
105 .num_dapm_widgets = ARRAY_SIZE(tegra_sgtl5000_dapm_widgets),
106 .fully_routed = true,
109 static int tegra_sgtl5000_driver_probe(struct platform_device *pdev)
111 struct device_node *np = pdev->dev.of_node;
112 struct snd_soc_card *card = &snd_soc_tegra_sgtl5000;
113 struct tegra_sgtl5000 *machine;
116 machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_sgtl5000),
121 card->dev = &pdev->dev;
122 snd_soc_card_set_drvdata(card, machine);
124 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
128 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
132 tegra_sgtl5000_dai.codecs->of_node = of_parse_phandle(np,
133 "nvidia,audio-codec", 0);
134 if (!tegra_sgtl5000_dai.codecs->of_node) {
136 "Property 'nvidia,audio-codec' missing or invalid\n");
141 tegra_sgtl5000_dai.cpus->of_node = of_parse_phandle(np,
142 "nvidia,i2s-controller", 0);
143 if (!tegra_sgtl5000_dai.cpus->of_node) {
145 "Property 'nvidia,i2s-controller' missing/invalid\n");
147 goto err_put_codec_of_node;
150 tegra_sgtl5000_dai.platforms->of_node = tegra_sgtl5000_dai.cpus->of_node;
152 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
154 goto err_put_cpu_of_node;
156 ret = snd_soc_register_card(card);
158 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
166 tegra_asoc_utils_fini(&machine->util_data);
168 of_node_put(tegra_sgtl5000_dai.cpus->of_node);
169 tegra_sgtl5000_dai.cpus->of_node = NULL;
170 tegra_sgtl5000_dai.platforms->of_node = NULL;
171 err_put_codec_of_node:
172 of_node_put(tegra_sgtl5000_dai.codecs->of_node);
173 tegra_sgtl5000_dai.codecs->of_node = NULL;
178 static int tegra_sgtl5000_driver_remove(struct platform_device *pdev)
180 struct snd_soc_card *card = platform_get_drvdata(pdev);
181 struct tegra_sgtl5000 *machine = snd_soc_card_get_drvdata(card);
184 ret = snd_soc_unregister_card(card);
186 tegra_asoc_utils_fini(&machine->util_data);
188 of_node_put(tegra_sgtl5000_dai.cpus->of_node);
189 tegra_sgtl5000_dai.cpus->of_node = NULL;
190 tegra_sgtl5000_dai.platforms->of_node = NULL;
191 of_node_put(tegra_sgtl5000_dai.codecs->of_node);
192 tegra_sgtl5000_dai.codecs->of_node = NULL;
197 static const struct of_device_id tegra_sgtl5000_of_match[] = {
198 { .compatible = "nvidia,tegra-audio-sgtl5000", },
202 static struct platform_driver tegra_sgtl5000_driver = {
205 .pm = &snd_soc_pm_ops,
206 .of_match_table = tegra_sgtl5000_of_match,
208 .probe = tegra_sgtl5000_driver_probe,
209 .remove = tegra_sgtl5000_driver_remove,
211 module_platform_driver(tegra_sgtl5000_driver);
213 MODULE_AUTHOR("Marcel Ziswiler <marcel@ziswiler.com>");
214 MODULE_DESCRIPTION("Tegra SGTL5000 machine ASoC driver");
215 MODULE_LICENSE("GPL v2");
216 MODULE_ALIAS("platform:" DRV_NAME);
217 MODULE_DEVICE_TABLE(of, tegra_sgtl5000_of_match);