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 <linux/input.h>
8 #include <linux/module.h>
9 #include <linux/of_device.h>
10 #include <linux/platform_device.h>
11 #include <sound/core.h>
12 #include <sound/jack.h>
13 #include <sound/pcm.h>
14 #include <sound/soc.h>
15 #include <sound/rt5682s.h>
16 #include <linux/soundwire/sdw.h>
18 #include "../codecs/rt5682.h"
19 #include "../codecs/rt5682s.h"
22 #include "qdsp6/q6afe.h"
24 #define DEFAULT_MCLK_RATE 19200000
25 #define RT5682_PLL_FREQ (48000 * 512)
26 #define MI2S_BCLK_RATE 1536000
28 struct sc7280_snd_data {
29 struct snd_soc_card card;
30 struct sdw_stream_runtime *sruntime[LPASS_MAX_PORTS];
31 u32 pri_mi2s_clk_count;
32 struct snd_soc_jack hs_jack;
33 struct snd_soc_jack hdmi_jack;
35 bool stream_prepared[LPASS_MAX_PORTS];
38 static void sc7280_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 sc7280_headset_init(struct snd_soc_pcm_runtime *rtd)
47 struct snd_soc_card *card = rtd->card;
48 struct sc7280_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_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
51 struct snd_soc_component *component = codec_dai->component;
52 struct snd_jack *jack;
55 if (!pdata->jack_setup) {
56 rval = snd_soc_card_jack_new(card, "Headset Jack",
57 SND_JACK_HEADSET | SND_JACK_LINEOUT |
59 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
60 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
61 SND_JACK_BTN_4 | SND_JACK_BTN_5,
65 dev_err(card->dev, "Unable to add Headset Jack\n");
69 jack = pdata->hs_jack.jack;
71 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
72 snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
73 snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
74 snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
76 jack->private_data = component;
77 jack->private_free = sc7280_jack_free;
78 pdata->jack_setup = true;
80 switch (cpu_dai->id) {
82 case LPASS_CDC_DMA_RX0:
83 case LPASS_CDC_DMA_TX3:
84 case TX_CODEC_DMA_TX_3:
85 for_each_rtd_codec_dais(rtd, i, codec_dai) {
86 rval = snd_soc_component_set_jack(component, &pdata->hs_jack, NULL);
87 if (rval != 0 && rval != -ENOTSUPP) {
88 dev_err(card->dev, "Failed to set jack: %d\n", rval);
100 static int sc7280_hdmi_init(struct snd_soc_pcm_runtime *rtd)
102 struct snd_soc_card *card = rtd->card;
103 struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(card);
104 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
105 struct snd_soc_component *component = codec_dai->component;
106 struct snd_jack *jack;
109 rval = snd_soc_card_jack_new(card, "HDMI Jack", SND_JACK_LINEOUT,
113 dev_err(card->dev, "Unable to add HDMI Jack\n");
117 jack = pdata->hdmi_jack.jack;
118 jack->private_data = component;
119 jack->private_free = sc7280_jack_free;
121 return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL);
124 static int sc7280_rt5682_init(struct snd_soc_pcm_runtime *rtd)
126 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
127 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
128 struct snd_soc_card *card = rtd->card;
129 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(card);
132 if (++data->pri_mi2s_clk_count == 1) {
133 snd_soc_dai_set_sysclk(cpu_dai,
136 SNDRV_PCM_STREAM_PLAYBACK);
138 snd_soc_dai_set_fmt(codec_dai,
139 SND_SOC_DAIFMT_CBC_CFC |
140 SND_SOC_DAIFMT_NB_NF |
143 ret = snd_soc_dai_set_pll(codec_dai, RT5682S_PLL2, RT5682S_PLL_S_MCLK,
144 DEFAULT_MCLK_RATE, RT5682_PLL_FREQ);
146 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
150 ret = snd_soc_dai_set_sysclk(codec_dai, RT5682S_SCLK_S_PLL2,
155 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n",
163 static int sc7280_init(struct snd_soc_pcm_runtime *rtd)
165 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
167 switch (cpu_dai->id) {
169 case LPASS_CDC_DMA_TX3:
170 case TX_CODEC_DMA_TX_3:
171 return sc7280_headset_init(rtd);
172 case LPASS_CDC_DMA_RX0:
173 case LPASS_CDC_DMA_VA_TX0:
175 case RX_CODEC_DMA_RX_0:
176 case SECONDARY_MI2S_RX:
177 case VA_CODEC_DMA_TX_0:
180 return sc7280_hdmi_init(rtd);
182 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
188 static int sc7280_snd_hw_params(struct snd_pcm_substream *substream,
189 struct snd_pcm_hw_params *params)
191 struct snd_pcm_runtime *runtime = substream->runtime;
192 struct snd_soc_pcm_runtime *rtd = substream->private_data;
193 struct snd_soc_dai *codec_dai;
194 const struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
195 struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
196 struct sdw_stream_runtime *sruntime;
199 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
200 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, 48000, 48000);
202 switch (cpu_dai->id) {
203 case LPASS_CDC_DMA_TX3:
204 case LPASS_CDC_DMA_RX0:
205 case RX_CODEC_DMA_RX_0:
206 case SECONDARY_MI2S_RX:
207 case TX_CODEC_DMA_TX_3:
208 case VA_CODEC_DMA_TX_0:
209 for_each_rtd_codec_dais(rtd, i, codec_dai) {
210 sruntime = snd_soc_dai_get_stream(codec_dai, substream->stream);
211 if (sruntime != ERR_PTR(-ENOTSUPP))
212 pdata->sruntime[cpu_dai->id] = sruntime;
220 static int sc7280_snd_swr_prepare(struct snd_pcm_substream *substream)
222 struct snd_soc_pcm_runtime *rtd = substream->private_data;
223 const struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
224 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
225 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
231 if (data->stream_prepared[cpu_dai->id]) {
232 sdw_disable_stream(sruntime);
233 sdw_deprepare_stream(sruntime);
234 data->stream_prepared[cpu_dai->id] = false;
237 ret = sdw_prepare_stream(sruntime);
241 ret = sdw_enable_stream(sruntime);
243 sdw_deprepare_stream(sruntime);
246 data->stream_prepared[cpu_dai->id] = true;
251 static int sc7280_snd_prepare(struct snd_pcm_substream *substream)
253 struct snd_soc_pcm_runtime *rtd = substream->private_data;
254 const struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
256 switch (cpu_dai->id) {
257 case LPASS_CDC_DMA_RX0:
258 case LPASS_CDC_DMA_TX3:
259 case RX_CODEC_DMA_RX_0:
260 case TX_CODEC_DMA_TX_3:
261 case VA_CODEC_DMA_TX_0:
262 return sc7280_snd_swr_prepare(substream);
270 static int sc7280_snd_hw_free(struct snd_pcm_substream *substream)
272 struct snd_soc_pcm_runtime *rtd = substream->private_data;
273 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
274 const struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
275 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
277 switch (cpu_dai->id) {
278 case LPASS_CDC_DMA_RX0:
279 case LPASS_CDC_DMA_TX3:
280 case RX_CODEC_DMA_RX_0:
281 case TX_CODEC_DMA_TX_3:
282 case VA_CODEC_DMA_TX_0:
283 if (sruntime && data->stream_prepared[cpu_dai->id]) {
284 sdw_disable_stream(sruntime);
285 sdw_deprepare_stream(sruntime);
286 data->stream_prepared[cpu_dai->id] = false;
295 static void sc7280_snd_shutdown(struct snd_pcm_substream *substream)
297 struct snd_soc_pcm_runtime *rtd = substream->private_data;
298 struct snd_soc_card *card = rtd->card;
299 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(card);
300 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
302 switch (cpu_dai->id) {
304 if (--data->pri_mi2s_clk_count == 0) {
305 snd_soc_dai_set_sysclk(cpu_dai,
308 SNDRV_PCM_STREAM_PLAYBACK);
311 case SECONDARY_MI2S_RX:
312 snd_soc_dai_set_sysclk(cpu_dai, Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
313 0, SNDRV_PCM_STREAM_PLAYBACK);
320 static int sc7280_snd_startup(struct snd_pcm_substream *substream)
322 unsigned int fmt = SND_SOC_DAIFMT_CBS_CFS;
323 unsigned int codec_dai_fmt = SND_SOC_DAIFMT_CBS_CFS;
324 struct snd_soc_pcm_runtime *rtd = substream->private_data;
325 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
326 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
329 switch (cpu_dai->id) {
331 ret = sc7280_rt5682_init(rtd);
333 case SECONDARY_MI2S_RX:
334 codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S;
336 snd_soc_dai_set_sysclk(cpu_dai, Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
337 MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
339 snd_soc_dai_set_fmt(cpu_dai, fmt);
340 snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
348 static const struct snd_soc_ops sc7280_ops = {
349 .startup = sc7280_snd_startup,
350 .hw_params = sc7280_snd_hw_params,
351 .hw_free = sc7280_snd_hw_free,
352 .prepare = sc7280_snd_prepare,
353 .shutdown = sc7280_snd_shutdown,
356 static const struct snd_soc_dapm_widget sc7280_snd_widgets[] = {
357 SND_SOC_DAPM_HP("Headphone Jack", NULL),
358 SND_SOC_DAPM_MIC("Headset Mic", NULL),
361 static int sc7280_snd_platform_probe(struct platform_device *pdev)
363 struct snd_soc_card *card;
364 struct sc7280_snd_data *data;
365 struct device *dev = &pdev->dev;
366 struct snd_soc_dai_link *link;
369 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
374 snd_soc_card_set_drvdata(card, data);
376 card->owner = THIS_MODULE;
377 card->driver_name = "SC7280";
380 card->dapm_widgets = sc7280_snd_widgets;
381 card->num_dapm_widgets = ARRAY_SIZE(sc7280_snd_widgets);
383 ret = qcom_snd_parse_of(card);
387 for_each_card_prelinks(card, i, link) {
388 link->init = sc7280_init;
389 link->ops = &sc7280_ops;
392 return devm_snd_soc_register_card(dev, card);
395 static const struct of_device_id sc7280_snd_device_id[] = {
396 { .compatible = "google,sc7280-herobrine" },
399 MODULE_DEVICE_TABLE(of, sc7280_snd_device_id);
401 static struct platform_driver sc7280_snd_driver = {
402 .probe = sc7280_snd_platform_probe,
404 .name = "msm-snd-sc7280",
405 .of_match_table = sc7280_snd_device_id,
406 .pm = &snd_soc_pm_ops,
409 module_platform_driver(sc7280_snd_driver);
411 MODULE_DESCRIPTION("sc7280 ASoC Machine Driver");
412 MODULE_LICENSE("GPL");