blob: 0555f7d73d0501c8cc2480d309c728a0f6f2a1e6 [file] [log] [blame]
Shunli Wang11c02692019-03-29 16:34:46 +08001// 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 <sound/pcm_params.h>
11#include <sound/soc.h>
12#include <sound/jack.h>
13#include <linux/pinctrl/consumer.h>
14
15#include "mt8183-afe-common.h"
16#include "../../codecs/ts3a227e.h"
17
Tzung-Bi Shih96ed7692019-08-30 15:42:37 +080018enum PINCTRL_PIN_STATE {
19 PIN_STATE_DEFAULT = 0,
20 PIN_TDM_OUT_ON,
21 PIN_TDM_OUT_OFF,
Tzung-Bi Shih2cc3cd52019-10-19 15:02:54 +080022 PIN_WOV,
Tzung-Bi Shih96ed7692019-08-30 15:42:37 +080023 PIN_STATE_MAX
24};
25
26static const char * const mt8183_pin_str[PIN_STATE_MAX] = {
Tzung-Bi Shih2cc3cd52019-10-19 15:02:54 +080027 "default", "aud_tdm_out_on", "aud_tdm_out_off", "wov",
Tzung-Bi Shih96ed7692019-08-30 15:42:37 +080028};
29
30struct mt8183_mt6358_ts3a227_max98357_priv {
31 struct pinctrl *pinctrl;
32 struct pinctrl_state *pin_states[PIN_STATE_MAX];
Tzung-Bi Shih95d779b2019-08-30 15:42:38 +080033 struct snd_soc_jack headset_jack;
Tzung-Bi Shih96ed7692019-08-30 15:42:37 +080034};
35
Shunli Wang11c02692019-03-29 16:34:46 +080036static int mt8183_mt6358_i2s_hw_params(struct snd_pcm_substream *substream,
37 struct snd_pcm_hw_params *params)
38{
39 struct snd_soc_pcm_runtime *rtd = substream->private_data;
40 unsigned int rate = params_rate(params);
41 unsigned int mclk_fs_ratio = 128;
42 unsigned int mclk_fs = rate * mclk_fs_ratio;
43
44 return snd_soc_dai_set_sysclk(rtd->cpu_dai,
45 0, mclk_fs, SND_SOC_CLOCK_OUT);
46}
47
48static const struct snd_soc_ops mt8183_mt6358_i2s_ops = {
49 .hw_params = mt8183_mt6358_i2s_hw_params,
50};
51
52static int mt8183_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
53 struct snd_pcm_hw_params *params)
54{
55 dev_dbg(rtd->dev, "%s(), fix format to 32bit\n", __func__);
56
57 /* fix BE i2s format to 32bit, clean param mask first */
58 snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
59 0, SNDRV_PCM_FORMAT_LAST);
60
61 params_set_format(params, SNDRV_PCM_FORMAT_S32_LE);
62 return 0;
63}
64
Tzung-Bi Shih1df1e542019-04-08 18:47:28 +080065static int
66mt8183_mt6358_ts3a227_max98357_bt_sco_startup(
67 struct snd_pcm_substream *substream)
68{
69 static const unsigned int rates[] = {
70 8000, 16000
71 };
72 static const struct snd_pcm_hw_constraint_list constraints_rates = {
73 .count = ARRAY_SIZE(rates),
74 .list = rates,
75 .mask = 0,
76 };
77 static const unsigned int channels[] = {
78 1,
79 };
80 static const struct snd_pcm_hw_constraint_list constraints_channels = {
81 .count = ARRAY_SIZE(channels),
82 .list = channels,
83 .mask = 0,
84 };
85
86 struct snd_pcm_runtime *runtime = substream->runtime;
87
88 snd_pcm_hw_constraint_list(runtime, 0,
89 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
90 runtime->hw.channels_max = 1;
91 snd_pcm_hw_constraint_list(runtime, 0,
92 SNDRV_PCM_HW_PARAM_CHANNELS,
93 &constraints_channels);
94
95 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
96 snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
97
98 return 0;
99}
100
101static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_bt_sco_ops = {
102 .startup = mt8183_mt6358_ts3a227_max98357_bt_sco_startup,
103};
104
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900105/* FE */
106SND_SOC_DAILINK_DEFS(playback1,
107 DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
108 DAILINK_COMP_ARRAY(COMP_DUMMY()),
109 DAILINK_COMP_ARRAY(COMP_EMPTY()));
110
111SND_SOC_DAILINK_DEFS(playback2,
112 DAILINK_COMP_ARRAY(COMP_CPU("DL2")),
113 DAILINK_COMP_ARRAY(COMP_DUMMY()),
114 DAILINK_COMP_ARRAY(COMP_EMPTY()));
115
116SND_SOC_DAILINK_DEFS(playback3,
117 DAILINK_COMP_ARRAY(COMP_CPU("DL3")),
118 DAILINK_COMP_ARRAY(COMP_DUMMY()),
119 DAILINK_COMP_ARRAY(COMP_EMPTY()));
120
121SND_SOC_DAILINK_DEFS(capture1,
122 DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
123 DAILINK_COMP_ARRAY(COMP_DUMMY()),
124 DAILINK_COMP_ARRAY(COMP_EMPTY()));
125
126SND_SOC_DAILINK_DEFS(capture2,
127 DAILINK_COMP_ARRAY(COMP_CPU("UL2")),
128 DAILINK_COMP_ARRAY(COMP_DUMMY()),
129 DAILINK_COMP_ARRAY(COMP_EMPTY()));
130
131SND_SOC_DAILINK_DEFS(capture3,
132 DAILINK_COMP_ARRAY(COMP_CPU("UL3")),
133 DAILINK_COMP_ARRAY(COMP_DUMMY()),
134 DAILINK_COMP_ARRAY(COMP_EMPTY()));
135
136SND_SOC_DAILINK_DEFS(capture_mono,
137 DAILINK_COMP_ARRAY(COMP_CPU("UL_MONO_1")),
138 DAILINK_COMP_ARRAY(COMP_DUMMY()),
139 DAILINK_COMP_ARRAY(COMP_EMPTY()));
140
141SND_SOC_DAILINK_DEFS(playback_hdmi,
142 DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
143 DAILINK_COMP_ARRAY(COMP_DUMMY()),
144 DAILINK_COMP_ARRAY(COMP_EMPTY()));
145
Tzung-Bi Shih2cc3cd52019-10-19 15:02:54 +0800146SND_SOC_DAILINK_DEFS(wake_on_voice,
147 DAILINK_COMP_ARRAY(COMP_DUMMY()),
148 DAILINK_COMP_ARRAY(COMP_DUMMY()),
149 DAILINK_COMP_ARRAY(COMP_EMPTY()));
150
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900151/* BE */
152SND_SOC_DAILINK_DEFS(primary_codec,
153 DAILINK_COMP_ARRAY(COMP_CPU("ADDA")),
154 DAILINK_COMP_ARRAY(COMP_CODEC("mt6358-sound", "mt6358-snd-codec-aif1")),
155 DAILINK_COMP_ARRAY(COMP_EMPTY()));
156
157SND_SOC_DAILINK_DEFS(pcm1,
158 DAILINK_COMP_ARRAY(COMP_CPU("PCM 1")),
159 DAILINK_COMP_ARRAY(COMP_DUMMY()),
160 DAILINK_COMP_ARRAY(COMP_EMPTY()));
161
162SND_SOC_DAILINK_DEFS(pcm2,
163 DAILINK_COMP_ARRAY(COMP_CPU("PCM 2")),
164 DAILINK_COMP_ARRAY(COMP_DUMMY()),
165 DAILINK_COMP_ARRAY(COMP_EMPTY()));
166
167SND_SOC_DAILINK_DEFS(i2s0,
168 DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
169 DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
170 DAILINK_COMP_ARRAY(COMP_EMPTY()));
171
172SND_SOC_DAILINK_DEFS(i2s1,
173 DAILINK_COMP_ARRAY(COMP_CPU("I2S1")),
174 DAILINK_COMP_ARRAY(COMP_DUMMY()),
175 DAILINK_COMP_ARRAY(COMP_EMPTY()));
176
177SND_SOC_DAILINK_DEFS(i2s2,
178 DAILINK_COMP_ARRAY(COMP_CPU("I2S2")),
179 DAILINK_COMP_ARRAY(COMP_DUMMY()),
180 DAILINK_COMP_ARRAY(COMP_EMPTY()));
181
182SND_SOC_DAILINK_DEFS(i2s3,
183 DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
184 DAILINK_COMP_ARRAY(COMP_CODEC("max98357a", "HiFi")),
185 DAILINK_COMP_ARRAY(COMP_EMPTY()));
186
187SND_SOC_DAILINK_DEFS(i2s5,
188 DAILINK_COMP_ARRAY(COMP_CPU("I2S5")),
189 DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
190 DAILINK_COMP_ARRAY(COMP_EMPTY()));
191
192SND_SOC_DAILINK_DEFS(tdm,
193 DAILINK_COMP_ARRAY(COMP_CPU("TDM")),
194 DAILINK_COMP_ARRAY(COMP_DUMMY()),
195 DAILINK_COMP_ARRAY(COMP_EMPTY()));
196
Jiaxin Yu6191cbd2019-08-22 20:51:00 +0800197static int mt8183_mt6358_tdm_startup(struct snd_pcm_substream *substream)
198{
199 struct snd_soc_pcm_runtime *rtd = substream->private_data;
200 struct mt8183_mt6358_ts3a227_max98357_priv *priv =
201 snd_soc_card_get_drvdata(rtd->card);
202 int ret;
203
204 if (IS_ERR(priv->pin_states[PIN_TDM_OUT_ON]))
205 return PTR_ERR(priv->pin_states[PIN_TDM_OUT_ON]);
206
207 ret = pinctrl_select_state(priv->pinctrl,
208 priv->pin_states[PIN_TDM_OUT_ON]);
209 if (ret)
210 dev_err(rtd->card->dev, "%s failed to select state %d\n",
211 __func__, ret);
212
213 return ret;
214}
215
216static void mt8183_mt6358_tdm_shutdown(struct snd_pcm_substream *substream)
217{
218 struct snd_soc_pcm_runtime *rtd = substream->private_data;
219 struct mt8183_mt6358_ts3a227_max98357_priv *priv =
220 snd_soc_card_get_drvdata(rtd->card);
221 int ret;
222
223 if (IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF]))
224 return;
225
226 ret = pinctrl_select_state(priv->pinctrl,
227 priv->pin_states[PIN_TDM_OUT_OFF]);
228 if (ret)
229 dev_err(rtd->card->dev, "%s failed to select state %d\n",
230 __func__, ret);
231}
232
233static struct snd_soc_ops mt8183_mt6358_tdm_ops = {
234 .startup = mt8183_mt6358_tdm_startup,
235 .shutdown = mt8183_mt6358_tdm_shutdown,
236};
237
Tzung-Bi Shih2cc3cd52019-10-19 15:02:54 +0800238static int
239mt8183_mt6358_ts3a227_max98357_wov_startup(
240 struct snd_pcm_substream *substream)
241{
242 struct snd_soc_pcm_runtime *rtd = substream->private_data;
243 struct snd_soc_card *card = rtd->card;
244 struct mt8183_mt6358_ts3a227_max98357_priv *priv =
245 snd_soc_card_get_drvdata(card);
246
247 return pinctrl_select_state(priv->pinctrl,
248 priv->pin_states[PIN_WOV]);
249}
250
251static void
252mt8183_mt6358_ts3a227_max98357_wov_shutdown(
253 struct snd_pcm_substream *substream)
254{
255 struct snd_soc_pcm_runtime *rtd = substream->private_data;
256 struct snd_soc_card *card = rtd->card;
257 struct mt8183_mt6358_ts3a227_max98357_priv *priv =
258 snd_soc_card_get_drvdata(card);
259 int ret;
260
261 ret = pinctrl_select_state(priv->pinctrl,
262 priv->pin_states[PIN_STATE_DEFAULT]);
263 if (ret)
264 dev_err(card->dev, "%s failed to select state %d\n",
265 __func__, ret);
266}
267
268static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_wov_ops = {
269 .startup = mt8183_mt6358_ts3a227_max98357_wov_startup,
270 .shutdown = mt8183_mt6358_ts3a227_max98357_wov_shutdown,
271};
272
Shunli Wang11c02692019-03-29 16:34:46 +0800273static struct snd_soc_dai_link
274mt8183_mt6358_ts3a227_max98357_dai_links[] = {
275 /* FE */
276 {
277 .name = "Playback_1",
278 .stream_name = "Playback_1",
Shunli Wang11c02692019-03-29 16:34:46 +0800279 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
280 SND_SOC_DPCM_TRIGGER_PRE},
281 .dynamic = 1,
282 .dpcm_playback = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900283 SND_SOC_DAILINK_REG(playback1),
Shunli Wang11c02692019-03-29 16:34:46 +0800284 },
285 {
286 .name = "Playback_2",
287 .stream_name = "Playback_2",
Shunli Wang11c02692019-03-29 16:34:46 +0800288 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
289 SND_SOC_DPCM_TRIGGER_PRE},
290 .dynamic = 1,
291 .dpcm_playback = 1,
Tzung-Bi Shih1df1e542019-04-08 18:47:28 +0800292 .ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900293 SND_SOC_DAILINK_REG(playback2),
Shunli Wang11c02692019-03-29 16:34:46 +0800294 },
295 {
296 .name = "Playback_3",
297 .stream_name = "Playback_3",
Shunli Wang11c02692019-03-29 16:34:46 +0800298 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
299 SND_SOC_DPCM_TRIGGER_PRE},
300 .dynamic = 1,
301 .dpcm_playback = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900302 SND_SOC_DAILINK_REG(playback3),
Shunli Wang11c02692019-03-29 16:34:46 +0800303 },
304 {
305 .name = "Capture_1",
306 .stream_name = "Capture_1",
Shunli Wang11c02692019-03-29 16:34:46 +0800307 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
308 SND_SOC_DPCM_TRIGGER_PRE},
309 .dynamic = 1,
310 .dpcm_capture = 1,
Tzung-Bi Shih1df1e542019-04-08 18:47:28 +0800311 .ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900312 SND_SOC_DAILINK_REG(capture1),
Shunli Wang11c02692019-03-29 16:34:46 +0800313 },
314 {
315 .name = "Capture_2",
316 .stream_name = "Capture_2",
Shunli Wang11c02692019-03-29 16:34:46 +0800317 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
318 SND_SOC_DPCM_TRIGGER_PRE},
319 .dynamic = 1,
320 .dpcm_capture = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900321 SND_SOC_DAILINK_REG(capture2),
Shunli Wang11c02692019-03-29 16:34:46 +0800322 },
323 {
324 .name = "Capture_3",
325 .stream_name = "Capture_3",
Shunli Wang11c02692019-03-29 16:34:46 +0800326 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
327 SND_SOC_DPCM_TRIGGER_PRE},
328 .dynamic = 1,
329 .dpcm_capture = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900330 SND_SOC_DAILINK_REG(capture3),
Shunli Wang11c02692019-03-29 16:34:46 +0800331 },
332 {
333 .name = "Capture_Mono_1",
334 .stream_name = "Capture_Mono_1",
Shunli Wang11c02692019-03-29 16:34:46 +0800335 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
336 SND_SOC_DPCM_TRIGGER_PRE},
337 .dynamic = 1,
338 .dpcm_capture = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900339 SND_SOC_DAILINK_REG(capture_mono),
Shunli Wang11c02692019-03-29 16:34:46 +0800340 },
341 {
342 .name = "Playback_HDMI",
343 .stream_name = "Playback_HDMI",
Shunli Wang11c02692019-03-29 16:34:46 +0800344 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
345 SND_SOC_DPCM_TRIGGER_PRE},
346 .dynamic = 1,
347 .dpcm_playback = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900348 SND_SOC_DAILINK_REG(playback_hdmi),
Shunli Wang11c02692019-03-29 16:34:46 +0800349 },
Tzung-Bi Shih2cc3cd52019-10-19 15:02:54 +0800350 {
351 .name = "Wake on Voice",
352 .stream_name = "Wake on Voice",
353 .ignore_suspend = 1,
354 .ignore = 1,
355 SND_SOC_DAILINK_REG(wake_on_voice),
356 .ops = &mt8183_mt6358_ts3a227_max98357_wov_ops,
357 },
358
Shunli Wang11c02692019-03-29 16:34:46 +0800359 /* BE */
360 {
361 .name = "Primary Codec",
Shunli Wang11c02692019-03-29 16:34:46 +0800362 .no_pcm = 1,
363 .dpcm_playback = 1,
364 .dpcm_capture = 1,
365 .ignore_suspend = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900366 SND_SOC_DAILINK_REG(primary_codec),
Shunli Wang11c02692019-03-29 16:34:46 +0800367 },
368 {
369 .name = "PCM 1",
Shunli Wang11c02692019-03-29 16:34:46 +0800370 .no_pcm = 1,
371 .dpcm_playback = 1,
372 .dpcm_capture = 1,
373 .ignore_suspend = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900374 SND_SOC_DAILINK_REG(pcm1),
Shunli Wang11c02692019-03-29 16:34:46 +0800375 },
376 {
377 .name = "PCM 2",
Shunli Wang11c02692019-03-29 16:34:46 +0800378 .no_pcm = 1,
379 .dpcm_playback = 1,
380 .dpcm_capture = 1,
381 .ignore_suspend = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900382 SND_SOC_DAILINK_REG(pcm2),
Shunli Wang11c02692019-03-29 16:34:46 +0800383 },
384 {
385 .name = "I2S0",
Shunli Wang11c02692019-03-29 16:34:46 +0800386 .no_pcm = 1,
387 .dpcm_capture = 1,
388 .ignore_suspend = 1,
389 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
390 .ops = &mt8183_mt6358_i2s_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900391 SND_SOC_DAILINK_REG(i2s0),
Shunli Wang11c02692019-03-29 16:34:46 +0800392 },
393 {
394 .name = "I2S1",
Shunli Wang11c02692019-03-29 16:34:46 +0800395 .no_pcm = 1,
396 .dpcm_playback = 1,
397 .ignore_suspend = 1,
398 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
399 .ops = &mt8183_mt6358_i2s_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900400 SND_SOC_DAILINK_REG(i2s1),
Shunli Wang11c02692019-03-29 16:34:46 +0800401 },
402 {
403 .name = "I2S2",
Shunli Wang11c02692019-03-29 16:34:46 +0800404 .no_pcm = 1,
405 .dpcm_capture = 1,
406 .ignore_suspend = 1,
407 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
408 .ops = &mt8183_mt6358_i2s_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900409 SND_SOC_DAILINK_REG(i2s2),
Shunli Wang11c02692019-03-29 16:34:46 +0800410 },
411 {
412 .name = "I2S3",
Shunli Wang11c02692019-03-29 16:34:46 +0800413 .no_pcm = 1,
414 .dpcm_playback = 1,
415 .ignore_suspend = 1,
416 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
417 .ops = &mt8183_mt6358_i2s_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900418 SND_SOC_DAILINK_REG(i2s3),
Shunli Wang11c02692019-03-29 16:34:46 +0800419 },
420 {
421 .name = "I2S5",
Shunli Wang11c02692019-03-29 16:34:46 +0800422 .no_pcm = 1,
423 .dpcm_playback = 1,
424 .ignore_suspend = 1,
425 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
426 .ops = &mt8183_mt6358_i2s_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900427 SND_SOC_DAILINK_REG(i2s5),
Shunli Wang11c02692019-03-29 16:34:46 +0800428 },
429 {
430 .name = "TDM",
Shunli Wang11c02692019-03-29 16:34:46 +0800431 .no_pcm = 1,
Jiaxin Yu8e58c522019-08-24 12:37:24 +0800432 .dai_fmt = SND_SOC_DAIFMT_I2S |
433 SND_SOC_DAIFMT_IB_IF |
434 SND_SOC_DAIFMT_CBM_CFM,
Shunli Wang11c02692019-03-29 16:34:46 +0800435 .dpcm_playback = 1,
436 .ignore_suspend = 1,
Jiaxin Yu6191cbd2019-08-22 20:51:00 +0800437 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
438 .ops = &mt8183_mt6358_tdm_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900439 SND_SOC_DAILINK_REG(tdm),
Shunli Wang11c02692019-03-29 16:34:46 +0800440 },
441};
442
Shunli Wang11c02692019-03-29 16:34:46 +0800443static struct snd_soc_card mt8183_mt6358_ts3a227_max98357_card = {
444 .name = "mt8183_mt6358_ts3a227_max98357",
445 .owner = THIS_MODULE,
446 .dai_link = mt8183_mt6358_ts3a227_max98357_dai_links,
447 .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_max98357_dai_links),
Shunli Wang11c02692019-03-29 16:34:46 +0800448};
449
450static int
451mt8183_mt6358_ts3a227_max98357_headset_init(struct snd_soc_component *component)
452{
453 int ret;
Tzung-Bi Shih95d779b2019-08-30 15:42:38 +0800454 struct mt8183_mt6358_ts3a227_max98357_priv *priv =
455 snd_soc_card_get_drvdata(component->card);
Shunli Wang11c02692019-03-29 16:34:46 +0800456
457 /* Enable Headset and 4 Buttons Jack detection */
458 ret = snd_soc_card_jack_new(&mt8183_mt6358_ts3a227_max98357_card,
459 "Headset Jack",
460 SND_JACK_HEADSET |
461 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
462 SND_JACK_BTN_2 | SND_JACK_BTN_3,
Tzung-Bi Shih95d779b2019-08-30 15:42:38 +0800463 &priv->headset_jack,
Tzung-Bi Shih108d0d42019-05-22 22:55:05 +0800464 NULL, 0);
Shunli Wang11c02692019-03-29 16:34:46 +0800465 if (ret)
466 return ret;
467
Tzung-Bi Shih95d779b2019-08-30 15:42:38 +0800468 ret = ts3a227e_enable_jack_detect(component, &priv->headset_jack);
Shunli Wang11c02692019-03-29 16:34:46 +0800469
470 return ret;
471}
472
Tzung-Bi Shihfc347482019-08-30 15:42:39 +0800473static struct snd_soc_aux_dev mt8183_mt6358_ts3a227_max98357_headset_dev = {
474 .dlc = COMP_EMPTY(),
475 .init = mt8183_mt6358_ts3a227_max98357_headset_init,
476};
477
Shunli Wang11c02692019-03-29 16:34:46 +0800478static int
479mt8183_mt6358_ts3a227_max98357_dev_probe(struct platform_device *pdev)
480{
481 struct snd_soc_card *card = &mt8183_mt6358_ts3a227_max98357_card;
Tzung-Bi Shih2cc3cd52019-10-19 15:02:54 +0800482 struct device_node *platform_node, *ec_codec;
Shunli Wang11c02692019-03-29 16:34:46 +0800483 struct snd_soc_dai_link *dai_link;
Jiaxin Yu6191cbd2019-08-22 20:51:00 +0800484 struct mt8183_mt6358_ts3a227_max98357_priv *priv;
485 int ret;
Tzung-Bi Shih63ba8e42019-05-22 22:54:02 +0800486 int i;
Shunli Wang11c02692019-03-29 16:34:46 +0800487
488 card->dev = &pdev->dev;
489
490 platform_node = of_parse_phandle(pdev->dev.of_node,
491 "mediatek,platform", 0);
492 if (!platform_node) {
493 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
494 return -EINVAL;
495 }
496
Tzung-Bi Shih2cc3cd52019-10-19 15:02:54 +0800497 ec_codec = of_parse_phandle(pdev->dev.of_node, "mediatek,ec-codec", 0);
498
Shunli Wang11c02692019-03-29 16:34:46 +0800499 for_each_card_prelinks(card, i, dai_link) {
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900500 if (dai_link->platforms->name)
Shunli Wang11c02692019-03-29 16:34:46 +0800501 continue;
Tzung-Bi Shih2cc3cd52019-10-19 15:02:54 +0800502
503 if (ec_codec && strcmp(dai_link->name, "Wake on Voice") == 0) {
504 dai_link->cpus[0].name = NULL;
505 dai_link->cpus[0].of_node = ec_codec;
506 dai_link->cpus[0].dai_name = NULL;
507 dai_link->codecs[0].name = NULL;
508 dai_link->codecs[0].of_node = ec_codec;
509 dai_link->codecs[0].dai_name = "Wake on Voice";
510 dai_link->platforms[0].of_node = ec_codec;
511 dai_link->ignore = 0;
512 } else {
513 dai_link->platforms->of_node = platform_node;
514 }
Shunli Wang11c02692019-03-29 16:34:46 +0800515 }
516
Kuninori Morimotob812cd52019-08-08 14:53:52 +0900517 mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node =
Shunli Wang11c02692019-03-29 16:34:46 +0800518 of_parse_phandle(pdev->dev.of_node,
519 "mediatek,headset-codec", 0);
Kuninori Morimotob812cd52019-08-08 14:53:52 +0900520 if (mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node) {
Tzung-Bi Shiha962a8092019-07-16 11:24:17 +0800521 card->aux_dev = &mt8183_mt6358_ts3a227_max98357_headset_dev;
522 card->num_aux_devs = 1;
Shunli Wang11c02692019-03-29 16:34:46 +0800523 }
524
Jiaxin Yu6191cbd2019-08-22 20:51:00 +0800525 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
526 if (!priv)
527 return -ENOMEM;
528
529 snd_soc_card_set_drvdata(card, priv);
530
531 priv->pinctrl = devm_pinctrl_get(&pdev->dev);
532 if (IS_ERR(priv->pinctrl)) {
533 dev_err(&pdev->dev, "%s devm_pinctrl_get failed\n",
Shunli Wang11c02692019-03-29 16:34:46 +0800534 __func__);
Jiaxin Yu6191cbd2019-08-22 20:51:00 +0800535 return PTR_ERR(priv->pinctrl);
536 }
537
Tzung-Bi Shihe5b3ae32019-08-30 15:42:40 +0800538 for (i = 0; i < PIN_STATE_MAX; i++) {
Jiaxin Yu6191cbd2019-08-22 20:51:00 +0800539 priv->pin_states[i] = pinctrl_lookup_state(priv->pinctrl,
540 mt8183_pin_str[i]);
541 if (IS_ERR(priv->pin_states[i])) {
542 ret = PTR_ERR(priv->pin_states[i]);
543 dev_info(&pdev->dev, "%s Can't find pin state %s %d\n",
544 __func__, mt8183_pin_str[i], ret);
545 }
546 }
547
548 if (!IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF])) {
549 ret = pinctrl_select_state(priv->pinctrl,
550 priv->pin_states[PIN_TDM_OUT_OFF]);
551 if (ret)
552 dev_info(&pdev->dev,
553 "%s failed to select state %d\n",
554 __func__, ret);
555 }
556
557 if (!IS_ERR(priv->pin_states[PIN_STATE_DEFAULT])) {
558 ret = pinctrl_select_state(priv->pinctrl,
559 priv->pin_states[PIN_STATE_DEFAULT]);
560 if (ret)
561 dev_info(&pdev->dev,
562 "%s failed to select state %d\n",
563 __func__, ret);
Shunli Wang11c02692019-03-29 16:34:46 +0800564 }
565
Tzung-Bi Shih63ba8e42019-05-22 22:54:02 +0800566 return devm_snd_soc_register_card(&pdev->dev, card);
Shunli Wang11c02692019-03-29 16:34:46 +0800567}
568
569#ifdef CONFIG_OF
570static const struct of_device_id mt8183_mt6358_ts3a227_max98357_dt_match[] = {
571 {.compatible = "mediatek,mt8183_mt6358_ts3a227_max98357",},
572 {}
573};
574#endif
575
576static struct platform_driver mt8183_mt6358_ts3a227_max98357_driver = {
577 .driver = {
578 .name = "mt8183_mt6358_ts3a227_max98357",
Shunli Wang11c02692019-03-29 16:34:46 +0800579#ifdef CONFIG_OF
580 .of_match_table = mt8183_mt6358_ts3a227_max98357_dt_match,
581#endif
582 },
583 .probe = mt8183_mt6358_ts3a227_max98357_dev_probe,
584};
585
586module_platform_driver(mt8183_mt6358_ts3a227_max98357_driver);
587
588/* Module information */
589MODULE_DESCRIPTION("MT8183-MT6358-TS3A227-MAX98357 ALSA SoC machine driver");
590MODULE_AUTHOR("Shunli Wang <shunli.wang@mediatek.com>");
591MODULE_LICENSE("GPL v2");
592MODULE_ALIAS("mt8183_mt6358_ts3a227_max98357 soc card");