blob: c5d45a6147bd02e552c2b51dd9ce9da0daa0a822 [file] [log] [blame]
Marcel Ziswiler04445682016-06-19 03:00:00 +02001/*
2 * tegra_sgtl5000.c - Tegra machine ASoC driver for boards using SGTL5000 codec
3 *
4 * Author: Marcel Ziswiler <marcel@ziswiler.com>
5 *
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.
9 *
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
13 * more details.
14 *
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/>.
17 *
18 * Based on code copyright/by:
19 *
20 * Copyright (C) 2010-2012 - NVIDIA, Inc.
21 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
22 * Copyright 2007 Wolfson Microelectronics PLC.
23 */
24
25#include <linux/module.h>
26#include <linux/platform_device.h>
27#include <linux/slab.h>
28#include <linux/gpio.h>
29#include <linux/of_gpio.h>
30
31#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
35
36#include "../codecs/sgtl5000.h"
37
38#include "tegra_asoc_utils.h"
39
40#define DRV_NAME "tegra-snd-sgtl5000"
41
42struct tegra_sgtl5000 {
43 struct tegra_asoc_utils_data util_data;
44};
45
46static int tegra_sgtl5000_hw_params(struct snd_pcm_substream *substream,
47 struct snd_pcm_hw_params *params)
48{
49 struct snd_soc_pcm_runtime *rtd = substream->private_data;
50 struct snd_soc_dai *codec_dai = rtd->codec_dai;
51 struct snd_soc_card *card = rtd->card;
52 struct tegra_sgtl5000 *machine = snd_soc_card_get_drvdata(card);
53 int srate, mclk;
54 int err;
55
56 srate = params_rate(params);
57 switch (srate) {
58 case 11025:
59 case 22050:
60 case 44100:
61 case 88200:
62 mclk = 11289600;
63 break;
64 default:
65 mclk = 12288000;
66 break;
67 }
68
69 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
70 if (err < 0) {
71 dev_err(card->dev, "Can't configure clocks\n");
72 return err;
73 }
74
75 err = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk,
76 SND_SOC_CLOCK_IN);
77 if (err < 0) {
78 dev_err(card->dev, "codec_dai clock not set\n");
79 return err;
80 }
81
82 return 0;
83}
84
Julia Lawall8a7a2822016-10-15 16:55:51 +020085static const struct snd_soc_ops tegra_sgtl5000_ops = {
Marcel Ziswiler04445682016-06-19 03:00:00 +020086 .hw_params = tegra_sgtl5000_hw_params,
87};
88
89static const struct snd_soc_dapm_widget tegra_sgtl5000_dapm_widgets[] = {
90 SND_SOC_DAPM_HP("Headphone Jack", NULL),
91 SND_SOC_DAPM_LINE("Line In Jack", NULL),
92 SND_SOC_DAPM_MIC("Mic Jack", NULL),
93};
94
Kuninori Morimotof4d9dd92019-06-06 13:18:51 +090095SND_SOC_DAILINK_DEFS(hifi,
96 DAILINK_COMP_ARRAY(COMP_EMPTY()),
97 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "sgtl5000")),
98 DAILINK_COMP_ARRAY(COMP_EMPTY()));
99
Marcel Ziswiler04445682016-06-19 03:00:00 +0200100static struct snd_soc_dai_link tegra_sgtl5000_dai = {
101 .name = "sgtl5000",
102 .stream_name = "HiFi",
Marcel Ziswiler04445682016-06-19 03:00:00 +0200103 .ops = &tegra_sgtl5000_ops,
104 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
105 SND_SOC_DAIFMT_CBS_CFS,
Kuninori Morimotof4d9dd92019-06-06 13:18:51 +0900106 SND_SOC_DAILINK_REG(hifi),
Marcel Ziswiler04445682016-06-19 03:00:00 +0200107};
108
109static struct snd_soc_card snd_soc_tegra_sgtl5000 = {
110 .name = "tegra-sgtl5000",
111 .owner = THIS_MODULE,
112 .dai_link = &tegra_sgtl5000_dai,
113 .num_links = 1,
114 .dapm_widgets = tegra_sgtl5000_dapm_widgets,
115 .num_dapm_widgets = ARRAY_SIZE(tegra_sgtl5000_dapm_widgets),
116 .fully_routed = true,
117};
118
119static int tegra_sgtl5000_driver_probe(struct platform_device *pdev)
120{
121 struct device_node *np = pdev->dev.of_node;
122 struct snd_soc_card *card = &snd_soc_tegra_sgtl5000;
123 struct tegra_sgtl5000 *machine;
124 int ret;
125
126 machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_sgtl5000),
127 GFP_KERNEL);
Codrut Grosue2c187a2017-02-25 13:18:08 +0200128 if (!machine)
Marcel Ziswiler04445682016-06-19 03:00:00 +0200129 return -ENOMEM;
Marcel Ziswiler04445682016-06-19 03:00:00 +0200130
131 card->dev = &pdev->dev;
Marcel Ziswiler04445682016-06-19 03:00:00 +0200132 snd_soc_card_set_drvdata(card, machine);
133
134 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
135 if (ret)
136 goto err;
137
138 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
139 if (ret)
140 goto err;
141
Kuninori Morimotof4d9dd92019-06-06 13:18:51 +0900142 tegra_sgtl5000_dai.codecs->of_node = of_parse_phandle(np,
Marcel Ziswiler04445682016-06-19 03:00:00 +0200143 "nvidia,audio-codec", 0);
Kuninori Morimotof4d9dd92019-06-06 13:18:51 +0900144 if (!tegra_sgtl5000_dai.codecs->of_node) {
Marcel Ziswiler04445682016-06-19 03:00:00 +0200145 dev_err(&pdev->dev,
146 "Property 'nvidia,audio-codec' missing or invalid\n");
147 ret = -EINVAL;
148 goto err;
149 }
150
Kuninori Morimotof4d9dd92019-06-06 13:18:51 +0900151 tegra_sgtl5000_dai.cpus->of_node = of_parse_phandle(np,
Marcel Ziswiler04445682016-06-19 03:00:00 +0200152 "nvidia,i2s-controller", 0);
Kuninori Morimotof4d9dd92019-06-06 13:18:51 +0900153 if (!tegra_sgtl5000_dai.cpus->of_node) {
Marcel Ziswiler04445682016-06-19 03:00:00 +0200154 dev_err(&pdev->dev,
155 "Property 'nvidia,i2s-controller' missing/invalid\n");
156 ret = -EINVAL;
Marcel Ziswilera85227d2018-10-16 12:47:29 +0200157 goto err_put_codec_of_node;
Marcel Ziswiler04445682016-06-19 03:00:00 +0200158 }
159
Kuninori Morimotof4d9dd92019-06-06 13:18:51 +0900160 tegra_sgtl5000_dai.platforms->of_node = tegra_sgtl5000_dai.cpus->of_node;
Marcel Ziswiler04445682016-06-19 03:00:00 +0200161
162 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
163 if (ret)
Marcel Ziswilera85227d2018-10-16 12:47:29 +0200164 goto err_put_cpu_of_node;
Marcel Ziswiler04445682016-06-19 03:00:00 +0200165
166 ret = snd_soc_register_card(card);
167 if (ret) {
168 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
169 ret);
170 goto err_fini_utils;
171 }
172
173 return 0;
174
175err_fini_utils:
176 tegra_asoc_utils_fini(&machine->util_data);
Marcel Ziswilera85227d2018-10-16 12:47:29 +0200177err_put_cpu_of_node:
Kuninori Morimotof4d9dd92019-06-06 13:18:51 +0900178 of_node_put(tegra_sgtl5000_dai.cpus->of_node);
179 tegra_sgtl5000_dai.cpus->of_node = NULL;
180 tegra_sgtl5000_dai.platforms->of_node = NULL;
Marcel Ziswilera85227d2018-10-16 12:47:29 +0200181err_put_codec_of_node:
Kuninori Morimotof4d9dd92019-06-06 13:18:51 +0900182 of_node_put(tegra_sgtl5000_dai.codecs->of_node);
183 tegra_sgtl5000_dai.codecs->of_node = NULL;
Marcel Ziswiler04445682016-06-19 03:00:00 +0200184err:
185 return ret;
186}
187
188static int tegra_sgtl5000_driver_remove(struct platform_device *pdev)
189{
190 struct snd_soc_card *card = platform_get_drvdata(pdev);
191 struct tegra_sgtl5000 *machine = snd_soc_card_get_drvdata(card);
192 int ret;
193
194 ret = snd_soc_unregister_card(card);
195
196 tegra_asoc_utils_fini(&machine->util_data);
197
Kuninori Morimotof4d9dd92019-06-06 13:18:51 +0900198 of_node_put(tegra_sgtl5000_dai.cpus->of_node);
199 tegra_sgtl5000_dai.cpus->of_node = NULL;
200 tegra_sgtl5000_dai.platforms->of_node = NULL;
201 of_node_put(tegra_sgtl5000_dai.codecs->of_node);
202 tegra_sgtl5000_dai.codecs->of_node = NULL;
Marcel Ziswilera85227d2018-10-16 12:47:29 +0200203
Marcel Ziswiler04445682016-06-19 03:00:00 +0200204 return ret;
205}
206
207static const struct of_device_id tegra_sgtl5000_of_match[] = {
208 { .compatible = "nvidia,tegra-audio-sgtl5000", },
209 { /* sentinel */ },
210};
211
212static struct platform_driver tegra_sgtl5000_driver = {
213 .driver = {
214 .name = DRV_NAME,
215 .pm = &snd_soc_pm_ops,
216 .of_match_table = tegra_sgtl5000_of_match,
217 },
218 .probe = tegra_sgtl5000_driver_probe,
219 .remove = tegra_sgtl5000_driver_remove,
220};
221module_platform_driver(tegra_sgtl5000_driver);
222
223MODULE_AUTHOR("Marcel Ziswiler <marcel@ziswiler.com>");
224MODULE_DESCRIPTION("Tegra SGTL5000 machine ASoC driver");
225MODULE_LICENSE("GPL v2");
226MODULE_ALIAS("platform:" DRV_NAME);
227MODULE_DEVICE_TABLE(of, tegra_sgtl5000_of_match);