xref: /openbmc/linux/sound/soc/qcom/sdm845.c (revision af9b2ff010f593d81e2f5fb04155e9fc25b9dfd0)
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 <linux/soundwire/sdw.h>
15 #include <uapi/linux/input-event-codes.h>
16 #include "common.h"
17 #include "qdsp6/q6afe.h"
18 #include "../codecs/rt5663.h"
19 
20 #define DRIVER_NAME	"sdm845"
21 #define DEFAULT_SAMPLE_RATE_48K		48000
22 #define DEFAULT_MCLK_RATE		24576000
23 #define TDM_BCLK_RATE		6144000
24 #define MI2S_BCLK_RATE		1536000
25 #define LEFT_SPK_TDM_TX_MASK    0x30
26 #define RIGHT_SPK_TDM_TX_MASK   0xC0
27 #define SPK_TDM_RX_MASK         0x03
28 #define NUM_TDM_SLOTS           8
29 #define SLIM_MAX_TX_PORTS 16
30 #define SLIM_MAX_RX_PORTS 13
31 #define WCD934X_DEFAULT_MCLK_RATE	9600000
32 
33 struct sdm845_snd_data {
34 	struct snd_soc_jack jack;
35 	bool jack_setup;
36 	bool slim_port_setup;
37 	bool stream_prepared[AFE_PORT_MAX];
38 	struct snd_soc_card *card;
39 	uint32_t pri_mi2s_clk_count;
40 	uint32_t sec_mi2s_clk_count;
41 	uint32_t quat_tdm_clk_count;
42 	struct sdw_stream_runtime *sruntime[AFE_PORT_MAX];
43 };
44 
45 static struct snd_soc_jack_pin sdm845_jack_pins[] = {
46 	{
47 		.pin = "Headphone Jack",
48 		.mask = SND_JACK_HEADPHONE,
49 	},
50 	{
51 		.pin = "Headset Mic",
52 		.mask = SND_JACK_MICROPHONE,
53 	},
54 };
55 
56 static unsigned int tdm_slot_offset[8] = {0, 4, 8, 12, 16, 20, 24, 28};
57 
sdm845_slim_snd_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)58 static int sdm845_slim_snd_hw_params(struct snd_pcm_substream *substream,
59 				     struct snd_pcm_hw_params *params)
60 {
61 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
62 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
63 	struct snd_soc_dai *codec_dai;
64 	struct sdm845_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
65 	u32 rx_ch[SLIM_MAX_RX_PORTS], tx_ch[SLIM_MAX_TX_PORTS];
66 	struct sdw_stream_runtime *sruntime;
67 	u32 rx_ch_cnt = 0, tx_ch_cnt = 0;
68 	int ret = 0, i;
69 
70 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
71 		sruntime = snd_soc_dai_get_stream(codec_dai,
72 						  substream->stream);
73 		if (sruntime != ERR_PTR(-ENOTSUPP))
74 			pdata->sruntime[cpu_dai->id] = sruntime;
75 
76 		ret = snd_soc_dai_get_channel_map(codec_dai,
77 				&tx_ch_cnt, tx_ch, &rx_ch_cnt, rx_ch);
78 
79 		if (ret != 0 && ret != -ENOTSUPP) {
80 			pr_err("failed to get codec chan map, err:%d\n", ret);
81 			return ret;
82 		} else if (ret == -ENOTSUPP) {
83 			/* Ignore unsupported */
84 			continue;
85 		}
86 
87 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
88 			ret = snd_soc_dai_set_channel_map(cpu_dai, 0, NULL,
89 							  rx_ch_cnt, rx_ch);
90 		else
91 			ret = snd_soc_dai_set_channel_map(cpu_dai, tx_ch_cnt,
92 							  tx_ch, 0, NULL);
93 		if (ret != 0 && ret != -ENOTSUPP) {
94 			dev_err(rtd->dev, "failed to set cpu chan map, err:%d\n", ret);
95 			return ret;
96 		}
97 	}
98 
99 	return 0;
100 }
101 
sdm845_tdm_snd_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)102 static int sdm845_tdm_snd_hw_params(struct snd_pcm_substream *substream,
103 					struct snd_pcm_hw_params *params)
104 {
105 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
106 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
107 	struct snd_soc_dai *codec_dai;
108 	int ret = 0, j;
109 	int channels, slot_width;
110 
111 	switch (params_format(params)) {
112 	case SNDRV_PCM_FORMAT_S16_LE:
113 		slot_width = 16;
114 		break;
115 	default:
116 		dev_err(rtd->dev, "%s: invalid param format 0x%x\n",
117 				__func__, params_format(params));
118 		return -EINVAL;
119 	}
120 
121 	channels = params_channels(params);
122 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
123 		ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0, 0x3,
124 				8, slot_width);
125 		if (ret < 0) {
126 			dev_err(rtd->dev, "%s: failed to set tdm slot, err:%d\n",
127 					__func__, ret);
128 			goto end;
129 		}
130 
131 		ret = snd_soc_dai_set_channel_map(cpu_dai, 0, NULL,
132 				channels, tdm_slot_offset);
133 		if (ret < 0) {
134 			dev_err(rtd->dev, "%s: failed to set channel map, err:%d\n",
135 					__func__, ret);
136 			goto end;
137 		}
138 	} else {
139 		ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0xf, 0,
140 				8, slot_width);
141 		if (ret < 0) {
142 			dev_err(rtd->dev, "%s: failed to set tdm slot, err:%d\n",
143 					__func__, ret);
144 			goto end;
145 		}
146 
147 		ret = snd_soc_dai_set_channel_map(cpu_dai, channels,
148 				tdm_slot_offset, 0, NULL);
149 		if (ret < 0) {
150 			dev_err(rtd->dev, "%s: failed to set channel map, err:%d\n",
151 					__func__, ret);
152 			goto end;
153 		}
154 	}
155 
156 	for_each_rtd_codec_dais(rtd, j, codec_dai) {
157 
158 		if (!strcmp(codec_dai->component->name_prefix, "Left")) {
159 			ret = snd_soc_dai_set_tdm_slot(
160 					codec_dai, LEFT_SPK_TDM_TX_MASK,
161 					SPK_TDM_RX_MASK, NUM_TDM_SLOTS,
162 					slot_width);
163 			if (ret < 0) {
164 				dev_err(rtd->dev,
165 					"DEV0 TDM slot err:%d\n", ret);
166 				return ret;
167 			}
168 		}
169 
170 		if (!strcmp(codec_dai->component->name_prefix, "Right")) {
171 			ret = snd_soc_dai_set_tdm_slot(
172 					codec_dai, RIGHT_SPK_TDM_TX_MASK,
173 					SPK_TDM_RX_MASK, NUM_TDM_SLOTS,
174 					slot_width);
175 			if (ret < 0) {
176 				dev_err(rtd->dev,
177 					"DEV1 TDM slot err:%d\n", ret);
178 				return ret;
179 			}
180 		}
181 	}
182 
183 end:
184 	return ret;
185 }
186 
sdm845_snd_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)187 static int sdm845_snd_hw_params(struct snd_pcm_substream *substream,
188 					struct snd_pcm_hw_params *params)
189 {
190 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
191 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
192 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
193 	int ret = 0;
194 
195 	switch (cpu_dai->id) {
196 	case PRIMARY_MI2S_RX:
197 	case PRIMARY_MI2S_TX:
198 		/*
199 		 * Use ASRC for internal clocks, as PLL rate isn't multiple
200 		 * of BCLK.
201 		 */
202 		rt5663_sel_asrc_clk_src(
203 			codec_dai->component,
204 			RT5663_DA_STEREO_FILTER | RT5663_AD_STEREO_FILTER,
205 			RT5663_CLK_SEL_I2S1_ASRC);
206 		ret = snd_soc_dai_set_sysclk(
207 			codec_dai, RT5663_SCLK_S_MCLK, DEFAULT_MCLK_RATE,
208 			SND_SOC_CLOCK_IN);
209 		if (ret < 0)
210 			dev_err(rtd->dev,
211 				"snd_soc_dai_set_sysclk err = %d\n", ret);
212 		break;
213 	case QUATERNARY_TDM_RX_0:
214 	case QUATERNARY_TDM_TX_0:
215 		ret = sdm845_tdm_snd_hw_params(substream, params);
216 		break;
217 	case SLIMBUS_0_RX...SLIMBUS_6_TX:
218 		ret = sdm845_slim_snd_hw_params(substream, params);
219 		break;
220 	case QUATERNARY_MI2S_RX:
221 		break;
222 	default:
223 		pr_err("%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
224 		break;
225 	}
226 	return ret;
227 }
228 
sdm845_jack_free(struct snd_jack * jack)229 static void sdm845_jack_free(struct snd_jack *jack)
230 {
231 	struct snd_soc_component *component = jack->private_data;
232 
233 	snd_soc_component_set_jack(component, NULL, NULL);
234 }
235 
sdm845_dai_init(struct snd_soc_pcm_runtime * rtd)236 static int sdm845_dai_init(struct snd_soc_pcm_runtime *rtd)
237 {
238 	struct snd_soc_component *component;
239 	struct snd_soc_card *card = rtd->card;
240 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
241 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
242 	struct sdm845_snd_data *pdata = snd_soc_card_get_drvdata(card);
243 	struct snd_soc_dai_link *link = rtd->dai_link;
244 	struct snd_jack *jack;
245 	/*
246 	 * Codec SLIMBUS configuration
247 	 * RX1, RX2, RX3, RX4, RX5, RX6, RX7, RX8, RX9, RX10, RX11, RX12, RX13
248 	 * TX1, TX2, TX3, TX4, TX5, TX6, TX7, TX8, TX9, TX10, TX11, TX12, TX13
249 	 * TX14, TX15, TX16
250 	 */
251 	unsigned int rx_ch[SLIM_MAX_RX_PORTS] = {144, 145, 146, 147, 148, 149,
252 					150, 151, 152, 153, 154, 155, 156};
253 	unsigned int tx_ch[SLIM_MAX_TX_PORTS] = {128, 129, 130, 131, 132, 133,
254 					    134, 135, 136, 137, 138, 139,
255 					    140, 141, 142, 143};
256 	int rval, i;
257 
258 
259 	if (!pdata->jack_setup) {
260 		rval = snd_soc_card_jack_new_pins(card, "Headset Jack",
261 						  SND_JACK_HEADSET |
262 						  SND_JACK_HEADPHONE |
263 						  SND_JACK_BTN_0 | SND_JACK_BTN_1 |
264 						  SND_JACK_BTN_2 | SND_JACK_BTN_3,
265 						  &pdata->jack,
266 						  sdm845_jack_pins,
267 						  ARRAY_SIZE(sdm845_jack_pins));
268 
269 		if (rval < 0) {
270 			dev_err(card->dev, "Unable to add Headphone Jack\n");
271 			return rval;
272 		}
273 
274 		jack = pdata->jack.jack;
275 
276 		snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
277 		snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
278 		snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
279 		snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
280 		pdata->jack_setup = true;
281 	}
282 
283 	switch (cpu_dai->id) {
284 	case PRIMARY_MI2S_RX:
285 		jack  = pdata->jack.jack;
286 		component = codec_dai->component;
287 
288 		jack->private_data = component;
289 		jack->private_free = sdm845_jack_free;
290 		rval = snd_soc_component_set_jack(component,
291 						  &pdata->jack, NULL);
292 		if (rval != 0 && rval != -ENOTSUPP) {
293 			dev_warn(card->dev, "Failed to set jack: %d\n", rval);
294 			return rval;
295 		}
296 		break;
297 	case SLIMBUS_0_RX...SLIMBUS_6_TX:
298 		/* setting up wcd multiple times for slim port is redundant */
299 		if (pdata->slim_port_setup || !link->no_pcm)
300 			return 0;
301 
302 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
303 			rval = snd_soc_dai_set_channel_map(codec_dai,
304 							  ARRAY_SIZE(tx_ch),
305 							  tx_ch,
306 							  ARRAY_SIZE(rx_ch),
307 							  rx_ch);
308 			if (rval != 0 && rval != -ENOTSUPP)
309 				return rval;
310 
311 			snd_soc_dai_set_sysclk(codec_dai, 0,
312 					       WCD934X_DEFAULT_MCLK_RATE,
313 					       SNDRV_PCM_STREAM_PLAYBACK);
314 
315 			rval = snd_soc_component_set_jack(codec_dai->component,
316 							  &pdata->jack, NULL);
317 			if (rval != 0 && rval != -ENOTSUPP) {
318 				dev_warn(card->dev, "Failed to set jack: %d\n", rval);
319 				return rval;
320 			}
321 		}
322 
323 		pdata->slim_port_setup = true;
324 
325 		break;
326 	default:
327 		break;
328 	}
329 
330 	return 0;
331 }
332 
333 
sdm845_snd_startup(struct snd_pcm_substream * substream)334 static int sdm845_snd_startup(struct snd_pcm_substream *substream)
335 {
336 	unsigned int fmt = SND_SOC_DAIFMT_BP_FP;
337 	unsigned int codec_dai_fmt = SND_SOC_DAIFMT_BC_FC;
338 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
339 	struct snd_soc_card *card = rtd->card;
340 	struct sdm845_snd_data *data = snd_soc_card_get_drvdata(card);
341 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
342 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
343 	int j;
344 	int ret;
345 
346 	switch (cpu_dai->id) {
347 	case PRIMARY_MI2S_RX:
348 	case PRIMARY_MI2S_TX:
349 		codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF;
350 		if (++(data->pri_mi2s_clk_count) == 1) {
351 			snd_soc_dai_set_sysclk(cpu_dai,
352 				Q6AFE_LPASS_CLK_ID_MCLK_1,
353 				DEFAULT_MCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
354 			snd_soc_dai_set_sysclk(cpu_dai,
355 				Q6AFE_LPASS_CLK_ID_PRI_MI2S_IBIT,
356 				MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
357 		}
358 		snd_soc_dai_set_fmt(cpu_dai, fmt);
359 		snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
360 		break;
361 
362 	case SECONDARY_MI2S_TX:
363 		codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S;
364 		if (++(data->sec_mi2s_clk_count) == 1) {
365 			snd_soc_dai_set_sysclk(cpu_dai,
366 				Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
367 				MI2S_BCLK_RATE,	SNDRV_PCM_STREAM_CAPTURE);
368 		}
369 		snd_soc_dai_set_fmt(cpu_dai, fmt);
370 		snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
371 		break;
372 	case QUATERNARY_MI2S_RX:
373 		snd_soc_dai_set_sysclk(cpu_dai,
374 			Q6AFE_LPASS_CLK_ID_QUAD_MI2S_IBIT,
375 			MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
376 		snd_soc_dai_set_fmt(cpu_dai, fmt);
377 
378 
379 		break;
380 
381 	case QUATERNARY_TDM_RX_0:
382 	case QUATERNARY_TDM_TX_0:
383 		if (++(data->quat_tdm_clk_count) == 1) {
384 			snd_soc_dai_set_sysclk(cpu_dai,
385 				Q6AFE_LPASS_CLK_ID_QUAD_TDM_IBIT,
386 				TDM_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
387 		}
388 
389 		codec_dai_fmt |= SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_DSP_B;
390 
391 		for_each_rtd_codec_dais(rtd, j, codec_dai) {
392 
393 			if (!strcmp(codec_dai->component->name_prefix,
394 				    "Left")) {
395 				ret = snd_soc_dai_set_fmt(
396 						codec_dai, codec_dai_fmt);
397 				if (ret < 0) {
398 					dev_err(rtd->dev,
399 						"Left TDM fmt err:%d\n", ret);
400 					return ret;
401 				}
402 			}
403 
404 			if (!strcmp(codec_dai->component->name_prefix,
405 				    "Right")) {
406 				ret = snd_soc_dai_set_fmt(
407 						codec_dai, codec_dai_fmt);
408 				if (ret < 0) {
409 					dev_err(rtd->dev,
410 						"Right TDM slot err:%d\n", ret);
411 					return ret;
412 				}
413 			}
414 		}
415 		break;
416 	case SLIMBUS_0_RX...SLIMBUS_6_TX:
417 		break;
418 
419 	default:
420 		pr_err("%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
421 		break;
422 	}
423 	return 0;
424 }
425 
sdm845_snd_shutdown(struct snd_pcm_substream * substream)426 static void  sdm845_snd_shutdown(struct snd_pcm_substream *substream)
427 {
428 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
429 	struct snd_soc_card *card = rtd->card;
430 	struct sdm845_snd_data *data = snd_soc_card_get_drvdata(card);
431 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
432 
433 	switch (cpu_dai->id) {
434 	case PRIMARY_MI2S_RX:
435 	case PRIMARY_MI2S_TX:
436 		if (--(data->pri_mi2s_clk_count) == 0) {
437 			snd_soc_dai_set_sysclk(cpu_dai,
438 				Q6AFE_LPASS_CLK_ID_MCLK_1,
439 				0, SNDRV_PCM_STREAM_PLAYBACK);
440 			snd_soc_dai_set_sysclk(cpu_dai,
441 				Q6AFE_LPASS_CLK_ID_PRI_MI2S_IBIT,
442 				0, SNDRV_PCM_STREAM_PLAYBACK);
443 		}
444 		break;
445 
446 	case SECONDARY_MI2S_TX:
447 		if (--(data->sec_mi2s_clk_count) == 0) {
448 			snd_soc_dai_set_sysclk(cpu_dai,
449 				Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
450 				0, SNDRV_PCM_STREAM_CAPTURE);
451 		}
452 		break;
453 
454 	case QUATERNARY_TDM_RX_0:
455 	case QUATERNARY_TDM_TX_0:
456 		if (--(data->quat_tdm_clk_count) == 0) {
457 			snd_soc_dai_set_sysclk(cpu_dai,
458 				Q6AFE_LPASS_CLK_ID_QUAD_TDM_IBIT,
459 				0, SNDRV_PCM_STREAM_PLAYBACK);
460 		}
461 		break;
462 	case SLIMBUS_0_RX...SLIMBUS_6_TX:
463 	case QUATERNARY_MI2S_RX:
464 		break;
465 
466 	default:
467 		pr_err("%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
468 		break;
469 	}
470 }
471 
sdm845_snd_prepare(struct snd_pcm_substream * substream)472 static int sdm845_snd_prepare(struct snd_pcm_substream *substream)
473 {
474 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
475 	struct sdm845_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
476 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
477 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
478 	int ret;
479 
480 	if (!sruntime)
481 		return 0;
482 
483 	if (data->stream_prepared[cpu_dai->id]) {
484 		sdw_disable_stream(sruntime);
485 		sdw_deprepare_stream(sruntime);
486 		data->stream_prepared[cpu_dai->id] = false;
487 	}
488 
489 	ret = sdw_prepare_stream(sruntime);
490 	if (ret)
491 		return ret;
492 
493 	/**
494 	 * NOTE: there is a strict hw requirement about the ordering of port
495 	 * enables and actual WSA881x PA enable. PA enable should only happen
496 	 * after soundwire ports are enabled if not DC on the line is
497 	 * accumulated resulting in Click/Pop Noise
498 	 * PA enable/mute are handled as part of codec DAPM and digital mute.
499 	 */
500 
501 	ret = sdw_enable_stream(sruntime);
502 	if (ret) {
503 		sdw_deprepare_stream(sruntime);
504 		return ret;
505 	}
506 	data->stream_prepared[cpu_dai->id] = true;
507 
508 	return ret;
509 }
510 
sdm845_snd_hw_free(struct snd_pcm_substream * substream)511 static int sdm845_snd_hw_free(struct snd_pcm_substream *substream)
512 {
513 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
514 	struct sdm845_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
515 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
516 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
517 
518 	if (sruntime && data->stream_prepared[cpu_dai->id]) {
519 		sdw_disable_stream(sruntime);
520 		sdw_deprepare_stream(sruntime);
521 		data->stream_prepared[cpu_dai->id] = false;
522 	}
523 
524 	return 0;
525 }
526 
527 static const struct snd_soc_ops sdm845_be_ops = {
528 	.hw_params = sdm845_snd_hw_params,
529 	.hw_free = sdm845_snd_hw_free,
530 	.prepare = sdm845_snd_prepare,
531 	.startup = sdm845_snd_startup,
532 	.shutdown = sdm845_snd_shutdown,
533 };
534 
sdm845_be_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)535 static int sdm845_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
536 				struct snd_pcm_hw_params *params)
537 {
538 	struct snd_interval *rate = hw_param_interval(params,
539 					SNDRV_PCM_HW_PARAM_RATE);
540 	struct snd_interval *channels = hw_param_interval(params,
541 					SNDRV_PCM_HW_PARAM_CHANNELS);
542 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
543 
544 	rate->min = rate->max = DEFAULT_SAMPLE_RATE_48K;
545 	channels->min = channels->max = 2;
546 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
547 
548 	return 0;
549 }
550 
551 static const struct snd_soc_dapm_widget sdm845_snd_widgets[] = {
552 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
553 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
554 	SND_SOC_DAPM_SPK("Left Spk", NULL),
555 	SND_SOC_DAPM_SPK("Right Spk", NULL),
556 	SND_SOC_DAPM_MIC("Int Mic", NULL),
557 };
558 
559 static const struct snd_kcontrol_new sdm845_snd_controls[] = {
560 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
561 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
562 };
563 
sdm845_add_ops(struct snd_soc_card * card)564 static void sdm845_add_ops(struct snd_soc_card *card)
565 {
566 	struct snd_soc_dai_link *link;
567 	int i;
568 
569 	for_each_card_prelinks(card, i, link) {
570 		if (link->no_pcm == 1) {
571 			link->ops = &sdm845_be_ops;
572 			link->be_hw_params_fixup = sdm845_be_hw_params_fixup;
573 		}
574 		link->init = sdm845_dai_init;
575 	}
576 }
577 
sdm845_snd_platform_probe(struct platform_device * pdev)578 static int sdm845_snd_platform_probe(struct platform_device *pdev)
579 {
580 	struct snd_soc_card *card;
581 	struct sdm845_snd_data *data;
582 	struct device *dev = &pdev->dev;
583 	int ret;
584 
585 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
586 	if (!card)
587 		return -ENOMEM;
588 
589 	/* Allocate the private data */
590 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
591 	if (!data)
592 		return -ENOMEM;
593 
594 	card->driver_name = DRIVER_NAME;
595 	card->dapm_widgets = sdm845_snd_widgets;
596 	card->num_dapm_widgets = ARRAY_SIZE(sdm845_snd_widgets);
597 	card->controls = sdm845_snd_controls;
598 	card->num_controls = ARRAY_SIZE(sdm845_snd_controls);
599 	card->dev = dev;
600 	card->owner = THIS_MODULE;
601 	dev_set_drvdata(dev, card);
602 	ret = qcom_snd_parse_of(card);
603 	if (ret)
604 		return ret;
605 
606 	data->card = card;
607 	snd_soc_card_set_drvdata(card, data);
608 
609 	sdm845_add_ops(card);
610 	return devm_snd_soc_register_card(dev, card);
611 }
612 
613 static const struct of_device_id sdm845_snd_device_id[]  = {
614 	{ .compatible = "qcom,sdm845-sndcard" },
615 	/* Do not grow the list for compatible devices */
616 	{ .compatible = "qcom,db845c-sndcard" },
617 	{ .compatible = "lenovo,yoga-c630-sndcard" },
618 	{},
619 };
620 MODULE_DEVICE_TABLE(of, sdm845_snd_device_id);
621 
622 static struct platform_driver sdm845_snd_driver = {
623 	.probe = sdm845_snd_platform_probe,
624 	.driver = {
625 		.name = "msm-snd-sdm845",
626 		.of_match_table = sdm845_snd_device_id,
627 	},
628 };
629 module_platform_driver(sdm845_snd_driver);
630 
631 MODULE_DESCRIPTION("sdm845 ASoC Machine Driver");
632 MODULE_LICENSE("GPL");
633