1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19#include <linux/gpio.h>
20#include <linux/of_gpio.h>
21
22#include <sound/core.h>
23#include <sound/jack.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/soc.h>
27
28#include "../codecs/alc5632.h"
29
30#include "tegra_asoc_utils.h"
31
32#define DRV_NAME "tegra-alc5632"
33
34struct tegra_alc5632 {
35 struct tegra_asoc_utils_data util_data;
36 int gpio_hp_det;
37};
38
39static int tegra_alc5632_asoc_hw_params(struct snd_pcm_substream *substream,
40 struct snd_pcm_hw_params *params)
41{
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_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
46 int srate, mclk;
47 int err;
48
49 srate = params_rate(params);
50 mclk = 512 * srate;
51
52 err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk);
53 if (err < 0) {
54 dev_err(card->dev, "Can't configure clocks\n");
55 return err;
56 }
57
58 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
59 SND_SOC_CLOCK_IN);
60 if (err < 0) {
61 dev_err(card->dev, "codec_dai clock not set\n");
62 return err;
63 }
64
65 return 0;
66}
67
68static const struct snd_soc_ops tegra_alc5632_asoc_ops = {
69 .hw_params = tegra_alc5632_asoc_hw_params,
70};
71
72static struct snd_soc_jack tegra_alc5632_hs_jack;
73
74static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = {
75 {
76 .pin = "Headset Mic",
77 .mask = SND_JACK_MICROPHONE,
78 },
79 {
80 .pin = "Headset Stereophone",
81 .mask = SND_JACK_HEADPHONE,
82 },
83};
84
85static struct snd_soc_jack_gpio tegra_alc5632_hp_jack_gpio = {
86 .name = "Headset detection",
87 .report = SND_JACK_HEADSET,
88 .debounce_time = 150,
89};
90
91static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
92 SND_SOC_DAPM_SPK("Int Spk", NULL),
93 SND_SOC_DAPM_HP("Headset Stereophone", NULL),
94 SND_SOC_DAPM_MIC("Headset Mic", NULL),
95 SND_SOC_DAPM_MIC("Digital Mic", NULL),
96};
97
98static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
99 SOC_DAPM_PIN_SWITCH("Int Spk"),
100};
101
102static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
103{
104 int ret;
105 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(rtd->card);
106
107 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
108 SND_JACK_HEADSET,
109 &tegra_alc5632_hs_jack,
110 tegra_alc5632_hs_jack_pins,
111 ARRAY_SIZE(tegra_alc5632_hs_jack_pins));
112 if (ret)
113 return ret;
114
115 if (gpio_is_valid(machine->gpio_hp_det)) {
116 tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det;
117 snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack,
118 1,
119 &tegra_alc5632_hp_jack_gpio);
120 }
121
122 snd_soc_dapm_force_enable_pin(&rtd->card->dapm, "MICBIAS1");
123
124 return 0;
125}
126
127static struct snd_soc_dai_link tegra_alc5632_dai = {
128 .name = "ALC5632",
129 .stream_name = "ALC5632 PCM",
130 .codec_dai_name = "alc5632-hifi",
131 .init = tegra_alc5632_asoc_init,
132 .ops = &tegra_alc5632_asoc_ops,
133 .dai_fmt = SND_SOC_DAIFMT_I2S
134 | SND_SOC_DAIFMT_NB_NF
135 | SND_SOC_DAIFMT_CBS_CFS,
136};
137
138static struct snd_soc_card snd_soc_tegra_alc5632 = {
139 .name = "tegra-alc5632",
140 .owner = THIS_MODULE,
141 .dai_link = &tegra_alc5632_dai,
142 .num_links = 1,
143 .controls = tegra_alc5632_controls,
144 .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
145 .dapm_widgets = tegra_alc5632_dapm_widgets,
146 .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
147 .fully_routed = true,
148};
149
150static int tegra_alc5632_probe(struct platform_device *pdev)
151{
152 struct device_node *np = pdev->dev.of_node;
153 struct snd_soc_card *card = &snd_soc_tegra_alc5632;
154 struct tegra_alc5632 *alc5632;
155 int ret;
156
157 alc5632 = devm_kzalloc(&pdev->dev,
158 sizeof(struct tegra_alc5632), GFP_KERNEL);
159 if (!alc5632)
160 return -ENOMEM;
161
162 card->dev = &pdev->dev;
163 snd_soc_card_set_drvdata(card, alc5632);
164
165 alc5632->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
166 if (alc5632->gpio_hp_det == -EPROBE_DEFER)
167 return -EPROBE_DEFER;
168
169 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
170 if (ret)
171 goto err;
172
173 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
174 if (ret)
175 goto err;
176
177 tegra_alc5632_dai.codec_of_node = of_parse_phandle(
178 pdev->dev.of_node, "nvidia,audio-codec", 0);
179
180 if (!tegra_alc5632_dai.codec_of_node) {
181 dev_err(&pdev->dev,
182 "Property 'nvidia,audio-codec' missing or invalid\n");
183 ret = -EINVAL;
184 goto err;
185 }
186
187 tegra_alc5632_dai.cpu_of_node = of_parse_phandle(np,
188 "nvidia,i2s-controller", 0);
189 if (!tegra_alc5632_dai.cpu_of_node) {
190 dev_err(&pdev->dev,
191 "Property 'nvidia,i2s-controller' missing or invalid\n");
192 ret = -EINVAL;
193 goto err;
194 }
195
196 tegra_alc5632_dai.platform_of_node = tegra_alc5632_dai.cpu_of_node;
197
198 ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
199 if (ret)
200 goto err;
201
202 ret = snd_soc_register_card(card);
203 if (ret) {
204 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
205 ret);
206 goto err_fini_utils;
207 }
208
209 return 0;
210
211err_fini_utils:
212 tegra_asoc_utils_fini(&alc5632->util_data);
213err:
214 return ret;
215}
216
217static int tegra_alc5632_remove(struct platform_device *pdev)
218{
219 struct snd_soc_card *card = platform_get_drvdata(pdev);
220 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
221
222 snd_soc_unregister_card(card);
223
224 tegra_asoc_utils_fini(&machine->util_data);
225
226 return 0;
227}
228
229static const struct of_device_id tegra_alc5632_of_match[] = {
230 { .compatible = "nvidia,tegra-audio-alc5632", },
231 {},
232};
233
234static struct platform_driver tegra_alc5632_driver = {
235 .driver = {
236 .name = DRV_NAME,
237 .pm = &snd_soc_pm_ops,
238 .of_match_table = tegra_alc5632_of_match,
239 },
240 .probe = tegra_alc5632_probe,
241 .remove = tegra_alc5632_remove,
242};
243module_platform_driver(tegra_alc5632_driver);
244
245MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
246MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
247MODULE_LICENSE("GPL");
248MODULE_ALIAS("platform:" DRV_NAME);
249MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);
250