1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
85static const struct snd_soc_ops tegra_sgtl5000_ops = {
86 .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
95static struct snd_soc_dai_link tegra_sgtl5000_dai = {
96 .name = "sgtl5000",
97 .stream_name = "HiFi",
98 .codec_dai_name = "sgtl5000",
99 .ops = &tegra_sgtl5000_ops,
100 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
101 SND_SOC_DAIFMT_CBS_CFS,
102};
103
104static struct snd_soc_card snd_soc_tegra_sgtl5000 = {
105 .name = "tegra-sgtl5000",
106 .owner = THIS_MODULE,
107 .dai_link = &tegra_sgtl5000_dai,
108 .num_links = 1,
109 .dapm_widgets = tegra_sgtl5000_dapm_widgets,
110 .num_dapm_widgets = ARRAY_SIZE(tegra_sgtl5000_dapm_widgets),
111 .fully_routed = true,
112};
113
114static int tegra_sgtl5000_driver_probe(struct platform_device *pdev)
115{
116 struct device_node *np = pdev->dev.of_node;
117 struct snd_soc_card *card = &snd_soc_tegra_sgtl5000;
118 struct tegra_sgtl5000 *machine;
119 int ret;
120
121 machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_sgtl5000),
122 GFP_KERNEL);
123 if (!machine)
124 return -ENOMEM;
125
126 card->dev = &pdev->dev;
127 snd_soc_card_set_drvdata(card, machine);
128
129 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
130 if (ret)
131 goto err;
132
133 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
134 if (ret)
135 goto err;
136
137 tegra_sgtl5000_dai.codec_of_node = of_parse_phandle(np,
138 "nvidia,audio-codec", 0);
139 if (!tegra_sgtl5000_dai.codec_of_node) {
140 dev_err(&pdev->dev,
141 "Property 'nvidia,audio-codec' missing or invalid\n");
142 ret = -EINVAL;
143 goto err;
144 }
145
146 tegra_sgtl5000_dai.cpu_of_node = of_parse_phandle(np,
147 "nvidia,i2s-controller", 0);
148 if (!tegra_sgtl5000_dai.cpu_of_node) {
149 dev_err(&pdev->dev,
150 "Property 'nvidia,i2s-controller' missing/invalid\n");
151 ret = -EINVAL;
152 goto err;
153 }
154
155 tegra_sgtl5000_dai.platform_of_node = tegra_sgtl5000_dai.cpu_of_node;
156
157 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
158 if (ret)
159 goto err;
160
161 ret = snd_soc_register_card(card);
162 if (ret) {
163 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
164 ret);
165 goto err_fini_utils;
166 }
167
168 return 0;
169
170err_fini_utils:
171 tegra_asoc_utils_fini(&machine->util_data);
172err:
173 return ret;
174}
175
176static int tegra_sgtl5000_driver_remove(struct platform_device *pdev)
177{
178 struct snd_soc_card *card = platform_get_drvdata(pdev);
179 struct tegra_sgtl5000 *machine = snd_soc_card_get_drvdata(card);
180 int ret;
181
182 ret = snd_soc_unregister_card(card);
183
184 tegra_asoc_utils_fini(&machine->util_data);
185
186 return ret;
187}
188
189static const struct of_device_id tegra_sgtl5000_of_match[] = {
190 { .compatible = "nvidia,tegra-audio-sgtl5000", },
191 { },
192};
193
194static struct platform_driver tegra_sgtl5000_driver = {
195 .driver = {
196 .name = DRV_NAME,
197 .pm = &snd_soc_pm_ops,
198 .of_match_table = tegra_sgtl5000_of_match,
199 },
200 .probe = tegra_sgtl5000_driver_probe,
201 .remove = tegra_sgtl5000_driver_remove,
202};
203module_platform_driver(tegra_sgtl5000_driver);
204
205MODULE_AUTHOR("Marcel Ziswiler <marcel@ziswiler.com>");
206MODULE_DESCRIPTION("Tegra SGTL5000 machine ASoC driver");
207MODULE_LICENSE("GPL v2");
208MODULE_ALIAS("platform:" DRV_NAME);
209MODULE_DEVICE_TABLE(of, tegra_sgtl5000_of_match);
210