1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright (c) 2020, The Linux Foundation. All rights reserved.
5 // sc7180.c -- ALSA SoC Machine driver for SC7180
7 #include <dt-bindings/sound/sc7180-lpass.h>
8 #include <linux/gpio.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <sound/core.h>
14 #include <sound/jack.h>
15 #include <sound/pcm.h>
16 #include <sound/soc.h>
17 #include <uapi/linux/input-event-codes.h>
19 #include "../codecs/rt5682.h"
20 #include "../codecs/rt5682s.h"
24 #define DEFAULT_MCLK_RATE 19200000
25 #define RT5682_PLL1_FREQ (48000 * 512)
27 #define DRIVER_NAME "SC7180"
29 struct sc7180_snd_data {
30 struct snd_soc_card card;
31 u32 pri_mi2s_clk_count;
32 struct snd_soc_jack hs_jack;
33 struct snd_soc_jack hdmi_jack;
34 struct gpio_desc *dmic_sel;
38 static void sc7180_jack_free(struct snd_jack *jack)
40 struct snd_soc_component *component = jack->private_data;
42 snd_soc_component_set_jack(component, NULL, NULL);
45 static int sc7180_headset_init(struct snd_soc_pcm_runtime *rtd)
47 struct snd_soc_card *card = rtd->card;
48 struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card);
49 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
50 struct snd_soc_component *component = codec_dai->component;
51 struct snd_jack *jack;
54 rval = snd_soc_card_jack_new(
58 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
59 SND_JACK_BTN_2 | SND_JACK_BTN_3,
63 dev_err(card->dev, "Unable to add Headset Jack\n");
67 jack = pdata->hs_jack.jack;
69 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
70 snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
71 snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
72 snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
74 jack->private_data = component;
75 jack->private_free = sc7180_jack_free;
77 return snd_soc_component_set_jack(component, &pdata->hs_jack, NULL);
80 static int sc7180_hdmi_init(struct snd_soc_pcm_runtime *rtd)
82 struct snd_soc_card *card = rtd->card;
83 struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card);
84 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
85 struct snd_soc_component *component = codec_dai->component;
86 struct snd_jack *jack;
89 rval = snd_soc_card_jack_new(
95 dev_err(card->dev, "Unable to add HDMI Jack\n");
99 jack = pdata->hdmi_jack.jack;
100 jack->private_data = component;
101 jack->private_free = sc7180_jack_free;
103 return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL);
106 static int sc7180_init(struct snd_soc_pcm_runtime *rtd)
108 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
110 switch (cpu_dai->id) {
112 return sc7180_headset_init(rtd);
116 return sc7180_hdmi_init(rtd);
118 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
125 static int sc7180_snd_startup(struct snd_pcm_substream *substream)
127 struct snd_soc_pcm_runtime *rtd = substream->private_data;
128 struct snd_soc_card *card = rtd->card;
129 struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card);
130 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
131 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
132 int pll_id, pll_source, pll_in, pll_out, clk_id, ret;
134 if (!strcmp(codec_dai->name, "rt5682-aif1")) {
135 pll_source = RT5682_PLL1_S_MCLK;
137 clk_id = RT5682_SCLK_S_PLL1;
138 pll_out = RT5682_PLL1_FREQ;
139 pll_in = DEFAULT_MCLK_RATE;
140 } else if (!strcmp(codec_dai->name, "rt5682s-aif1")) {
141 pll_source = RT5682S_PLL_S_MCLK;
142 pll_id = RT5682S_PLL2;
143 clk_id = RT5682S_SCLK_S_PLL2;
144 pll_out = RT5682_PLL1_FREQ;
145 pll_in = DEFAULT_MCLK_RATE;
148 switch (cpu_dai->id) {
150 if (++data->pri_mi2s_clk_count == 1) {
151 snd_soc_dai_set_sysclk(cpu_dai,
154 SNDRV_PCM_STREAM_PLAYBACK);
157 snd_soc_dai_set_fmt(codec_dai,
158 SND_SOC_DAIFMT_BC_FC |
159 SND_SOC_DAIFMT_NB_NF |
162 /* Configure PLL1 for codec */
163 ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source,
166 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
170 /* Configure sysclk for codec */
171 ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, pll_out,
174 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n",
183 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
190 static int dmic_get(struct snd_kcontrol *kcontrol,
191 struct snd_ctl_elem_value *ucontrol)
193 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
194 struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card);
196 ucontrol->value.integer.value[0] = data->dmic_switch;
200 static int dmic_set(struct snd_kcontrol *kcontrol,
201 struct snd_ctl_elem_value *ucontrol)
203 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
204 struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card);
206 data->dmic_switch = ucontrol->value.integer.value[0];
207 gpiod_set_value(data->dmic_sel, data->dmic_switch);
211 static void sc7180_snd_shutdown(struct snd_pcm_substream *substream)
213 struct snd_soc_pcm_runtime *rtd = substream->private_data;
214 struct snd_soc_card *card = rtd->card;
215 struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card);
216 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
218 switch (cpu_dai->id) {
220 if (--data->pri_mi2s_clk_count == 0) {
221 snd_soc_dai_set_sysclk(cpu_dai,
224 SNDRV_PCM_STREAM_PLAYBACK);
232 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
238 static int sc7180_adau7002_init(struct snd_soc_pcm_runtime *rtd)
240 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
242 switch (cpu_dai->id) {
248 return sc7180_hdmi_init(rtd);
250 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
257 static int sc7180_adau7002_snd_startup(struct snd_pcm_substream *substream)
259 struct snd_soc_pcm_runtime *rtd = substream->private_data;
260 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
261 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
262 struct snd_pcm_runtime *runtime = substream->runtime;
264 switch (cpu_dai->id) {
266 snd_soc_dai_set_fmt(codec_dai,
267 SND_SOC_DAIFMT_CBS_CFS |
268 SND_SOC_DAIFMT_NB_NF |
270 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
271 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 32);
279 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
286 static const struct snd_soc_ops sc7180_ops = {
287 .startup = sc7180_snd_startup,
288 .shutdown = sc7180_snd_shutdown,
291 static const struct snd_soc_ops sc7180_adau7002_ops = {
292 .startup = sc7180_adau7002_snd_startup,
295 static const struct snd_soc_dapm_widget sc7180_snd_widgets[] = {
296 SND_SOC_DAPM_HP("Headphone Jack", NULL),
297 SND_SOC_DAPM_MIC("Headset Mic", NULL),
300 static const struct snd_soc_dapm_widget sc7180_adau7002_snd_widgets[] = {
301 SND_SOC_DAPM_MIC("DMIC", NULL),
304 static const char * const dmic_mux_text[] = {
309 static SOC_ENUM_SINGLE_DECL(sc7180_dmic_enum,
310 SND_SOC_NOPM, 0, dmic_mux_text);
312 static const struct snd_kcontrol_new sc7180_dmic_mux_control =
313 SOC_DAPM_ENUM_EXT("DMIC Select Mux", sc7180_dmic_enum,
316 static const struct snd_soc_dapm_widget sc7180_snd_dual_mic_widgets[] = {
317 SND_SOC_DAPM_HP("Headphone Jack", NULL),
318 SND_SOC_DAPM_MIC("Headset Mic", NULL),
319 SND_SOC_DAPM_MIC("DMIC", NULL),
320 SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0, &sc7180_dmic_mux_control),
323 static const struct snd_soc_dapm_route sc7180_snd_dual_mic_audio_route[] = {
324 {"Dmic Mux", "Front Mic", "DMIC"},
325 {"Dmic Mux", "Rear Mic", "DMIC"},
328 static int sc7180_snd_platform_probe(struct platform_device *pdev)
330 struct snd_soc_card *card;
331 struct sc7180_snd_data *data;
332 struct device *dev = &pdev->dev;
333 struct snd_soc_dai_link *link;
336 bool no_headphone = false;
338 /* Allocate the private data */
339 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
344 snd_soc_card_set_drvdata(card, data);
346 card->owner = THIS_MODULE;
347 card->driver_name = DRIVER_NAME;
349 card->dapm_widgets = sc7180_snd_widgets;
350 card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_widgets);
352 if (of_property_read_bool(dev->of_node, "dmic-gpios")) {
353 card->dapm_widgets = sc7180_snd_dual_mic_widgets,
354 card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_dual_mic_widgets),
355 card->dapm_routes = sc7180_snd_dual_mic_audio_route,
356 card->num_dapm_routes = ARRAY_SIZE(sc7180_snd_dual_mic_audio_route),
357 data->dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW);
358 if (IS_ERR(data->dmic_sel)) {
359 dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n", PTR_ERR(data->dmic_sel));
360 return PTR_ERR(data->dmic_sel);
364 if (of_device_is_compatible(dev->of_node, "google,sc7180-coachz")) {
366 card->dapm_widgets = sc7180_adau7002_snd_widgets;
367 card->num_dapm_widgets = ARRAY_SIZE(sc7180_adau7002_snd_widgets);
370 ret = qcom_snd_parse_of(card);
374 for_each_card_prelinks(card, i, link) {
376 link->ops = &sc7180_adau7002_ops;
377 link->init = sc7180_adau7002_init;
379 link->ops = &sc7180_ops;
380 link->init = sc7180_init;
384 return devm_snd_soc_register_card(dev, card);
387 static const struct of_device_id sc7180_snd_device_id[] = {
388 {.compatible = "google,sc7180-trogdor"},
389 {.compatible = "google,sc7180-coachz"},
392 MODULE_DEVICE_TABLE(of, sc7180_snd_device_id);
394 static struct platform_driver sc7180_snd_driver = {
395 .probe = sc7180_snd_platform_probe,
397 .name = "msm-snd-sc7180",
398 .of_match_table = sc7180_snd_device_id,
399 .pm = &snd_soc_pm_ops,
402 module_platform_driver(sc7180_snd_driver);
404 MODULE_DESCRIPTION("sc7180 ASoC Machine Driver");
405 MODULE_LICENSE("GPL v2");