xref: /openbmc/linux/sound/soc/mediatek/mt8183/mt8183-mt6358-ts3a227-max98357.c (revision f97cee494dc92395a668445bcd24d34c89f4ff8c)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // mt8183-mt6358.c  --
4 //	MT8183-MT6358-TS3A227-MAX98357 ALSA SoC machine driver
5 //
6 // Copyright (c) 2018 MediaTek Inc.
7 // Author: Shunli Wang <shunli.wang@mediatek.com>
8 
9 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/pinctrl/consumer.h>
12 #include <sound/hdmi-codec.h>
13 #include <sound/jack.h>
14 #include <sound/pcm_params.h>
15 #include <sound/soc.h>
16 
17 #include "../../codecs/rt1015.h"
18 #include "../../codecs/ts3a227e.h"
19 #include "mt8183-afe-common.h"
20 
21 #define RT1015_CODEC_DAI "rt1015-aif"
22 #define RT1015_DEV0_NAME "rt1015.6-0028"
23 #define RT1015_DEV1_NAME "rt1015.6-0029"
24 
25 enum PINCTRL_PIN_STATE {
26 	PIN_STATE_DEFAULT = 0,
27 	PIN_TDM_OUT_ON,
28 	PIN_TDM_OUT_OFF,
29 	PIN_WOV,
30 	PIN_STATE_MAX
31 };
32 
33 static const char * const mt8183_pin_str[PIN_STATE_MAX] = {
34 	"default", "aud_tdm_out_on", "aud_tdm_out_off", "wov",
35 };
36 
37 struct mt8183_mt6358_ts3a227_max98357_priv {
38 	struct pinctrl *pinctrl;
39 	struct pinctrl_state *pin_states[PIN_STATE_MAX];
40 	struct snd_soc_jack headset_jack, hdmi_jack;
41 };
42 
43 static int mt8183_mt6358_i2s_hw_params(struct snd_pcm_substream *substream,
44 				       struct snd_pcm_hw_params *params)
45 {
46 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
47 	unsigned int rate = params_rate(params);
48 	unsigned int mclk_fs_ratio = 128;
49 	unsigned int mclk_fs = rate * mclk_fs_ratio;
50 
51 	return snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0),
52 				      0, mclk_fs, SND_SOC_CLOCK_OUT);
53 }
54 
55 static const struct snd_soc_ops mt8183_mt6358_i2s_ops = {
56 	.hw_params = mt8183_mt6358_i2s_hw_params,
57 };
58 
59 static int
60 mt8183_mt6358_rt1015_i2s_hw_params(struct snd_pcm_substream *substream,
61 				   struct snd_pcm_hw_params *params)
62 {
63 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
64 	unsigned int rate = params_rate(params);
65 	unsigned int mclk_fs_ratio = 128;
66 	unsigned int mclk_fs = rate * mclk_fs_ratio;
67 	struct snd_soc_card *card = rtd->card;
68 	struct snd_soc_dai *codec_dai;
69 	int ret, i;
70 
71 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
72 		ret = snd_soc_dai_set_bclk_ratio(codec_dai, 64);
73 		if (ret < 0) {
74 			dev_err(card->dev, "failed to set bclk ratio\n");
75 			return ret;
76 		}
77 
78 		ret = snd_soc_dai_set_pll(codec_dai, 0, RT1015_PLL_S_BCLK,
79 				rate * 64, rate * 256);
80 		if (ret < 0) {
81 			dev_err(card->dev, "failed to set pll\n");
82 			return ret;
83 		}
84 
85 		ret = snd_soc_dai_set_sysclk(codec_dai, RT1015_SCLK_S_PLL,
86 				rate * 256, SND_SOC_CLOCK_IN);
87 		if (ret < 0) {
88 			dev_err(card->dev, "failed to set sysclk\n");
89 			return ret;
90 		}
91 	}
92 
93 	return snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0),
94 				      0, mclk_fs, SND_SOC_CLOCK_OUT);
95 }
96 
97 static const struct snd_soc_ops mt8183_mt6358_rt1015_i2s_ops = {
98 	.hw_params = mt8183_mt6358_rt1015_i2s_hw_params,
99 };
100 
101 static int mt8183_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
102 				      struct snd_pcm_hw_params *params)
103 {
104 	dev_dbg(rtd->dev, "%s(), fix format to 32bit\n", __func__);
105 
106 	/* fix BE i2s format to 32bit, clean param mask first */
107 	snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
108 			     0, SNDRV_PCM_FORMAT_LAST);
109 
110 	params_set_format(params, SNDRV_PCM_FORMAT_S32_LE);
111 	return 0;
112 }
113 
114 static int mt8183_rt1015_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
115 					     struct snd_pcm_hw_params *params)
116 {
117 	dev_dbg(rtd->dev, "%s(), fix format to 32bit\n", __func__);
118 
119 	/* fix BE i2s format to 32bit, clean param mask first */
120 	snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
121 			     0, SNDRV_PCM_FORMAT_LAST);
122 
123 	params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
124 	return 0;
125 }
126 
127 static int
128 mt8183_mt6358_ts3a227_max98357_bt_sco_startup(
129 	struct snd_pcm_substream *substream)
130 {
131 	static const unsigned int rates[] = {
132 		8000, 16000
133 	};
134 	static const struct snd_pcm_hw_constraint_list constraints_rates = {
135 		.count = ARRAY_SIZE(rates),
136 		.list  = rates,
137 		.mask = 0,
138 	};
139 	static const unsigned int channels[] = {
140 		1,
141 	};
142 	static const struct snd_pcm_hw_constraint_list constraints_channels = {
143 		.count = ARRAY_SIZE(channels),
144 		.list = channels,
145 		.mask = 0,
146 	};
147 
148 	struct snd_pcm_runtime *runtime = substream->runtime;
149 
150 	snd_pcm_hw_constraint_list(runtime, 0,
151 			SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
152 	runtime->hw.channels_max = 1;
153 	snd_pcm_hw_constraint_list(runtime, 0,
154 			SNDRV_PCM_HW_PARAM_CHANNELS,
155 			&constraints_channels);
156 
157 	runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
158 	snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
159 
160 	return 0;
161 }
162 
163 static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_bt_sco_ops = {
164 	.startup = mt8183_mt6358_ts3a227_max98357_bt_sco_startup,
165 };
166 
167 /* FE */
168 SND_SOC_DAILINK_DEFS(playback1,
169 	DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
170 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
171 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
172 
173 SND_SOC_DAILINK_DEFS(playback2,
174 	DAILINK_COMP_ARRAY(COMP_CPU("DL2")),
175 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
176 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
177 
178 SND_SOC_DAILINK_DEFS(playback3,
179 	DAILINK_COMP_ARRAY(COMP_CPU("DL3")),
180 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
181 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
182 
183 SND_SOC_DAILINK_DEFS(capture1,
184 	DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
185 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
186 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
187 
188 SND_SOC_DAILINK_DEFS(capture2,
189 	DAILINK_COMP_ARRAY(COMP_CPU("UL2")),
190 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
191 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
192 
193 SND_SOC_DAILINK_DEFS(capture3,
194 	DAILINK_COMP_ARRAY(COMP_CPU("UL3")),
195 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
196 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
197 
198 SND_SOC_DAILINK_DEFS(capture_mono,
199 	DAILINK_COMP_ARRAY(COMP_CPU("UL_MONO_1")),
200 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
201 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
202 
203 SND_SOC_DAILINK_DEFS(playback_hdmi,
204 	DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
205 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
206 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
207 
208 SND_SOC_DAILINK_DEFS(wake_on_voice,
209 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
210 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
211 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
212 
213 /* BE */
214 SND_SOC_DAILINK_DEFS(primary_codec,
215 	DAILINK_COMP_ARRAY(COMP_CPU("ADDA")),
216 	DAILINK_COMP_ARRAY(COMP_CODEC("mt6358-sound", "mt6358-snd-codec-aif1")),
217 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
218 
219 SND_SOC_DAILINK_DEFS(pcm1,
220 	DAILINK_COMP_ARRAY(COMP_CPU("PCM 1")),
221 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
222 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
223 
224 SND_SOC_DAILINK_DEFS(pcm2,
225 	DAILINK_COMP_ARRAY(COMP_CPU("PCM 2")),
226 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
227 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
228 
229 SND_SOC_DAILINK_DEFS(i2s0,
230 	DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
231 	DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
232 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
233 
234 SND_SOC_DAILINK_DEFS(i2s1,
235 	DAILINK_COMP_ARRAY(COMP_CPU("I2S1")),
236 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
237 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
238 
239 SND_SOC_DAILINK_DEFS(i2s2,
240 	DAILINK_COMP_ARRAY(COMP_CPU("I2S2")),
241 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
242 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
243 
244 SND_SOC_DAILINK_DEFS(i2s3_max98357a,
245 	DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
246 	DAILINK_COMP_ARRAY(COMP_CODEC("max98357a", "HiFi")),
247 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
248 
249 SND_SOC_DAILINK_DEFS(i2s3_rt1015,
250 	DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
251 	DAILINK_COMP_ARRAY(COMP_CODEC(RT1015_DEV0_NAME, RT1015_CODEC_DAI),
252 			   COMP_CODEC(RT1015_DEV1_NAME, RT1015_CODEC_DAI)),
253 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
254 
255 SND_SOC_DAILINK_DEFS(i2s5,
256 	DAILINK_COMP_ARRAY(COMP_CPU("I2S5")),
257 	DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
258 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
259 
260 SND_SOC_DAILINK_DEFS(tdm,
261 	DAILINK_COMP_ARRAY(COMP_CPU("TDM")),
262 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
263 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
264 
265 static int mt8183_mt6358_tdm_startup(struct snd_pcm_substream *substream)
266 {
267 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
268 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
269 		snd_soc_card_get_drvdata(rtd->card);
270 	int ret;
271 
272 	if (IS_ERR(priv->pin_states[PIN_TDM_OUT_ON]))
273 		return PTR_ERR(priv->pin_states[PIN_TDM_OUT_ON]);
274 
275 	ret = pinctrl_select_state(priv->pinctrl,
276 				   priv->pin_states[PIN_TDM_OUT_ON]);
277 	if (ret)
278 		dev_err(rtd->card->dev, "%s failed to select state %d\n",
279 			__func__, ret);
280 
281 	return ret;
282 }
283 
284 static void mt8183_mt6358_tdm_shutdown(struct snd_pcm_substream *substream)
285 {
286 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
287 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
288 		snd_soc_card_get_drvdata(rtd->card);
289 	int ret;
290 
291 	if (IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF]))
292 		return;
293 
294 	ret = pinctrl_select_state(priv->pinctrl,
295 				   priv->pin_states[PIN_TDM_OUT_OFF]);
296 	if (ret)
297 		dev_err(rtd->card->dev, "%s failed to select state %d\n",
298 			__func__, ret);
299 }
300 
301 static struct snd_soc_ops mt8183_mt6358_tdm_ops = {
302 	.startup = mt8183_mt6358_tdm_startup,
303 	.shutdown = mt8183_mt6358_tdm_shutdown,
304 };
305 
306 static int
307 mt8183_mt6358_ts3a227_max98357_wov_startup(
308 	struct snd_pcm_substream *substream)
309 {
310 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
311 	struct snd_soc_card *card = rtd->card;
312 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
313 			snd_soc_card_get_drvdata(card);
314 
315 	return pinctrl_select_state(priv->pinctrl,
316 				    priv->pin_states[PIN_WOV]);
317 }
318 
319 static void
320 mt8183_mt6358_ts3a227_max98357_wov_shutdown(
321 	struct snd_pcm_substream *substream)
322 {
323 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
324 	struct snd_soc_card *card = rtd->card;
325 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
326 			snd_soc_card_get_drvdata(card);
327 	int ret;
328 
329 	ret = pinctrl_select_state(priv->pinctrl,
330 				   priv->pin_states[PIN_STATE_DEFAULT]);
331 	if (ret)
332 		dev_err(card->dev, "%s failed to select state %d\n",
333 			__func__, ret);
334 }
335 
336 static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_wov_ops = {
337 	.startup = mt8183_mt6358_ts3a227_max98357_wov_startup,
338 	.shutdown = mt8183_mt6358_ts3a227_max98357_wov_shutdown,
339 };
340 
341 static int
342 mt8183_mt6358_ts3a227_max98357_hdmi_init(struct snd_soc_pcm_runtime *rtd)
343 {
344 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
345 		snd_soc_card_get_drvdata(rtd->card);
346 	int ret;
347 
348 	ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT,
349 				    &priv->hdmi_jack, NULL, 0);
350 	if (ret)
351 		return ret;
352 
353 	return hdmi_codec_set_jack_detect(asoc_rtd_to_codec(rtd, 0)->component,
354 					  &priv->hdmi_jack);
355 }
356 
357 static struct snd_soc_dai_link mt8183_mt6358_ts3a227_dai_links[] = {
358 	/* FE */
359 	{
360 		.name = "Playback_1",
361 		.stream_name = "Playback_1",
362 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
363 			    SND_SOC_DPCM_TRIGGER_PRE},
364 		.dynamic = 1,
365 		.dpcm_playback = 1,
366 		SND_SOC_DAILINK_REG(playback1),
367 	},
368 	{
369 		.name = "Playback_2",
370 		.stream_name = "Playback_2",
371 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
372 			    SND_SOC_DPCM_TRIGGER_PRE},
373 		.dynamic = 1,
374 		.dpcm_playback = 1,
375 		.ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
376 		SND_SOC_DAILINK_REG(playback2),
377 	},
378 	{
379 		.name = "Playback_3",
380 		.stream_name = "Playback_3",
381 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
382 			    SND_SOC_DPCM_TRIGGER_PRE},
383 		.dynamic = 1,
384 		.dpcm_playback = 1,
385 		SND_SOC_DAILINK_REG(playback3),
386 	},
387 	{
388 		.name = "Capture_1",
389 		.stream_name = "Capture_1",
390 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
391 			    SND_SOC_DPCM_TRIGGER_PRE},
392 		.dynamic = 1,
393 		.dpcm_capture = 1,
394 		.ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
395 		SND_SOC_DAILINK_REG(capture1),
396 	},
397 	{
398 		.name = "Capture_2",
399 		.stream_name = "Capture_2",
400 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
401 			    SND_SOC_DPCM_TRIGGER_PRE},
402 		.dynamic = 1,
403 		.dpcm_capture = 1,
404 		SND_SOC_DAILINK_REG(capture2),
405 	},
406 	{
407 		.name = "Capture_3",
408 		.stream_name = "Capture_3",
409 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
410 			    SND_SOC_DPCM_TRIGGER_PRE},
411 		.dynamic = 1,
412 		.dpcm_capture = 1,
413 		SND_SOC_DAILINK_REG(capture3),
414 	},
415 	{
416 		.name = "Capture_Mono_1",
417 		.stream_name = "Capture_Mono_1",
418 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
419 			    SND_SOC_DPCM_TRIGGER_PRE},
420 		.dynamic = 1,
421 		.dpcm_capture = 1,
422 		SND_SOC_DAILINK_REG(capture_mono),
423 	},
424 	{
425 		.name = "Playback_HDMI",
426 		.stream_name = "Playback_HDMI",
427 		.trigger = {SND_SOC_DPCM_TRIGGER_PRE,
428 			    SND_SOC_DPCM_TRIGGER_PRE},
429 		.dynamic = 1,
430 		.dpcm_playback = 1,
431 		SND_SOC_DAILINK_REG(playback_hdmi),
432 	},
433 	{
434 		.name = "Wake on Voice",
435 		.stream_name = "Wake on Voice",
436 		.ignore_suspend = 1,
437 		.ignore = 1,
438 		SND_SOC_DAILINK_REG(wake_on_voice),
439 		.ops = &mt8183_mt6358_ts3a227_max98357_wov_ops,
440 	},
441 
442 	/* BE */
443 	{
444 		.name = "Primary Codec",
445 		.no_pcm = 1,
446 		.dpcm_playback = 1,
447 		.dpcm_capture = 1,
448 		.ignore_suspend = 1,
449 		SND_SOC_DAILINK_REG(primary_codec),
450 	},
451 	{
452 		.name = "PCM 1",
453 		.no_pcm = 1,
454 		.dpcm_playback = 1,
455 		.dpcm_capture = 1,
456 		.ignore_suspend = 1,
457 		SND_SOC_DAILINK_REG(pcm1),
458 	},
459 	{
460 		.name = "PCM 2",
461 		.no_pcm = 1,
462 		.dpcm_playback = 1,
463 		.dpcm_capture = 1,
464 		.ignore_suspend = 1,
465 		SND_SOC_DAILINK_REG(pcm2),
466 	},
467 	{
468 		.name = "I2S0",
469 		.no_pcm = 1,
470 		.dpcm_capture = 1,
471 		.ignore_suspend = 1,
472 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
473 		.ops = &mt8183_mt6358_i2s_ops,
474 		SND_SOC_DAILINK_REG(i2s0),
475 	},
476 	{
477 		.name = "I2S1",
478 		.no_pcm = 1,
479 		.dpcm_playback = 1,
480 		.ignore_suspend = 1,
481 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
482 		.ops = &mt8183_mt6358_i2s_ops,
483 		SND_SOC_DAILINK_REG(i2s1),
484 	},
485 	{
486 		.name = "I2S2",
487 		.no_pcm = 1,
488 		.dpcm_capture = 1,
489 		.ignore_suspend = 1,
490 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
491 		.ops = &mt8183_mt6358_i2s_ops,
492 		SND_SOC_DAILINK_REG(i2s2),
493 	},
494 	{
495 		.name = "I2S3",
496 		.no_pcm = 1,
497 		.dpcm_playback = 1,
498 		.ignore_suspend = 1,
499 	},
500 	{
501 		.name = "I2S5",
502 		.no_pcm = 1,
503 		.dpcm_playback = 1,
504 		.ignore_suspend = 1,
505 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
506 		.ops = &mt8183_mt6358_i2s_ops,
507 		SND_SOC_DAILINK_REG(i2s5),
508 	},
509 	{
510 		.name = "TDM",
511 		.no_pcm = 1,
512 		.dai_fmt = SND_SOC_DAIFMT_I2S |
513 			   SND_SOC_DAIFMT_IB_IF |
514 			   SND_SOC_DAIFMT_CBM_CFM,
515 		.dpcm_playback = 1,
516 		.ignore_suspend = 1,
517 		.be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
518 		.ops = &mt8183_mt6358_tdm_ops,
519 		.init = mt8183_mt6358_ts3a227_max98357_hdmi_init,
520 		SND_SOC_DAILINK_REG(tdm),
521 	},
522 };
523 
524 static struct snd_soc_card mt8183_mt6358_ts3a227_max98357_card = {
525 	.name = "mt8183_mt6358_ts3a227_max98357",
526 	.owner = THIS_MODULE,
527 	.dai_link = mt8183_mt6358_ts3a227_dai_links,
528 	.num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
529 };
530 
531 static struct snd_soc_card mt8183_mt6358_ts3a227_max98357b_card = {
532 	.name = "mt8183_mt6358_ts3a227_max98357b",
533 	.owner = THIS_MODULE,
534 	.dai_link = mt8183_mt6358_ts3a227_dai_links,
535 	.num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
536 };
537 
538 static struct snd_soc_codec_conf mt8183_mt6358_ts3a227_rt1015_amp_conf[] = {
539 	{
540 		.dlc = COMP_CODEC_CONF(RT1015_DEV0_NAME),
541 		.name_prefix = "Left",
542 	},
543 	{
544 		.dlc = COMP_CODEC_CONF(RT1015_DEV1_NAME),
545 		.name_prefix = "Right",
546 	},
547 };
548 
549 static struct snd_soc_card mt8183_mt6358_ts3a227_rt1015_card = {
550 	.name = "mt8183_mt6358_ts3a227_rt1015",
551 	.owner = THIS_MODULE,
552 	.dai_link = mt8183_mt6358_ts3a227_dai_links,
553 	.num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
554 	.codec_conf = mt8183_mt6358_ts3a227_rt1015_amp_conf,
555 	.num_configs = ARRAY_SIZE(mt8183_mt6358_ts3a227_rt1015_amp_conf),
556 };
557 
558 static int
559 mt8183_mt6358_ts3a227_max98357_headset_init(struct snd_soc_component *component)
560 {
561 	int ret;
562 	struct mt8183_mt6358_ts3a227_max98357_priv *priv =
563 			snd_soc_card_get_drvdata(component->card);
564 
565 	/* Enable Headset and 4 Buttons Jack detection */
566 	ret = snd_soc_card_jack_new(component->card,
567 				    "Headset Jack",
568 				    SND_JACK_HEADSET |
569 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
570 				    SND_JACK_BTN_2 | SND_JACK_BTN_3,
571 				    &priv->headset_jack,
572 				    NULL, 0);
573 	if (ret)
574 		return ret;
575 
576 	ret = ts3a227e_enable_jack_detect(component, &priv->headset_jack);
577 
578 	return ret;
579 }
580 
581 static struct snd_soc_aux_dev mt8183_mt6358_ts3a227_max98357_headset_dev = {
582 	.dlc = COMP_EMPTY(),
583 	.init = mt8183_mt6358_ts3a227_max98357_headset_init,
584 };
585 
586 static int
587 mt8183_mt6358_ts3a227_max98357_dev_probe(struct platform_device *pdev)
588 {
589 	struct snd_soc_card *card;
590 	struct device_node *platform_node, *ec_codec, *hdmi_codec;
591 	struct snd_soc_dai_link *dai_link;
592 	struct mt8183_mt6358_ts3a227_max98357_priv *priv;
593 	const struct of_device_id *match;
594 	int ret, i;
595 
596 	platform_node = of_parse_phandle(pdev->dev.of_node,
597 					 "mediatek,platform", 0);
598 	if (!platform_node) {
599 		dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
600 		return -EINVAL;
601 	}
602 
603 	match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
604 	if (!match || !match->data)
605 		return -EINVAL;
606 
607 	card = (struct snd_soc_card *)match->data;
608 	card->dev = &pdev->dev;
609 
610 	ec_codec = of_parse_phandle(pdev->dev.of_node, "mediatek,ec-codec", 0);
611 	hdmi_codec = of_parse_phandle(pdev->dev.of_node,
612 				      "mediatek,hdmi-codec", 0);
613 
614 	for_each_card_prelinks(card, i, dai_link) {
615 		if (ec_codec && strcmp(dai_link->name, "Wake on Voice") == 0) {
616 			dai_link->cpus[0].name = NULL;
617 			dai_link->cpus[0].of_node = ec_codec;
618 			dai_link->cpus[0].dai_name = NULL;
619 			dai_link->codecs[0].name = NULL;
620 			dai_link->codecs[0].of_node = ec_codec;
621 			dai_link->codecs[0].dai_name = "Wake on Voice";
622 			dai_link->platforms[0].of_node = ec_codec;
623 			dai_link->ignore = 0;
624 		}
625 
626 		if (strcmp(dai_link->name, "I2S3") == 0) {
627 			if (card == &mt8183_mt6358_ts3a227_max98357_card ||
628 			    card == &mt8183_mt6358_ts3a227_max98357b_card) {
629 				dai_link->be_hw_params_fixup =
630 					mt8183_i2s_hw_params_fixup;
631 				dai_link->ops = &mt8183_mt6358_i2s_ops;
632 				dai_link->cpus = i2s3_max98357a_cpus;
633 				dai_link->num_cpus =
634 					ARRAY_SIZE(i2s3_max98357a_cpus);
635 				dai_link->codecs = i2s3_max98357a_codecs;
636 				dai_link->num_codecs =
637 					ARRAY_SIZE(i2s3_max98357a_codecs);
638 				dai_link->platforms = i2s3_max98357a_platforms;
639 				dai_link->num_platforms =
640 					ARRAY_SIZE(i2s3_max98357a_platforms);
641 			} else if (card == &mt8183_mt6358_ts3a227_rt1015_card) {
642 				dai_link->be_hw_params_fixup =
643 					mt8183_rt1015_i2s_hw_params_fixup;
644 				dai_link->ops = &mt8183_mt6358_rt1015_i2s_ops;
645 				dai_link->cpus = i2s3_rt1015_cpus;
646 				dai_link->num_cpus =
647 					ARRAY_SIZE(i2s3_rt1015_cpus);
648 				dai_link->codecs = i2s3_rt1015_codecs;
649 				dai_link->num_codecs =
650 					ARRAY_SIZE(i2s3_rt1015_codecs);
651 				dai_link->platforms = i2s3_rt1015_platforms;
652 				dai_link->num_platforms =
653 					ARRAY_SIZE(i2s3_rt1015_platforms);
654 			}
655 		}
656 
657 		if (card == &mt8183_mt6358_ts3a227_max98357b_card) {
658 			if (strcmp(dai_link->name, "I2S2") == 0 ||
659 			    strcmp(dai_link->name, "I2S3") == 0)
660 				dai_link->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
661 						    SND_SOC_DAIFMT_NB_NF |
662 						    SND_SOC_DAIFMT_CBM_CFM;
663 		}
664 
665 		if (hdmi_codec && strcmp(dai_link->name, "TDM") == 0)
666 			dai_link->codecs->of_node = hdmi_codec;
667 
668 		if (!dai_link->platforms->name)
669 			dai_link->platforms->of_node = platform_node;
670 	}
671 
672 	mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node =
673 		of_parse_phandle(pdev->dev.of_node,
674 				 "mediatek,headset-codec", 0);
675 	if (mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node) {
676 		card->aux_dev = &mt8183_mt6358_ts3a227_max98357_headset_dev;
677 		card->num_aux_devs = 1;
678 	}
679 
680 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
681 	if (!priv)
682 		return -ENOMEM;
683 
684 	snd_soc_card_set_drvdata(card, priv);
685 
686 	priv->pinctrl = devm_pinctrl_get(&pdev->dev);
687 	if (IS_ERR(priv->pinctrl)) {
688 		dev_err(&pdev->dev, "%s devm_pinctrl_get failed\n",
689 			__func__);
690 		return PTR_ERR(priv->pinctrl);
691 	}
692 
693 	for (i = 0; i < PIN_STATE_MAX; i++) {
694 		priv->pin_states[i] = pinctrl_lookup_state(priv->pinctrl,
695 							   mt8183_pin_str[i]);
696 		if (IS_ERR(priv->pin_states[i])) {
697 			ret = PTR_ERR(priv->pin_states[i]);
698 			dev_info(&pdev->dev, "%s Can't find pin state %s %d\n",
699 				 __func__, mt8183_pin_str[i], ret);
700 		}
701 	}
702 
703 	if (!IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF])) {
704 		ret = pinctrl_select_state(priv->pinctrl,
705 					   priv->pin_states[PIN_TDM_OUT_OFF]);
706 		if (ret)
707 			dev_info(&pdev->dev,
708 				 "%s failed to select state %d\n",
709 				 __func__, ret);
710 	}
711 
712 	if (!IS_ERR(priv->pin_states[PIN_STATE_DEFAULT])) {
713 		ret = pinctrl_select_state(priv->pinctrl,
714 					   priv->pin_states[PIN_STATE_DEFAULT]);
715 		if (ret)
716 			dev_info(&pdev->dev,
717 				 "%s failed to select state %d\n",
718 				 __func__, ret);
719 	}
720 
721 	return devm_snd_soc_register_card(&pdev->dev, card);
722 }
723 
724 #ifdef CONFIG_OF
725 static const struct of_device_id mt8183_mt6358_ts3a227_max98357_dt_match[] = {
726 	{
727 		.compatible = "mediatek,mt8183_mt6358_ts3a227_max98357",
728 		.data = &mt8183_mt6358_ts3a227_max98357_card,
729 	},
730 	{
731 		.compatible = "mediatek,mt8183_mt6358_ts3a227_max98357b",
732 		.data = &mt8183_mt6358_ts3a227_max98357b_card,
733 	},
734 	{
735 		.compatible = "mediatek,mt8183_mt6358_ts3a227_rt1015",
736 		.data = &mt8183_mt6358_ts3a227_rt1015_card,
737 	},
738 	{}
739 };
740 #endif
741 
742 static struct platform_driver mt8183_mt6358_ts3a227_max98357_driver = {
743 	.driver = {
744 		.name = "mt8183_mt6358_ts3a227",
745 #ifdef CONFIG_OF
746 		.of_match_table = mt8183_mt6358_ts3a227_max98357_dt_match,
747 #endif
748 	},
749 	.probe = mt8183_mt6358_ts3a227_max98357_dev_probe,
750 };
751 
752 module_platform_driver(mt8183_mt6358_ts3a227_max98357_driver);
753 
754 /* Module information */
755 MODULE_DESCRIPTION("MT8183-MT6358-TS3A227-MAX98357 ALSA SoC machine driver");
756 MODULE_AUTHOR("Shunli Wang <shunli.wang@mediatek.com>");
757 MODULE_LICENSE("GPL v2");
758 MODULE_ALIAS("mt8183_mt6358_ts3a227_max98357 soc card");
759