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