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