xref: /openbmc/linux/sound/soc/qcom/sc7280.c (revision a266ef69)
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 
18 #include "../codecs/rt5682.h"
19 #include "../codecs/rt5682s.h"
20 #include "common.h"
21 #include "lpass.h"
22 #include "qdsp6/q6afe.h"
23 
24 #define DEFAULT_MCLK_RATE              19200000
25 #define RT5682_PLL_FREQ (48000 * 512)
26 #define MI2S_BCLK_RATE		1536000
27 
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;
34 	bool jack_setup;
35 	bool stream_prepared[LPASS_MAX_PORTS];
36 };
37 
38 static void sc7280_jack_free(struct snd_jack *jack)
39 {
40 	struct snd_soc_component *component = jack->private_data;
41 
42 	snd_soc_component_set_jack(component, NULL, NULL);
43 }
44 
45 static int sc7280_headset_init(struct snd_soc_pcm_runtime *rtd)
46 {
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;
53 	int rval, i;
54 
55 	if (!pdata->jack_setup) {
56 		rval = snd_soc_card_jack_new(card, "Headset Jack",
57 					     SND_JACK_HEADSET | SND_JACK_LINEOUT |
58 					     SND_JACK_MECHANICAL |
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,
62 					     &pdata->hs_jack);
63 
64 		if (rval < 0) {
65 			dev_err(card->dev, "Unable to add Headset Jack\n");
66 			return rval;
67 		}
68 
69 		jack = pdata->hs_jack.jack;
70 
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);
75 
76 		jack->private_data = component;
77 		jack->private_free = sc7280_jack_free;
78 		pdata->jack_setup = true;
79 	}
80 	switch (cpu_dai->id) {
81 	case MI2S_PRIMARY:
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);
89 				return rval;
90 			}
91 		}
92 		break;
93 	default:
94 		break;
95 	}
96 
97 	return 0;
98 }
99 
100 static int sc7280_hdmi_init(struct snd_soc_pcm_runtime *rtd)
101 {
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;
107 	int rval;
108 
109 	rval = snd_soc_card_jack_new(card, "HDMI Jack",	SND_JACK_LINEOUT,
110 				     &pdata->hdmi_jack);
111 
112 	if (rval < 0) {
113 		dev_err(card->dev, "Unable to add HDMI Jack\n");
114 		return rval;
115 	}
116 
117 	jack = pdata->hdmi_jack.jack;
118 	jack->private_data = component;
119 	jack->private_free = sc7280_jack_free;
120 
121 	return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL);
122 }
123 
124 static int sc7280_rt5682_init(struct snd_soc_pcm_runtime *rtd)
125 {
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);
130 	int ret;
131 
132 	if (++data->pri_mi2s_clk_count == 1) {
133 		snd_soc_dai_set_sysclk(cpu_dai,
134 			LPASS_MCLK0,
135 			DEFAULT_MCLK_RATE,
136 			SNDRV_PCM_STREAM_PLAYBACK);
137 	}
138 	snd_soc_dai_set_fmt(codec_dai,
139 				SND_SOC_DAIFMT_CBC_CFC |
140 				SND_SOC_DAIFMT_NB_NF |
141 				SND_SOC_DAIFMT_I2S);
142 
143 	ret = snd_soc_dai_set_pll(codec_dai, RT5682S_PLL2, RT5682S_PLL_S_MCLK,
144 					DEFAULT_MCLK_RATE, RT5682_PLL_FREQ);
145 	if (ret) {
146 		dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
147 		return ret;
148 	}
149 
150 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682S_SCLK_S_PLL2,
151 					RT5682_PLL_FREQ,
152 					SND_SOC_CLOCK_IN);
153 
154 	if (ret) {
155 		dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n",
156 			ret);
157 		return ret;
158 	}
159 
160 	return 0;
161 }
162 
163 static int sc7280_init(struct snd_soc_pcm_runtime *rtd)
164 {
165 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
166 
167 	switch (cpu_dai->id) {
168 	case MI2S_PRIMARY:
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:
174 	case MI2S_SECONDARY:
175 	case RX_CODEC_DMA_RX_0:
176 	case SECONDARY_MI2S_RX:
177 	case VA_CODEC_DMA_TX_0:
178 		return 0;
179 	case LPASS_DP_RX:
180 		return sc7280_hdmi_init(rtd);
181 	default:
182 		dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
183 	}
184 
185 	return -EINVAL;
186 }
187 
188 static int sc7280_snd_hw_params(struct snd_pcm_substream *substream,
189 				struct snd_pcm_hw_params *params)
190 {
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;
197 	int i;
198 
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);
201 
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;
213 		}
214 		break;
215 	}
216 
217 	return 0;
218 }
219 
220 static int sc7280_snd_swr_prepare(struct snd_pcm_substream *substream)
221 {
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];
226 	int ret;
227 
228 	if (!sruntime)
229 		return 0;
230 
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;
235 	}
236 
237 	ret = sdw_prepare_stream(sruntime);
238 	if (ret)
239 		return ret;
240 
241 	ret = sdw_enable_stream(sruntime);
242 	if (ret) {
243 		sdw_deprepare_stream(sruntime);
244 		return ret;
245 	}
246 	data->stream_prepared[cpu_dai->id] = true;
247 
248 	return ret;
249 }
250 
251 static int sc7280_snd_prepare(struct snd_pcm_substream *substream)
252 {
253 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
254 	const struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
255 
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);
263 	default:
264 		break;
265 	}
266 
267 	return 0;
268 }
269 
270 static int sc7280_snd_hw_free(struct snd_pcm_substream *substream)
271 {
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];
276 
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;
287 		}
288 		break;
289 	default:
290 		break;
291 	}
292 	return 0;
293 }
294 
295 static void sc7280_snd_shutdown(struct snd_pcm_substream *substream)
296 {
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);
301 
302 	switch (cpu_dai->id) {
303 	case MI2S_PRIMARY:
304 		if (--data->pri_mi2s_clk_count == 0) {
305 			snd_soc_dai_set_sysclk(cpu_dai,
306 					       LPASS_MCLK0,
307 					       0,
308 					       SNDRV_PCM_STREAM_PLAYBACK);
309 		}
310 		break;
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);
314 		break;
315 	default:
316 		break;
317 	}
318 }
319 
320 static int sc7280_snd_startup(struct snd_pcm_substream *substream)
321 {
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);
327 	int ret = 0;
328 
329 	switch (cpu_dai->id) {
330 	case MI2S_PRIMARY:
331 		ret = sc7280_rt5682_init(rtd);
332 		break;
333 	case SECONDARY_MI2S_RX:
334 		codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S;
335 
336 		snd_soc_dai_set_sysclk(cpu_dai, Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
337 			MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
338 
339 		snd_soc_dai_set_fmt(cpu_dai, fmt);
340 		snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
341 		break;
342 	default:
343 		break;
344 	}
345 	return ret;
346 }
347 
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,
354 };
355 
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),
359 };
360 
361 static int sc7280_snd_platform_probe(struct platform_device *pdev)
362 {
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;
367 	int ret, i;
368 
369 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
370 	if (!data)
371 		return -ENOMEM;
372 
373 	card = &data->card;
374 	snd_soc_card_set_drvdata(card, data);
375 
376 	card->owner = THIS_MODULE;
377 	card->driver_name = "SC7280";
378 	card->dev = dev;
379 
380 	card->dapm_widgets = sc7280_snd_widgets;
381 	card->num_dapm_widgets = ARRAY_SIZE(sc7280_snd_widgets);
382 
383 	ret = qcom_snd_parse_of(card);
384 	if (ret)
385 		return ret;
386 
387 	for_each_card_prelinks(card, i, link) {
388 		link->init = sc7280_init;
389 		link->ops = &sc7280_ops;
390 	}
391 
392 	return devm_snd_soc_register_card(dev, card);
393 }
394 
395 static const struct of_device_id sc7280_snd_device_id[]  = {
396 	{ .compatible = "google,sc7280-herobrine" },
397 	{}
398 };
399 MODULE_DEVICE_TABLE(of, sc7280_snd_device_id);
400 
401 static struct platform_driver sc7280_snd_driver = {
402 	.probe = sc7280_snd_platform_probe,
403 	.driver = {
404 		.name = "msm-snd-sc7280",
405 		.of_match_table = sc7280_snd_device_id,
406 		.pm = &snd_soc_pm_ops,
407 	},
408 };
409 module_platform_driver(sc7280_snd_driver);
410 
411 MODULE_DESCRIPTION("sc7280 ASoC Machine Driver");
412 MODULE_LICENSE("GPL");
413