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
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>
31
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>
37
38#include "../codecs/rt5640.h"
39
40#include "tegra_asoc_utils.h"
41
42#define DRV_NAME "tegra-snd-rt5640"
43
44struct tegra_rt5640 {
45 struct tegra_asoc_utils_data util_data;
46 int gpio_hp_det;
47};
48
49static int tegra_rt5640_asoc_hw_params(struct snd_pcm_substream *substream,
50 struct snd_pcm_hw_params *params)
51{
52 struct snd_soc_pcm_runtime *rtd = substream->private_data;
53 struct snd_soc_dai *codec_dai = rtd->codec_dai;
54 struct snd_soc_card *card = rtd->card;
55 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
56 int srate, mclk;
57 int err;
58
59 srate = params_rate(params);
60 mclk = 256 * srate;
61
62 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
63 if (err < 0) {
64 dev_err(card->dev, "Can't configure clocks\n");
65 return err;
66 }
67
68 err = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_MCLK, mclk,
69 SND_SOC_CLOCK_IN);
70 if (err < 0) {
71 dev_err(card->dev, "codec_dai clock not set\n");
72 return err;
73 }
74
75 return 0;
76}
77
78static struct snd_soc_ops tegra_rt5640_ops = {
79 .hw_params = tegra_rt5640_asoc_hw_params,
80};
81
82static struct snd_soc_jack tegra_rt5640_hp_jack;
83
84static struct snd_soc_jack_pin tegra_rt5640_hp_jack_pins[] = {
85 {
86 .pin = "Headphones",
87 .mask = SND_JACK_HEADPHONE,
88 },
89};
90
91static struct snd_soc_jack_gpio tegra_rt5640_hp_jack_gpio = {
92 .name = "Headphone detection",
93 .report = SND_JACK_HEADPHONE,
94 .debounce_time = 150,
95 .invert = 1,
96};
97
98static const struct snd_soc_dapm_widget tegra_rt5640_dapm_widgets[] = {
99 SND_SOC_DAPM_HP("Headphones", NULL),
100 SND_SOC_DAPM_SPK("Speakers", NULL),
101 SND_SOC_DAPM_MIC("Mic Jack", NULL),
102};
103
104static const struct snd_kcontrol_new tegra_rt5640_controls[] = {
105 SOC_DAPM_PIN_SWITCH("Speakers"),
106};
107
108static int tegra_rt5640_asoc_init(struct snd_soc_pcm_runtime *rtd)
109{
110 struct snd_soc_dai *codec_dai = rtd->codec_dai;
111 struct snd_soc_codec *codec = codec_dai->codec;
112 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(rtd->card);
113
114 snd_soc_jack_new(codec, "Headphones", SND_JACK_HEADPHONE,
115 &tegra_rt5640_hp_jack);
116 snd_soc_jack_add_pins(&tegra_rt5640_hp_jack,
117 ARRAY_SIZE(tegra_rt5640_hp_jack_pins),
118 tegra_rt5640_hp_jack_pins);
119
120 if (gpio_is_valid(machine->gpio_hp_det)) {
121 tegra_rt5640_hp_jack_gpio.gpio = machine->gpio_hp_det;
122 snd_soc_jack_add_gpios(&tegra_rt5640_hp_jack,
123 1,
124 &tegra_rt5640_hp_jack_gpio);
125 }
126
127 return 0;
128}
129
130static int tegra_rt5640_card_remove(struct snd_soc_card *card)
131{
132 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
133
134 if (gpio_is_valid(machine->gpio_hp_det)) {
135 snd_soc_jack_free_gpios(&tegra_rt5640_hp_jack, 1,
136 &tegra_rt5640_hp_jack_gpio);
137 }
138
139 return 0;
140}
141
142static struct snd_soc_dai_link tegra_rt5640_dai = {
143 .name = "RT5640",
144 .stream_name = "RT5640 PCM",
145 .codec_dai_name = "rt5640-aif1",
146 .init = tegra_rt5640_asoc_init,
147 .ops = &tegra_rt5640_ops,
148 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
149 SND_SOC_DAIFMT_CBS_CFS,
150};
151
152static struct snd_soc_card snd_soc_tegra_rt5640 = {
153 .name = "tegra-rt5640",
154 .owner = THIS_MODULE,
155 .remove = tegra_rt5640_card_remove,
156 .dai_link = &tegra_rt5640_dai,
157 .num_links = 1,
158 .controls = tegra_rt5640_controls,
159 .num_controls = ARRAY_SIZE(tegra_rt5640_controls),
160 .dapm_widgets = tegra_rt5640_dapm_widgets,
161 .num_dapm_widgets = ARRAY_SIZE(tegra_rt5640_dapm_widgets),
162 .fully_routed = true,
163};
164
165static int tegra_rt5640_probe(struct platform_device *pdev)
166{
167 struct device_node *np = pdev->dev.of_node;
168 struct snd_soc_card *card = &snd_soc_tegra_rt5640;
169 struct tegra_rt5640 *machine;
170 int ret;
171
172 machine = devm_kzalloc(&pdev->dev,
173 sizeof(struct tegra_rt5640), GFP_KERNEL);
174 if (!machine) {
175 dev_err(&pdev->dev, "Can't allocate tegra_rt5640\n");
176 return -ENOMEM;
177 }
178
179 card->dev = &pdev->dev;
180 platform_set_drvdata(pdev, card);
181 snd_soc_card_set_drvdata(card, machine);
182
183 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
184 if (machine->gpio_hp_det == -EPROBE_DEFER)
185 return -EPROBE_DEFER;
186
187 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
188 if (ret)
189 goto err;
190
191 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
192 if (ret)
193 goto err;
194
195 tegra_rt5640_dai.codec_of_node = of_parse_phandle(np,
196 "nvidia,audio-codec", 0);
197 if (!tegra_rt5640_dai.codec_of_node) {
198 dev_err(&pdev->dev,
199 "Property 'nvidia,audio-codec' missing or invalid\n");
200 ret = -EINVAL;
201 goto err;
202 }
203
204 tegra_rt5640_dai.cpu_of_node = of_parse_phandle(np,
205 "nvidia,i2s-controller", 0);
206 if (!tegra_rt5640_dai.cpu_of_node) {
207 dev_err(&pdev->dev,
208 "Property 'nvidia,i2s-controller' missing or invalid\n");
209 ret = -EINVAL;
210 goto err;
211 }
212
213 tegra_rt5640_dai.platform_of_node = tegra_rt5640_dai.cpu_of_node;
214
215 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
216 if (ret)
217 goto err;
218
219 ret = snd_soc_register_card(card);
220 if (ret) {
221 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
222 ret);
223 goto err_fini_utils;
224 }
225
226 return 0;
227
228err_fini_utils:
229 tegra_asoc_utils_fini(&machine->util_data);
230err:
231 return ret;
232}
233
234static int tegra_rt5640_remove(struct platform_device *pdev)
235{
236 struct snd_soc_card *card = platform_get_drvdata(pdev);
237 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
238
239 snd_soc_unregister_card(card);
240
241 tegra_asoc_utils_fini(&machine->util_data);
242
243 return 0;
244}
245
246static const struct of_device_id tegra_rt5640_of_match[] = {
247 { .compatible = "nvidia,tegra-audio-rt5640", },
248 {},
249};
250
251static struct platform_driver tegra_rt5640_driver = {
252 .driver = {
253 .name = DRV_NAME,
254 .owner = THIS_MODULE,
255 .pm = &snd_soc_pm_ops,
256 .of_match_table = tegra_rt5640_of_match,
257 },
258 .probe = tegra_rt5640_probe,
259 .remove = tegra_rt5640_remove,
260};
261module_platform_driver(tegra_rt5640_driver);
262
263MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
264MODULE_DESCRIPTION("Tegra+RT5640 machine ASoC driver");
265MODULE_LICENSE("GPL v2");
266MODULE_ALIAS("platform:" DRV_NAME);
267MODULE_DEVICE_TABLE(of, tegra_rt5640_of_match);
268