xref: /openbmc/linux/sound/soc/qcom/sdm845.c (revision c8ec3743)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/of_device.h>
9 #include <sound/core.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm_params.h>
12 #include <sound/jack.h>
13 #include <sound/soc.h>
14 #include <uapi/linux/input-event-codes.h>
15 #include "common.h"
16 #include "qdsp6/q6afe.h"
17 #include "../codecs/rt5663.h"
18 
19 #define DEFAULT_SAMPLE_RATE_48K		48000
20 #define DEFAULT_MCLK_RATE		24576000
21 #define TDM_BCLK_RATE		6144000
22 #define MI2S_BCLK_RATE		1536000
23 #define LEFT_SPK_TDM_TX_MASK    0x30
24 #define RIGHT_SPK_TDM_TX_MASK   0xC0
25 #define SPK_TDM_RX_MASK         0x03
26 #define NUM_TDM_SLOTS           8
27 
28 struct sdm845_snd_data {
29 	struct snd_soc_jack jack;
30 	bool jack_setup;
31 	struct snd_soc_card *card;
32 	uint32_t pri_mi2s_clk_count;
33 	uint32_t sec_mi2s_clk_count;
34 	uint32_t quat_tdm_clk_count;
35 };
36 
37 static unsigned int tdm_slot_offset[8] = {0, 4, 8, 12, 16, 20, 24, 28};
38 
39 static int sdm845_tdm_snd_hw_params(struct snd_pcm_substream *substream,
40 					struct snd_pcm_hw_params *params)
41 {
42 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
43 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
44 	int ret = 0, j;
45 	int channels, slot_width;
46 
47 	switch (params_format(params)) {
48 	case SNDRV_PCM_FORMAT_S16_LE:
49 		slot_width = 16;
50 		break;
51 	default:
52 		dev_err(rtd->dev, "%s: invalid param format 0x%x\n",
53 				__func__, params_format(params));
54 		return -EINVAL;
55 	}
56 
57 	channels = params_channels(params);
58 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
59 		ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0, 0x3,
60 				8, slot_width);
61 		if (ret < 0) {
62 			dev_err(rtd->dev, "%s: failed to set tdm slot, err:%d\n",
63 					__func__, ret);
64 			goto end;
65 		}
66 
67 		ret = snd_soc_dai_set_channel_map(cpu_dai, 0, NULL,
68 				channels, tdm_slot_offset);
69 		if (ret < 0) {
70 			dev_err(rtd->dev, "%s: failed to set channel map, err:%d\n",
71 					__func__, ret);
72 			goto end;
73 		}
74 	} else {
75 		ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0xf, 0,
76 				8, slot_width);
77 		if (ret < 0) {
78 			dev_err(rtd->dev, "%s: failed to set tdm slot, err:%d\n",
79 					__func__, ret);
80 			goto end;
81 		}
82 
83 		ret = snd_soc_dai_set_channel_map(cpu_dai, channels,
84 				tdm_slot_offset, 0, NULL);
85 		if (ret < 0) {
86 			dev_err(rtd->dev, "%s: failed to set channel map, err:%d\n",
87 					__func__, ret);
88 			goto end;
89 		}
90 	}
91 
92 	for (j = 0; j < rtd->num_codecs; j++) {
93 		struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
94 
95 		if (!strcmp(codec_dai->component->name_prefix, "Left")) {
96 			ret = snd_soc_dai_set_tdm_slot(
97 					codec_dai, LEFT_SPK_TDM_TX_MASK,
98 					SPK_TDM_RX_MASK, NUM_TDM_SLOTS,
99 					slot_width);
100 			if (ret < 0) {
101 				dev_err(rtd->dev,
102 					"DEV0 TDM slot err:%d\n", ret);
103 				return ret;
104 			}
105 		}
106 
107 		if (!strcmp(codec_dai->component->name_prefix, "Right")) {
108 			ret = snd_soc_dai_set_tdm_slot(
109 					codec_dai, RIGHT_SPK_TDM_TX_MASK,
110 					SPK_TDM_RX_MASK, NUM_TDM_SLOTS,
111 					slot_width);
112 			if (ret < 0) {
113 				dev_err(rtd->dev,
114 					"DEV1 TDM slot err:%d\n", ret);
115 				return ret;
116 			}
117 		}
118 	}
119 
120 end:
121 	return ret;
122 }
123 
124 static int sdm845_snd_hw_params(struct snd_pcm_substream *substream,
125 					struct snd_pcm_hw_params *params)
126 {
127 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
128 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
129 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
130 	int ret = 0;
131 
132 	switch (cpu_dai->id) {
133 	case PRIMARY_MI2S_RX:
134 	case PRIMARY_MI2S_TX:
135 		/*
136 		 * Use ASRC for internal clocks, as PLL rate isn't multiple
137 		 * of BCLK.
138 		 */
139 		rt5663_sel_asrc_clk_src(
140 			codec_dai->component,
141 			RT5663_DA_STEREO_FILTER | RT5663_AD_STEREO_FILTER,
142 			RT5663_CLK_SEL_I2S1_ASRC);
143 		ret = snd_soc_dai_set_sysclk(
144 			codec_dai, RT5663_SCLK_S_MCLK, DEFAULT_MCLK_RATE,
145 			SND_SOC_CLOCK_IN);
146 		if (ret < 0)
147 			dev_err(rtd->dev,
148 				"snd_soc_dai_set_sysclk err = %d\n", ret);
149 		break;
150 	case QUATERNARY_TDM_RX_0:
151 	case QUATERNARY_TDM_TX_0:
152 		ret = sdm845_tdm_snd_hw_params(substream, params);
153 		break;
154 	default:
155 		pr_err("%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
156 		break;
157 	}
158 	return ret;
159 }
160 
161 static void sdm845_jack_free(struct snd_jack *jack)
162 {
163 	struct snd_soc_component *component = jack->private_data;
164 
165 	snd_soc_component_set_jack(component, NULL, NULL);
166 }
167 
168 static int sdm845_dai_init(struct snd_soc_pcm_runtime *rtd)
169 {
170 	struct snd_soc_component *component;
171 	struct snd_soc_card *card = rtd->card;
172 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
173 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
174 	struct sdm845_snd_data *pdata = snd_soc_card_get_drvdata(card);
175 	struct snd_jack *jack;
176 	int rval;
177 
178 	if (!pdata->jack_setup) {
179 		rval = snd_soc_card_jack_new(card, "Headset Jack",
180 				SND_JACK_HEADSET |
181 				SND_JACK_HEADPHONE |
182 				SND_JACK_BTN_0 | SND_JACK_BTN_1 |
183 				SND_JACK_BTN_2 | SND_JACK_BTN_3,
184 				&pdata->jack, NULL, 0);
185 
186 		if (rval < 0) {
187 			dev_err(card->dev, "Unable to add Headphone Jack\n");
188 			return rval;
189 		}
190 
191 		jack = pdata->jack.jack;
192 
193 		snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
194 		snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
195 		snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
196 		snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
197 		pdata->jack_setup = true;
198 	}
199 
200 	switch (cpu_dai->id) {
201 	case PRIMARY_MI2S_RX:
202 		jack  = pdata->jack.jack;
203 		component = codec_dai->component;
204 
205 		jack->private_data = component;
206 		jack->private_free = sdm845_jack_free;
207 		rval = snd_soc_component_set_jack(component,
208 						  &pdata->jack, NULL);
209 		if (rval != 0 && rval != -ENOTSUPP) {
210 			dev_warn(card->dev, "Failed to set jack: %d\n", rval);
211 			return rval;
212 		}
213 		break;
214 	default:
215 		break;
216 	}
217 
218 	return 0;
219 }
220 
221 
222 static int sdm845_snd_startup(struct snd_pcm_substream *substream)
223 {
224 	unsigned int fmt = SND_SOC_DAIFMT_CBS_CFS;
225 	unsigned int codec_dai_fmt = SND_SOC_DAIFMT_CBS_CFS;
226 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
227 	struct snd_soc_card *card = rtd->card;
228 	struct sdm845_snd_data *data = snd_soc_card_get_drvdata(card);
229 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
230 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
231 	int j;
232 	int ret;
233 
234 	switch (cpu_dai->id) {
235 	case PRIMARY_MI2S_RX:
236 	case PRIMARY_MI2S_TX:
237 		codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF;
238 		if (++(data->pri_mi2s_clk_count) == 1) {
239 			snd_soc_dai_set_sysclk(cpu_dai,
240 				Q6AFE_LPASS_CLK_ID_MCLK_1,
241 				DEFAULT_MCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
242 			snd_soc_dai_set_sysclk(cpu_dai,
243 				Q6AFE_LPASS_CLK_ID_PRI_MI2S_IBIT,
244 				MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
245 		}
246 		snd_soc_dai_set_fmt(cpu_dai, fmt);
247 		snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
248 		break;
249 
250 	case SECONDARY_MI2S_TX:
251 		if (++(data->sec_mi2s_clk_count) == 1) {
252 			snd_soc_dai_set_sysclk(cpu_dai,
253 				Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
254 				MI2S_BCLK_RATE,	SNDRV_PCM_STREAM_CAPTURE);
255 		}
256 		snd_soc_dai_set_fmt(cpu_dai, fmt);
257 		break;
258 
259 	case QUATERNARY_TDM_RX_0:
260 	case QUATERNARY_TDM_TX_0:
261 		if (++(data->quat_tdm_clk_count) == 1) {
262 			snd_soc_dai_set_sysclk(cpu_dai,
263 				Q6AFE_LPASS_CLK_ID_QUAD_TDM_IBIT,
264 				TDM_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
265 		}
266 
267 		codec_dai_fmt |= SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_DSP_B;
268 
269 		for (j = 0; j < rtd->num_codecs; j++) {
270 			codec_dai = rtd->codec_dais[j];
271 
272 			if (!strcmp(codec_dai->component->name_prefix,
273 				    "Left")) {
274 				ret = snd_soc_dai_set_fmt(
275 						codec_dai, codec_dai_fmt);
276 				if (ret < 0) {
277 					dev_err(rtd->dev,
278 						"Left TDM fmt err:%d\n", ret);
279 					return ret;
280 				}
281 			}
282 
283 			if (!strcmp(codec_dai->component->name_prefix,
284 				    "Right")) {
285 				ret = snd_soc_dai_set_fmt(
286 						codec_dai, codec_dai_fmt);
287 				if (ret < 0) {
288 					dev_err(rtd->dev,
289 						"Right TDM slot err:%d\n", ret);
290 					return ret;
291 				}
292 			}
293 		}
294 		break;
295 
296 	default:
297 		pr_err("%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
298 		break;
299 	}
300 	return 0;
301 }
302 
303 static void  sdm845_snd_shutdown(struct snd_pcm_substream *substream)
304 {
305 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
306 	struct snd_soc_card *card = rtd->card;
307 	struct sdm845_snd_data *data = snd_soc_card_get_drvdata(card);
308 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
309 
310 	switch (cpu_dai->id) {
311 	case PRIMARY_MI2S_RX:
312 	case PRIMARY_MI2S_TX:
313 		if (--(data->pri_mi2s_clk_count) == 0) {
314 			snd_soc_dai_set_sysclk(cpu_dai,
315 				Q6AFE_LPASS_CLK_ID_MCLK_1,
316 				0, SNDRV_PCM_STREAM_PLAYBACK);
317 			snd_soc_dai_set_sysclk(cpu_dai,
318 				Q6AFE_LPASS_CLK_ID_PRI_MI2S_IBIT,
319 				0, SNDRV_PCM_STREAM_PLAYBACK);
320 		};
321 		break;
322 
323 	case SECONDARY_MI2S_TX:
324 		if (--(data->sec_mi2s_clk_count) == 0) {
325 			snd_soc_dai_set_sysclk(cpu_dai,
326 				Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
327 				0, SNDRV_PCM_STREAM_CAPTURE);
328 		}
329 		break;
330 
331 	case QUATERNARY_TDM_RX_0:
332 	case QUATERNARY_TDM_TX_0:
333 		if (--(data->quat_tdm_clk_count) == 0) {
334 			snd_soc_dai_set_sysclk(cpu_dai,
335 				Q6AFE_LPASS_CLK_ID_QUAD_TDM_IBIT,
336 				0, SNDRV_PCM_STREAM_PLAYBACK);
337 		}
338 		break;
339 
340 	default:
341 		pr_err("%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
342 		break;
343 	}
344 }
345 
346 static const struct snd_soc_ops sdm845_be_ops = {
347 	.hw_params = sdm845_snd_hw_params,
348 	.startup = sdm845_snd_startup,
349 	.shutdown = sdm845_snd_shutdown,
350 };
351 
352 static int sdm845_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
353 				struct snd_pcm_hw_params *params)
354 {
355 	struct snd_interval *rate = hw_param_interval(params,
356 					SNDRV_PCM_HW_PARAM_RATE);
357 	struct snd_interval *channels = hw_param_interval(params,
358 					SNDRV_PCM_HW_PARAM_CHANNELS);
359 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
360 
361 	rate->min = rate->max = DEFAULT_SAMPLE_RATE_48K;
362 	channels->min = channels->max = 2;
363 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
364 
365 	return 0;
366 }
367 
368 static const struct snd_soc_dapm_widget sdm845_snd_widgets[] = {
369 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
370 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
371 	SND_SOC_DAPM_SPK("Left Spk", NULL),
372 	SND_SOC_DAPM_SPK("Right Spk", NULL),
373 	SND_SOC_DAPM_MIC("Int Mic", NULL),
374 };
375 
376 static void sdm845_add_ops(struct snd_soc_card *card)
377 {
378 	struct snd_soc_dai_link *link;
379 	int i;
380 
381 	for_each_card_prelinks(card, i, link) {
382 		if (link->no_pcm == 1) {
383 			link->ops = &sdm845_be_ops;
384 			link->be_hw_params_fixup = sdm845_be_hw_params_fixup;
385 		}
386 		link->init = sdm845_dai_init;
387 	}
388 }
389 
390 static int sdm845_snd_platform_probe(struct platform_device *pdev)
391 {
392 	struct snd_soc_card *card;
393 	struct sdm845_snd_data *data;
394 	struct device *dev = &pdev->dev;
395 	int ret;
396 
397 	card = kzalloc(sizeof(*card), GFP_KERNEL);
398 	if (!card)
399 		return -ENOMEM;
400 
401 	/* Allocate the private data */
402 	data = kzalloc(sizeof(*data), GFP_KERNEL);
403 	if (!data) {
404 		ret = -ENOMEM;
405 		goto data_alloc_fail;
406 	}
407 
408 	card->dapm_widgets = sdm845_snd_widgets;
409 	card->num_dapm_widgets = ARRAY_SIZE(sdm845_snd_widgets);
410 	card->dev = dev;
411 	dev_set_drvdata(dev, card);
412 	ret = qcom_snd_parse_of(card);
413 	if (ret) {
414 		dev_err(dev, "Error parsing OF data\n");
415 		goto parse_dt_fail;
416 	}
417 
418 	data->card = card;
419 	snd_soc_card_set_drvdata(card, data);
420 
421 	sdm845_add_ops(card);
422 	ret = snd_soc_register_card(card);
423 	if (ret) {
424 		dev_err(dev, "Sound card registration failed\n");
425 		goto register_card_fail;
426 	}
427 	return ret;
428 
429 register_card_fail:
430 	kfree(card->dai_link);
431 parse_dt_fail:
432 	kfree(data);
433 data_alloc_fail:
434 	kfree(card);
435 	return ret;
436 }
437 
438 static int sdm845_snd_platform_remove(struct platform_device *pdev)
439 {
440 	struct snd_soc_card *card = dev_get_drvdata(&pdev->dev);
441 	struct sdm845_snd_data *data = snd_soc_card_get_drvdata(card);
442 
443 	snd_soc_unregister_card(card);
444 	kfree(card->dai_link);
445 	kfree(data);
446 	kfree(card);
447 	return 0;
448 }
449 
450 static const struct of_device_id sdm845_snd_device_id[]  = {
451 	{ .compatible = "qcom,sdm845-sndcard" },
452 	{},
453 };
454 MODULE_DEVICE_TABLE(of, sdm845_snd_device_id);
455 
456 static struct platform_driver sdm845_snd_driver = {
457 	.probe = sdm845_snd_platform_probe,
458 	.remove = sdm845_snd_platform_remove,
459 	.driver = {
460 		.name = "msm-snd-sdm845",
461 		.of_match_table = sdm845_snd_device_id,
462 	},
463 };
464 module_platform_driver(sdm845_snd_driver);
465 
466 MODULE_DESCRIPTION("sdm845 ASoC Machine Driver");
467 MODULE_LICENSE("GPL v2");
468