1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
5 // ALSA SoC Machine driver for sc7280
7 #include <dt-bindings/sound/qcom,lpass.h>
8 #include <dt-bindings/sound/qcom,q6afe.h>
9 #include <linux/input.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.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 <sound/rt5682s.h>
18 #include <linux/soundwire/sdw.h>
19 #include <sound/pcm_params.h>
21 #include "../codecs/rt5682.h"
22 #include "../codecs/rt5682s.h"
25 #include "qdsp6/q6afe.h"
27 #define DEFAULT_MCLK_RATE 19200000
28 #define RT5682_PLL_FREQ (48000 * 512)
29 #define MI2S_BCLK_RATE 1536000
31 struct sc7280_snd_data {
32 struct snd_soc_card card;
33 struct sdw_stream_runtime *sruntime[LPASS_MAX_PORTS];
34 u32 pri_mi2s_clk_count;
35 struct snd_soc_jack hs_jack;
36 struct snd_soc_jack hdmi_jack;
38 bool stream_prepared[LPASS_MAX_PORTS];
41 static void sc7280_jack_free(struct snd_jack *jack)
43 struct snd_soc_component *component = jack->private_data;
45 snd_soc_component_set_jack(component, NULL, NULL);
48 static struct snd_soc_jack_pin sc7280_jack_pins[] = {
50 .pin = "Headphone Jack",
51 .mask = SND_JACK_HEADPHONE,
55 .mask = SND_JACK_MICROPHONE,
59 static int sc7280_headset_init(struct snd_soc_pcm_runtime *rtd)
61 struct snd_soc_card *card = rtd->card;
62 struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(card);
63 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
64 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
65 struct snd_soc_component *component = codec_dai->component;
66 struct snd_jack *jack;
69 if (!pdata->jack_setup) {
70 rval = snd_soc_card_jack_new_pins(card, "Headset Jack",
71 SND_JACK_HEADSET | SND_JACK_LINEOUT |
73 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
74 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
75 SND_JACK_BTN_4 | SND_JACK_BTN_5,
78 ARRAY_SIZE(sc7280_jack_pins));
81 dev_err(card->dev, "Unable to add Headset Jack\n");
85 jack = pdata->hs_jack.jack;
87 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
88 snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
89 snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
90 snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
92 jack->private_data = component;
93 jack->private_free = sc7280_jack_free;
94 pdata->jack_setup = true;
96 switch (cpu_dai->id) {
98 case LPASS_CDC_DMA_RX0:
99 case LPASS_CDC_DMA_TX3:
100 case TX_CODEC_DMA_TX_3:
101 for_each_rtd_codec_dais(rtd, i, codec_dai) {
102 rval = snd_soc_component_set_jack(component, &pdata->hs_jack, NULL);
103 if (rval != 0 && rval != -ENOTSUPP) {
104 dev_err(card->dev, "Failed to set jack: %d\n", rval);
116 static int sc7280_hdmi_init(struct snd_soc_pcm_runtime *rtd)
118 struct snd_soc_card *card = rtd->card;
119 struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(card);
120 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
121 struct snd_soc_component *component = codec_dai->component;
122 struct snd_jack *jack;
125 rval = snd_soc_card_jack_new(card, "HDMI Jack", SND_JACK_LINEOUT,
129 dev_err(card->dev, "Unable to add HDMI Jack\n");
133 jack = pdata->hdmi_jack.jack;
134 jack->private_data = component;
135 jack->private_free = sc7280_jack_free;
137 return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL);
140 static int sc7280_rt5682_init(struct snd_soc_pcm_runtime *rtd)
142 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
143 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
144 struct snd_soc_card *card = rtd->card;
145 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(card);
148 if (++data->pri_mi2s_clk_count == 1) {
149 snd_soc_dai_set_sysclk(cpu_dai,
152 SNDRV_PCM_STREAM_PLAYBACK);
154 snd_soc_dai_set_fmt(codec_dai,
155 SND_SOC_DAIFMT_CBC_CFC |
156 SND_SOC_DAIFMT_NB_NF |
159 ret = snd_soc_dai_set_pll(codec_dai, RT5682S_PLL2, RT5682S_PLL_S_MCLK,
160 DEFAULT_MCLK_RATE, RT5682_PLL_FREQ);
162 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
166 ret = snd_soc_dai_set_sysclk(codec_dai, RT5682S_SCLK_S_PLL2,
171 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n",
179 static int sc7280_init(struct snd_soc_pcm_runtime *rtd)
181 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
183 switch (cpu_dai->id) {
185 case LPASS_CDC_DMA_TX3:
186 case TX_CODEC_DMA_TX_3:
187 return sc7280_headset_init(rtd);
188 case LPASS_CDC_DMA_RX0:
189 case LPASS_CDC_DMA_VA_TX0:
191 case RX_CODEC_DMA_RX_0:
192 case SECONDARY_MI2S_RX:
193 case VA_CODEC_DMA_TX_0:
196 return sc7280_hdmi_init(rtd);
198 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
204 static int sc7280_snd_hw_params(struct snd_pcm_substream *substream,
205 struct snd_pcm_hw_params *params)
207 struct snd_pcm_runtime *runtime = substream->runtime;
208 struct snd_soc_pcm_runtime *rtd = substream->private_data;
209 struct snd_soc_dai *codec_dai;
210 const struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
211 struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
212 struct sdw_stream_runtime *sruntime;
215 if (!rtd->dai_link->no_pcm) {
216 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
217 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, 48000, 48000);
220 switch (cpu_dai->id) {
221 case LPASS_CDC_DMA_TX3:
222 case LPASS_CDC_DMA_RX0:
223 case RX_CODEC_DMA_RX_0:
224 case SECONDARY_MI2S_RX:
225 case TX_CODEC_DMA_TX_3:
226 case VA_CODEC_DMA_TX_0:
227 for_each_rtd_codec_dais(rtd, i, codec_dai) {
228 sruntime = snd_soc_dai_get_stream(codec_dai, substream->stream);
229 if (sruntime != ERR_PTR(-ENOTSUPP))
230 pdata->sruntime[cpu_dai->id] = sruntime;
238 static int sc7280_snd_swr_prepare(struct snd_pcm_substream *substream)
240 struct snd_soc_pcm_runtime *rtd = substream->private_data;
241 const struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
242 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
243 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
249 if (data->stream_prepared[cpu_dai->id]) {
250 sdw_disable_stream(sruntime);
251 sdw_deprepare_stream(sruntime);
252 data->stream_prepared[cpu_dai->id] = false;
255 ret = sdw_prepare_stream(sruntime);
259 ret = sdw_enable_stream(sruntime);
261 sdw_deprepare_stream(sruntime);
264 data->stream_prepared[cpu_dai->id] = true;
269 static int sc7280_snd_prepare(struct snd_pcm_substream *substream)
271 struct snd_soc_pcm_runtime *rtd = substream->private_data;
272 const struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
274 switch (cpu_dai->id) {
275 case LPASS_CDC_DMA_RX0:
276 case LPASS_CDC_DMA_TX3:
277 case RX_CODEC_DMA_RX_0:
278 case TX_CODEC_DMA_TX_3:
279 case VA_CODEC_DMA_TX_0:
280 return sc7280_snd_swr_prepare(substream);
288 static int sc7280_snd_hw_free(struct snd_pcm_substream *substream)
290 struct snd_soc_pcm_runtime *rtd = substream->private_data;
291 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
292 const struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
293 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
295 switch (cpu_dai->id) {
296 case LPASS_CDC_DMA_RX0:
297 case LPASS_CDC_DMA_TX3:
298 case RX_CODEC_DMA_RX_0:
299 case TX_CODEC_DMA_TX_3:
300 case VA_CODEC_DMA_TX_0:
301 if (sruntime && data->stream_prepared[cpu_dai->id]) {
302 sdw_disable_stream(sruntime);
303 sdw_deprepare_stream(sruntime);
304 data->stream_prepared[cpu_dai->id] = false;
313 static void sc7280_snd_shutdown(struct snd_pcm_substream *substream)
315 struct snd_soc_pcm_runtime *rtd = substream->private_data;
316 struct snd_soc_card *card = rtd->card;
317 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(card);
318 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
320 switch (cpu_dai->id) {
322 if (--data->pri_mi2s_clk_count == 0) {
323 snd_soc_dai_set_sysclk(cpu_dai,
326 SNDRV_PCM_STREAM_PLAYBACK);
329 case SECONDARY_MI2S_RX:
330 snd_soc_dai_set_sysclk(cpu_dai, Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
331 0, SNDRV_PCM_STREAM_PLAYBACK);
338 static int sc7280_snd_startup(struct snd_pcm_substream *substream)
340 unsigned int fmt = SND_SOC_DAIFMT_CBS_CFS;
341 unsigned int codec_dai_fmt = SND_SOC_DAIFMT_CBS_CFS;
342 struct snd_soc_pcm_runtime *rtd = substream->private_data;
343 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
344 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
347 switch (cpu_dai->id) {
349 ret = sc7280_rt5682_init(rtd);
351 case SECONDARY_MI2S_RX:
352 codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S;
354 snd_soc_dai_set_sysclk(cpu_dai, Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
355 MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
357 snd_soc_dai_set_fmt(cpu_dai, fmt);
358 snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
366 static const struct snd_soc_ops sc7280_ops = {
367 .startup = sc7280_snd_startup,
368 .hw_params = sc7280_snd_hw_params,
369 .hw_free = sc7280_snd_hw_free,
370 .prepare = sc7280_snd_prepare,
371 .shutdown = sc7280_snd_shutdown,
374 static const struct snd_soc_dapm_widget sc7280_snd_widgets[] = {
375 SND_SOC_DAPM_HP("Headphone Jack", NULL),
376 SND_SOC_DAPM_MIC("Headset Mic", NULL),
379 static const struct snd_kcontrol_new sc7280_snd_controls[] = {
380 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
381 SOC_DAPM_PIN_SWITCH("Headset Mic"),
384 static int sc7280_snd_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
385 struct snd_pcm_hw_params *params)
387 struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
388 struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
389 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
391 rate->min = rate->max = 48000;
392 channels->min = channels->max = 2;
393 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
398 static int sc7280_snd_platform_probe(struct platform_device *pdev)
400 struct snd_soc_card *card;
401 struct sc7280_snd_data *data;
402 struct device *dev = &pdev->dev;
403 struct snd_soc_dai_link *link;
406 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
411 snd_soc_card_set_drvdata(card, data);
413 card->owner = THIS_MODULE;
414 card->driver_name = "SC7280";
417 card->dapm_widgets = sc7280_snd_widgets;
418 card->num_dapm_widgets = ARRAY_SIZE(sc7280_snd_widgets);
419 card->controls = sc7280_snd_controls;
420 card->num_controls = ARRAY_SIZE(sc7280_snd_controls);
422 ret = qcom_snd_parse_of(card);
426 for_each_card_prelinks(card, i, link) {
427 link->init = sc7280_init;
428 link->ops = &sc7280_ops;
429 if (link->no_pcm == 1)
430 link->be_hw_params_fixup = sc7280_snd_be_hw_params_fixup;
433 return devm_snd_soc_register_card(dev, card);
436 static const struct of_device_id sc7280_snd_device_id[] = {
437 { .compatible = "google,sc7280-herobrine" },
440 MODULE_DEVICE_TABLE(of, sc7280_snd_device_id);
442 static struct platform_driver sc7280_snd_driver = {
443 .probe = sc7280_snd_platform_probe,
445 .name = "msm-snd-sc7280",
446 .of_match_table = sc7280_snd_device_id,
447 .pm = &snd_soc_pm_ops,
450 module_platform_driver(sc7280_snd_driver);
452 MODULE_DESCRIPTION("sc7280 ASoC Machine Driver");
453 MODULE_LICENSE("GPL");