1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
4 //
5 // ALSA SoC Machine driver for sc7280
6
7 #include <dt-bindings/sound/qcom,lpass.h>
8 #include <dt-bindings/sound/qcom,q6afe.h>
9 #include <linux/input.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <sound/core.h>
13 #include <sound/jack.h>
14 #include <sound/pcm.h>
15 #include <sound/soc.h>
16 #include <sound/rt5682s.h>
17 #include <linux/soundwire/sdw.h>
18 #include <sound/pcm_params.h>
19
20 #include "../codecs/rt5682.h"
21 #include "../codecs/rt5682s.h"
22 #include "common.h"
23 #include "lpass.h"
24 #include "qdsp6/q6afe.h"
25 #include "sdw.h"
26
27 #define DEFAULT_MCLK_RATE 19200000
28 #define RT5682_PLL_FREQ (48000 * 512)
29 #define MI2S_BCLK_RATE 1536000
30
31 struct sc7280_snd_data {
32 struct snd_soc_card card;
33 u32 pri_mi2s_clk_count;
34 struct snd_soc_jack hs_jack;
35 struct snd_soc_jack hdmi_jack;
36 bool jack_setup;
37 bool stream_prepared[LPASS_MAX_PORTS];
38 };
39
sc7280_jack_free(struct snd_jack * jack)40 static void sc7280_jack_free(struct snd_jack *jack)
41 {
42 struct snd_soc_component *component = jack->private_data;
43
44 snd_soc_component_set_jack(component, NULL, NULL);
45 }
46
47 static struct snd_soc_jack_pin sc7280_jack_pins[] = {
48 {
49 .pin = "Headphone Jack",
50 .mask = SND_JACK_HEADPHONE,
51 },
52 {
53 .pin = "Headset Mic",
54 .mask = SND_JACK_MICROPHONE,
55 },
56 };
57
sc7280_headset_init(struct snd_soc_pcm_runtime * rtd)58 static int sc7280_headset_init(struct snd_soc_pcm_runtime *rtd)
59 {
60 struct snd_soc_card *card = rtd->card;
61 struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(card);
62 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
63 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
64 struct snd_soc_component *component = codec_dai->component;
65 struct snd_jack *jack;
66 int rval, i;
67
68 if (!pdata->jack_setup) {
69 rval = snd_soc_card_jack_new_pins(card, "Headset Jack",
70 SND_JACK_HEADSET | SND_JACK_LINEOUT |
71 SND_JACK_MECHANICAL |
72 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
73 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
74 SND_JACK_BTN_4 | SND_JACK_BTN_5,
75 &pdata->hs_jack,
76 sc7280_jack_pins,
77 ARRAY_SIZE(sc7280_jack_pins));
78
79 if (rval < 0) {
80 dev_err(card->dev, "Unable to add Headset Jack\n");
81 return rval;
82 }
83
84 jack = pdata->hs_jack.jack;
85
86 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
87 snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
88 snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
89 snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
90
91 jack->private_data = component;
92 jack->private_free = sc7280_jack_free;
93 pdata->jack_setup = true;
94 }
95 switch (cpu_dai->id) {
96 case MI2S_PRIMARY:
97 case LPASS_CDC_DMA_RX0:
98 case LPASS_CDC_DMA_TX3:
99 case TX_CODEC_DMA_TX_3:
100 for_each_rtd_codec_dais(rtd, i, codec_dai) {
101 rval = snd_soc_component_set_jack(component, &pdata->hs_jack, NULL);
102 if (rval != 0 && rval != -ENOTSUPP) {
103 dev_err(card->dev, "Failed to set jack: %d\n", rval);
104 return rval;
105 }
106 }
107 break;
108 default:
109 break;
110 }
111
112 return 0;
113 }
114
sc7280_hdmi_init(struct snd_soc_pcm_runtime * rtd)115 static int sc7280_hdmi_init(struct snd_soc_pcm_runtime *rtd)
116 {
117 struct snd_soc_card *card = rtd->card;
118 struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(card);
119 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
120 struct snd_soc_component *component = codec_dai->component;
121 struct snd_jack *jack;
122 int rval;
123
124 rval = snd_soc_card_jack_new(card, "HDMI Jack", SND_JACK_LINEOUT,
125 &pdata->hdmi_jack);
126
127 if (rval < 0) {
128 dev_err(card->dev, "Unable to add HDMI Jack\n");
129 return rval;
130 }
131
132 jack = pdata->hdmi_jack.jack;
133 jack->private_data = component;
134 jack->private_free = sc7280_jack_free;
135
136 return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL);
137 }
138
sc7280_rt5682_init(struct snd_soc_pcm_runtime * rtd)139 static int sc7280_rt5682_init(struct snd_soc_pcm_runtime *rtd)
140 {
141 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
142 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
143 struct snd_soc_card *card = rtd->card;
144 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(card);
145 int ret;
146
147 if (++data->pri_mi2s_clk_count == 1) {
148 snd_soc_dai_set_sysclk(cpu_dai,
149 LPASS_MCLK0,
150 DEFAULT_MCLK_RATE,
151 SNDRV_PCM_STREAM_PLAYBACK);
152 }
153 snd_soc_dai_set_fmt(codec_dai,
154 SND_SOC_DAIFMT_CBC_CFC |
155 SND_SOC_DAIFMT_NB_NF |
156 SND_SOC_DAIFMT_I2S);
157
158 ret = snd_soc_dai_set_pll(codec_dai, RT5682S_PLL2, RT5682S_PLL_S_MCLK,
159 DEFAULT_MCLK_RATE, RT5682_PLL_FREQ);
160 if (ret) {
161 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
162 return ret;
163 }
164
165 ret = snd_soc_dai_set_sysclk(codec_dai, RT5682S_SCLK_S_PLL2,
166 RT5682_PLL_FREQ,
167 SND_SOC_CLOCK_IN);
168
169 if (ret) {
170 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n",
171 ret);
172 return ret;
173 }
174
175 return 0;
176 }
177
sc7280_init(struct snd_soc_pcm_runtime * rtd)178 static int sc7280_init(struct snd_soc_pcm_runtime *rtd)
179 {
180 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
181
182 switch (cpu_dai->id) {
183 case MI2S_PRIMARY:
184 case LPASS_CDC_DMA_TX3:
185 case TX_CODEC_DMA_TX_3:
186 return sc7280_headset_init(rtd);
187 case LPASS_CDC_DMA_RX0:
188 case LPASS_CDC_DMA_VA_TX0:
189 case MI2S_SECONDARY:
190 case RX_CODEC_DMA_RX_0:
191 case SECONDARY_MI2S_RX:
192 case VA_CODEC_DMA_TX_0:
193 return 0;
194 case LPASS_DP_RX:
195 return sc7280_hdmi_init(rtd);
196 default:
197 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
198 }
199
200 return -EINVAL;
201 }
202
sc7280_snd_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)203 static int sc7280_snd_hw_params(struct snd_pcm_substream *substream,
204 struct snd_pcm_hw_params *params)
205 {
206 struct snd_pcm_runtime *runtime = substream->runtime;
207 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
208
209 if (!rtd->dai_link->no_pcm) {
210 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
211 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, 48000, 48000);
212 }
213
214 return 0;
215 }
216
sc7280_snd_swr_prepare(struct snd_pcm_substream * substream)217 static int sc7280_snd_swr_prepare(struct snd_pcm_substream *substream)
218 {
219 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
220 const struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
221 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
222
223 return qcom_snd_sdw_prepare(substream, &data->stream_prepared[cpu_dai->id]);
224 }
225
sc7280_snd_prepare(struct snd_pcm_substream * substream)226 static int sc7280_snd_prepare(struct snd_pcm_substream *substream)
227 {
228 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
229 const struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
230
231 switch (cpu_dai->id) {
232 case LPASS_CDC_DMA_RX0:
233 case LPASS_CDC_DMA_TX3:
234 case RX_CODEC_DMA_RX_0:
235 case TX_CODEC_DMA_TX_3:
236 case VA_CODEC_DMA_TX_0:
237 return sc7280_snd_swr_prepare(substream);
238 default:
239 break;
240 }
241
242 return 0;
243 }
244
sc7280_snd_hw_free(struct snd_pcm_substream * substream)245 static int sc7280_snd_hw_free(struct snd_pcm_substream *substream)
246 {
247 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
248 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
249 const struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
250
251 return qcom_snd_sdw_hw_free(substream, &data->stream_prepared[cpu_dai->id]);
252 }
253
sc7280_snd_shutdown(struct snd_pcm_substream * substream)254 static void sc7280_snd_shutdown(struct snd_pcm_substream *substream)
255 {
256 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
257 struct snd_soc_card *card = rtd->card;
258 struct sc7280_snd_data *data = snd_soc_card_get_drvdata(card);
259 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
260
261 switch (cpu_dai->id) {
262 case MI2S_PRIMARY:
263 if (--data->pri_mi2s_clk_count == 0) {
264 snd_soc_dai_set_sysclk(cpu_dai,
265 LPASS_MCLK0,
266 0,
267 SNDRV_PCM_STREAM_PLAYBACK);
268 }
269 break;
270 case SECONDARY_MI2S_RX:
271 snd_soc_dai_set_sysclk(cpu_dai, Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
272 0, SNDRV_PCM_STREAM_PLAYBACK);
273 break;
274 default:
275 break;
276 }
277
278 qcom_snd_sdw_shutdown(substream);
279 }
280
sc7280_snd_startup(struct snd_pcm_substream * substream)281 static int sc7280_snd_startup(struct snd_pcm_substream *substream)
282 {
283 unsigned int fmt = SND_SOC_DAIFMT_CBC_CFC;
284 unsigned int codec_dai_fmt = SND_SOC_DAIFMT_CBC_CFC;
285 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
286 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
287 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
288 int ret = 0;
289
290 switch (cpu_dai->id) {
291 case MI2S_PRIMARY:
292 ret = sc7280_rt5682_init(rtd);
293 if (ret)
294 return ret;
295 break;
296 case SECONDARY_MI2S_RX:
297 codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S;
298
299 snd_soc_dai_set_sysclk(cpu_dai, Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
300 MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
301
302 snd_soc_dai_set_fmt(cpu_dai, fmt);
303 snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
304 break;
305 default:
306 break;
307 }
308
309 return qcom_snd_sdw_startup(substream);
310 }
311
312 static const struct snd_soc_ops sc7280_ops = {
313 .startup = sc7280_snd_startup,
314 .hw_params = sc7280_snd_hw_params,
315 .hw_free = sc7280_snd_hw_free,
316 .prepare = sc7280_snd_prepare,
317 .shutdown = sc7280_snd_shutdown,
318 };
319
320 static const struct snd_soc_dapm_widget sc7280_snd_widgets[] = {
321 SND_SOC_DAPM_HP("Headphone Jack", NULL),
322 SND_SOC_DAPM_MIC("Headset Mic", NULL),
323 };
324
325 static const struct snd_kcontrol_new sc7280_snd_controls[] = {
326 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
327 SOC_DAPM_PIN_SWITCH("Headset Mic"),
328 };
329
sc7280_snd_be_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)330 static int sc7280_snd_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
331 struct snd_pcm_hw_params *params)
332 {
333 struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
334 struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
335 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
336
337 rate->min = rate->max = 48000;
338 channels->min = channels->max = 2;
339 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
340
341 return 0;
342 }
343
sc7280_snd_platform_probe(struct platform_device * pdev)344 static int sc7280_snd_platform_probe(struct platform_device *pdev)
345 {
346 struct snd_soc_card *card;
347 struct sc7280_snd_data *data;
348 struct device *dev = &pdev->dev;
349 struct snd_soc_dai_link *link;
350 int ret, i;
351
352 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
353 if (!data)
354 return -ENOMEM;
355
356 card = &data->card;
357 snd_soc_card_set_drvdata(card, data);
358
359 card->owner = THIS_MODULE;
360 card->driver_name = "SC7280";
361 card->dev = dev;
362
363 card->dapm_widgets = sc7280_snd_widgets;
364 card->num_dapm_widgets = ARRAY_SIZE(sc7280_snd_widgets);
365 card->controls = sc7280_snd_controls;
366 card->num_controls = ARRAY_SIZE(sc7280_snd_controls);
367
368 ret = qcom_snd_parse_of(card);
369 if (ret)
370 return ret;
371
372 for_each_card_prelinks(card, i, link) {
373 link->init = sc7280_init;
374 link->ops = &sc7280_ops;
375 if (link->no_pcm == 1)
376 link->be_hw_params_fixup = sc7280_snd_be_hw_params_fixup;
377 }
378
379 return devm_snd_soc_register_card(dev, card);
380 }
381
382 static const struct of_device_id sc7280_snd_device_id[] = {
383 { .compatible = "google,sc7280-herobrine" },
384 {}
385 };
386 MODULE_DEVICE_TABLE(of, sc7280_snd_device_id);
387
388 static struct platform_driver sc7280_snd_driver = {
389 .probe = sc7280_snd_platform_probe,
390 .driver = {
391 .name = "msm-snd-sc7280",
392 .of_match_table = sc7280_snd_device_id,
393 .pm = &snd_soc_pm_ops,
394 },
395 };
396 module_platform_driver(sc7280_snd_driver);
397
398 MODULE_DESCRIPTION("sc7280 ASoC Machine Driver");
399 MODULE_LICENSE("GPL");
400